wok view python-rpi-pyglow/stuff/vortex.py @ rev 16511

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