wok-current annotate python-rpi-pyglow/stuff/vortex.py @ rev 24538

updated fbcat (0.5.1 -> 0.5.2)
author Hans-G?nter Theisgen
date Wed Feb 23 15:14:49 2022 +0100 (2022-02-23)
parents 7b9df94be9aa
children
rev   line source
pankso@16511 1 # Sample script for PiGlow that creates a continuous whirly vortex animation
pankso@16511 2 #
pankso@16529 3 # Official Pimorini example modified by Christophe Lincoln for SliTaz ARM
pankso@16529 4 # See our GitHub repository for more information: https://github.com/pimoroni/piglow
pankso@16511 5 #
pankso@16511 6
pankso@16511 7 import time
pankso@16511 8 from smbus import SMBus
pankso@16511 9
pankso@16511 10 # command register addresses for the SN3218 IC used in PiGlow
pankso@16511 11 CMD_ENABLE_OUTPUT = 0x00
pankso@16511 12 CMD_ENABLE_LEDS = 0x13
pankso@16511 13 CMD_SET_PWM_VALUES = 0x01
pankso@16511 14 CMD_UPDATE = 0x16
pankso@16511 15
pankso@16511 16 class PiGlow:
pankso@16511 17 i2c_addr = 0x54 # fixed i2c address of SN3218 ic
pankso@16511 18 bus = None
pankso@16511 19
pankso@16511 20 def __init__(self, i2c_bus=1):
pankso@16511 21 self.bus = SMBus(i2c_bus)
pankso@16511 22
pankso@16511 23 # first we tell the SN3218 to enable output (turn on)
pankso@16511 24 self.write_i2c(CMD_ENABLE_OUTPUT, 0x01)
pankso@16511 25
pankso@16511 26 # then we ask it to enable each bank of LEDs (0-5, 6-11, and 12-17)
pankso@16511 27 self.write_i2c(CMD_ENABLE_LEDS, [0xFF, 0xFF, 0xFF])
pankso@16511 28
pankso@16511 29 def update_leds(self, values):
pankso@16511 30 self.write_i2c(CMD_SET_PWM_VALUES, values)
pankso@16511 31 self.write_i2c(CMD_UPDATE, 0xFF)
pankso@16511 32
pankso@16511 33 # a helper that writes the given value or list of values to the SN3218 IC
pankso@16511 34 # over the i2c protocol
pankso@16511 35 def write_i2c(self, reg_addr, value):
pankso@16511 36 # if a single value is provided then wrap it in a list so we can treat
pankso@16511 37 # all writes in teh same way
pankso@16511 38 if not isinstance(value, list):
pankso@16511 39 value = [value];
pankso@16511 40
pankso@16511 41 # write the data to the SN3218
pankso@16511 42 self.bus.write_i2c_block_data(self.i2c_addr, reg_addr, value)
pankso@16511 43
pankso@16511 44 # a list of 18 values between 0 - 255 that represent each LED on the PiGlow.
pankso@16511 45 # to change the LEDs we set the values in this array and then pass it to the
pankso@16511 46 # update_leds() function to actually update the LDEs
pankso@16511 47 values = [0x01,0x02,0x04,0x08,0x10,0x18,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xC0,0xE0,0xFF]
pankso@16511 48
pankso@16511 49 # create an instance of our PiGlow class and tell it that "1" is the I2C bus
pankso@16511 50 # index (should be 0 for old old old Pis)
pankso@16511 51 piglow = PiGlow(1)
pankso@16511 52
pankso@16511 53 # loop forever, i mean why would we ever want to stop now the party has started?
pankso@16511 54 # you can however use Ctrl+C to stop the script and reset the LEDs to off state
pankso@16511 55 try:
pankso@16529 56 print "Use Ctrl-C to stop"
pankso@16529 57 while True:
pankso@16529 58 # pop the first value off then drop it back on again - this just cycles the values around
pankso@16529 59 values.append(values.pop(0))
pankso@16511 60
pankso@16529 61 # update the piglow with current values
pankso@16529 62 piglow.update_leds(values)
pankso@16511 63
pankso@16529 64 # sleep for a bit, don't go too fast!
pankso@16529 65 time.sleep(0.1)
pankso@16511 66
pankso@16511 67 except KeyboardInterrupt:
pankso@16511 68 # set all the LEDs to "off" when Ctrl+C is pressed before exiting
pankso@16529 69 values = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
pankso@16529 70 piglow.update_leds(values)
pankso@16511 71