Last active
January 23, 2020 20:58
-
-
Save smittytone/dccc21ee25dd93e48249 to your computer and use it in GitHub Desktop.
Python source code for macOS task-complete notifier project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import time | |
| import sys | |
| import os | |
| import Adafruit_GPIO as GPIO | |
| import Adafruit_GPIO.FT232H as FT232H | |
| # This class code derivaes from Adafruit's tutorial | |
| # at https://learn.adafruit.com/adafruit-ft232h-breakout | |
| # I had to up the SPI speed to 75MHz to work | |
| # cu.usbserial-141 | |
| class NeoPixel_FT232H(object): | |
| def __init__(self, n): | |
| self.ft232h = FT232H.FT232H() | |
| self.spi = FT232H.SPI(self.ft232h, max_speed_hz=7500000, mode=0, bitorder=FT232H.MSBFIRST) | |
| self.buffer = bytearray(n * 24) | |
| self.lookup = self.build_byte_lookup() | |
| def build_byte_lookup(self): | |
| lookup = {} | |
| for i in range(256): | |
| value = bytearray() | |
| for j in range(7, -1, -1): | |
| if ((i >> j) & 1) == 0: | |
| value.append(0b11100000) | |
| else: | |
| value.append(0b11111000) | |
| lookup[i] = value | |
| return lookup | |
| def set_pixel_color(self, n, r, g, b): | |
| # Set the pixel RGB color for the pixel at position n. | |
| index = n * 24 | |
| self.buffer[index :index+8 ] = self.lookup[int(g)] | |
| self.buffer[index+8 :index+16] = self.lookup[int(r)] | |
| self.buffer[index+16 :index+24] = self.lookup[int(b)] | |
| def show(self): | |
| # Send the pixel buffer out the SPI data output pin (D1) | |
| self.spi.write(self.buffer) | |
| # START | |
| if __name__ == '__main__': | |
| FT232H.use_FT232H() | |
| pixel_count = 1 | |
| pixels = NeoPixel_FT232H(pixel_count) | |
| filename = '/Users/smittytone/.status' # Enter your own path here | |
| brightness = 30 # Brightness control as a percentage | |
| delay = 0.1 | |
| redVal = 252 | |
| greenVal = 0 | |
| blueVal = 0 | |
| redDel = 2 | |
| greenDel = 2 | |
| blueDel = 2 | |
| redOn = True | |
| greenOn = False | |
| blueOn = False | |
| saveRed = 252 | |
| saveGreen = 0 | |
| saveBlue = 0 | |
| noteOne = False | |
| print 'Ctrl-C to quit' | |
| while True: | |
| try: | |
| pixels.set_pixel_color(0, (redVal * brightness / 100), (greenVal * brightness / 100), (blueVal * brightness / 100)) | |
| pixels.show() | |
| if noteOne: | |
| # Notification flag set | |
| if redVal != 0: | |
| redVal = 0 | |
| else: | |
| redVal = 255 | |
| else: | |
| # Notification flag unset; run a colour sweep effect | |
| if redOn: | |
| redVal = redVal + redDel | |
| if redVal > 254: | |
| redVal = 254 | |
| redDel = -2 | |
| greenOn = True | |
| if redVal < 1: | |
| redDel = 2 | |
| redOn = False | |
| redVal = 0 | |
| if greenOn: | |
| greenVal = greenVal + greenDel | |
| if greenVal > 254: | |
| greenDel = -2 | |
| blueOn = True | |
| greenVal = 254 | |
| if greenVal < 1: | |
| greenDel = 2 | |
| greenOn = False | |
| greenVal = 0 | |
| if blueOn: | |
| blueVal = blueVal + blueDel | |
| if blueVal > 254: | |
| blueDel = -1 | |
| redOn = True | |
| blueVal = 254 | |
| if blueVal < 1: | |
| blueDel = 2 | |
| blueOn = False | |
| blueVal = 0 | |
| # Check file | |
| if os.path.exists(filename): | |
| file = open(filename) | |
| text = file.read() | |
| file.close() | |
| # File contains three values, eg. 0.0.0 | |
| # First is a marker for the red light (1 = on; 0 = off) | |
| items = text.split('.') | |
| if items[0] != '0': | |
| # Status file indicates notification light should be on | |
| if noteOne == False: | |
| # So turn it on only if it's currently off | |
| noteOne = True | |
| saveGreen = greenVal | |
| greenVal = 0 | |
| saveRed = redVal | |
| redVal = 255 | |
| saveBlue = blueVal | |
| blueVal = 0 | |
| delay = 0.5 | |
| else: | |
| # Disable the notification... | |
| if noteOne: | |
| # ...but only if it's actually on | |
| noteOne = False | |
| redVal = saveRed | |
| greenVal = saveGreen | |
| blueVal = saveBlue | |
| delay = 0.1 | |
| else: | |
| # No file error - warn the user | |
| print 'No status file' | |
| noteOne = False | |
| redVal = saveRed | |
| greenVal = saveGreen | |
| blueVal = saveBlue | |
| delay = 0.1 | |
| time.sleep(delay) | |
| except KeyboardInterrupt: | |
| for i in range(pixel_count): | |
| pixels.set_pixel_color(0, 0, 0, 0) | |
| pixels.show() | |
| sys.exit(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment