-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserver.py
174 lines (150 loc) Β· 4.21 KB
/
webserver.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
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
import logging
import time
import machine
from property import Property
from thing import Thing
from value import Value
from server import MultipleThings, WebThingServer
from np import start_the_reactors, blink_enemy, ultraman_mode, rotate, display_odd
log = logging.getLogger(__name__)
NEOPIXEL_PIN = 13
START = 1
BLINK = 2
ULTRAMAN = 3
SPIN = 4
ODD = 5
class Led(Thing):
def __init__(self, ledPin):
Thing.__init__(self,
'My Reactor',
['OnOffSwitch', 'Light'],
'My Reactor built with SparkFun ESP32 Thing')
self.ledPin = ledPin
self.np = machine.Neopixel(machine.Pin(ledPin, machine.Pin.OUT), 24)
self.brightness = 20
self.blue = 0
self.color = '#0055FF'
self.green = 0
self.red = 0
self.on = False
self.start = False
self.odd = False
self.action = None
self.add_property(
Property(self,
'on',
Value(self.on, self.spinningReactor),
metadata={
'@type': 'OnOffProperty',
'label': 'Spinning Reactor',
'type': 'boolean',
'description': 'Turn on the Reactor',
}))
self.add_property(
Property(self,
'start',
Value(self.start, self.bootReactor),
metadata={
'@type': 'OnOffProperty',
'label': 'Start My Reactor',
'type': 'boolean',
'description': 'Turn on the Reactor',
}))
self.add_property(
Property(self,
'odd',
Value(self.odd, self.oddReactor),
metadata={
'@type': 'OnOffProperty',
'label': 'Display Odd Reactor',
'type': 'boolean',
'description': 'Turn on the Reactor',
}))
self.add_property(
Property(self,
'brightness',
Value(self.brightness, self.ultramanMode),
metadata={
'@type': 'BrightnessProperty',
'label': 'Become Ultraman',
'type': 'number',
'description': 'Become Ultraman',
'min': 0,
'max': 100,
'unit': 'percent',
}))
self.add_property(
Property(self,
'color',
Value(self.color, self.blinkEnemy),
metadata={
'@type': 'ColorProperty',
'label': 'Blink Enemy',
'type': 'string',
'description': 'Blink Enemy',
}))
self.updateReactor()
def constructNp(self):
self.np = machine.Neopixel(machine.Pin(self.ledPin, machine.Pin.OUT), 24)
def spinningReactor(self, onOff):
self.on = onOff
self.action = SPIN
self.updateReactor()
def bootReactor(self, onOff):
self.start = onOff
self.action = START
self.updateReactor()
def oddReactor(self, onOff):
self.odd = onOff
self.action = ODD
self.updateReactor()
def convertToRgb(self, color):
red = int(color[1:3], 16) / 256 * 100
green = int(color[3:5], 16) / 256 * 100
blue = int(color[5:7], 16) / 256 * 100
return (red, green, blue)
def convertToHex(self, color):
return int(hex(int(color[1:], 16)), 16)
def blinkEnemy(self, color):
self.color = color
self.red, self.green, self.blue = self.convertToRgb(color)
self.action = BLINK
self.updateReactor()
def ultramanMode(self, brightness):
self.brightness = brightness
self.action = ULTRAMAN
self.updateReactor()
def updateReactor(self):
if not self.on and not self.start and not self.odd:
#self.np.deinit()
#self.constructNp()
self.np.clear()
return
#log.debug("Reactor Updated, action: {}".format(self.action))
#log.debug("brightness: {}".format(self.brightness))
#log.debug("on: {}".format(self.on))
#log.debug("red: {}, green: {}, blue: {}".format(self.red, self.green, self.blue))
hex_color = self.convertToHex(self.color)
#log.debug("hex: {}".format(hex_color))
hue, saturation, brightness = self.np.RGBtoHSB(hex_color)
if self.action == SPIN:
log.debug("rotate")
rotate(self.np, 20, hue, saturation, brightness)
elif self.action == START:
log.debug("start")
start_the_reactors(self.np, 250, hue, saturation, brightness, False)
elif self.action == ODD:
log.debug("display odd")
display_odd(self.np, 250, hue, saturation, brightness, False)
def run_server():
log.info('run_server')
led = Led(NEOPIXEL_PIN)
start_the_reactors(led.np, 250, clear=True)
server = WebThingServer(MultipleThings([led], 'SparkFun-ESP32-Thing'), port=80)
try:
log.info('starting the server')
server.start()
except KeyBoardInterrupt:
log.info('stopping the server')
server.stop()
log.info('done')