From ede52d59db07864e3387ac58f757ab0378ddffd9 Mon Sep 17 00:00:00 2001 From: Romain Bazile Date: Sat, 5 Dec 2020 11:39:49 +0100 Subject: [PATCH] double all i2c calls and change nginx config in update script --- flows/main.json | 2 +- scripts/bash/update.sh | 18 ++++----- scripts/planktoscope/light.py | 74 ++++++++++++++++++++++++++++++----- 3 files changed, 75 insertions(+), 19 deletions(-) diff --git a/flows/main.json b/flows/main.json index aab087b..abe6866 100644 --- a/flows/main.json +++ b/flows/main.json @@ -1427,7 +1427,7 @@ "type": "python3-function", "z": "1371dec5.76e671", "name": "fan control", - "func": "#!/usr/bin/python\nimport smbus\n\nstate = msg[\"payload\"]\n\nbus = smbus.SMBus(1)\n\nDEVICE_ADDRESS = 0x0d\n\nif state == \"off\":\n bus.write_byte_data(DEVICE_ADDRESS, 0x08, 0x00)\nif state == \"on\":\n bus.write_byte_data(DEVICE_ADDRESS, 0x08, 0x01)\n", + "func": "#!/usr/bin/python\nimport smbus\n\nstate = msg[\"payload\"]\n\nbus = smbus.SMBus(1)\n\nDEVICE_ADDRESS = 0x0d\n\n# command happens twice, for reasons\nif state == \"off\":\n bus.write_byte_data(DEVICE_ADDRESS, 0x08, 0x00)\n bus.write_byte_data(DEVICE_ADDRESS, 0x08, 0x00)\nif state == \"on\":\n bus.write_byte_data(DEVICE_ADDRESS, 0x08, 0x01)\n bus.write_byte_data(DEVICE_ADDRESS, 0x08, 0x01)\n", "outputs": 1, "x": 1110, "y": 40, diff --git a/scripts/bash/update.sh b/scripts/bash/update.sh index 9dab73c..63a9df6 100755 --- a/scripts/bash/update.sh +++ b/scripts/bash/update.sh @@ -25,15 +25,15 @@ function special(){ sudo rm /etc/nginx/sites-enabled/img.conf sudo nginx -t && sudo systemctl reload nginx fi - if [[ -f "/etc/nginx/sites-available/gallery.conf" ]]; then - ${log} "Updating the old nginx config" - sudo rm /etc/nginx/sites-available/gallery.conf - sudo rm /etc/nginx/sites-enabled/gallery.conf - fi - if ! [[ -f "/etc/nginx/sites-available/gallery.conf" ]]; then + if ! [[ -f "/etc/nginx/sites-enabled/gallery.conf" ]]; then ${log} "Nginx config is not installed, doing that now" - sudo cp /home/pi/PlanktonScope/scripts/gallery/gallery.conf /etc/nginx/sites-available/gallery.conf - sudo ln -s /etc/nginx/sites-available/gallery.conf /etc/nginx/sites-enabled/gallery.conf + sudo ln -s /home/pi/PlanktonScope/scripts/gallery/gallery.conf /etc/nginx/sites-enabled/gallery.conf + sudo nginx -t && sudo systemctl reload nginx + fi + if [[ -f "/etc/nginx/sites-available/gallery.conf" ]]; then + ${log} "Nginx config is installed, changing the links now" + sudo rm /etc/nginx/sites-enabled/gallery.conf /etc/nginx/sites-available/gallery.conf + sudo ln -s /home/pi/PlanktonScope/scripts/gallery/gallery.conf /etc/nginx/sites-enabled/gallery.conf sudo nginx -t && sudo systemctl reload nginx fi } @@ -44,10 +44,10 @@ cd /home/pi/PlanktonScope || { echo "/home/pi/PlanktonScope does not exist"; exi # TODO We need to add here a way to load the latest version of this script and execute it again remote=$(git ls-remote -h origin master | awk '{print $1}') local=$(git rev-parse HEAD) -${log} "Local : $local - Remote: $remote" if [[ "$local" == "$remote" ]]; then ${log} "nothing to do!" else + ${log} "Local and Remote are different, we have to update!" git fetch UPDATE=$(git diff --numstat origin/master scripts/bash/update.sh | awk '/update.sh/ {print $NF}') if [[ -n "${UPDATE}" ]]; then diff --git a/scripts/planktoscope/light.py b/scripts/planktoscope/light.py index a49a62a..aa21ec1 100644 --- a/scripts/planktoscope/light.py +++ b/scripts/planktoscope/light.py @@ -8,13 +8,12 @@ # Logger library compatible with multiprocessing from loguru import logger +import subprocess # nosec # Library to send command over I2C for the light module on the fan try: import smbus2 as smbus except ModuleNotFoundError: # We need this to install the library on machine that do not have the module yet - import subprocess # nosec - subprocess.run("pip3 install smbus2".split()) # nosec import smbus2 as smbus @@ -55,6 +54,11 @@ class EffectColor(enum.IntEnum): White = 6 +def i2c_update(): + # Update the I2C Bus in order to really update the LEDs new values + subprocess.Popen("i2cdetect -y 1".split(), stdout=subprocess.PIPE) # nosec + + ################################################################################ # LEDs functions ################################################################################ @@ -62,12 +66,16 @@ def setRGB(R, G, B): """Update all LED at the same time""" try: with smbus.SMBus(1) as bus: + bus.write_byte_data(DEVICE_ADDRESS, Register.led_select, 0xFF) bus.write_byte_data( DEVICE_ADDRESS, Register.led_select, 0xFF ) # 0xFF write to all LEDs, 0x01/0x02/0x03 to choose first, second or third LED bus.write_byte_data(DEVICE_ADDRESS, Register.red, R & 0xFF) bus.write_byte_data(DEVICE_ADDRESS, Register.green, G & 0xFF) bus.write_byte_data(DEVICE_ADDRESS, Register.blue, B & 0xFF) + bus.write_byte_data(DEVICE_ADDRESS, Register.red, R & 0xFF) + bus.write_byte_data(DEVICE_ADDRESS, Register.green, G & 0xFF) + bus.write_byte_data(DEVICE_ADDRESS, Register.blue, B & 0xFF) except Exception as e: logger.exception(f"An Exception has occured in the light library at {e}") @@ -77,6 +85,7 @@ def setRGBOff(): try: with smbus.SMBus(1) as bus: bus.write_byte_data(DEVICE_ADDRESS, Register.rgb_off, 0x00) + bus.write_byte_data(DEVICE_ADDRESS, Register.rgb_off, 0x00) except Exception as e: logger.exception(f"An Exception has occured in the light library at {e}") @@ -121,50 +130,71 @@ def setRGBColor(bus, color): def ready(): with smbus.SMBus(1) as bus: + setRGBColor(bus, EffectColor.Green) setRGBColor(bus, EffectColor.Green) setRGBSpeed(bus, 1) + setRGBSpeed(bus, 1) + setRGBEffect(bus, Effect.Breathing) setRGBEffect(bus, Effect.Breathing) def error(): with smbus.SMBus(1) as bus: + setRGBColor(bus, EffectColor.Red) setRGBColor(bus, EffectColor.Red) setRGBSpeed(bus, 3) + setRGBSpeed(bus, 3) + setRGBEffect(bus, Effect.Water) setRGBEffect(bus, Effect.Water) def interrupted(): with smbus.SMBus(1) as bus: + setRGBColor(bus, EffectColor.Yellow) setRGBColor(bus, EffectColor.Yellow) setRGBSpeed(bus, 3) + setRGBSpeed(bus, 3) + setRGBEffect(bus, Effect.Water) setRGBEffect(bus, Effect.Water) def pumping(): with smbus.SMBus(1) as bus: + setRGBColor(bus, EffectColor.Blue) setRGBColor(bus, EffectColor.Blue) setRGBSpeed(bus, 3) + setRGBSpeed(bus, 3) + setRGBEffect(bus, Effect.Water) setRGBEffect(bus, Effect.Water) def focusing(): with smbus.SMBus(1) as bus: + setRGBColor(bus, EffectColor.Purple) setRGBColor(bus, EffectColor.Purple) setRGBSpeed(bus, 3) + setRGBSpeed(bus, 3) + setRGBEffect(bus, Effect.Water) setRGBEffect(bus, Effect.Water) def imaging(): with smbus.SMBus(1) as bus: + setRGBColor(bus, EffectColor.White) setRGBColor(bus, EffectColor.White) setRGBSpeed(bus, 1) + setRGBSpeed(bus, 1) + setRGBEffect(bus, Effect.Breathing) setRGBEffect(bus, Effect.Breathing) def segmenting(): with smbus.SMBus(1) as bus: + setRGBColor(bus, EffectColor.Purple) setRGBColor(bus, EffectColor.Purple) setRGBSpeed(bus, 1) + setRGBSpeed(bus, 1) + setRGBEffect(bus, Effect.Breathing) setRGBEffect(bus, Effect.Breathing) @@ -175,22 +205,48 @@ if __name__ == "__main__": print("ready") ready() - time.sleep(10) + time.sleep(5) print("error") error() - time.sleep(10) + time.sleep(5) print("pumping") pumping() - time.sleep(10) + time.sleep(5) print("focusing") focusing() - time.sleep(10) + time.sleep(5) print("imaging") imaging() - time.sleep(10) + time.sleep(5) print("segmenting") segmenting() - time.sleep(10) + time.sleep(5) + print("with i2c_update now!") + print("ready") + ready() + i2c_update() + time.sleep(5) + print("error") + error() + i2c_update() + time.sleep(5) + print("pumping") + pumping() + i2c_update() + time.sleep(5) + print("focusing") + focusing() + i2c_update() + time.sleep(5) + print("imaging") + imaging() + i2c_update() + time.sleep(5) + print("segmenting") + segmenting() + i2c_update() + time.sleep(5) + with smbus.SMBus(1) as bus: setRGBSpeed(bus, 3) for effect in Effect: @@ -206,4 +262,4 @@ if __name__ == "__main__": setRGBColor(bus, color) time.sleep(2) - setRGBOff() \ No newline at end of file + setRGBOff()