wok diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python-rpi-pyglow/stuff/vortex.py Thu Apr 24 20:43:02 2014 +0200 1.3 @@ -0,0 +1,71 @@ 1.4 +# Sample script for PiGlow that creates a continuous whirly vortex animation 1.5 +# 1.6 +# Please see our GitHub repository for more information: https://github.com/pimoroni/piglow 1.7 +# 1.8 +# Once running you'll need to press ctrl-C to cancel stop the script 1.9 + 1.10 +import time 1.11 +from smbus import SMBus 1.12 + 1.13 +# command register addresses for the SN3218 IC used in PiGlow 1.14 +CMD_ENABLE_OUTPUT = 0x00 1.15 +CMD_ENABLE_LEDS = 0x13 1.16 +CMD_SET_PWM_VALUES = 0x01 1.17 +CMD_UPDATE = 0x16 1.18 + 1.19 +class PiGlow: 1.20 + i2c_addr = 0x54 # fixed i2c address of SN3218 ic 1.21 + bus = None 1.22 + 1.23 + def __init__(self, i2c_bus=1): 1.24 + self.bus = SMBus(i2c_bus) 1.25 + 1.26 + # first we tell the SN3218 to enable output (turn on) 1.27 + self.write_i2c(CMD_ENABLE_OUTPUT, 0x01) 1.28 + 1.29 + # then we ask it to enable each bank of LEDs (0-5, 6-11, and 12-17) 1.30 + self.write_i2c(CMD_ENABLE_LEDS, [0xFF, 0xFF, 0xFF]) 1.31 + 1.32 + def update_leds(self, values): 1.33 + print "update pwm" 1.34 + self.write_i2c(CMD_SET_PWM_VALUES, values) 1.35 + self.write_i2c(CMD_UPDATE, 0xFF) 1.36 + 1.37 + # a helper that writes the given value or list of values to the SN3218 IC 1.38 + # over the i2c protocol 1.39 + def write_i2c(self, reg_addr, value): 1.40 + # if a single value is provided then wrap it in a list so we can treat 1.41 + # all writes in teh same way 1.42 + if not isinstance(value, list): 1.43 + value = [value]; 1.44 + 1.45 + # write the data to the SN3218 1.46 + self.bus.write_i2c_block_data(self.i2c_addr, reg_addr, value) 1.47 + 1.48 +# a list of 18 values between 0 - 255 that represent each LED on the PiGlow. 1.49 +# to change the LEDs we set the values in this array and then pass it to the 1.50 +# update_leds() function to actually update the LDEs 1.51 +values = [0x01,0x02,0x04,0x08,0x10,0x18,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xC0,0xE0,0xFF] 1.52 + 1.53 +# create an instance of our PiGlow class and tell it that "1" is the I2C bus 1.54 +# index (should be 0 for old old old Pis) 1.55 +piglow = PiGlow(1) 1.56 + 1.57 +# loop forever, i mean why would we ever want to stop now the party has started? 1.58 +# you can however use Ctrl+C to stop the script and reset the LEDs to off state 1.59 +try: 1.60 + while True: 1.61 + # pop the first value off then drop it back on again - this just cycles the values around 1.62 + values.append(values.pop(0)) 1.63 + 1.64 + # update the piglow with current values 1.65 + piglow.update_leds(values) 1.66 + 1.67 + # sleep for a bit, don't go too fast! 1.68 + time.sleep(0.1) 1.69 + 1.70 +except KeyboardInterrupt: 1.71 + # set all the LEDs to "off" when Ctrl+C is pressed before exiting 1.72 + values = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] 1.73 + piglow.update_leds(values) 1.74 +