-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathstartupSequence.py
72 lines (60 loc) · 2.64 KB
/
startupSequence.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
import asyncio
import time
from lightscontroller import LED_COUNT
class StartupSequenceController:
def __init__(self, config, servoController, lightsController, tts):
audioConfig = config["AUDIO"]
servoConfig = config["SERVO"]
self.greeting = audioConfig["Greeting"]
self.neutral = int(servoConfig["Neutral"])
self.min = int(servoConfig["Min"])
self.max = int(servoConfig["Max"])
self.servoController = servoController
self.lightsController = lightsController
self.tts = tts
self.startLoop()
def startLoop(self):
loop = asyncio.get_event_loop()
self.task = loop.create_task(self.doSequence())
async def doSequence(self):
self.lightsController.lightsOff()
await asyncio.sleep(2)
servoInterval = 0.25
self.servoController.changeServo(self.neutral)
await asyncio.sleep(servoInterval)
self.servoController.changeServo(self.min)
await asyncio.sleep(servoInterval)
self.servoController.changeServo(self.max)
await asyncio.sleep(servoInterval)
transitionTime = float(1)
startTime = time.time()
targetTime = startTime + transitionTime
servoTravel = self.max - self.neutral
ledFraction = float(1) / LED_COUNT
self.lightsController.lights.set_global_brightness(1)
self.lightsController.lights.clear_strip()
while time.time() < targetTime:
timeFraction = (time.time() - startTime) / transitionTime
servoPosition = self.max - servoTravel * timeFraction
self.servoController.changeServo(servoPosition)
ledsOn = round(timeFraction / ledFraction)
for ix in range(0, ledsOn):
self.lightsController.lights.set_pixel_rgb(LED_COUNT - ix - 1, 0x002e00)
self.lightsController.lights.show()
await asyncio.sleep(0.05)
for ix in range(0, LED_COUNT):
self.lightsController.lights.set_pixel_rgb(ix, 0x002e00)
self.lightsController.lights.show()
self.servoController.changeServo(self.neutral)
if self.greeting:
self.tts.sayText(self.greeting)
await asyncio.sleep(0.1)
self.lightsController.lightsOff()
await asyncio.sleep(0.1)
for ix in range(0, LED_COUNT):
self.lightsController.lights.set_pixel_rgb(ix, 0x002e00)
self.lightsController.lights.show()
await asyncio.sleep(0.1)
self.servoController.stopServo()
self.lightsController.lights.set_global_brightness(31)
self.lightsController.lights.clear_strip()