light: catch exceptions

This commit is contained in:
Romain Bazile 2020-11-27 11:30:29 +01:00
parent 1cf3d1df12
commit a62891eb6a

View file

@ -5,6 +5,9 @@
# Turn off using this command line : # Turn off using this command line :
# python3.7 path/to/file/light.py off # python3.7 path/to/file/light.py off
# Logger library compatible with multiprocessing
from loguru import logger
# Library to send command over I2C for the light module on the fan # Library to send command over I2C for the light module on the fan
import smbus import smbus
import RPi.GPIO import RPi.GPIO
@ -31,17 +34,23 @@ def i2c_update():
def setRGB(R, G, B): def setRGB(R, G, B):
"""Update all LED at the same time""" """Update all LED at the same time"""
bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0xFF) try:
bus.write_byte_data(DEVICE_ADDRESS, 0x01, R & 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0xFF)
bus.write_byte_data(DEVICE_ADDRESS, 0x02, G & 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x01, R & 0xFF)
bus.write_byte_data(DEVICE_ADDRESS, 0x03, B & 0xFF) bus.write_byte_data(DEVICE_ADDRESS, 0x02, G & 0xFF)
i2c_update() bus.write_byte_data(DEVICE_ADDRESS, 0x03, B & 0xFF)
i2c_update()
except Exception as e:
logger.exception(f"An Exception has occured in the light library at {e}")
def setRGBOff(): def setRGBOff():
"""Turn off the RGB LED""" """Turn off the RGB LED"""
bus.write_byte_data(DEVICE_ADDRESS, 0x07, 0x00) try:
i2c_update() bus.write_byte_data(DEVICE_ADDRESS, 0x07, 0x00)
i2c_update()
except Exception as e:
logger.exception(f"An Exception has occured in the light library at {e}")
def setRGBEffect(effect): def setRGBEffect(effect):
@ -55,13 +64,19 @@ def setRGBEffect(effect):
""" """
if effect >= 0 and effect <= 4: if effect >= 0 and effect <= 4:
bus.write_byte_data(DEVICE_ADDRESS, rgb_effect_reg, effect & 0xFF) try:
bus.write_byte_data(DEVICE_ADDRESS, rgb_effect_reg, effect & 0xFF)
except Exception as e:
logger.exception(f"An Exception has occured in the light library at {e}")
def setRGBSpeed(speed): def setRGBSpeed(speed):
"""Set the effect speed, 1-3, 3 being the fastest speed""" """Set the effect speed, 1-3, 3 being the fastest speed"""
if speed >= 1 and speed <= 3: if speed >= 1 and speed <= 3:
bus.write_byte_data(DEVICE_ADDRESS, rgb_speed_reg, speed & 0xFF) try:
bus.write_byte_data(DEVICE_ADDRESS, rgb_speed_reg, speed & 0xFF)
except Exception as e:
logger.exception(f"An Exception has occured in the light library at {e}")
def setRGBColor(color): def setRGBColor(color):
@ -77,7 +92,10 @@ def setRGBColor(color):
""" """
if color >= 0 and color <= 6: if color >= 0 and color <= 6:
bus.write_byte_data(DEVICE_ADDRESS, rgb_color_reg, color & 0xFF) try:
bus.write_byte_data(DEVICE_ADDRESS, rgb_color_reg, color & 0xFF)
except Exception as e:
logger.exception(f"An Exception has occured in the light library at {e}")
def light(state): def light(state):