-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
98 lines (66 loc) · 1.67 KB
/
app.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
from time import sleep
from enum import Enum
from ctrl9001 import Pigeon, State
from gpiozero import Button, LED
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008 as MCP3008
GPIO_POWER_BUTTON = 24
SPI_PORT = 0
SPI_DEVICE = 0
MAX_SLIDE_VALUE = 1023
SLIDE_MODIFIER = 400
CYCLE_TIME = 100
class Color(Enum):
RED = 1
GREEN = 2
class RGLED:
def __init__(self, red, green):
self.red_led = red
self.green_led = green
self.color = None
def toggle(self):
if self.color is Color.RED:
self.green()
elif self.color is Color.GREEN:
self.red()
elif self.color is None:
self.red()
def green(self):
self.red_led.off()
self.green_led.on()
self.color = Color.GREEN
def red(self):
self.green_led.off()
self.red_led.on()
self.color = Color.RED
pigeon = Pigeon()
state = State()
mcp = MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
power_button = Button(GPIO_POWER_BUTTON)
rgled = RGLED(LED(22), LED(27))
rgled.red()
def calc_operating_ratio(slide_state):
# Calculate slide state in percent
return (100 - (100 / (MAX_SLIDE_VALUE + (SLIDE_MODIFIER * 2))) * \
(slide_state + SLIDE_MODIFIER)) * 0.01
def switch_power():
state.power = not state.power
rgled.toggle()
pigeon.push(state)
power_button.when_pressed = switch_power
try:
while True:
ratio_slide_state_1 = mcp.read_adc(0)
ratio_slide_state_2 = mcp.read_adc(1)
print(ratio_slide_state_1)
print(ratio_slide_state_2)
state.cycle_time = CYCLE_TIME
state.operating_ratios = [
calc_operating_ratio(ratio_slide_state_1),
calc_operating_ratio(ratio_slide_state_2)
]
pigeon.push(state)
sleep(0.5)
except KeyboardInterrupt:
state.power = False
pigeon.push(state)