def setup(): """Set up the backlight on GFX HAT""" global _sn3218 import sn3218 as _sn3218 _sn3218.enable() _sn3218.enable_leds(0b111111111111111111) _sn3218.output(_buf)
def setup(): global automation_hat, automation_phat, sn3218, _ads1015, _is_setup, _t_update_lights if _is_setup: return True _is_setup = True GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) try: import smbus except ImportError: if version_info[0] < 3: raise ImportError( "This library requires python-smbus\nInstall with: sudo apt install python-smbus" ) elif version_info[0] == 3: raise ImportError( "This library requires python3-smbus\nInstall with: sudo apt install python3-smbus" ) _ads1015 = ads1015(smbus.SMBus(1)) if _ads1015.available() is False: raise RuntimeError("No ADC detected, check your connections") try: import sn3218 except ImportError: raise ImportError( "This library requires sn3218\nInstall with: sudo pip install sn3218" ) except IOError: pass if sn3218 is not None: sn3218.enable() sn3218.enable_leds(0b111111111111111111) automation_hat = True automation_phat = False _t_update_lights = AsyncWorker(_update_lights) _t_update_lights.start() atexit.register(_exit)
def setup(): global _is_setup, sn3218 if _is_setup: return True try: import sn3218 except ImportError: raise ImportError( "This library requires the sn3218 module\nInstall with: sudo pip install sn3218" ) sn3218.enable() sn3218.enable_leds(0b111111111111111111) atexit.register(_exit) _is_setup = True
def test_enable_leds(self): """Enable different combinations of LEDs""" sn3218.output([60] * 18) for _ in range(3): sn3218.enable_leds(0b101010101010101010) delay() sn3218.enable_leds(0b010101010101010101) delay() sn3218.enable_leds(0b000000000000000000) delay() sn3218.enable_leds(0b111111111111111111) delay()
RELAY_3 = 16 INPUT_1 = 26 INPUT_2 = 20 INPUT_3 = 21 OUTPUT_1 = 5 OUTPUT_2 = 12 OUTPUT_3 = 6 i2c = None try: i2c = sn3218.i2c sn3218.enable() sn3218.enable_leds(0b111111111111111111) except NameError: i2c = SMBus(1) sn3218 = None pass ads1015 = ads1015(i2c) if ads1015.available() is False: exit("No ADC detected, check your connections") _led_states = [0] * 18 _led_dirty = False class SNLight(object): def __init__(self, index):
error = """This library requires the sn3218 module. Install with: sudo pip install sn3218""" exit(error) stupid_led_mappings = [0, 1, 2, 4, 6, 8, 10, 12, 14, 16] led_values = [0 for x in range(18)] enable_leds = 0 WIDTH = 10 HEIGHT = 1 for x in stupid_led_mappings: enable_leds |= 1 << x sn3218.enable_leds(enable_leds) sn3218.enable() def set_led(index, value): led_values[stupid_led_mappings[index]] = value def show(): sn3218.output(led_values) def clear(): global led_values led_values = [0 for x in range(18)] show()
def action_leds(action_level, one, two, three): """ Formats the enable_mask for enabling/disabling LEDs Args: action_level (int): Level of LEDs to be actioned one (int): Relay one status two (int): Relay two status three (int): Relay three status """ # Initialise parameters global enable_mask # Set the enable_mask and return the value if action_level == 1 and one == 1 and two == 0 and three == 0: # Level 1 enable_mask = 0b111000000001000000 elif action_level == 1 and one == 0 and two == 1 and three == 0: enable_mask = 0b111000000100000000 elif action_level == 1 and one == 0 and two == 0 and three == 1: enable_mask = 0b111000010000000000 elif action_level == 1 and one == 1 and two == 1 and three == 0: enable_mask = 0b111000000101000000 elif action_level == 1 and one == 1 and two == 0 and three == 1: enable_mask = 0b111000010001000000 elif action_level == 1 and one == 0 and two == 1 and three == 1: enable_mask = 0b111000010100000000 elif action_level == 1 and one == 1 and two == 1 and three == 1: enable_mask = 0b111000010101000000 elif action_level == 2 and one == 1 and two == 0 and three == 0: # Level 2 enable_mask = 0b111000000001000000 elif action_level == 2 and one == 0 and two == 1 and three == 0: enable_mask = 0b111000000100000000 elif action_level == 2 and one == 0 and two == 0 and three == 1: enable_mask = 0b111000010000000000 elif action_level == 2 and one == 1 and two == 1 and three == 0: enable_mask = 0b111000000101000000 elif action_level == 2 and one == 1 and two == 0 and three == 1: enable_mask = 0b111000010001000000 elif action_level == 2 and one == 0 and two == 1 and three == 1: enable_mask = 0b111000010100000000 elif action_level == 2 and one == 1 and two == 1 and three == 1: enable_mask = 0b111000010101000000 elif action_level == 2 and one == 0 and two == 0 and three == 0: enable_mask = 0b011000000000000000 elif action_level == 3 and one == 1 and two == 0 and three == 0: # Level 3 enable_mask = 0b100000000001000000 elif action_level == 3 and one == 0 and two == 1 and three == 0: enable_mask = 0b100000000100000000 elif action_level == 3 and one == 0 and two == 0 and three == 1: enable_mask = 0b100000010000000000 elif action_level == 3 and one == 1 and two == 1 and three == 0: enable_mask = 0b100000000101000000 elif action_level == 3 and one == 1 and two == 0 and three == 1: enable_mask = 0b100000010001000000 elif action_level == 3 and one == 0 and two == 1 and three == 1: enable_mask = 0b100000010100000000 elif action_level == 3 and one == 1 and two == 1 and three == 1: enable_mask = 0b100000010101000000 elif action_level == 3 and one == 0 and two == 0 and three == 0: enable_mask = 0b000000000000000000 # Enable/Disable LEDs enable_leds(enable_mask) output([int((math.sin(float(416) / 64.0) + 1.0) * 128.0)] * 18)
import sn3218 import atexit import time sn3218.enable() sn3218.enable_leds(0b111111111111111111) clear_on_exit = True auto_update = False _legs = [ # r o y g b w [ 6, 7, 8, 5, 4, 9 ], [ 17, 16, 15, 13, 11, 10 ], [ 0, 1, 2, 3, 14, 12 ] ] _map = _legs[0] + _legs[1] + _legs[2] _values = [0] * 18 colours = { "red" : 0, "orange" : 1, "yellow" : 2, "green" : 3, "blue" : 4, "white" : 5 } def white(v): ring(5,v)
def test_enable_leds_non_int(self): """Enable fails with wrong type""" sn3218.output([60] * 18) with self.assertRaisesRegex(TypeError, 'must be an integer'): sn3218.enable_leds("banana")
def setUp(self): """Reset chip for each test""" sn3218.reset() sn3218.enable() sn3218.enable_leds(0b111111111111111111)