├── .gitignore ├── LICENSE ├── PyGlow.py ├── README.md ├── __init__.py ├── examples ├── bin_clock.py ├── clock.py ├── cpu.py ├── pulsetest.py ├── set_leds.py └── test.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | */*.pyc 3 | *~ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jason Barnett, Ben Lebherz, Austin Parker, Jon@Pimoroni, 4 | Jiri Tyr 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /PyGlow.py: -------------------------------------------------------------------------------- 1 | ##### 2 | # 3 | # PyGlow 4 | # 5 | ##### 6 | # 7 | # Python module to control Pimoronis PiGlow 8 | # [http://shop.pimoroni.com/products/piglow] 9 | # 10 | # For more information and documentation see: 11 | # https://github.com/benleb/PYGlow 12 | # 13 | ##### 14 | # 15 | # Author: 16 | # 17 | # Ben Lebherz (@ben_leb) 18 | # 19 | # Contributors: 20 | # 21 | # Austin Parker (@austinlparker) 22 | # - pulse features 23 | # Jon@Pimoroni 24 | # - gamma correction mapping 25 | # Jiri Tyr 26 | # - PEP8 code compliance 27 | # - code refactoring 28 | # - updated project documentation 29 | # - improvements of the pulse function 30 | # - removed the redundant pulse_* methods 31 | # 32 | ##### 33 | 34 | # Import some modules 35 | from smbus import SMBus 36 | from time import sleep 37 | import RPi.GPIO as rpi 38 | import re 39 | 40 | 41 | # Define GPIO addresses 42 | I2C_ADDR = 0x54 43 | EN_OUTPUT_ADDR = 0x00 44 | EN_ARM1_ADDR = 0x13 45 | EN_ARM2_ADDR = 0x14 46 | EN_ARM3_ADDR = 0x15 47 | UPD_PWM_ADDR = 0x16 48 | 49 | # Pulse direction 50 | UP = 1 51 | DOWN = -1 52 | BOTH = 0 53 | 54 | # Define global variables 55 | LED_LIST = tuple(range(1, 19)) 56 | LED_HEX_LIST = ( 57 | 0x07, 0x08, 0x09, 0x06, 0x05, 0x0A, 58 | 0x12, 0x11, 0x10, 0x0E, 0x0C, 0x0B, 59 | 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0D) 60 | ARM_LIST = tuple(range(1, 4)) 61 | ARM_LED_LIST = list(map(tuple, (range(1, 7), range(7, 13), range(13, 19)))) 62 | COLOR_LIST = tuple(range(1, 7)) 63 | COLOR_NAME_LIST = ("white", "blue", "green", "yellow", "orange", "red") 64 | COLOR_LED_LIST = ( 65 | (6, 12, 18), (5, 11, 17), (4, 10, 16), (3, 9, 15), (2, 8, 14), (1, 7, 13)) 66 | GAMMA_TABLE = ( 67 | 0, 1, 1, 1, 1, 1, 1, 1, 68 | 1, 1, 1, 1, 1, 1, 1, 1, 69 | 1, 1, 1, 1, 1, 1, 1, 1, 70 | 1, 1, 1, 1, 1, 1, 1, 1, 71 | 2, 2, 2, 2, 2, 2, 2, 2, 72 | 2, 2, 2, 2, 2, 2, 2, 2, 73 | 2, 2, 2, 3, 3, 3, 3, 3, 74 | 3, 3, 3, 3, 3, 3, 3, 3, 75 | 4, 4, 4, 4, 4, 4, 4, 4, 76 | 4, 4, 4, 5, 5, 5, 5, 5, 77 | 5, 5, 5, 6, 6, 6, 6, 6, 78 | 6, 6, 7, 7, 7, 7, 7, 7, 79 | 8, 8, 8, 8, 8, 8, 9, 9, 80 | 9, 9, 10, 10, 10, 10, 10, 11, 81 | 11, 11, 11, 12, 12, 12, 13, 13, 82 | 13, 13, 14, 14, 14, 15, 15, 15, 83 | 16, 16, 16, 17, 17, 18, 18, 18, 84 | 19, 19, 20, 20, 20, 21, 21, 22, 85 | 22, 23, 23, 24, 24, 25, 26, 26, 86 | 27, 27, 28, 29, 29, 30, 31, 31, 87 | 32, 33, 33, 34, 35, 36, 36, 37, 88 | 38, 39, 40, 41, 42, 42, 43, 44, 89 | 45, 46, 47, 48, 50, 51, 52, 53, 90 | 54, 55, 57, 58, 59, 60, 62, 63, 91 | 64, 66, 67, 69, 70, 72, 74, 75, 92 | 77, 79, 80, 82, 84, 86, 88, 90, 93 | 91, 94, 96, 98, 100, 102, 104, 107, 94 | 109, 111, 114, 116, 119, 122, 124, 127, 95 | 130, 133, 136, 139, 142, 145, 148, 151, 96 | 155, 158, 161, 165, 169, 172, 176, 180, 97 | 184, 188, 192, 196, 201, 205, 210, 214, 98 | 219, 224, 229, 234, 239, 244, 250, 255) 99 | 100 | 101 | class PyGlow: 102 | def __init__( 103 | self, 104 | brightness=None, speed=None, pulse=None, pulse_dir=None): 105 | 106 | # Check what Raspberry Pi version we got 107 | if rpi.RPI_REVISION == 1: 108 | i2c_bus = 0 109 | elif rpi.RPI_REVISION == 2 or rpi.RPI_REVISION == 3: 110 | i2c_bus = 1 111 | else: 112 | raise PyGlowException( 113 | self, "Unknown Raspberry Pi hardware revision: %s" % 114 | (rpi.RPI_REVISION)) 115 | 116 | # Enables the LEDs 117 | self.bus = SMBus(i2c_bus) 118 | 119 | # Tell the SN3218 to enable output 120 | self.bus.write_byte_data(I2C_ADDR, EN_OUTPUT_ADDR, 0x01) 121 | 122 | # Enable each LED arm 123 | self.bus.write_byte_data(I2C_ADDR, EN_ARM1_ADDR, 0xFF) 124 | self.bus.write_byte_data(I2C_ADDR, EN_ARM2_ADDR, 0xFF) 125 | self.bus.write_byte_data(I2C_ADDR, EN_ARM3_ADDR, 0xFF) 126 | 127 | # Set default brightness and pulsing params 128 | self.brightness = brightness 129 | self.speed = speed 130 | self.pulse = pulse 131 | self.pulse_dir = pulse_dir 132 | 133 | # Define the LED state variable 134 | self.__STATE = {'leds': {}, 'params': {}} 135 | 136 | def led( 137 | self, 138 | led, brightness=None, speed=None, pulse=None, pulse_dir=None): 139 | 140 | # Make it list if it's not a list 141 | if isinstance(led, int): 142 | led = [led] 143 | 144 | # Light up the choosen LED 145 | self.set_leds(led, brightness, speed, pulse, pulse_dir).update_leds() 146 | 147 | def color( 148 | self, 149 | color, brightness=None, speed=None, pulse=None, pulse_dir=None): 150 | 151 | leds = () 152 | 153 | # Check if an available color is choosen 154 | if color in COLOR_LIST: 155 | leds = COLOR_LED_LIST[color - 1] 156 | elif color in COLOR_NAME_LIST: 157 | leds = COLOR_LED_LIST[COLOR_NAME_LIST.index(color)] 158 | else: 159 | raise PyGlowException( 160 | self, "Invalid color: %s. Color must be a number from 1 to 6 " 161 | "or a name (%s)." % (color, ', '.join(COLOR_NAME_LIST))) 162 | 163 | # Light up the choosen LEDs 164 | self.set_leds(leds, brightness, speed, pulse, pulse_dir).update_leds() 165 | 166 | def arm( 167 | self, 168 | arm, brightness=None, speed=None, pulse=None, pulse_dir=None): 169 | 170 | leds = () 171 | 172 | # Check if an existing arm is choosen 173 | if arm in ARM_LIST: 174 | leds = ARM_LED_LIST[arm - 1] 175 | else: 176 | raise PyGlowException( 177 | self, "Invalid arm number: %s. Arm must be a number from 1 to " 178 | "3." % (arm)) 179 | 180 | # Light up the choosen LEDs 181 | self.set_leds(leds, brightness, speed, pulse, pulse_dir).update_leds() 182 | 183 | def all(self, brightness=None, speed=None, pulse=None, pulse_dir=None): 184 | # Light up all LEDs 185 | self.set_leds( 186 | LED_LIST, brightness, speed, pulse, pulse_dir).update_leds() 187 | 188 | def set_leds( 189 | self, 190 | leds, brightness=None, speed=None, pulse=None, pulse_dir=None): 191 | 192 | p = self.__STATE['params'] 193 | 194 | # Store parameters value 195 | if brightness is not None: 196 | p['brightness'] = brightness 197 | else: 198 | brightness = self.brightness 199 | if speed is not None: 200 | p['speed'] = speed 201 | if pulse is not None: 202 | p['pulse'] = pulse 203 | if pulse_dir is not None: 204 | p['pulse_dir'] = pulse_dir 205 | 206 | # Check brightness value 207 | if not 0 <= brightness <= 255: 208 | raise PyGlowException( 209 | self, "Invalid brightness level: %s. Brightness level must be " 210 | "a number from 0 to 255." % (brightness)) 211 | 212 | # Pick the gamma-corrected value 213 | gc_value = GAMMA_TABLE[brightness] 214 | 215 | for led in leds: 216 | m = re.match('^([a-z]+)([1-3])$', str(led)) 217 | 218 | if m: 219 | color = m.group(1) 220 | arm = int(m.group(2)) 221 | color_index = None 222 | 223 | # Check if the color has a valid name 224 | if color in COLOR_NAME_LIST: 225 | color_index = COLOR_NAME_LIST.index(color) 226 | else: 227 | raise PyGlowException( 228 | self, 229 | "Invalid color name: %s. Color name must be one of " 230 | "the following: %s." % 231 | (color, ', '.join(COLOR_NAME_LIST))) 232 | 233 | # Check if the arm has a valid number 234 | if arm in ARM_LIST: 235 | led = LED_HEX_LIST[6 * (arm - 1) + 5 - color_index] 236 | else: 237 | raise PyGlowException( 238 | self, "Invalid arm number: %s. Arm must be a number " 239 | "from 1 to 3." % (arm)) 240 | elif ( 241 | isinstance(led, int) and 242 | 1 <= led <= 18): 243 | 244 | led = LED_HEX_LIST[led - 1] 245 | else: 246 | raise PyGlowException( 247 | self, "Invalid LED number %s. LED must be a number from 1 " 248 | "to 18." % (led)) 249 | 250 | # Store the LED and brightness value 251 | self.__STATE['leds'][led] = gc_value 252 | 253 | return self 254 | 255 | def update_leds(self): 256 | p = self.__STATE['params'] 257 | 258 | # Set default parameters value 259 | if 'brightness' not in p or p['brightness'] is None: 260 | p['brightness'] = self.brightness 261 | if 'speed' not in p or p['speed'] is None: 262 | p['speed'] = self.speed 263 | if 'pulse' not in p or p['pulse'] is None: 264 | p['pulse'] = self.pulse 265 | if 'pulse_dir' not in p or p['pulse_dir'] is None: 266 | p['pulse_dir'] = self.pulse_dir 267 | 268 | # Decide whether to pulse or just to light up 269 | if p['brightness'] > 0 and p['pulse']: 270 | self.__pulse( 271 | self.__STATE['leds'].keys(), 272 | p['brightness'], p['speed'], p['pulse_dir']) 273 | else: 274 | self.__write_data(self.__STATE['leds'].items()) 275 | 276 | # Reset the SET variable 277 | self.__STATE = {'leds': {}, 'params': {}} 278 | 279 | def __pulse(self, leds, brightness, speed, pulse_dir): 280 | # Check speed value 281 | if speed < 1: 282 | raise PyGlowException( 283 | self, "Invalid speed: %s. Speed must be a positive non-zero " 284 | "number." % (speed)) 285 | 286 | # Choose pulsation style 287 | elif pulse_dir == UP: 288 | self.__pulse_loop(leds, 0, brightness, speed, UP) 289 | elif pulse_dir == DOWN: 290 | self.__pulse_loop(leds, brightness, 0, speed, DOWN) 291 | else: 292 | self.__pulse_loop(leds, 0, brightness, speed / 2, UP) 293 | self.__pulse_loop(leds, brightness, 0, speed / 2, DOWN) 294 | 295 | def __pulse_loop(self, leds, b_from, b_to, speed, direction): 296 | # Bigger from the pair 297 | b_max = float(max([b_from, b_to])) 298 | 299 | # Starting value 300 | b_val = float(b_from) 301 | 302 | # Maximum steps per second 303 | s_max = 20.0 304 | # Compensation constant 305 | # (the comp_const velue is a compensation of the delay of the other 306 | # commands in the loop - it's influenced by the s_max value) 307 | comp_const = 1030.0 308 | 309 | # Number of steps of the loop 310 | steps = speed / 1000.0 * s_max 311 | 312 | # Default increment value 313 | inc_val = b_max / steps 314 | 315 | # Step up the brightness 316 | for n in range(1, int(steps) + 1): 317 | # Calculate new brightness value 318 | b_val += inc_val * direction 319 | 320 | # Round the final brightness value to the desired value 321 | if n == int(steps) or b_val < 0: 322 | b_val = b_to 323 | 324 | # Set the brightness 325 | self.__write_data( 326 | tuple( 327 | (n, GAMMA_TABLE[int(b_val)]) 328 | for n in self.__STATE['leds'].keys())) 329 | 330 | # Sleep for certain period 331 | sleep(speed / steps / comp_const) 332 | 333 | def __write_data(self, leds): 334 | # Set LED brightness 335 | for led, brightness in leds: 336 | self.bus.write_byte_data( 337 | I2C_ADDR, 338 | int(led), 339 | brightness) 340 | 341 | # Light up all chosen LEDs 342 | self.bus.write_byte_data(I2C_ADDR, UPD_PWM_ADDR, 0xFF) 343 | 344 | 345 | class PyGlowException(Exception): 346 | def __init__(self, parent, msg): 347 | # Switch all LEDs off 348 | parent.all(0) 349 | 350 | self.message = msg 351 | 352 | def __str__(self): 353 | return self.message 354 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PyGlow 2 | ====== 3 | 4 | PyGlow is a small Python module for the PiGlow addon by [Pimoroni](http://www.pimoroni.com/) which will let 5 | you flex the LED muscles of this fantastic addon. It was started as 6 | [PiGlow](https://github.com/Boeeerb/PiGlow) by Jason ([@Boeeerb](https://twitter.com/Boeeerb)) but I 7 | ([@ben_leb](https://twitter.com/ben_leb)) decided to fork it to provide a more clean and easier to use module. 8 | 9 | 10 | Features 11 | ======== 12 | 13 | - Control a single LED, a single arm, a single color or any combination of these 14 | - Pulsing LED 15 | - Gamma Correction (makes the progression from `0-255` more visually linear) 16 | 17 | 18 | Installation instructions 19 | ========================= 20 | 21 | Preparation 22 | ----------- 23 | 24 | For instructions on how to setup Raspberry Pi for use with PyGlow, please see 25 | https://github.com/pimoroni/piglow#setting-up-your-raspberry-pi 26 | 27 | 28 | Requirements 29 | ------------ 30 | 31 | PyGlow module requires the `smbus` Python module. It must be installed before 32 | the PyGlow module is used. Example of installation for 33 | [RASPBIAN](http://raspbian.org/): 34 | 35 | ``` 36 | sudo apt-get install python-smbus 37 | ``` 38 | 39 | 40 | Downloading and testing the PyGlow module 41 | ----------------------------------------- 42 | 43 | Now create a directory for python modules and then change to that directory: 44 | 45 | ``` 46 | $ mkdir -p ~/lib/python/ 47 | $ cd ~/lib/python/ 48 | ``` 49 | 50 | Next get the latest version of PyGlow python module: 51 | 52 | ``` 53 | git clone https://github.com/benleb/PyGlow.git 54 | ``` 55 | 56 | This will download the PyGlow module into the `~/lib/python/PyGlow` directory. 57 | 58 | In order to make the PyGlow module accessible for other scripts, a system 59 | environment variable with a path to the module must be exported (you can add 60 | it into your `~/.bashrc`): 61 | 62 | ``` 63 | export PYTHONPATH=$PYTHONPATH:~/lib/python 64 | ``` 65 | 66 | Now go to the `examples` directory and run the testing script: 67 | 68 | ``` 69 | cd ~/lib/python/PyGlow/examples/ 70 | python test.py 71 | ``` 72 | 73 | If the script loads, you can set the brightness of each LED color group by 74 | typing a number between `0` (off) and `255` (brightest). 75 | 76 | See the other files in the [`examples`](https://github.com/benleb/PyGlow/tree/master/examples) directory for more examples 77 | of how to use PyGlow. 78 | 79 | 80 | Install using pip 81 | ----------------- 82 | 83 | If you just want to install the PyGlow library for use in your own project, 84 | you can also install it using pip 85 | 86 | ``` 87 | $ pip install git+https://github.com/benleb/PyGlow.git 88 | ``` 89 | 90 | The PyGlow.py files will be downloaded and placed in the site-packages directory 91 | ready for use. 92 | 93 | 94 | Usage 95 | ===== 96 | 97 | Instantiation 98 | ------------- 99 | 100 | In order to be able to use PyGlow module, the `PyGlow()` class must be 101 | imported: 102 | 103 | ``` 104 | from PyGlow import PyGlow 105 | ``` 106 | 107 | Then it's possible to instantiate the `PyGlow()` object: 108 | 109 | ``` 110 | pyglow = PyGlow() 111 | ``` 112 | 113 | The `PyGlow()` object can accept four optional parameters: 114 | 115 | - `brightness=None` - sets default brightness level (value: number from `0` and 116 | `255`) 117 | - `speed=None` - sets default pulsing speed in milliseconds (value: number > 0) 118 | - `pulse=None` - enables pulsing by default (value: `True` or `False`) 119 | - `pulse_dir=None` - sets default pulsation direction (value: `UP`, `DOWN`, 120 | `BOTH`) 121 | 122 | The default parameter values are used across all functions unless overridden on 123 | the function level. If the default parameter value is not set during the object 124 | instantiation, it must be set on the function level. The value for the parameter 125 | `speed` and `pulse_dir` must be set only if the parameter `pulse=True`. 126 | 127 | 128 | Functions 129 | --------- 130 | 131 | Functions provided by the `PyGlow()` object are: 132 | 133 | 134 | ### `led(led, brightness=None, speed=None, pulse=None, pulse_dir=None)` 135 | 136 | Sets the specific `led` to the `brightness` level. The `led` is in the range of 137 | `1` to `18`. The `led` parameter can also be a list of individual leds. The 138 | other parameters can have the same value like in the case of the object 139 | instantiation. 140 | 141 | 142 | ### `color(color, brightness=None, speed=None, pulse=None, pulse_dir=None)` 143 | 144 | Sets the specific `color` group to the `brightness` level. The `color` group 145 | can be either a number or a name: 146 | 147 | - `1` = `white` 148 | - `2` = `blue` 149 | - `3` = `green` 150 | - `4` = `yellow` 151 | - `5` = `orange` 152 | - `6` = `red` 153 | 154 | The other parameters can have the same value like in the case of the object 155 | instantiation. 156 | 157 | 158 | ### `arm(arm, brightness=None, speed=None, pulse=None, pulse_dir=None)` 159 | 160 | Sets the specific LED `arm` to the `brightness` level. The `arm` value can be 161 | either `1`, `2` or `3`. The other parameters can have the same value like in 162 | the case of the object instantiation. 163 | 164 | 165 | ### `all(brightness=None, speed=None, pulse=None, pulse_dir=None)` 166 | 167 | Sets all LEDs to the `brightness` level. The parameters can have the same value 168 | like in the case of the object instantiation. 169 | 170 | 171 | ### `set_leds(leds, brightness=None, speed=None, pulse=None, pulse_dir=None)` 172 | 173 | Prepares the list of `leds` to be set to the `brightness` level. The `leds` 174 | list can be composed of numbers from `1` to `18` or connection of `color` name 175 | and `arm` number (e.g. `red1` will light up the `red` LED in the arm `1`). The 176 | other parameters can have the same value like in the case of the object 177 | instantiation. The set `brightness` will take effect only after the 178 | `update_leds()` is called (see bellow). 179 | 180 | 181 | ### `update_leds()` 182 | 183 | Sets the brightness level of all LEDs specified by the `set_leds()` function. 184 | 185 | ``` 186 | leds_odd = [1, 3, 5, 7, 9, 11, 13, 15, 17] 187 | leds_even = [2, 4, 6, 8, 10, 12, 14, 16, 18] 188 | pyglow.set_leds(leds_odd, 150) 189 | pyglow.set_leds(leds_even, 10) 190 | pyglow.update_leds() 191 | ``` 192 | 193 | 194 | Files 195 | ===== 196 | 197 | - `pyglow.py` - Python module providing the PyGlow class 198 | - `examples/bin_clock.py` - binary clock by Jiri Tyr 199 | - `examples/clock.py` - binary clock by Jason ([@Boeeerb](https://twitter.com/Boeeerb)) 200 | - `examples/cpu.py` - CPU percentage indicator by Jason ([@Boeeerb](https://twitter.com/Boeeerb)) 201 | - `examples/pulsetest.py` - shows how to use LED pulsing 202 | - `examples/set_leds.py` - shows how `set_leds()` and `update_leds()` works 203 | - `examples/test.py` - allows to choose the brightness of each LED color group 204 | 205 | 206 | Contributing 207 | ============ 208 | 209 | 1. Fork it 210 | 2. Create your feature branch (`git checkout -b my-new-feature`) 211 | 3. Commit your changes (`git commit -am 'Add some feature'`) 212 | 4. Push to the branch (`git push origin my-new-feature`) 213 | 5. Create new Pull Request 214 | 215 | 216 | License 217 | ======= 218 | 219 | This module is release under the MIT license. 220 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | from .PyGlow import * 2 | -------------------------------------------------------------------------------- /examples/bin_clock.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | 3 | ##### 4 | # 5 | # PyGlow 6 | # 7 | ##### 8 | # 9 | # Python module to control Pimoronis PiGlow 10 | # [http://shop.pimoroni.com/products/piglow] 11 | # 12 | # * bin_clock.py - binary clock by Jiri Tyr 13 | # 14 | ##### 15 | 16 | from __future__ import print_function 17 | 18 | 19 | from datetime import datetime 20 | from PyGlow import PyGlow, ARM_LED_LIST, BOTH 21 | from sys import stdout 22 | from time import sleep 23 | 24 | 25 | def int2bin(num): 26 | return int('{0:b}'.format(num)) 27 | 28 | 29 | def print_time(pg): 30 | now = datetime.now() 31 | cur_time = [now.hour, now.minute, now.second] 32 | 33 | bin_time = tuple(list(map(int2bin, cur_time)) + cur_time) 34 | 35 | stdout.write(' %0.5d | %0.6d | %0.6d (%0.2d:%0.2d:%0.2d)\r' % bin_time) 36 | stdout.flush() 37 | 38 | lst = [] 39 | 40 | for arm_index, arm_bin in enumerate(bin_time[0:3]): 41 | for led_index, c in enumerate("%0.6d" % arm_bin): 42 | if c == '1': 43 | lst.append(ARM_LED_LIST[arm_index][led_index]) 44 | 45 | pg.set_leds(lst).update_leds() 46 | 47 | 48 | def main(): 49 | print(' %5s | %6s | %6s' % ('Hour', 'Minute', 'Second')) 50 | 51 | pg = PyGlow(brightness=150, pulse=True, speed=1000, pulse_dir=BOTH) 52 | 53 | try: 54 | while True: 55 | print_time(pg) 56 | except KeyboardInterrupt: 57 | print('') 58 | pg.all(0) 59 | 60 | 61 | if __name__ == '__main__': 62 | main() 63 | -------------------------------------------------------------------------------- /examples/clock.py: -------------------------------------------------------------------------------- 1 | ##### 2 | # 3 | # PyGlow 4 | # 5 | ##### 6 | # 7 | # Python module to control Pimoronis PiGlow 8 | # [http://shop.pimoroni.com/products/piglow] 9 | # 10 | # * clock.py - binary clock by Jason (@Boeeerb) & Remi (rparpa) 11 | # [https://github.com/Boeeerb/PiGlow] 12 | # 13 | ##### 14 | 15 | 16 | from PyGlow import PyGlow 17 | from time import sleep 18 | from datetime import datetime 19 | 20 | 21 | pyglow = PyGlow() 22 | 23 | ## 24 | # You can customise these settings: 25 | ## 26 | 27 | # Show 12 or 24hr clock - 0= 24hr, 1= 12hr 28 | show12hr = 0 29 | # Set brightness of LED - 1-255 30 | # (recommend 10-20, put 0 and you won't see it!) 31 | led_brightness = 50 32 | # Choose how to flash change of hour - 1= white leds, 2= all flash 33 | hour_flash = 2 34 | 35 | # arms 36 | arm_top = {i: 0 for i in range(1, 7)} 37 | arm_right = {i: 0 for i in range(7, 13)} 38 | arm_bottom = {i: 0 for i in range(13, 19)} 39 | 40 | # link arm to a time value 41 | armConfig = { 42 | "1_seconds": arm_top, 43 | "2_minutes": arm_right, 44 | "3_hours": arm_bottom, 45 | } 46 | 47 | ### 48 | # End of customising 49 | ### 50 | 51 | pyglow.all(0) 52 | 53 | hour_count = 0 54 | hour_current = 0 55 | 56 | 57 | def assign_binary_value_to_arm(binary_value, arm): 58 | arm_led_numbers = [n for n in sorted(arm.iterkeys())] 59 | return {arm_led_numbers[key]: value for key, value in enumerate(reversed(list(binary_value)))} 60 | 61 | 62 | def turn_on_off_led(hour, minute, second): 63 | bin_hour = "%06s" % bin(hour)[2:] 64 | bin_min = "%06s" % bin(minute)[2:] 65 | bin_sec = "%06s" % bin(second)[2:] 66 | 67 | armConfig["1_seconds"] = assign_binary_value_to_arm(bin_sec, armConfig["1_seconds"]) 68 | armConfig["2_minutes"] = assign_binary_value_to_arm(bin_min, armConfig["2_minutes"]) 69 | armConfig["3_hours"] = assign_binary_value_to_arm(bin_hour, armConfig["3_hours"]) 70 | 71 | for key in sorted(armConfig.iterkeys()): 72 | for led_number in sorted(armConfig[key].iterkeys()): 73 | pyglow.led(led_number, led_brightness if armConfig[key][led_number] == "1" else 0) 74 | 75 | while True: 76 | now = datetime.now() 77 | hour = now.hour 78 | 79 | if show12hr == 1 and now.hour > 12: 80 | hour -= 12 81 | 82 | # Check if current hour is different and set ready to flash hour 83 | if hour_current != hour: 84 | hour_count, hour_current = hour, hour 85 | 86 | turn_on_off_led(hour, now.minute, now.second) 87 | 88 | # Flash the white leds for the hour 89 | if hour_count != 0: 90 | sleep(0.5) 91 | if hour_flash == 1: 92 | pyglow.color("white", led_brightness) 93 | if hour_flash == 2: 94 | pyglow.all(led_brightness) 95 | sleep(0.5) 96 | hour_count -= 1 97 | else: 98 | sleep(0.1) 99 | -------------------------------------------------------------------------------- /examples/cpu.py: -------------------------------------------------------------------------------- 1 | ##### 2 | # 3 | # PyGlow 4 | # 5 | ##### 6 | # 7 | # Python module to control Pimoronis PiGlow 8 | # [http://shop.pimoroni.com/products/piglow] 9 | # 10 | # * cpu.py - cpu percentage utilisation indicator by Jason (@Boeeerb) 11 | # [https://github.com/Boeeerb/PiGlow] 12 | # ! requires psutil - sudo apt-get install python-psutil 13 | # 14 | ##### 15 | 16 | 17 | from PyGlow import PyGlow 18 | from time import sleep 19 | import psutil 20 | 21 | 22 | pyglow = PyGlow() 23 | 24 | while True: 25 | 26 | cpu = psutil.cpu_percent() 27 | pyglow.all(0) 28 | 29 | if cpu < 5: 30 | pyglow.color("white", 20) 31 | elif cpu < 20: 32 | pyglow.color("white", 20) 33 | pyglow.color("blue", 20) 34 | elif cpu < 40: 35 | pyglow.color("white", 20) 36 | pyglow.color("blue", 20) 37 | pyglow.color("green", 20) 38 | elif cpu < 60: 39 | pyglow.color("white", 20) 40 | pyglow.color("blue", 20) 41 | pyglow.color("green", 20) 42 | pyglow.color("yellow", 20) 43 | elif cpu < 80: 44 | pyglow.color("white", 20) 45 | pyglow.color("blue", 20) 46 | pyglow.color("green", 20) 47 | pyglow.color("yellow", 20) 48 | pyglow.color("orange", 20) 49 | else: 50 | pyglow.all(20) 51 | 52 | sleep(0.2) 53 | -------------------------------------------------------------------------------- /examples/pulsetest.py: -------------------------------------------------------------------------------- 1 | ##### 2 | # 3 | # PyGlow 4 | # 5 | ##### 6 | # 7 | # Python module to control Pimoronis PiGlow 8 | # [http://shop.pimoroni.com/products/piglow] 9 | # 10 | # * pulsetest.py - test the pulsing light feature 11 | # 12 | ##### 13 | 14 | 15 | from PyGlow import PyGlow 16 | 17 | 18 | b = input("Maximum brightness: ") 19 | s = input("Speed in milliseconds (try 1000 as a default): ") 20 | 21 | pyglow = PyGlow(brightness=int(b), speed=int(s), pulse=True) 22 | 23 | pyglow.all(0) 24 | 25 | print("Pulsing 1 Light") 26 | pyglow.led(1) 27 | 28 | print("Pulsing Arms") 29 | pyglow.arm(1) 30 | pyglow.arm(2) 31 | pyglow.arm(3) 32 | 33 | print("Pulsing All") 34 | pyglow.all() 35 | -------------------------------------------------------------------------------- /examples/set_leds.py: -------------------------------------------------------------------------------- 1 | ##### 2 | # 3 | # PyGlow 4 | # 5 | ##### 6 | # 7 | # Python module to control Pimoronis PiGlow 8 | # [http://shop.pimoroni.com/products/piglow] 9 | # 10 | # * set_leds.py - how to control a individual set of leds by Ben (@ben_leb) 11 | # 12 | ##### 13 | 14 | 15 | from PyGlow import PyGlow 16 | from time import sleep 17 | 18 | 19 | pyglow = PyGlow() 20 | 21 | try: 22 | while True: 23 | # Choose a set of leds 24 | leds = [1, 3, 5, 11, 13, 15] 25 | # Save them with the brightness you want 26 | pyglow.set_leds(leds, 50) 27 | # Wait to demonstrate... 28 | sleep(3) 29 | # Light up the leds! 30 | pyglow.update_leds() 31 | 32 | sleep(3) 33 | 34 | # Now we want to shut down the first set 35 | pyglow.set_leds(leds, 0) 36 | # ...build a newer, brighter set 37 | leds = [2, 4, 9] 38 | pyglow.set_leds(leds, 150) 39 | # and update the leds! 40 | pyglow.update_leds() 41 | 42 | except KeyboardInterrupt: 43 | pyglow.all(0) 44 | -------------------------------------------------------------------------------- /examples/test.py: -------------------------------------------------------------------------------- 1 | ##### 2 | # 3 | # PyGlow 4 | # 5 | ##### 6 | # 7 | # Python module to control Pimoronis PiGlow 8 | # [http://shop.pimoroni.com/products/piglow] 9 | # 10 | # * test.py - set brightness for each color individually 11 | # 12 | ##### 13 | 14 | 15 | from PyGlow import PyGlow 16 | 17 | 18 | pyglow = PyGlow() 19 | 20 | val = input("White: ") 21 | pyglow.color("white", int(val)) 22 | 23 | val = input("Blue: ") 24 | pyglow.color("blue", int(val)) 25 | 26 | val = input("Green: ") 27 | pyglow.color("green", int(val)) 28 | 29 | val = input("Yellow: ") 30 | pyglow.color("yellow", int(val)) 31 | 32 | val = input("Orange: ") 33 | pyglow.color("orange", int(val)) 34 | 35 | val = input("Red: ") 36 | pyglow.color("red", int(val)) 37 | 38 | val = input("All: ") 39 | pyglow.all(int(val)) 40 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | 3 | classifiers = ['Development Status :: 4 - Beta', 4 | 'Operating System :: POSIX :: Linux', 5 | 'License :: OSI Approved :: MIT License', 6 | 'Intended Audience :: Developers', 7 | 'Programming Language :: Python :: 2.6', 8 | 'Programming Language :: Python :: 2.7', 9 | 'Programming Language :: Python :: 3', 10 | 'Topic :: Software Development', 11 | 'Topic :: System :: Hardware'] 12 | 13 | setup(name= 'PyGlow', 14 | version= '0.2', 15 | author= 'Ben Lebherz', 16 | author_email= 'git@benleb.de', 17 | description= 'A module to control the PiGlow Raspberry Pi Addon Board', 18 | long_description= 'A module to control the PiGlow Raspberry Pi Addon Board from Pimoroni', 19 | license= 'MIT', 20 | keywords= 'Raspberry Pi PiGlow', 21 | url= 'https://github.com/benleb/PyGlow', 22 | classifiers = classifiers, 23 | py_modules= ['PyGlow'], 24 | install_requires= ['rpi.gpio >= 0.5.4'] 25 | ) 26 | --------------------------------------------------------------------------------