forked from EtalumaSupport/LumaViewPro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledboard.py
195 lines (164 loc) · 7.01 KB
/
ledboard.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python3
'''
MIT License
Copyright (c) 2023 Etaluma, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyribackground_downght notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
```
This open source software was developed for use with Etaluma microscopes.
AUTHORS:
Kevin Peter Hickerson, The Earthineering Company
Anna Iwaniec Hickerson, Keck Graduate Institute
Gerard Decker, The Earthineering Company
MODIFIED:
March 20 2023
'''
from numpy import False_
import serial
import serial.tools.list_ports as list_ports
from lvp_logger import logger
import time
class LEDBoard:
#----------------------------------------------------------
# Initialize connection through microcontroller
#----------------------------------------------------------
def __init__(self, **kwargs):
logger.info('[LED Class ] LEDBoard.__init__()')
ports = list_ports.comports(include_links = True)
self.found = False
for port in ports:
if (port.vid == 0x0424) and (port.pid == 0x704C):
logger.info(f'[LED Class ] LED Controller at {port.device}')
self.port = port.device
self.found = True
break
self.baudrate=115200
self.bytesize=serial.EIGHTBITS
self.parity=serial.PARITY_NONE
self.stopbits=serial.STOPBITS_ONE
self.timeout=0.01 # seconds
self.write_timeout=0.01 # seconds
self.driver = False
try:
logger.info('[LED Class ] Found LED controller and about to establish connection.')
self.connect()
except:
logger.exception('[LED Class ] Found LED controller but unable to establish connection.')
raise
def connect(self):
""" Try to connect to the LED controller based on the known VID/PID"""
try:
logger.info('[LED Class ] Found LED controller and about to create driver.')
self.driver = serial.Serial(port=self.port,
baudrate=self.baudrate,
bytesize=self.bytesize,
parity=self.parity,
stopbits=self.stopbits,
timeout=self.timeout,
write_timeout=self.write_timeout)
#self.driver.close()
#self.driver.open()
# self.exchange_command('import main.py')
# self.exchange_command('import main.py')
logger.info('[LED Class ] LEDBoard.connect() succeeded')
#Sometimes the firmware fails to start (or the port has a \x00 left in the buffer), this forces MicroPython to reset, and the normal firmware just complains
self.driver.write(b'\x04\n')
logger.debug('[LED Class ] LEDBOARD.connect() port initial state: %r'%self.driver.readline())
except:
self.driver = False
logger.exception('[LED Class ] LEDBoard.connect() failed')
def exchange_command(self, command):
""" Exchange command through serial to LED board
This should NOT be used in a script. It is intended for other functions to access"""
stream = command.encode('utf-8')+b"\n"
if self.driver != False:
try:
self.driver.write(stream)
response = self.driver.readline()
self.driver.flushInput()
self.driver.flush()
response = response.decode("utf-8","ignore")
logger.info('[LED Class ] LEDBoard.exchange_command('+command+') succeeded: %r'%response)
return response[:-2]
except serial.SerialTimeoutException:
self.driver = False
logger.exception('[LED Class ] LEDBoard.exchange_command('+command+') Serial Timeout Occurred')
except:
self.driver = False
else:
try:
self.connect()
except:
return
def color2ch(self, color):
""" Convert color name to numerical channel """
if color == 'Blue':
return 0
elif color == 'Green':
return 1
elif color == 'Red':
return 2
elif color == 'BF':
return 3
elif color == 'PC':
return 4
elif color == 'EP':
return 5
else: # BF
return 3
def ch2color(self, channel):
""" Convert numerical channel to color name """
if channel == 0:
return 'Blue'
elif channel == 1:
return 'Green'
elif channel == 2:
return 'Red'
elif channel == 3:
return 'BF'
elif channel == 4:
return 'PC'
elif channel == 5:
return 'EP'
else:
return 'BF'
# interperet commands
# ------------------------------------------
# board status: 'STATUS' case insensitive
# LED enable: 'LED' channel '_ENT' where channel is numbers 0 through 5, or S (plural/all)
# LED disable: 'LED' channel '_ENF' where channel is numbers 0 through 5, or S (plural/all)
# LED on: 'LED' channel '_MA' where channel is numbers 0 through 5, or S (plural/all)
# and MA is numerical representation of mA
# LED off: 'LED' channel '_OFF' where channel is numbers 0 through 5, or S (plural/all)
def leds_enable(self):
command = 'LEDS_ENT'
self.exchange_command(command)
def leds_disable(self):
command = 'LEDS_ENF'
self.exchange_command(command)
def led_on(self, channel, mA):
""" Turn on LED at channel number at mA power """
command = 'LED' + str(int(channel)) + '_' + str(int(mA))
self.exchange_command(command)
def led_off(self, channel):
""" Turn off LED at channel number """
command = 'LED' + str(int(channel)) + '_OFF'
self.exchange_command(command)
def leds_off(self):
""" Turn off all LEDs """
command = 'LEDS_OFF'
self.exchange_command(command)