1414
1515import RPi .GPIO
1616
17- import subprocess # nosec
18-
1917# Library to send command over I2C for the light module on the fan
2018import smbus2 as smbus
2119
2422
2523logger .info ("planktoscope.light is loaded" )
2624
27-
28- def i2c_update ():
29- # Update the I2C Bus in order to really update the LEDs new values
30- subprocess .Popen ("i2cdetect -y 1" .split (), stdout = subprocess .PIPE ) # nosec
31-
32-
3325class i2c_led :
3426 """
3527 LM36011 Led controller
@@ -48,7 +40,8 @@ class Register(enum.IntEnum):
4840 # This constant defines the current (mA) sent to the LED, 10 allows the use of the full ISO scale and results in a voltage of 2.77v
4941 DEFAULT_CURRENT = 10
5042
51- LED_selectPin = 18
43+ # This is the BCM pin of the led
44+ LED_PIN = 18
5245
5346 def __init__ (self ):
5447 self .VLED_short = False
@@ -59,8 +52,8 @@ def __init__(self):
5952 self .IVFM = False
6053 RPi .GPIO .setwarnings (False )
6154 RPi .GPIO .setmode (RPi .GPIO .BCM )
62- RPi .GPIO .setup (self .LED_selectPin , RPi .GPIO .OUT )
63- self . output_to_led1 ( )
55+ RPi .GPIO .setup (self .LED_PIN , RPi .GPIO .OUT )
56+ RPi . GPIO . output ( self . LED_PIN , RPi . GPIO . HIGH )
6457 self .on = False
6558 try :
6659 self .force_reset ()
@@ -78,14 +71,6 @@ def __init__(self):
7871 raise
7972 logger .debug (f"LED module id is { led_id } " )
8073
81- def output_to_led1 (self ):
82- logger .debug ("Switching output to LED 1" )
83- RPi .GPIO .output (self .LED_selectPin , RPi .GPIO .HIGH )
84-
85- def output_to_led2 (self ):
86- logger .debug ("Switching output to LED 2" )
87- RPi .GPIO .output (self .LED_selectPin , RPi .GPIO .LOW )
88-
8974 def get_id (self ):
9075 led_id = self ._read_byte (self .Register .id_reset )
9176 led_id = led_id & 0b111111
@@ -173,51 +158,6 @@ def _read_byte(self, address):
173158 b = bus .read_byte_data (self .DEVICE_ADDRESS , address )
174159 return b
175160
176-
177- class pwm_led :
178- def __init__ (self , led ):
179- RPi .GPIO .setmode (RPi .GPIO .BCM )
180- self .led = led
181- if self .led == 0 :
182- RPi .GPIO .setup (led0Pin , RPi .GPIO .OUT )
183- RPi .GPIO .output (led0Pin , RPi .GPIO .LOW )
184- self .pwm0 = RPi .GPIO .PWM (led0Pin , FREQUENCY )
185- self .pwm0 .start (0 )
186- elif self .led == 1 :
187- RPi .GPIO .setup (led1Pin , RPi .GPIO .OUT )
188- RPi .GPIO .output (led1Pin , RPi .GPIO .LOW )
189- self .pwm1 = RPi .GPIO .PWM (led1Pin , FREQUENCY )
190- self .pwm1 .start (0 )
191-
192- def change_duty (self , dc ):
193- if self .led == 0 :
194- self .pwm0 .ChangeDutyCycle (dc )
195- elif self .led == 1 :
196- self .pwm1 .ChangeDutyCycle (dc )
197-
198- def off (self ):
199- if self .led == 0 :
200- logger .debug ("Turning led 1 off" )
201- self .pwm0 .ChangeDutyCycle (0 )
202- elif self .led == 1 :
203- logger .debug ("Turning led 2 off" )
204- self .pwm1 .ChangeDutyCycle (0 )
205-
206- def on (self ):
207- if self .led == 0 :
208- logger .debug ("Turning led 1 on" )
209- self .pwm0 .ChangeDutyCycle (100 )
210- elif self .led == 1 :
211- logger .debug ("Turning led 2 on" )
212- self .pwm1 .ChangeDutyCycle (100 )
213-
214- def stop (self ):
215- if self .led == 0 :
216- self .pwm0 .stop ()
217- elif self .led == 1 :
218- self .pwm1 .stop ()
219-
220-
221161################################################################################
222162# Main Segmenter class
223163################################################################################
@@ -239,39 +179,24 @@ def __init__(self, event):
239179 try :
240180 self .led = i2c_led ()
241181 self .led .set_torch_current (self .led .DEFAULT_CURRENT )
242- self .led .output_to_led1 ()
243182 self .led .activate_torch_ramp ()
244183 self .led .activate_torch ()
245184 time .sleep (0.5 )
246- self .led .output_to_led2 ()
247- time .sleep (0.5 )
248185 self .led .deactivate_torch ()
249- self .led .output_to_led1 ()
250186 except Exception as e :
251187 logger .error (
252188 f"We have encountered an error trying to start the LED module, stopping now, exception is { e } "
253189 )
254- self .led .output_to_led2 ()
255190 raise e
256191 else :
257192 logger .success ("planktoscope.light is initialised and ready to go!" )
258193
259- def led_off (self , led ):
260- if led == 0 :
261- logger .debug ("Turning led 1 off" )
262- elif led == 1 :
263- logger .debug ("Turning led 2 off" )
194+ def led_off (self ):
195+ logger .debug ("Turning led off" )
264196 self .led .deactivate_torch ()
265197
266- def led_on (self , led ):
267- if led not in [0 , 1 ]:
268- raise ValueError ("Led number is wrong" )
269- if led == 0 :
270- logger .debug ("Turning led 1 on" )
271- self .led .output_to_led1 ()
272- elif led == 1 :
273- logger .debug ("Turning led 2 on" )
274- self .led .output_to_led2 ()
198+ def led_on (self ):
199+ logger .debug ("Turning led on" )
275200 self .led .activate_torch ()
276201
277202 @logger .catch
@@ -293,40 +218,21 @@ def treat_message(self):
293218 return
294219 if last_message :
295220 if "action" in last_message :
296- if last_message ["action" ] == "on" :
297- # {"action":"on", "led":"1"}
221+ action = last_message ["action" ]
222+ if action == "on" :
223+ # {"action":"on"}
298224 logger .info ("Turning the light on." )
299- if "led" not in last_message or last_message ["led" ] == 1 :
300- self .led_on (0 )
301- self .light_client .client .publish (
302- "status/light" , '{"status":"Led 1: On"}'
303- )
304- elif last_message ["led" ] == 2 :
305- self .led_on (1 )
306- self .light_client .client .publish (
307- "status/light" , '{"status":"Led 2: On"}'
308- )
309- else :
310- self .light_client .client .publish (
311- "status/light" , '{"status":"Error with led number"}'
312- )
313- elif last_message ["action" ] == "off" :
314- # {"action":"off", "led":"1"}
225+ self .led_on ()
226+ self .light_client .client .publish (
227+ "status/light" , '{"status":"On"}'
228+ )
229+ elif action == "off" :
230+ # {"action":"off"}
315231 logger .info ("Turn the light off." )
316- if "led" not in last_message or last_message ["led" ] == 1 :
317- self .led_off (0 )
318- self .light_client .client .publish (
319- "status/light" , '{"status":"Led 1: Off"}'
320- )
321- elif last_message ["led" ] == 2 :
322- self .led_off (1 )
323- self .light_client .client .publish (
324- "status/light" , '{"status":"Led 2: Off"}'
325- )
326- else :
327- self .light_client .client .publish (
328- "status/light" , '{"status":"Error with led number"}'
329- )
232+ self .led_off ()
233+ self .light_client .client .publish (
234+ "status/light" , '{"status":"Off"}'
235+ )
330236 else :
331237 logger .warning (
332238 f"We did not understand the received request { action } - { last_message } "
@@ -403,7 +309,6 @@ def run(self):
403309if __name__ == "__main__" :
404310 led = i2c_led ()
405311 led .set_torch_current (30 )
406- led .output_to_led1 ()
407312 led .activate_torch_ramp ()
408313 led .activate_torch ()
409314 time .sleep (5 )
0 commit comments