-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raspberry.py
123 lines (86 loc) · 2.91 KB
/
Raspberry.py
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
try:
import RPi.GPIO as GPIO
except ImportError:
import GPIO_dummy as GPIO # dummy GPIO for development off board
import Playlist
import Audio
import time
import Config
import Votes
import ServerCommunication
import atexit
import threading
import LED
PLAY_PIN = 17
RECORD_PIN = 18
VOTE_UP_PIN = 22
VOTE_DOWN_PIN = 27
class State:
NONE = 0
CITY_VOICE = 1
USER_VOICE = 2
RECORDING = 3
def __init__(self):
self.value = State.NONE
state = State()
def play_callback():
# TODO: not properly implemented yet
if state.value is State.RECORDING:
return
state.value = State.USER_VOICE
time.sleep(10)
state.value = State.CITY_VOICE
class RecordThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stop = False
def run(self):
state.value = State.RECORDING
Playlist.control.stop()
Audio.control.start_recording(Config.data_path + "/rec")
leds = LED.CountDown()
for i in range(0, LED.NUMBER_OF_LEDS-1):
leds.tick()
time.sleep(float(Config.recording_time)/LED.NUMBER_OF_LEDS)
Audio.control.stop_recording()
ServerCommunication.post_audio_message(Config.data_path + "/rec")
state.value = State.CITY_VOICE
# TODO: we should sleep longer here...
time.sleep(2.0)
Playlist.control.play()
def record_callback():
record_thread = RecordThread()
record_thread.start()
def vote_up_callback():
if state.value is not State.USER_VOICE:
return
# TODO: need proper id of playing audio here
Votes.add_vote("922ae72c-1862-4d91-a08b-4f2738ad0354", True)
def vote_down_callback():
if state.value is not State.USER_VOICE:
return
# TODO: need proper id of playing audio here
Votes.add_vote("922ae72c-1862-4d91-a08b-4f2738ad0354", False)
def button_callback(pin):
if pin is PLAY_PIN:
play_callback()
elif pin is RECORD_PIN:
record_callback()
elif pin is VOTE_DOWN_PIN:
vote_down_callback()
elif pin is VOTE_UP_PIN:
vote_up_callback()
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(PLAY_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(RECORD_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(VOTE_UP_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(VOTE_DOWN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(PLAY_PIN, GPIO.RISING, callback=button_callback, bouncetime=200)
GPIO.add_event_detect(RECORD_PIN, GPIO.RISING, callback=button_callback, bouncetime=200)
GPIO.add_event_detect(VOTE_UP_PIN, GPIO.RISING, callback=button_callback, bouncetime=200)
GPIO.add_event_detect(VOTE_DOWN_PIN, GPIO.RISING, callback=button_callback, bouncetime=200)
state.value = State.CITY_VOICE
def cleanup():
GPIO.cleanup()
atexit.register(cleanup)