Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 9d8bd44

Browse files
authored
Port light.py to gpiozero (#80)
1 parent 894a816 commit 9d8bd44

File tree

4 files changed

+129
-19
lines changed

4 files changed

+129
-19
lines changed

control/planktoscopehat/planktoscope/light.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44
# Logger library compatible with multiprocessing
55
from loguru import logger
66

7+
from gpiozero import DigitalOutputDevice
8+
79
import os, time
810

911
# Library for starting processes
1012
import multiprocessing
1113

1214
# Basic planktoscope libraries
13-
import planktoscope.mqtt
14-
15-
import RPi.GPIO
15+
from . import mqtt
1616

1717
# Library to send command over I2C for the light module on the fan
1818
import smbus2 as smbus
1919

2020
import enum
2121

22-
2322
logger.info("planktoscope.light is loaded")
2423

2524
class i2c_led:
@@ -40,20 +39,19 @@ class Register(enum.IntEnum):
4039
# 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
4140
DEFAULT_CURRENT = 10
4241

43-
# This is the BCM pin of the led
44-
LED_PIN = 18
45-
4642
def __init__(self):
43+
# The led is controlled by LM36011
44+
# but on version < 2 of the PlanktoScope hat (for example PlanktoScope v2.6)
45+
# the circuit is connected to that pin so it needs to be high
46+
# pin is assigned to self to prevent gpiozero from immediately releasing it
47+
self.__pin = DigitalOutputDevice(pin=18, initial_value=True)
48+
4749
self.VLED_short = False
4850
self.thermal_scale = False
4951
self.thermal_shutdown = False
5052
self.UVLO = False
5153
self.flash_timeout = False
5254
self.IVFM = False
53-
RPi.GPIO.setwarnings(False)
54-
RPi.GPIO.setmode(RPi.GPIO.BCM)
55-
RPi.GPIO.setup(self.LED_PIN, RPi.GPIO.OUT)
56-
RPi.GPIO.output(self.LED_PIN, RPi.GPIO.HIGH)
5755
self.on = False
5856
try:
5957
self.force_reset()
@@ -280,7 +278,7 @@ def run(self):
280278
)
281279

282280
# MQTT Service connection
283-
self.light_client = planktoscope.mqtt.MQTT_Client(
281+
self.light_client = mqtt.MQTT_Client(
284282
topic="light", name="light_client"
285283
)
286284

@@ -299,7 +297,6 @@ def run(self):
299297
self.led.set_torch_current(1)
300298
self.led.set_flash_current(1)
301299
self.led.get_flags()
302-
RPi.GPIO.cleanup()
303300
self.light_client.client.publish("status/light", '{"status":"Dead"}')
304301
self.light_client.shutdown()
305302
logger.success("Light process shut down! See you!")
@@ -320,4 +317,3 @@ def run(self):
320317
led.set_torch_current(1)
321318
led.set_flash_current(1)
322319
led.get_flags()
323-
RPi.GPIO.cleanup()

control/planktoscopehat/planktoscope/stepper.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
import time
33
import json
44
import os
5-
import planktoscope.mqtt
5+
from . import mqtt
66
import multiprocessing
7-
import RPi.GPIO
87

98
import shush
109

@@ -417,7 +416,7 @@ def run(self):
417416
# it doesn't see changes and calls made by self.actuator_client because this one
418417
# only exist in the master process
419418
# see https://stackoverflow.com/questions/17172878/using-pythons-multiprocessing-process-class
420-
self.actuator_client = planktoscope.mqtt.MQTT_Client(
419+
self.actuator_client = mqtt.MQTT_Client(
421420
topic="actuator/#", name="actuator_client"
422421
)
423422
# Publish the status "Ready" to via MQTT to Node-RED

control/poetry.lock

Lines changed: 115 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

control/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ adafruit-circuitpython-motor = "~3.3.1"
4545
adafruit-ssd1306 = "~1.6.2"
4646
adafruit-platformdetect = "~3.45.2"
4747
smbus2 = "~0.4.1"
48+
spidev = "~3.5"
49+
gpiozero = "^2.0.1"
4850
# Note: the following packages are only indirect dependencies, but we need to download wheels from
4951
# the appropriate sources, so we must explicitly select them here
5052
rpi-ws281x = "~5.0.0"
5153
sysv-ipc = "~1.1.0"
52-
spidev = "~3.5"
5354

5455
[tool.poetry.group.hw-dev]
5556
optional = true

0 commit comments

Comments
 (0)