-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
212 lines (176 loc) · 5.16 KB
/
GUI.py
File metadata and controls
212 lines (176 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
"""Simple test for keypad on I2C RGB character LCD Shield or Pi Plate kits"""
import time
import board
import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd
import os
import sys
import git
import pygame
from pygame import mixer
def menu_control(header, menu_text):
index = 0
new_index = 0
menu_length = len(menu_text)
lcd.clear()
lcd.message = header + " \n " + menu_text[index]
confirm = False
while not confirm:
if lcd.left_button:
new_index = (new_index - 1) % menu_length
#elif lcd.up_button:
# new_index = (new_index - 1) % menu_length
#elif lcd.down_button:
# new_index = (new_index + 1) % menu_length
elif lcd.right_button:
new_index = (new_index + 1) % menu_length
elif lcd.select_button:
choice = index
#lcd.clear()
#lcd.message = menu_text[index] + "\nwas chosen"
#time.sleep(0.2)
confirm = True
return choice
else:
time.sleep(0.075)
if new_index != index:
index = new_index
lcd.clear()
print(header)
lcd.message = header + " \n " + menu_text[index]
time.sleep(0.075)
def play_audio(vol, left_filename, right_filename):
"""
Play the .wav file
:param vol: Volume audio is played at (0-1)
:param filename: Name of audio file (55hz.wav)
:return: None
"""
mixer.pre_init(44100, -16, 2, 512) # <-- fixes sound lag delay
mixer.init()
pygame.mixer.set_num_channels(2)
print("audio/" + left_filename)
left_audio = mixer.Sound("audio/" + left_filename)
right_audio = mixer.Sound("audio/" + right_filename)
left_channel = mixer.Channel(0)
right_channel = mixer.Channel(1)
left_channel.play(left_audio)
time.sleep(.1)
right_channel.play(right_audio)
left_channel.set_volume(vol, 0)
right_channel.set_volume(0, vol)
time.sleep(max(left_audio.get_length(), right_audio.get_length()))
def git_pull():
repo = git.Repo("/home/pi/SpiderPi/")
repo.remotes.origin.pull()
def restart():
lcd.clear()
lcd.message = ' restarting...'
# time.sleep(0.25)
os.execv(sys.executable, ['python'] + sys.argv)
mixer.init()
print('starting gui with a new message')
# Modify this if you have a different sized Character LCD
lcd_columns = 16
lcd_rows = 2
# Initialise I2C bus.
i2c = board.I2C() # uses board.SCL and board.SDA
# Initialise the LCD class
lcd = character_lcd.Character_LCD_RGB_I2C(i2c, lcd_columns, lcd_rows)
lcd.clear()
lcd.color = [100, 0, 0]
first_menu = [" play?", " update?", " exit?"]
while True:
first_choice = menu_control(" Main Menu: ", first_menu)
if first_choice == 1:
lcd.clear()
lcd.message = ' updating...'
git_pull()
lcd.message = ' update complete'
time.sleep(0.25)
lcd.clear()
restart()
elif first_choice == 0:
second_menu = os.listdir("audio/")
lcd.clear()
left_choice = menu_control(" left signal", second_menu)
lcd.clear()
right_choice = menu_control(" right signal", second_menu)
lcd.clear()
lcd.message = ' playing...'
print("audio choices", second_menu[left_choice], second_menu[right_choice])
print(type(second_menu[left_choice]))
play_audio(1, second_menu[left_choice], second_menu[right_choice])
lcd.clear()
lcd.message = ' finished'
time.sleep(0.25)
elif first_choice == 2:
lcd.clear()
lcd.color = [0,0,0]
exit()
# pygame.init()
# screen = pygame.display.set_mode((640, 480), pygame.RESIZABLE)
# while True:
# for event in pygame.event.get():
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_ESCAPE:
# print('Quit')
# pygame.quit()
# raise SystemExit(0)
'''
lcd.clear()
# Set LCD color to red
lcd.color = [100, 0, 0]
time.sleep(1)
# Print two line message
lcd.message = "Hello\nCircuitPython"
# Wait 5s
time.sleep(5)
# Set LCD color to blue
lcd.color = [0, 100, 0]
time.sleep(1)
# Set LCD color to green
lcd.color = [0, 0, 100]
time.sleep(1)
# Set LCD color to purple
lcd.color = [50, 0, 50]
time.sleep(1)
lcd.clear()
# Print two line message right to left
lcd.text_direction = lcd.RIGHT_TO_LEFT
lcd.message = "Hello\nCircuitPython"
# Wait 5s
time.sleep(5)
# Return text direction to left to right
lcd.text_direction = lcd.LEFT_TO_RIGHT
# Display cursor
lcd.clear()
lcd.cursor = True
lcd.message = "Cursor! "
# Wait 5s
time.sleep(5)
# Display blinking cursor
lcd.clear()
lcd.blink = True
lcd.message = "Blinky Cursor!"
# Wait 5s
time.sleep(5)
lcd.blink = False
lcd.clear()
# Create message to scroll
scroll_msg = "<-- Scroll"
lcd.message = scroll_msg
# Scroll to the left
for i in range(len(scroll_msg)):
time.sleep(0.5)
lcd.move_left()
lcd.clear()
time.sleep(1)
lcd.message = "Going to sleep\nCya later!"
time.sleep(5)
# Turn off LCD backlights and clear text
lcd.color = [0, 0, 0]
lcd.clear()
'''