def strand_test(): """ Basically the same as the Adafruit stand test """ test_strip = Adafruit_DotStar(ALL_LIGHTS, 12000000, order='bgr') test_strip.begin() test_strip.show() head = 0 # Index of first 'on' pixel tail = -10 # Index of last 'off' pixel color = 0xFF0000 # 'On' color (starts red) counter = ALL_LIGHTS * 3 + 10 while counter > 0: # Loop forever test_strip.setPixelColor(head, color) # Turn on 'head' pixel test_strip.setPixelColor(tail, 0) # Turn off 'tail' test_strip.show() # Refresh strip time.sleep(1.0 / 100) # Pause 20 milliseconds (~50 fps) head += 1 # Advance head position if head >= ALL_LIGHTS: # Off end of strip? head = 0 # Reset to start color >>= 8 # Red->green->blue->black if color == 0: color = 0xFF0000 # If black, reset to red tail += 1 # Advance tail position if tail >= ALL_LIGHTS: tail = 0 # Off end? Reset counter -= 1
class LedStrip: forward = True numpixels = 80 head = 0 tail = -10 def random_color(self): return random.choice(color_library) def __init__(self, datapin, clockpin, brightness): self.datapin = datapin self.clockpin = clockpin self.strip = Adafruit_DotStar(self.numpixels, self.datapin, self.clockpin, order='bgr') self.strip.begin() self.set_brightness(brightness) self.color = self.random_color() def set_pixel(self, pos, color): r, g, b = toRGB(color) self.strip.setPixelColor(pos, gamma[r], gamma[g], gamma[b]) def set_brightness(self, brightness): self.brightness = clamp(brightness, 0, 255) self.strip.setBrightness(self.brightness) def render(self): self.strip.show()
class LedStrip: forward = True numpixels = 80 head = 0 tail = -10 def random_color(self): return random.choice(colors) def __init__(self, datapin, clockpin): self.datapin = datapin self.clockpin = clockpin self.strip = Adafruit_DotStar(self.numpixels, self.datapin, self.clockpin, order='bgr') self.strip.begin() self.strip.setBrightness(255) self.color = self.random_color() def display_pixel(self, pos, color): self.strip.setPixelColor(pos, color) def render(self): self.strip.show()
def init_strip( numpixels , brightness , datapin , clockpin): strip = Adafruit_DotStar(numpixels , 125000000) #strip = Adafruit_DotStar(numpixels, datapin , clockpin) #Initialize pins for output strip.begin() strip.setBrightness(brightness) return strip
def main(render_sdl): if render_sdl: from sdl.driver import sdl_init from sdl.driver import sdl_draw window, renderer, pixels = sdl_init() else: from dotstar import Adafruit_DotStar strip = Adafruit_DotStar() strip.begin() t0 = time.time() i = 0 while True: t = time.time() - t0 i += 1 try: importlib.reload(face) f = face.Face() f.render(t=t, i=i) except (ModuleNotFoundError): continue if render_sdl: print('rendering sdl') sdl_draw(pixels, window, f.grid) else: arr = face.to_arr() render_dotstar(strip, arr)
def lightshow(): numpixels = 72 # Number of LEDs in strip # Here's how to control the strip from any two GPIO pins: datapin = 23 clockpin = 24 strip = Adafruit_DotStar(numpixels, datapin, clockpin) strip.begin() # Initialize pins for output strip.setBrightness(128) # Limit brightness to ~1/4 duty cycle #First, turn all black offspot = 1 offcolor = 0x000000 while offspot >= 1: strip.setPixelColor(offspot, offcolor) offspot += 1 if offspot == 72: break #set initial spots and colors of Jupiter and moons jupspot = 36 #jupiter position jupcolor = 0xFF8801 #jupiter color iospot = iopos #Io position iocolor = 0x9932cc #Io color eurspot = eurpos #Europa position eurcolor = 0x9932cc #Europa color ganspot = ganpos #Ganymede position gancolor = 0x9932cc #Ganymede color calspot = calpos #Calisto position calcolor = 0x9932cc #Calisto color lastcolor = 0x000000 lastiospot = (iospot - 1) gang = [iospot, eurspot, ganspot, calspot] #list of the four moon positions while True: # Loop forever strip.setBrightness(128) # Limit brightness strip.setPixelColor(jupspot, jupcolor) # Turn on jupiter to orange strip.setPixelColor(gang, 0) #Turn off last plots gang[:] = [x + 1 for x in a] # +=1 for each member of gang strip.setPixelColor(gang, iocolor) # Turn all moons to blue strip.show() # Refresh strip time.sleep(1.0 / 5) if t == now: strip.setBrightness(128) #max brightness time.sleep(3) if (iospot >= numpixels): iospot = 0 if (lastiospot >= numpixels): lastiospot = 0
def strandtest(): #!/usr/bin/python # Simple strand test for Adafruit Dot Star RGB LED strip. # This is a basic diagnostic tool, NOT a graphics demo...helps confirm # correct wiring and tests each pixel's ability to display red, green # and blue and to forward data down the line. By limiting the number # and color of LEDs, it's reasonably safe to power a couple meters off # USB. DON'T try that with other code! import time from dotstar import Adafruit_DotStar numpixels = 72 # Number of LEDs in strip # Here's how to control the strip from any two GPIO pins: datapin = 23 clockpin = 24 strip = Adafruit_DotStar(numpixels, datapin, clockpin) # Alternate ways of declaring strip: # Adafruit_DotStar(npix, dat, clk, 1000000) # Bitbang @ ~1 MHz # Adafruit_DotStar(npix) # Use SPI (pins 10=MOSI, 11=SCLK) # Adafruit_DotStar(npix, 32000000) # SPI @ ~32 MHz # Adafruit_DotStar() # SPI, No pixel buffer # Adafruit_DotStar(32000000) # 32 MHz SPI, no pixel buf # See image-pov.py for explanation of no-pixel-buffer use. # Append "order='gbr'" to declaration for proper colors w/older DotStar strips) strip.begin() # Initialize pins for output strip.setBrightness(64) # Limit brightness to ~1/4 duty cycle # Runs 10 LEDs at a time along strip, cycling through red, green and blue. # This requires about 200 mA for all the 'on' pixels + 1 mA per 'off' pixel. head = 0 # Index of first 'on' pixel tail = -10 # Index of last 'off' pixel color = 0xFF0000 # 'On' color (starts red) stranditer = 4 strandnum = 0 while strandnum <= stranditer: # Loop forever strip.setPixelColor(head, color) # Turn on 'head' pixel strip.setPixelColor(tail, 0) # Turn off 'tail' strip.show() # Refresh strip time.sleep(1.0 / 50) # Pause 20 milliseconds (~50 fps) head += 1 # Advance head position if(head >= numpixels): # Off end of strip? head = 0 # Reset to start color >>= 8 # Red->green->blue->black if(color == 0): color = 0xFF0000 # If black, reset to red tail += 1 # Advance tail position if(tail >= numpixels): tail = 0 # Off end? Reset strandnum += 1
def init_arena(): "Create the strip and buffer objects" global strip, arena, changed_panels global spi_clock, height, pwidth, width, npanels strip = Adafruit_DotStar(height*pwidth, spi_clock) strip.begin() #TODO For maximum speed, use a byte array of RGB values instead # (see `image-pov.py` in the Adafruit library for example code) arena = ["black"] * height * width changed_panels = [False] * npanels
class PhysicalStrip: def __init__(self, numpixels): self._strip = Adafruit_DotStar(numpixels) self._strip.begin() def setPixelColor(self, i, color): self._strip.setPixelColor(i, color) def show(self): self._strip.show()
def main(): # set up audio input... recorder = alsaaudio.PCM(alsaaudio.PCM_CAPTURE) recorder.setchannels(CHANNELS) recorder.setrate(RATE) recorder.setformat(INFORMAT) recorder.setperiodsize(FRAMESIZE) # Set up off button GPIO.setmode(GPIO.BCM) GPIO.setup(23,GPIO.IN,pull_up_down = GPIO.PUD_UP) # Initialize colors of each LED... strip = Adafruit_DotStar(numpixels) strip.begin() for i in range(15): time.sleep(float(1.0/float(i+1))) strip.setBrightness(int(stripBright*(i+1)/15)) strip.setPixelColor(i,colors[i][0],colors[i][1],colors[i][2]) strip.setPixelColor(29-i,colors[29-i][0],colors[29-i][1],colors[29-i][2]) strip.show() time.sleep(1) # MAIN LOOP: i=0 bigtime = 0.0 valsold = [] print "IN MAIN LOOP" try: while True: # Check for off button press on = GPIO.input(23) if on == False: shutdown(strip) # Read music and get magnitudes for FRAMESIZE length Y = getMagnitudes(recorder) if Y != None: # Update LED strip based on magnitudes vals = controlLights(strip,Y,valsold) # Update valsold list which is used by my smoothAvg function # to make a running average of brightnesses rather than actual brightnesses valsold.insert(0,vals) if len(valsold) > 20: valsold.pop() if i % 1000 == 0: print "TIME:",time.time()-bigtime print "ITERATION: ",i bigtime = time.time() i+=1 except KeyboardInterrupt: pass
class LedStrip: forward = True numpixels = 80 head = 0 tail = -10 def random_color(self): return random.choice(colors) def __init__(self, datapin, clockpin): self.datapin = datapin self.clockpin = clockpin self.strip = Adafruit_DotStar(self.numpixels, self.datapin, self.clockpin, order='bgr') self.strip.begin() self.strip.setBrightness(255) self.color = self.random_color() def step(self): if self.forward: if self.head < self.numpixels: self.strip.setPixelColor(self.head, self.color) # Turn on 'head' pixel self.strip.setPixelColor(self.tail, 0) # Turn off 'tail' self.head += 1 self.tail += 1 else: self.color = self.random_color() self.head = 70 self.tail = 80 self.forward = False else: # backwards if self.head >= 0: self.strip.setPixelColor(self.head, self.color) # Turn on 'head' pixel self.strip.setPixelColor(self.tail, 0) # Turn off 'tail' self.head = self.head - 1 self.tail = self.tail - 1 else: self.head = 10 self.tail = 0 self.color = self.random_color() self.forward = True def render(self): self.strip.show()
def main(): # Alternate ways of declaring strip: # Only use hardware SPI # print "Data pin GPIO/BCM {0}".format(datapin) # print "Clock pin GPIO/BCM {0}".format(clockpin) print("Opening LED strip with {0} pixels".format(numpixels)) # NOTE: This is not the same as omitting data/clock pin args!!! # strip = Adafruit_DotStar(numpixels, datapin, clockpin, order='gbr') # Use SPI (pins 10=MOSI, 11=SCLK) # strip = Adafruit_DotStar(numpixels, datapin, clockpin, order='grb') # Use SPI (pins 10=MOSI, 11=SCLK) # strip = Adafruit_DotStar(numpixels) # Use SPI (pins 10=MOSI, 11=SCLK) # strip = Adafruit_DotStar(numpixels, order='gbr') # Use SPI (pins 10=MOSI, 11=SCLK) # NOTE: The default color order is BRG (not RGB) strip = Adafruit_DotStar( numpixels, order='rgb'.encode('utf-8')) # Use SPI (pins 10=MOSI, 11=SCLK) strip.begin() # Initialize pins for output strip.setBrightness(brightness) # Limit brightness #strip.setBrightness(127) # Unlimited brightness print("Hit Ctrl-C to end test") try: while True: # scroll_pixels(strip, Color(255, 83, 13), numpixels * 20) # This one pretty much produces the expected results (gamma applied) #scroll_pixels(strip, Color(255, 83, 13, gamma=True), numpixels * 20) #scroll_pixels(strip, rgb_color(255, 0, 0), numpixels * 20) #scroll_pixels(strip, rgb_color(0, 255, 0), numpixels * 20) #scroll_pixels(strip, rgb_color(0, 0, 255), numpixels * 20) #solid_fill(strip, Color(255, 0, 0, gamma=True), iterations=2) #solid_fill(strip, Color(0, 255, 0, gamma=True), iterations=2) #solid_fill(strip, Color(0, 0, 255, gamma=True), iterations=2) wheel_fill(strip, iterations=1) print("Pass complete") except (KeyboardInterrupt, Exception) as ex: print(ex) print("") print("Turning off all lights...") # Not well documented, but this is how you turn # off everything strip.clear() strip.show() strip.close() print("Strip closed")
def rgbStrip(R, G, B): numpixels = 30; # Number of LEDs in strip # strip = Adafruit_DotStar(numpixels, rgb_strip_datapin, rgb_strip_clockpin) strip = Adafruit_DotStar(numpixels) # SPI @ ~32 MHz strip.begin() # Initialize pins for output strip.setBrightness(64) # Limit brightness to ~1/4 duty cycle # Runs 10 LEDs at a time along strip, cycling through red, green and blue. # This requires about 200 mA for all the 'on' pixels + 1 mA per 'off' pixel. led = 0 # Index of first 'on' pixel while (led != 30): # Loop for each light strip.setPixelColor(led, R, G, B) # Set pin color strip.show() # Refresh strip led += 1 # Advance head position\
def rgbStrip(R, G, B): numpixels = 30 # Number of LEDs in strip # strip = Adafruit_DotStar(numpixels, rgb_strip_datapin, rgb_strip_clockpin) strip = Adafruit_DotStar(numpixels) # SPI @ ~32 MHz strip.begin() # Initialize pins for output strip.setBrightness(64) # Limit brightness to ~1/4 duty cycle # Runs 10 LEDs at a time along strip, cycling through red, green and blue. # This requires about 200 mA for all the 'on' pixels + 1 mA per 'off' pixel. led = 0 # Index of first 'on' pixel while (led != 30): # Loop for each light strip.setPixelColor(led, R, G, B) # Set pin color strip.show() # Refresh strip led += 1 # Advance head position\
class Blinkt: def __init__(self, host): self.host = host self.strip = Adafruit_DotStar(numpixels, datapin, clockpin) self.strip.begin() self.strip.setBrightness(32) green = self.to_rgb(0,255,0) self.show_all(green) def to_rgb(self,r,g,b): return (g << 16) + (r << 8) + b def show(self, colour, pixel): self.strip.setPixelColor(pixel, colour) self.strip.show() def show_all(self, colour): for x in range(0,8): self.strip.setPixelColor(x, colour) self.strip.show()
def main(): # Alternate ways of declaring strip: print("Data pin GPIO/BCM {0}".format(datapin)) print("Clock pin GPIO/BCM {0}".format(clockpin)) print("Opening LED strip with {0} pixels".format(numpixels)) # The default here is SPI at 800 KHz # strip = Adafruit_DotStar(numpixels) # Use SPI (pins 10=MOSI, 11=SCLK by default) # This strip uses the specified pins at 800 KHz #strip = Adafruit_DotStar(numpixels, datapin, clockpin, order='gbr') # Use SPI (pins 10=MOSI, 11=SCLK) strip = Adafruit_DotStar( numpixels, order='gbr'.encode('utf-8')) # Use SPI (pins 10=MOSI, 11=SCLK) # strip = Adafruit_DotStar(numpixels, 32000000) # SPI @ ~32 MHz # strip = Adafruit_DotStar() # SPI, No pixel buffer # strip = Adafruit_DotStar(32000000) # 32 MHz SPI, no pixel buf # See image-pov.py for explanation of no-pixel-buffer use. # Append "order='gbr'" to declaration for proper colors w/older DotStar strips) strip.begin() # Initialize pins for output # strip.setBrightness(64) # Limit brightness to ~1/4 duty cycle strip.setBrightness(brightness) # Limit brightness # Runs 10 LEDs at a time along strip, cycling through red, green and blue. # This requires about 200 mA for all the 'on' pixels + 1 mA per 'off' pixel. print("Hit Ctrl-C to end test") try: while True: random_pixels(strip) run_all_effects(strip) scroll_pixels(strip, numpixels * 20) except (KeyboardInterrupt, Exception) as ex: print(ex) print("") print("Turning off all lights...") # Not well documented, but this is how you turn # off everything strip.clear() strip.show() strip.close() print("Strip closed")
class LEDStrip(): datapin = 24 clockpin = 25 npixels = 0 strip = None buffer = None sections_spec = [] sections = None def __init__(self): self.npixels = self.calc_npixels(self.sections_spec) self.strip = Adafruit_DotStar(self.npixels, self.datapin, self.clockpin) self.buffer = bytearray(self.npixels * 4) for i in range(0, self.npixels): self.buffer[i * 4] = 0xff self.sections = {} for spec in self.sections_spec: self.add_section(**spec) self.strip.begin() def calc_npixels(self, sections): npixels = 0 for spec in sections: if not "offset" in spec or spec["offset"] is None: spec["offset"] = npixels npixels = max(npixels, spec["offset"] + spec["length"]) return npixels def add_section(self, name=None, offset=None, length=0, direction=1, voffset=0, vmul=1): self.sections[name] = LEDStripSection(self, offset, length, direction, voffset, vmul)
def rgbStripTest(): numpixels = 30 # Number of LEDs in strip # strip = Adafruit_DotStar(numpixels, datapin, clockpin) strip = Adafruit_DotStar(numpixels, 12000000) # SPI @ ~32 MHz strip.begin() # Initialize pins for output strip.setBrightness(64) # Limit brightness to ~1/4 duty cycle # Runs 10 LEDs at a time along strip, cycling through red, green and blue. # This requires about 200 mA for all the 'on' pixels + 1 mA per 'off' pixel. head = 0 # Index of first 'on' pixel tail = -10 # Index of last 'off' pixel color = 0xFF0000 # 'On' color (starts red) repeat = 0 while True: # Loop forever strip.setPixelColor(head, color) # Turn on 'head' pixel strip.setPixelColor(tail, 0) # Turn off 'tail' strip.show() # Refresh strip time.sleep(1.0 / 50) # Pause 20 milliseconds (~50 fps) head += 1 # Advance head position if (head >= numpixels): # Off end of strip? head = 0 # Reset to start color >>= 8 # Red->green->blue->black if (color == 0): color = 0xFF0000 # If black, reset to red tail += 1 # Advance tail position if (tail >= numpixels): tail = 0 # Off end? Reset repeat += 1 if (repeat == 10): rgbStripOff(strip) break
class StripClient(object): def __init__(self, numpixels): self.numpixels = numpixels self.strip = Adafruit_DotStar(numpixels) self.strip.begin() self.strip.setBrightness(64) def clear(self): self.strip.clear() print("clear") def setBrightness(self, value): self.strip.setBrightness(value) self.strip.show() print("setBrightness:", value) def setPixelColor(self, pixel, color): color_value = ((color[0] & 0xFF) << 8) | ( (color[1] & 0xFF) << 16) | (color[2] & 0xFF) self.strip.setPixelColor(pixel, color_value) self.strip.show() print("setPixelColor:", pixel, color) def show(self): self.strip.show() print("show") def getPixelColor(self): self.strip.getPixelColor() print("getPixelColor") def numPixels(self): return self.strip.numPixels() return self.numpixels def getBrightness(self): return self.strip.getBrightness() return 64
def rgbStripTest(): numpixels = 30; # Number of LEDs in strip # strip = Adafruit_DotStar(numpixels, datapin, clockpin) strip = Adafruit_DotStar(numpixels, 12000000) # SPI @ ~32 MHz strip.begin() # Initialize pins for output strip.setBrightness(64) # Limit brightness to ~1/4 duty cycle # Runs 10 LEDs at a time along strip, cycling through red, green and blue. # This requires about 200 mA for all the 'on' pixels + 1 mA per 'off' pixel. head = 0 # Index of first 'on' pixel tail = -10 # Index of last 'off' pixel color = 0xFF0000 # 'On' color (starts red) repeat = 0 while True: # Loop forever strip.setPixelColor(head, color) # Turn on 'head' pixel strip.setPixelColor(tail, 0) # Turn off 'tail' strip.show() # Refresh strip time.sleep(1.0 / 50) # Pause 20 milliseconds (~50 fps) head += 1 # Advance head position if(head >= numpixels): # Off end of strip? head = 0 # Reset to start color >>= 8 # Red->green->blue->black if(color == 0): color = 0xFF0000 # If black, reset to red tail += 1 # Advance tail position if(tail >= numpixels): tail = 0 # Off end? Reset repeat += 1 if(repeat == 10): rgbStripOff(strip) break;
r = data['requests'] logging.debug('request ' + str(r)) if data['version'] != v: v = data['version'] logging.debug('version ' + str(v)) logging.debug(data) return True, True thread.start_new_thread(getData, (), {'data': data}) return v, r num = 500 s = Adafruit_DotStar(num, 12000000) s.begin() s.setBrightness(255) def mono(h): return h >> 16 def rgb(d): return (d << 16) + (d << 8) + d def twinkle(p): i = mono(s.getPixelColor(p)) if i == 0: if random.random() < 0.03: s.setPixelColor(p, 0xFFFFFF) return True s.setPixelColor(p, 0)
import time import json import atexit from dotstar import Adafruit_DotStar NUM_PIXELS = 324 DATA_PIN = 23 CLOCK_PIN = 24 ANIMATION_PATH = "/opt/hat_server/animations/" STATUS_FILE = "{}current.status".format(ANIMATION_PATH) hat = Adafruit_DotStar(NUM_PIXELS, DATA_PIN, CLOCK_PIN, order='bgr') hat.begin() hat.setBrightness(16) while True: status = None with open(STATUS_FILE, 'r') as statusfile: status = json.loads(statusfile.read()) if (status['type'] == "off"): hat.clear() hat.show() time.sleep(2) continue annimation = None
# Scrape a web page and display it on a light string. import scrape import time # Define this to NeoPixel or DotStar type = "NeoPixel" num_pixels = 7 # Set up whichever type of strip we're using: if type == "DotStar": # For DotStars: from dotstar import Adafruit_DotStar, Color strip = Adafruit_DotStar(num_pixels, 12000000) strip.begin() else: # For NeoPixels: from neopixel import Adafruit_NeoPixel, Color, ws # LED strip configuration: LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!). LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) LED_DMA = 5 # DMA channel to use for generating signal (try 5) LED_BRIGHTNESS = 64 # Set to 0 for darkest and 255 for brightest LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53 LED_STRIP = ws.WS2811_STRIP_GRB # Strip type and colour ordering strip = Adafruit_NeoPixel(num_pixels, LED_PIN, LED_FREQ_HZ, LED_DMA,
def run_strip(mode="off", brightness=255): numpixels = 240 # Number of LEDs in strip print("run_strip inbound mode:", mode) # Here's how to control the strip from any two GPIO pins: datapin = 10 clockpin = 11 strip = Adafruit_DotStar(numpixels, datapin, clockpin) # brightness is an integer from 1 to 255 # mode, in time, should enable other things besides just being full on # strip.begin() # Initialize pins for output # strip.setBrightness(brightness) # Limit brightness to ~1/4 duty cycle # used for mode: comet # head = 0 # Index of first 'on' pixel # tail = -10 # Index of last 'off' pixel color = 0xFFFFFF # 'On' color (starts red) # some control variables for light power on/off easing _pow = 3 _frames = 50 _maxb = 255 # _max_brightness, might change when brightness configuration is enabled if mode == "off": print("run_strip: turn off strip") try: count = 0 const = _maxb**(1 / (_pow * 1.0)) is_turning_off = True strip.begin() while is_turning_off: count += 1 if count == _frames: is_turning_off = False strip.setBrightness(_maxb) # now that ramping is done set # to max configured brightness else: brightness = int(_maxb - (count / (_frames * 1.0) * const)**_pow) strip.setBrightness(brightness) for idx in range(numpixels): strip.setPixelColor(idx, color) strip.show() # Refresh strip time.sleep(1.0 / 50) except KeyboardInterrupt: print("cleaning up") GPIO.cleanup() strip.clear() strip.show() print("done") # try to set color to black/off before turning off to prevent final flash for idx in range(numpixels): strip.setPixelColor(idx, 0x000000) strip.setBrightness(0) # clean up interrupts? # GPIO.cleanup() # strip.clear() strip.show() # print("done") elif mode == "on": print("run_strip: starting strip") try: count = 0 const = _maxb**(1 / (_pow * 1.0)) is_turning_on = True strip.begin() while True: # Loop forever if is_turning_on: count += 1.0 if count == _frames: is_turning_on = False strip.setBrightness( _maxb) # now that ramping is done set # to max configured brightness else: brightness = int( (count / (_frames * 1.0) * const)**_pow) strip.setBrightness(brightness) for idx in range(numpixels): strip.setPixelColor(idx, color) # strip.setPixelColor(head, color) # Turn on 'head' pixel # strip.setPixelColor(tail, 0) # Turn off 'tail' strip.show() # Refresh strip time.sleep(1.0 / 50) # Pause 20 milliseconds (~50 fps) # head += 1 # Advance head position # if(head >= numpixels): # Off end of strip? # head = 0 # Reset to start # color >>= 8 # Red->green->blue->black # if(color == 0): color = 0xFF0000 # If black, reset to red # tail += 1 # Advance tail position # if(tail >= numpixels): tail = 0 # Off end? Reset except KeyboardInterrupt: print("cleaning up") GPIO.cleanup() strip.clear() strip.show() print("done") except SystemError as err: print("SystemError:", err)
class PovFan: def __init__(self): datapin = 10 clockpin = 11 self.column = None self.width = 0 self.sequence = [] self.cur_column = 0 self.images_folder = "" self.next_buffer_index = 0 self.last_push = 0 self.image_start_time = 0 if is_running_on_pi: self.strip = Adafruit_DotStar(0, datapin, clockpin, 18500000) else: self.strip = None def add_image(self, image_path, reverse): if is_running_on_pi == False: # print "add_image" return img = Image.open(image_path).convert("RGB") pixels = img.load() width = img.size[0] height = img.size[1] if (height < STRIP_LENGTH): assert ("trying to load image too small") column = [0 for x in range(width)] for x in range(width): column[x] = bytearray(STRIP_LENGTH * 4) bytess = img.tobytes() for x in range(width): offset = x * 3 multiplier = width * 3 self.strip.prepareBuffer(column[x], bytess, offset, multiplier, reverse) self.sequence.append(column) self.width = width img.close() def disabled_animation(): pass def clear_fan(): pass def loading_animation(): pass #TODO: set sequence length def load_sequence(self, sequence_path, fan_id, seq_size=-1): if is_running_on_pi == False: # print "load_sequence: ", sequence_path, fan_id return start = time.time() path = os.path.join(self.images_folder, 'incoming_images', sequence_path, "fan_" + str(fan_id)) files = sorted(glob.glob(os.path.join(path, "*.png"))) for i in range(len(files)): print "loading image ", i self.add_image(files[i], i % 2) print "loading took ", time.time() - start def transition_image(self, next_image): for i in range(len(self.column)): self.strip.pushBuffer(self.column[i], next_image[i], self.next_buffer_index) self.next_buffer_index = self.next_buffer_index + 1 if self.next_buffer_index / 2 > len(next_image[i]): self.next_buffer_index = 0 print "restart buffer loop" def next_image(self, magnet_synced=False): # check if image have been looping for enough time if time.time() - self.image_start_time > 15: self.next_buffer_index = 0 self.image_start_time = time.time() self.cur_column = (self.cur_column + 1) % len(self.sequence) print "next image is ", self.cur_column self.transition_image(self.sequence[self.cur_column]) def no_magnet_callback(self, timing): timing["lapse_time"] = timing["no_magnet_lapse_time"] timing["last_update"] = time.time() timing["lapses"] = timing["lapses"] + 1 timing["need_swap"] = 0 if timing["lapses"] % 10 == 0: print "MAGNET SENSOR INACTIVE! FALLBACK ESTIMATING SPEED" print "lapse ", timing["lapses"], " refresh count: ", timing[ "refresh_count"] print "lapse time", timing["lapse_time"] timing["refresh_count"] = 0 def play(self, length): if len(self.sequence) == 0: print "No sequence loaded! Cancel play" return if is_running_on_pi == False: return self.strip.begin() end_time = length + time.time() PIXELS_IN_CIRCLE = 2 * self.width self.column = [] for i in range(len(self.sequence[self.cur_column])): r = bytearray(STRIP_LENGTH * 4) r[:] = self.sequence[0][i] self.column.append(r) self.cur_column = 0 self.image_start_time = time.time() timing = { "lapse_time": 0.18, # time to complete lapse "last_update": time.time(), # last frame time "lapses": 0, # number of whole rotations (or every magnet on) "refresh_count": 0, # number of columns showed "need_swap": 0, # track for estimating mid-lapse image swap "max_lapse_time": 0.33, # max time allowed before force swap "use_magnet": True, "no_magnet_lapse_time": 0.2 } print "playing sequence for ", length, "seconds" if is_running_on_pi: def sync_magnet(counter): a = timing def magnet_cbk(m): if not timing["use_magnet"]: return timing["lapse_time"] = m.estimated_rpm() timing["last_update"] = time.time() timing["lapses"] = timing["lapses"] + 1 timing["need_swap"] = 0 if timing["lapses"] % 10 == 0: # print "lapse ", timing["lapses"], " refresh count: ", timing["refresh_count"] # print "lapse time", timing["lapse_time"] timing["refresh_count"] = 0 return magnet_cbk magnet = MagnetButton(16) magnet.when_magnet = sync_magnet(timing) magnet.set_timeout(timing["max_lapse_time"]) while end_time > timing["last_update"]: timing["use_magnet"] = not (magnet.is_not_responding()) if not timing["use_magnet"]: if time.time( ) > timing["last_update"] + timing["no_magnet_lapse_time"]: self.no_magnet_callback(timing) if timing["need_swap"] == 0: self.next_image() timing["need_swap"] = 1 c = int(PIXELS_IN_CIRCLE * (time.time() - timing["last_update"]) / timing["lapse_time"]) # TODO: make this cleaner... if c >= (self.width): if timing["need_swap"] == 1: self.next_image() timing["need_swap"] = 2 ## if overflowing since lapse is longer now c = c % self.width self.strip.show(self.column[c]) timing["refresh_count"] = timing["refresh_count"] + 1 time.sleep(0.0001) magnet.close() else: while end_time > timing["last_update"]: timing["last_update"] = time.time() self.strip.clear() self.strip.close() def stop(self): self.strip.clear() self.strip.close()
#!/usr/bin/env python import sys import os from dotstar import Adafruit_DotStar numpixels = 144 # Number of LEDs in strip def clear(strip): for row in xrange(numpixels): strip.setPixelColor(row, 0) strip.show(); strip = Adafruit_DotStar(numpixels, order='bgr') strip.begin() clear(strip)
class Bartender(MenuDelegate): def __init__(self): self.running = False # set the oled screen height self.screen_width = SCREEN_WIDTH self.screen_height = SCREEN_HEIGHT self.make_drink = MAKE_DRINK self.btn1Pin = LEFT_BTN_PIN self.btn2Pin = RIGHT_BTN_PIN self.clk = CLK_PIN self.dt = DT_PIN # configure interrupts for buttons #GPIO.setup(self.btn1Pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(self.btn2Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # configure interrupts for encoder GPIO.setup(self.clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(self.dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #Pi config RST = 21 DC = 20 SPI_PORT = 0 SPI_DEVICE = 0 # configure screen self.disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) self.disp.begin() self.disp.clear() self.disp.display() #draw self.image = Image.new('1', (SCREEN_WIDTH, SCREEN_HEIGHT)) self.draw = ImageDraw.Draw(self.image) self.draw.rectangle((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), outline=0, fill=0) self.font = ImageFont.load_default() # Very important... This lets py-gaugette 'know' what pins to use in order to reset the display # Change rows & cols values depending on your display dimensions. # load the pump configuration from file self.pump_configuration = Bartender.readJson( '/home/pi/Documents/PiDrink/static/json/pump_config.json') for pump in self.pump_configuration.keys(): GPIO.setup(self.pump_configuration[pump]["pin"], GPIO.OUT, initial=GPIO.HIGH) # load the current drink list self.drink_list = Bartender.readJson( '/home/pi/Documents/PiDrink/static/json/drink_list.json') self.drink_list = self.drink_list["drink_list"] # load the current drink options self.drink_options = Bartender.readJson( '/home/pi/Documents/PiDrink/static/json/drink_options.json') self.drink_options = self.drink_options["drink_options"] # setup pixels: self.numpixels = NUMBER_NEOPIXELS # Number of LEDs in strip # Here's how to control the strip from any two GPIO pins: datapin = NEOPIXEL_DATA_PIN clockpin = NEOPIXEL_CLOCK_PIN self.strip = Adafruit_DotStar(self.numpixels, datapin, clockpin) self.strip.begin() # Initialize pins for output self.strip.setBrightness( NEOPIXEL_BRIGHTNESS) # Limit brightness to ~1/4 duty cycle # turn everything off for i in range(0, self.numpixels): self.strip.setPixelColor(i, 0) self.strip.show() print('Done initializing') # def check_email(self): # status, email_ids = self.imap_server.search(None, '(UNSEEN)') # if email_ids == ['']: # mail_list = [] # else: # mail_list = self.get_subjects(email_ids) #FYI when calling the get_subjects function, the email is marked as 'read' # self.imap_server.close() # return mail_list # def get_subjects(self, email_ids): # subjects_list = [] # for e_id in email_ids[0].split(): # resp, data = self.imap_server.fetch(e_id, '(RFC822)') # perf = HeaderParser().parsestr(data[0][1]) # subjects_list.append(perf['Subject']) # return subjects_list def voice_command(self, mail_list): for mail in mail_list: found = False while (found == False): currmen = self.menuContext.currentMenu.getSelection() if (currmen.name == mail): self.makeDrink(currmen.name, currmen.attributes["ingredients"]) self.make_drink = False found = True else: self.menuContext.currentMenu.nextSelection() found = False @staticmethod def readJson(file): return json.load(open(file)) @staticmethod def writePumpConfiguration(configuration): with open("/home/pi/Documents/PiDrink/static/json/pump_config.json", "w") as jsonFile: json.dump(configuration, jsonFile) def startInterrupts(self): #GPIO.add_event_detect(self.btn1Pin, GPIO.FALLING, callback=self.left_btn, bouncetime=LEFT_PIN_BOUNCE) GPIO.add_event_detect(self.btn2Pin, GPIO.FALLING, callback=self.right_btn, bouncetime=RIGHT_PIN_BOUNCE) GPIO.add_event_detect(self.clk, GPIO.BOTH, callback=self.rotary_on_change, bouncetime=1000) def stopInterrupts(self): #GPIO.remove_event_detect(self.btn1Pin) GPIO.remove_event_detect(self.btn2Pin) GPIO.remove_event_detect(self.clk) def buildMenu(self): # create a new main menu m = Menu("Main Menu") # add drink options drink_opts = [] for d in self.drink_list: drink_opts.append( MenuItem('drink', d["name"], {"ingredients": d["ingredients"]})) configuration_menu = Menu("Configure") # add pump configuration options pump_opts = [] for p in sorted(self.pump_configuration.keys()): config = Menu(self.pump_configuration[p]["name"]) # add fluid options for each pump for opt in self.drink_options: # star the selected option selected = "*" if opt["value"] == self.pump_configuration[p][ "value"] else "" config.addOption( MenuItem('pump_selection', opt["name"], { "key": p, "value": opt["value"], "name": opt["name"] })) # add a back button so the user can return without modifying config.addOption(Back("Back")) config.setParent(configuration_menu) pump_opts.append(config) # add pump menus to the configuration menu configuration_menu.addOptions(pump_opts) # add a back button to the configuration menu configuration_menu.addOption(Back("Back")) # adds an option that cleans all pumps to the configuration menu configuration_menu.addOption(MenuItem('clean', 'Clean')) configuration_menu.setParent(m) m.addOptions(drink_opts) m.addOption(configuration_menu) # create a menu context self.menuContext = MenuContext(m, self) def filterDrinks(self, menu): """ Removes any drinks that can't be handled by the pump configuration """ for i in menu.options: if (i.type == "drink"): i.visible = False ingredients = i.attributes["ingredients"] presentIng = 0 for ing in ingredients.keys(): for p in self.pump_configuration.keys(): if (ing == self.pump_configuration[p]["value"]): presentIng += 1 if (presentIng == len(ingredients.keys())): i.visible = True elif (i.type == "menu"): self.filterDrinks(i) def selectConfigurations(self, menu): """ Adds a selection star to the pump configuration option """ for i in menu.options: if (i.type == "pump_selection"): key = i.attributes["key"] if (self.pump_configuration[key]["value"] == i.attributes["value"]): i.name = "%s %s" % (i.attributes["name"], "*") else: i.name = i.attributes["name"] elif (i.type == "menu"): self.selectConfigurations(i) def prepareForRender(self, menu): self.filterDrinks(menu) self.selectConfigurations(menu) return True def menuItemClicked(self, menuItem): if (menuItem.type == "drink"): if self.make_drink: self.makeDrink(menuItem.name, menuItem.attributes["ingredients"]) self.make_drink = False return True elif (menuItem.type == "pump_selection"): self.pump_configuration[menuItem.attributes["key"]][ "value"] = menuItem.attributes["value"] Bartender.writePumpConfiguration(self.pump_configuration) return True elif (menuItem.type == "clean"): if self.make_drink: self.clean() self.make_drink = False return True return False def changeConfiguration(self, pumps): # Change configuration of every pump for i in range(1, 7): self.pump_configuration["pump_" + str(i)]["value"] = pumps[i - 1] Bartender.writePumpConfiguration(self.pump_configuration) def clean(self): waitTime = 30 pumpProcesses = [] # cancel any button presses while the drink is being made # self.stopInterrupts() self.running = True for pump in self.pump_configuration.keys(): pump_p = threading.Thread(target=self.pour, args=( self.pump_configuration[pump]["pin"], waitTime, )) pumpProcesses.append(pump_p) disp_process = threading.Thread(target=self.progressBar, args=(waitTime, )) pumpProcesses.append(disp_process) # start the pump threads for process in pumpProcesses: process.start() # wait for threads to finish for process in pumpProcesses: process.join() # sleep for a couple seconds to make sure the interrupts don't get triggered time.sleep(2) # reenable interrupts # self.startInterrupts() self.running = False self.make_drink = True self.menuContext.showMenu() def displayMenuItem(self, menuItem): #Clear display self.draw.rectangle((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), outline=0, fill=0) self.draw.text((20, 10), menuItem.name, font=self.font, fill=255) self.disp.image(self.image) self.disp.display() def cycleLights(self): t = threading.currentThread() head = 0 # Index of first 'on' pixel tail = -10 # Index of last 'off' pixel color = 0xFF0000 # 'On' color (starts red) while getattr(t, "do_run", True): self.strip.setPixelColor(head, color) # Turn on 'head' pixel self.strip.setPixelColor(tail, 0) # Turn off 'tail' self.strip.show() # Refresh strip time.sleep(1.0 / 50) # Pause 20 milliseconds (~50 fps) head += 1 # Advance head position if (head >= self.numpixels): # Off end of strip? head = 0 # Reset to start color >>= 8 # Red->green->blue->black if (color == 0): color = 0xFF0000 # If black, reset to red tail += 1 # Advance tail position if (tail >= self.numpixels): tail = 0 # Off end? Reset def lightsEndingSequence(self): # make lights green for i in range(0, self.numpixels): self.strip.setPixelColor(i, 0xFF0000) self.strip.show() time.sleep(5) # turn lights off for i in range(0, self.numpixels): self.strip.setPixelColor(i, 0) self.strip.show() def pour(self, pin, waitTime): GPIO.output(pin, GPIO.LOW) time.sleep(waitTime) GPIO.output(pin, GPIO.HIGH) def progressBar(self, waitTime): interval = waitTime / 116.3 for x in range(1, 101): # Clear display self.draw.rectangle((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), outline=0, fill=0) #self.draw.text((55,20), str(x) + '%', font = self.font, fill=255) self.updateProgressBar(x, y=10) self.disp.image(self.image) self.disp.display() time.sleep(interval) # # self.disp.clear() # # self.disp.display() # self.image = Image.open('happycat_oled_32.ppm').convert('1') # self.disp.image(self.image) # self.disp.display() def makeDrink(self, drink, ingredients): if self.running: return # cancel any button presses while the drink is being made # self.stopInterrupts() print('Making a ' + drink) self.running = True # launch a thread to control lighting lightsThread = threading.Thread(target=self.cycleLights) lightsThread.start() # Make a list for each potential time maxTime = 0 pumpProcesses = [] for ing in ingredients.keys(): for pump in self.pump_configuration.keys(): if ing == self.pump_configuration[pump]["value"]: waitTime = ingredients[ing] * FLOW_RATE if (waitTime > maxTime): maxTime = waitTime pump_p = threading.Thread( target=self.pour, args=(self.pump_configuration[pump]["pin"], waitTime)) pumpProcesses.append(pump_p) disp_process = threading.Thread(target=self.progressBar, args=(maxTime, )) pumpProcesses.append(disp_process) # start the pump threads for process in pumpProcesses: process.start() # wait for threads to finish for process in pumpProcesses: process.join() # stop the light thread lightsThread.do_run = False lightsThread.join() # show the ending sequence lights self.lightsEndingSequence() # reenable interrupts # sleep for a couple seconds to make sure the interrupts don't get triggered time.sleep(2) # self.startInterrupts() self.running = False print('Finished making ' + drink) self.make_drink = True # show the main menu self.menuContext.showMenu() def left_btn(self, ctx): if not self.running: self.menuContext.advance() def right_btn(self, ctx): if not self.running: self.menuContext.select() def rotary_on_change(self, ctx): if not self.running: if GPIO.input(self.dt): self.menuContext.retreat() else: self.menuContext.advance() def get_ingredients_time(self, drink): # Get the ingredients for the specified drink found = False while (found == False): currmen = self.menuContext.currentMenu.getSelection() if (currmen.name == drink): ingredients = currmen.attributes["ingredients"] self.make_drink = False found = True else: self.menuContext.currentMenu.nextSelection() # Get the drink making time maxTime = 0 for ing in ingredients.keys(): for pump in self.pump_configuration.keys(): if ing == self.pump_configuration[pump]["value"]: waitTime = ingredients[ing] * FLOW_RATE if (waitTime > maxTime): maxTime = waitTime # Return making time and ingredients for drink return ingredients, maxTime def updateProgressBar(self, percent, x=15, y=10): height = 10 width = self.screen_width - 2 * x for w in range(0, width): self.draw.point((w + x, y), fill=255) self.draw.point((w + x, y + height), fill=255) for h in range(0, height): self.draw.point((x, h + y), fill=255) self.draw.point((self.screen_width - x, h + y), fill=255) for p in range(0, percent): p_loc = int(p / 100.0 * width) self.draw.point((x + p_loc, h + y), fill=255) self.draw.text((55, 20), str(percent) + '%', font=self.font, fill=255) #self.disp.image(self.image) #self.disp.display() def endprogram(self): global stopprogram stopprogram = True # Goodbye message self.draw.rectangle((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), outline=0, fill=0) self.draw.text((25, 10), "Goodbye...", font=self.font, fill=255) self.disp.image(self.image) self.disp.display() time.sleep(3) self.draw.rectangle((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), outline=0, fill=0) self.draw.text((15, 10), "Have a great day!", font=self.font, fill=255) self.disp.image(self.image) self.disp.display() time.sleep(3) self.disp.clear() self.disp.display() def run(self): self.startInterrupts() # main loop try: while True: if stopprogram: return # self.imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993) # self.imap_server.login(USERNAME, PASSWORD) # self.imap_server.select('INBOX') # mail_list = self.check_email() # if mail_list and not self.running: # self.voice_command(mail_list) time.sleep(0.1) except KeyboardInterrupt: GPIO.cleanup() # clean up GPIO on CTRL+C exit sys.exit(0) GPIO.cleanup() # clean up GPIO on normal exit traceback.print_exc()
import ephem import time from dotstar import Adafruit_DotStar #INITIALIZE AND CLEAR DOTSTAR numpixels = 72 # Number of LEDs in strip # Here's how to control the strip from any two GPIO pins: datapin = 23 clockpin = 24 strip = Adafruit_DotStar(numpixels, datapin, clockpin) strip.begin() # Initialize pins for output strip.setBrightness(8) # Limit brightness to ~1/4 duty cycle #turn all leds to black (off) offspot = 1 offcolor = 0x000000 nump = 73 while offspot < nump: strip.setPixelColor(offspot, offcolor) offspot += 1 strip.show() #PYEPHEM PART--GET MOON SPOTS moons = ((ephem.Io(), 'i'), (ephem.Europa(), 'e'),
class Bartender(MenuDelegate): def __init__(self): self.running = False # set the oled screen height self.screen_width = SCREEN_WIDTH self.screen_height = SCREEN_HEIGHT self.btn1Pin = LEFT_BTN_PIN self.btn2Pin = RIGHT_BTN_PIN # configure interrups for buttons GPIO.setup(self.btn1Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(self.btn2Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # configure screen spi_bus = 0 spi_device = 0 # Very important... This lets py-gaugette 'know' what pins to use in order to reset the display self.led = disp = Adafruit_SSD1306.SSD1306_128_64( rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000) ) # Change rows & cols values depending on your display dimensions. # Initialize library. self.led.begin() # Clear display. self.led.clear() self.led.display() # Create image buffer. # Make sure to create image with mode '1' for 1-bit color. self.image = Image.new('1', (self.screen_width, self.screen_height)) # Load default font. self.font = ImageFont.truetype("FreeMono.ttf", 15) # Create drawing object. self.draw = ImageDraw.Draw(self.image) # load the pump configuration from file self.pump_configuration = Bartender.readPumpConfiguration() for pump in self.pump_configuration.keys(): GPIO.setup(self.pump_configuration[pump]["pin"], GPIO.OUT, initial=GPIO.HIGH) # setup pixels: self.numpixels = NUMBER_NEOPIXELS # Number of LEDs in strip # Here's how to control the strip from any two GPIO pins: datapin = NEOPIXEL_DATA_PIN clockpin = NEOPIXEL_CLOCK_PIN self.strip = Adafruit_DotStar(self.numpixels, datapin, clockpin) self.strip.begin() # Initialize pins for output self.strip.setBrightness( NEOPIXEL_BRIGHTNESS) # Limit brightness to ~1/4 duty cycle # Set the Default or "StandBy Light" to Blue in this case for i in range(0, self.numpixels): self.strip.setPixelColor(i, COLOR_BLUE) self.strip.show() print "Done initializing" @staticmethod def readPumpConfiguration(): return json.load(open('pump_config.json')) @staticmethod def writePumpConfiguration(configuration): with open("pump_config.json", "w") as jsonFile: json.dump(configuration, jsonFile) def startInterrupts(self): self.running = True GPIO.add_event_detect(self.btn1Pin, GPIO.FALLING, callback=self.left_btn, bouncetime=LEFT_PIN_BOUNCE) time.sleep(1) GPIO.add_event_detect(self.btn2Pin, GPIO.FALLING, callback=self.right_btn, bouncetime=RIGHT_PIN_BOUNCE) time.sleep(1) self.running = False def buildMenu(self, drink_list, drink_options): # create a new main menu m = Menu("Main Menu") # add drink options drink_opts = [] for d in drink_list: drink_opts.append( MenuItem('drink', d["name"], {"ingredients": d["ingredients"]})) configuration_menu = Menu("Configure") # add pump configuration options pump_opts = [] for p in sorted(self.pump_configuration.keys()): config = Menu(self.pump_configuration[p]["name"]) # add fluid options for each pump for opt in drink_options: # star the selected option selected = "*" if opt["value"] == self.pump_configuration[p][ "value"] else "" config.addOption( MenuItem('pump_selection', opt["name"], { "key": p, "value": opt["value"], "name": opt["name"] })) # add a back button so the user can return without modifying config.addOption(Back("Back")) config.setParent(configuration_menu) pump_opts.append(config) # add pump menus to the configuration menu configuration_menu.addOptions(pump_opts) # add a back button to the configuration menu configuration_menu.addOption(Back("Back")) # adds an option that cleans all pumps to the configuration menu configuration_menu.addOption(MenuItem('clean', 'Clean')) configuration_menu.setParent(m) m.addOptions(drink_opts) m.addOption(configuration_menu) # create a menu context self.menuContext = MenuContext(m, self) def filterDrinks(self, menu): """ Removes any drinks that can't be handled by the pump configuration """ for i in menu.options: if (i.type == "drink"): i.visible = False ingredients = i.attributes["ingredients"] presentIng = 0 for ing in ingredients.keys(): for p in self.pump_configuration.keys(): if (ing == self.pump_configuration[p]["value"]): presentIng += 1 if (presentIng == len(ingredients.keys())): i.visible = True elif (i.type == "menu"): self.filterDrinks(i) def selectConfigurations(self, menu): """ Adds a selection star to the pump configuration option """ for i in menu.options: if (i.type == "pump_selection"): key = i.attributes["key"] if (self.pump_configuration[key]["value"] == i.attributes["value"]): i.name = "%s %s" % (i.attributes["name"], "*") else: i.name = i.attributes["name"] elif (i.type == "menu"): self.selectConfigurations(i) def prepareForRender(self, menu): self.filterDrinks(menu) self.selectConfigurations(menu) return True def menuItemClicked(self, menuItem): if (menuItem.type == "drink"): self.makeDrink(menuItem.name, menuItem.attributes["ingredients"]) return True elif (menuItem.type == "pump_selection"): self.pump_configuration[menuItem.attributes["key"]][ "value"] = menuItem.attributes["value"] Bartender.writePumpConfiguration(self.pump_configuration) return True elif (menuItem.type == "clean"): self.clean() return True return False def clean(self): waitTime = 20 pumpThreads = [] # cancel any button presses while the drink is being made # self.stopInterrupts() self.running = True for pump in self.pump_configuration.keys(): pump_t = threading.Thread( target=self.pour, args=(self.pump_configuration[pump]["pin"], waitTime)) pumpThreads.append(pump_t) # start the pump threads for thread in pumpThreads: thread.start() # start the progress bar self.progressBar(waitTime) # wait for threads to finish for thread in pumpThreads: thread.join() # show the main menu self.menuContext.showMenu() # sleep for a couple seconds to make sure the interrupts don't get triggered time.sleep(2) def displayMenuItem(self, menuItem): print menuItem.name self.led.clear() self.draw.rectangle((0, 0, self.screen_width, self.screen_height), outline=0, fill=0) self.draw.text((0, 20), str(menuItem.name), font=self.font, fill=255) self.led.image(self.image) self.led.display() def cycleLights(self): t = threading.currentThread() head = 0 # Index of first 'on' pixel tail = -10 # Index of last 'off' pixel color = COLOR_RED # 'On' color (starts red) while getattr(t, "do_run", True): self.strip.setPixelColor(head, color) # Turn on 'head' pixel self.strip.setPixelColor(tail, 0) # Turn off 'tail' self.strip.show() # Refresh strip time.sleep(1.0 / 50) # Pause 20 milliseconds (~50 fps) head += 1 # Advance head position if (head >= self.numpixels): # Off end of strip? head = 0 # Reset to start if (color == COLOR_RED): color = COLOR_YELLOW # if red,set to yellow elif (color == COLOR_YELLOW): color = COLOR_BLUE # if yellow,set to blue elif (color == COLOR_BLUE): color = COLOR_RED # if blue,set back to red tail += 1 # Advance tail position if (tail >= self.numpixels): tail = 0 # Off end? Reset def lightsEndingSequence(self): # make lights yellow for i in range(0, self.numpixels): self.strip.setPixelColor(i, COLOR_YELLOW) self.strip.show() os.system("mpg123 " + DONESOUND) # time.sleep(5) # set them back to blue "StandBy Light" for i in range(0, self.numpixels): self.strip.setPixelColor(i, COLOR_BLUE) self.strip.show() def pour(self, pin, waitTime): GPIO.output(pin, GPIO.LOW) time.sleep(waitTime) GPIO.output(pin, GPIO.HIGH) # other way of dealing with Display delay, Thanks Yogesh def progressBar(self, waitTime): #-with the outcommented version, it updates faster, but there is a limit with the delay, you have to figure out-# #mWaitTime = waitTime - 7 #interval = mWaitTime/ 100.0 #if interval < 0.07: # interval = 0 #for x in range(1, 101): interval = waitTime / 10.0 for x in range(1, 11): self.led.clear() self.draw.rectangle((0, 0, self.screen_width, self.screen_height), outline=0, fill=0) # self.updateProgressBar(x, y=35) self.updateProgressBar(x * 10, y=35) self.led.image(self.image) self.led.display() time.sleep(interval) def makeDrink(self, drink, ingredients): # cancel any button presses while the drink is being made # self.stopInterrupts() os.system("mpg123 " + DRINKME) self.running = True # launch a thread to control lighting lightsThread = threading.Thread(target=self.cycleLights) lightsThread.start() # Parse the drink ingredients and spawn threads for pumps maxTime = 0 pumpThreads = [] for ing in ingredients.keys(): for pump in self.pump_configuration.keys(): if ing == self.pump_configuration[pump]["value"]: vWaitTime = self.pump_configuration[pump]["flowrate"] waitTime = ingredients[ing] * vWaitTime if (waitTime > maxTime): maxTime = waitTime pump_t = threading.Thread( target=self.pour, args=(self.pump_configuration[pump]["pin"], waitTime)) pumpThreads.append(pump_t) # start the pump threads for thread in pumpThreads: thread.start() #Show in Console how long the Pumps running print("The pumps run for", maxTime, "seconds") # start the progress bar self.progressBar(maxTime) # wait for threads to finish for thread in pumpThreads: thread.join() # show the main menu self.menuContext.showMenu() # stop the light thread lightsThread.do_run = False lightsThread.join() # show the ending sequence lights self.lightsEndingSequence() # sleep for a couple seconds to make sure the interrupts don't get triggered # time.sleep(2); # reenable interrupts # self.startInterrupts() self.running = False def left_btn(self, ctx): print("LEFT_BTN pressed") if not self.running: self.running = True self.menuContext.advance() print("Finished processing button press") self.running = False def right_btn(self, ctx): print("RIGHT_BTN pressed") if not self.running: self.running = True self.menuContext.select() print("Finished processing button press") self.running = 2 print("Starting button timeout") def updateProgressBar(self, percent, x=15, y=15): height = 25 width = self.screen_width - 2 * x for w in range(0, width): self.draw.point((w + x, y), fill=255) self.draw.point((w + x, y + height), fill=255) for h in range(0, height): self.draw.point((x, h + y), fill=255) self.draw.point((self.screen_width - x, h + y), fill=255) for p in range(0, percent): p_loc = int(p / 100.0 * width) self.draw.point((x + p_loc, h + y), fill=255) def run(self): self.startInterrupts() # main loop try: try: while True: letter = raw_input(">") if letter == "l": self.left_btn(False) if letter == "r": self.right_btn(False) except EOFError: while True: time.sleep(0.1) if self.running not in (True, False): self.running -= 0.1 if self.running == 0: self.running = False print("Finished button timeout") except KeyboardInterrupt: GPIO.cleanup() # clean up GPIO on CTRL+C exit GPIO.cleanup() # clean up GPIO on normal exit traceback.print_exc()
def led_strip(numpixels): strip = Adafruit_DotStar(numpixels) strip.begin() return strip
class SnipsMatrix: queue = Queue.Queue() state_hotword = None state_waiting = None state_rotate = None state_time = None state_weather = None timerstop = None timer_hardware = None timer_short_app = None timer = None hotword_status = False custom_anim = False def __init__(self): numpixels = 128 datapin = 10 clockpin = 11 self.strip = Adafruit_DotStar(numpixels, datapin, clockpin, 1000000) self.strip.begin() self.strip.setBrightness(64) SnipsMatrix.state_hotword = AnimationImage('hotword', self.strip) SnipsMatrix.state_time = AnimationTime(self.strip) SnipsMatrix.state_rotate = AnimationRotate(self.strip, 0) SnipsMatrix.state_weather = AnimationWeather(self.strip) SnipsMatrix.custom_anim = SnipsMatrix.load_custom_animation(self.strip) SnipsMatrix.queue.put(snipsMatrixAction.Hotword()) SnipsMatrix.queue.put(snipsMatrixAction.Clear( DisplayPriority.hardware)) t = threading.Thread(target=SnipsMatrix.worker, args=()) t.start() def hotword_detected(self): SnipsMatrix.hotword_status = True SnipsMatrix.queue.put(snipsMatrixAction.Hotword()) def stop(self): print('stop all animation') self.stop_all_timer() SnipsMatrix.queue.put(snipsMatrixAction.Clear( DisplayPriority.hardware)) def exit(self): print('exit snipsmatrix') self.stop_all_timer() SnipsMatrix.queue.put(snipsMatrixAction.Exit()) def stop_hotword(self): print('stop hotword') SnipsMatrix.hotword_status = False SnipsMatrix.queue.put(snipsMatrixAction.Clear(DisplayPriority.hotword)) def save_image(self, name, directory, image): already_exist = True if (image is None): return if not os.path.exists(CONFIG_INI_DIR): os.makedirs(CONFIG_INI_DIR) if not os.path.exists(CONFIG_INI_DIR + directory): os.makedirs(CONFIG_INI_DIR + directory) already_exist = False f_name = "{}{}/{}".format(CONFIG_INI_DIR, directory, name) try: with open(f_name, 'w') as f: f.write(image) except IOError as e: print(e) return if already_exist: del SnipsMatrix.custom_anim[directory] SnipsMatrix.custom_anim[directory] = AnimationImage( CONFIG_INI_DIR + directory, self.strip, True) def show_time(self, duration, value): if duration is None: duration = 70 SnipsMatrix.create_timer_time(duration, value) def show_animation(self, name, duration=15): SnipsMatrix.queue.put(snipsMatrixAction.CustomAnimation(name)) if duration is None: duration = 12 SnipsMatrix.create_short_app_timer(duration) def show_timer(self, duration): if duration is None: return SnipsMatrix.create_timer(duration) def show_rotate(self, vol): if vol is None: return SnipsMatrix.queue.put(snipsMatrixAction.Rotate(vol)) SnipsMatrix.create_hardware_timer(10) def show_weather(self, tmp, weather): SnipsMatrix.queue.put(snipsMatrixAction.Weather(weather, tmp)) SnipsMatrix.create_short_app_timer(20) @staticmethod def load_custom_animation(strip): dirs = glob.glob("{}*/".format(CONFIG_INI_DIR)) names = [(x.split('/')[-2], x) for x in dirs] res = {} for k in names: res[k[0]] = AnimationImage(k[1], strip, True) res['light'] = AnimationImage('light', strip) res['music'] = AnimationImage('music', strip) return res @staticmethod def worker(): item = "" oldItem = "" goback = False flip = False while True: time.sleep(0.01) if (not SnipsMatrix.queue.empty()): oldItem = item goback = False item = SnipsMatrix.queue.get_nowait() SnipsMatrix.queue.task_done() print(item) if isinstance(item, snipsMatrixAction.Timer): if DisplayPriority.can_I_do_it(DisplayPriority.schedule_apps): SnipsMatrix.state_time.show(item, flip) item = "" else: item = oldItem if isinstance(item, snipsMatrixAction.Time): if DisplayPriority.can_I_do_it(DisplayPriority.short_apps): SnipsMatrix.state_time.show(item, flip) item = "" else: item = oldItem elif isinstance(item, snipsMatrixAction.Rotate): if DisplayPriority.can_I_do_it(DisplayPriority.hardware): SnipsMatrix.state_rotate.show(item) item = "" else: item = oldItem elif isinstance(item, snipsMatrixAction.Weather): if DisplayPriority.can_I_do_it(DisplayPriority.short_apps): SnipsMatrix.state_weather.show(item, flip) item = "" else: item = oldItem elif isinstance(item, snipsMatrixAction.Hotword): if DisplayPriority.can_I_do_it(DisplayPriority.hotword): SnipsMatrix.state_hotword.show() else: item = oldItem elif isinstance(item, snipsMatrixAction.Clear): if DisplayPriority.can_I_do_it(item.value): SnipsMatrix.state_hotword.reset(0) item = "" else: item = oldItem elif isinstance(item, snipsMatrixAction.Exit): return elif isinstance(item, snipsMatrixAction.CustomAnimation): if DisplayPriority.can_I_do_it(DisplayPriority.short_apps): SnipsMatrix.showCustomAnimation(item) else: item = oldItem @staticmethod def showCustomAnimation(item): if (item.value in SnipsMatrix.custom_anim): print(item.value) SnipsMatrix.custom_anim[item.value].show() @staticmethod def create_timer(duration, stop_time=None): if stop_time is None: stop_time = int(time.time()) + duration duration = stop_time - int(time.time()) if SnipsMatrix.timer: SnipsMatrix.timer.cancel() del SnipsMatrix.timer SnipsMatrix.timer = None if duration >= 0: SnipsMatrix.queue.put(snipsMatrixAction.Timer(duration)) elif duration >= -2: SnipsMatrix.queue.put(snipsMatrixAction.Timer(0)) else: SnipsMatrix.queue.put( snipsMatrixAction.Clear(DisplayPriority.schedule_apps)) return SnipsMatrix.timer = threading.Timer(1, SnipsMatrix.create_timer, args=[duration, stop_time]) SnipsMatrix.timer.start() @staticmethod def create_hardware_timer(duration): if SnipsMatrix.timer_hardware: SnipsMatrix.timer_hardware.cancel() del SnipsMatrix.timer_hardware SnipsMatrix.timer_hardware = None SnipsMatrix.timer_hardware = threading.Timer( duration, SnipsMatrix.stop_show_hardware) SnipsMatrix.timer_hardware.start() @staticmethod def create_short_app_timer(duration): if SnipsMatrix.timer_short_app: SnipsMatrix.timer_short_app.cancel() del SnipsMatrix.timer_short_app SnipsMatrix.timer_short_app = None SnipsMatrix.timer_short_app = threading.Timer( duration, SnipsMatrix.stop_show_short_app) SnipsMatrix.timer_short_app.start() @staticmethod def create_timer_time(duration, value): if value is None or value <= 0: SnipsMatrix.queue.put(snipsMatrixAction.Time(time.time())) else: SnipsMatrix.queue.put(snipsMatrixAction.Time(value)) if SnipsMatrix.timer_short_app: SnipsMatrix.timer_short_app.cancel() del SnipsMatrix.timer_short_app SnipsMatrix.timer_short_app = None if duration < 0: SnipsMatrix.queue.put( snipsMatrixAction.Clear(DisplayPriority.short_apps)) return SnipsMatrix.timer_short_app = threading.Timer( 1, SnipsMatrix.create_timer_time, args=[duration - 1, value]) SnipsMatrix.timer_short_app.start() @staticmethod def stop_all_timer(): if SnipsMatrix.timerstop: SnipsMatrix.timerstop.cancel() del SnipsMatrix.timerstop SnipsMatrix.timerstop = None if SnipsMatrix.timer: SnipsMatrix.timer.cancel() del SnipsMatrix.timer SnipsMatrix.timer = None if SnipsMatrix.timer_hardware: SnipsMatrix.timer_hardware.cancel() del SnipsMatrix.timer_hardware SnipsMatrix.timer_hardware = None if SnipsMatrix.timer_short_app: SnipsMatrix.timer_short_app.cancel() del SnipsMatrix.timer_short_app SnipsMatrix.timer_short_app = None @staticmethod def stop_show_hardware(): if SnipsMatrix.timer_hardware: SnipsMatrix.timer_hardware.cancel() del SnipsMatrix.timer_hardware SnipsMatrix.timer_hardware = None SnipsMatrix.queue.put(snipsMatrixAction.Clear( DisplayPriority.hardware)) @staticmethod def stop_show_short_app(): if SnipsMatrix.timer_short_app: SnipsMatrix.timer_short_app.cancel() del SnipsMatrix.timer_short_app SnipsMatrix.timer_short_app = None SnipsMatrix.queue.put( snipsMatrixAction.Clear(DisplayPriority.short_apps))
class Bartender(MenuDelegate): def __init__(self): self.running = False # set the oled screen height self.screen_width = SCREEN_WIDTH self.screen_height = SCREEN_HEIGHT self.btn1Pin = LEFT_BTN_PIN self.btn2Pin = RIGHT_BTN_PIN # configure interrups for buttons GPIO.setup(self.btn1Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(self.btn2Pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # configure screen spi_bus = 0 spi_device = 0 gpio = gaugette.gpio.GPIO() spi = gaugette.spi.SPI(spi_bus, spi_device) # Very important... This lets py-gaugette 'know' what pins to use in order to reset the display self.led = gaugette.ssd1306.SSD1306( gpio, spi, reset_pin=OLED_RESET_PIN, dc_pin=OLED_DC_PIN, rows=self.screen_height, cols=self.screen_width ) # Change rows & cols values depending on your display dimensions. self.led.begin() self.led.clear_display() self.led.display() self.led.invert_display() time.sleep(0.5) self.led.normal_display() time.sleep(0.5) # load the pump configuration from file self.pump_configuration = Bartender.readPumpConfiguration() for pump in self.pump_configuration.keys(): GPIO.setup(self.pump_configuration[pump]["pin"], GPIO.OUT, initial=GPIO.HIGH) # setup pixels: self.numpixels = NUMBER_NEOPIXELS # Number of LEDs in strip # Here's how to control the strip from any two GPIO pins: datapin = NEOPIXEL_DATA_PIN clockpin = NEOPIXEL_CLOCK_PIN self.strip = Adafruit_DotStar(self.numpixels, datapin, clockpin) self.strip.begin() # Initialize pins for output self.strip.setBrightness( NEOPIXEL_BRIGHTNESS) # Limit brightness to ~1/4 duty cycle # turn everything off for i in range(0, self.numpixels): self.strip.setPixelColor(i, 0) self.strip.show() print "Done initializing" @staticmethod def readPumpConfiguration(): return json.load(open('pump_config.json')) @staticmethod def writePumpConfiguration(configuration): with open("pump_config.json", "w") as jsonFile: json.dump(configuration, jsonFile) def startInterrupts(self): GPIO.add_event_detect(self.btn1Pin, GPIO.FALLING, callback=self.left_btn, bouncetime=LEFT_PIN_BOUNCE) GPIO.add_event_detect(self.btn2Pin, GPIO.FALLING, callback=self.right_btn, bouncetime=RIGHT_PIN_BOUNCE) def stopInterrupts(self): GPIO.remove_event_detect(self.btn1Pin) GPIO.remove_event_detect(self.btn2Pin) def buildMenu(self, drink_list, drink_options): # create a new main menu m = Menu("Main Menu") # add drink options drink_opts = [] for d in drink_list: drink_opts.append( MenuItem('drink', d["name"], {"ingredients": d["ingredients"]})) configuration_menu = Menu("Configure") # add pump configuration options pump_opts = [] for p in sorted(self.pump_configuration.keys()): config = Menu(self.pump_configuration[p]["name"]) # add fluid options for each pump for opt in drink_options: # star the selected option selected = "*" if opt["value"] == self.pump_configuration[p][ "value"] else "" config.addOption( MenuItem('pump_selection', opt["name"], { "key": p, "value": opt["value"], "name": opt["name"] })) # add a back button so the user can return without modifying config.addOption(Back("Back")) config.setParent(configuration_menu) pump_opts.append(config) # add pump menus to the configuration menu configuration_menu.addOptions(pump_opts) # add a back button to the configuration menu configuration_menu.addOption(Back("Back")) # adds an option that cleans all pumps to the configuration menu configuration_menu.addOption(MenuItem('clean', 'Clean')) configuration_menu.setParent(m) m.addOptions(drink_opts) m.addOption(configuration_menu) # create a menu context self.menuContext = MenuContext(m, self) def filterDrinks(self, menu): """ Removes any drinks that can't be handled by the pump configuration """ for i in menu.options: if (i.type == "drink"): i.visible = False ingredients = i.attributes["ingredients"] presentIng = 0 for ing in ingredients.keys(): for p in self.pump_configuration.keys(): if (ing == self.pump_configuration[p]["value"]): presentIng += 1 if (presentIng == len(ingredients.keys())): i.visible = True elif (i.type == "menu"): self.filterDrinks(i) def selectConfigurations(self, menu): """ Adds a selection star to the pump configuration option """ for i in menu.options: if (i.type == "pump_selection"): key = i.attributes["key"] if (self.pump_configuration[key]["value"] == i.attributes["value"]): i.name = "%s %s" % (i.attributes["name"], "*") else: i.name = i.attributes["name"] elif (i.type == "menu"): self.selectConfigurations(i) def prepareForRender(self, menu): self.filterDrinks(menu) self.selectConfigurations(menu) return True def menuItemClicked(self, menuItem): if (menuItem.type == "drink"): self.makeDrink(menuItem.name, menuItem.attributes["ingredients"]) return True elif (menuItem.type == "pump_selection"): self.pump_configuration[menuItem.attributes["key"]][ "value"] = menuItem.attributes["value"] Bartender.writePumpConfiguration(self.pump_configuration) return True elif (menuItem.type == "clean"): self.clean() return True return False def clean(self): waitTime = 20 pumpThreads = [] # cancel any button presses while the drink is being made # self.stopInterrupts() self.running = True for pump in self.pump_configuration.keys(): pump_t = threading.Thread( target=self.pour, args=(self.pump_configuration[pump]["pin"], waitTime)) pumpThreads.append(pump_t) # start the pump threads for thread in pumpThreads: thread.start() # start the progress bar self.progressBar(waitTime) # wait for threads to finish for thread in pumpThreads: thread.join() # show the main menu self.menuContext.showMenu() # sleep for a couple seconds to make sure the interrupts don't get triggered time.sleep(2) # reenable interrupts # self.startInterrupts() self.running = False def displayMenuItem(self, menuItem): print menuItem.name self.led.clear_display() self.led.draw_text2(0, 20, menuItem.name, 2) self.led.display() def cycleLights(self): t = threading.currentThread() head = 0 # Index of first 'on' pixel tail = -10 # Index of last 'off' pixel color = 0xFF0000 # 'On' color (starts red) while getattr(t, "do_run", True): self.strip.setPixelColor(head, color) # Turn on 'head' pixel self.strip.setPixelColor(tail, 0) # Turn off 'tail' self.strip.show() # Refresh strip time.sleep(1.0 / 50) # Pause 20 milliseconds (~50 fps) head += 1 # Advance head position if (head >= self.numpixels): # Off end of strip? head = 0 # Reset to start color >>= 8 # Red->green->blue->black if (color == 0): color = 0xFF0000 # If black, reset to red tail += 1 # Advance tail position if (tail >= self.numpixels): tail = 0 # Off end? Reset def lightsEndingSequence(self): # make lights green for i in range(0, self.numpixels): self.strip.setPixelColor(i, 0xFF0000) self.strip.show() time.sleep(5) # turn lights off for i in range(0, self.numpixels): self.strip.setPixelColor(i, 0) self.strip.show() def pour(self, pin, waitTime): GPIO.output(pin, GPIO.LOW) time.sleep(waitTime) GPIO.output(pin, GPIO.HIGH) def progressBar(self, waitTime): interval = waitTime / 100.0 for x in range(1, 101): self.led.clear_display() self.updateProgressBar(x, y=35) self.led.display() time.sleep(interval) def makeDrink(self, drink, ingredients): # cancel any button presses while the drink is being made # self.stopInterrupts() self.running = True # launch a thread to control lighting lightsThread = threading.Thread(target=self.cycleLights) lightsThread.start() # Parse the drink ingredients and spawn threads for pumps maxTime = 0 pumpThreads = [] for ing in ingredients.keys(): for pump in self.pump_configuration.keys(): if ing == self.pump_configuration[pump]["value"]: waitTime = ingredients[ing] * FLOW_RATE if (waitTime > maxTime): maxTime = waitTime pump_t = threading.Thread( target=self.pour, args=(self.pump_configuration[pump]["pin"], waitTime)) pumpThreads.append(pump_t) # start the pump threads for thread in pumpThreads: thread.start() # start the progress bar self.progressBar(maxTime) # wait for threads to finish for thread in pumpThreads: thread.join() # show the main menu self.menuContext.showMenu() # stop the light thread lightsThread.do_run = False lightsThread.join() # show the ending sequence lights self.lightsEndingSequence() # sleep for a couple seconds to make sure the interrupts don't get triggered time.sleep(2) # reenable interrupts # self.startInterrupts() self.running = False def left_btn(self, ctx): if not self.running: self.menuContext.advance() def right_btn(self, ctx): if not self.running: self.menuContext.select() def updateProgressBar(self, percent, x=15, y=15): height = 10 width = self.screen_width - 2 * x for w in range(0, width): self.led.draw_pixel(w + x, y) self.led.draw_pixel(w + x, y + height) for h in range(0, height): self.led.draw_pixel(x, h + y) self.led.draw_pixel(self.screen_width - x, h + y) for p in range(0, percent): p_loc = int(p / 100.0 * width) self.led.draw_pixel(x + p_loc, h + y) def run(self): self.startInterrupts() # main loop try: while True: time.sleep(0.1) except KeyboardInterrupt: GPIO.cleanup() # clean up GPIO on CTRL+C exit GPIO.cleanup() # clean up GPIO on normal exit traceback.print_exc()
class Elevator(threading.Thread): def __init__(self, kill_event, loop_time=1.0 / 60.0): self.status = "INIT" self.q = Queue() self.kill_event = kill_event self.timeout = loop_time self.baudrate = BAUDRATE self.dev = DEVICE self.strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN) self.strip.begin() self.strip.setBrightness(255) self.serial = serial.Serial(self.dev) self.serial.baudrate = 115200 # Initial state self.send_elevator_command(ELEVATOR_DOWN) self.status = "DOWN" self.set_lights(OFF) super(Elevator, self).__init__() def onThread(self, function, *args, **kwargs): self.q.put((function, args, kwargs)) def run(self): self.down() while True: if self.kill_event.is_set(): self.close() return try: function, args, kwargs = self.q.get(timeout=self.timeout) function(*args, **kwargs) except Empty: pass def send_elevator_command(self, command): self.serial.flushInput() # avoids that the input buffer overfloats self.serial.write(command) self.serial.flush() def up(self): self.status = "GOING_UP" self.send_elevator_command(ELEVATOR_UP) time.sleep(TIME_UP_S) self.status = "UP" self.set_lights(GREEN) def down(self): self.status = "GOING_DOWN" self.send_elevator_command(ELEVATOR_DOWN) self.set_lights(OFF) time.sleep(TIME_DOWN_S) self.status = "DOWN" time.sleep(TIME_TO_LEAVE_ELEVATOR_S) self.status = "FREE" def close(self): self.serial.close() def set_lights(self, color): for i in range(NUMPIXELS): self.strip.setPixelColor(i, color) if color == OFF: self.strip.setBrightness(0) else: self.strip.setBrightness(255) self.strip.show()
class UtilityFan: def __init__(self): datapin = 10 clockpin = 11 self.buffer = bytearray(STRIP_LENGTH * 4) self.buffer_clear = bytearray(STRIP_LENGTH * 4) self.color_hsv = [0, 1, 0.7] self.start_time = time.time() self.cur_trail = 0 for i in range(0, len(self.buffer_clear), 4): self.buffer_clear[i] = 0xFF self.buffer_clear[i + rOffset] = 0 self.buffer_clear[i + gOffset] = 0 self.buffer_clear[i + bOffset] = 0 if is_running_on_pi: self.strip = Adafruit_DotStar(0, datapin, clockpin, 18500000) else: self.strip = None def animation_step(self): if is_running_on_pi == False: return self.color_hsv[0] += 0.002 color = colorsys.hsv_to_rgb(self.color_hsv[0], self.color_hsv[1], self.color_hsv[2]) t = time.time() - self.start_time for i in range(0, len(self.buffer), 4): pos = abs(STRIP_LENGTH / 2 - i / 4) # v = 0.1* (0.5+ 0.3*math.sin(t*3)) * ((1 + math.sin(i*0.1 + t*11)) + (1 + math.sin(i*0.2 - t*7)) ) v = abs(self.cur_trail - pos) / 144.0 self.buffer[i] = 0xFF self.buffer[i + rOffset] = gamma[int(v * 255 * color[0])] #int(color[0] * v) self.buffer[i + gOffset] = gamma[int( v * 255 * color[1])] #gamma[int(color[1] * v) self.buffer[i + bOffset] = gamma[int(v * 255 * color[2])] #int(color[2] * v) self.cur_trail = abs(self.cur_trail + 1) % (STRIP_LENGTH) self.strip.show(self.buffer) def next(self, length): end_time = length + time.time() last_update = time.time() last_frame = time.time() self.strip.begin() while end_time > last_update: last_update = time.time() if last_frame + 0.2 < last_update: last_frame = last_update self.animation_step() self.strip.close() def clear(self): self.strip.begin() self.strip.show(self.buffer_clear) self.strip.close()
def init_led(args): strip = Adafruit_DotStar(args.numPixels, 12000000) strip.begin() strip.setBrightness(args.brightness) return strip
import time from dotstar import Adafruit_DotStar import MySQLdb import colorsys numpixels = 5 # Number of LEDs in strip # Here's how to control the strip from any two GPIO pins: datapin = 23 clockpin = 24 strip = Adafruit_DotStar(numpixels, datapin, clockpin) strip.begin() # Initialize pins for output strip.setBrightness(64) # Limit brightness to ~1/4 duty cycle dhost = "localhost" duser = "******" dpass = "******" database = "home" db = MySQLdb.connect(dhost,duser,dpass,database) def scale(value, leftMin, leftMax, rightMin, rightMax): # Figure out how 'wide' each range is leftSpan = leftMax - leftMin rightSpan = rightMax - rightMin # Convert the left range into a 0-1 range (float) valueScaled = float(value - leftMin) / float(leftSpan) # Convert the 0-1 range into a value in the right range. return rightMin + (valueScaled * rightSpan)
def main(argv): global MODE global INPUT global p MODE = sys.argv[1] # debug / pi INPUT = sys.argv[2] # camera / image / video p = ThreadPool(4) # MODE SETUP FOR LEDs or Display if MODE == 'debug': print('Running in Debug mode') cv2.namedWindow('preview') strip = True # If you're running on the PI, you want to setup the LED strip if MODE == 'pi': from dotstar import Adafruit_DotStar strip = Adafruit_DotStar(HEIGHT*WIDTH + OFFSET, DATAPIN, CLOCKPIN) strip.begin() # Lower power consumption, but makes it flicker. strip.setBrightness(LED_GLOBAL_BRIGHTNESS) bitmap = initialize_empty_bitmap() render_bitmap(bitmap, strip) # INPUT SELECTION SETUP # If you're using a USB camera for input # TODO: Allow for arg use for different cameras if INPUT == 'camera': if MODE == 'debug': cv2.namedWindow('cameraPreview', cv2.WINDOW_NORMAL) vc = cv2.VideoCapture(0) if vc.isOpened(): # vc.set(15, -10) vc.set(3,200) # These aren't accurate, but help vc.set(4,100) rval, frame = vc.read() else: rval = False while rval: rval, frame = vc.read() # if MODE == 'debug': # cv2.imshow('cameraPreview', frame) start = time.time() flow(frame, bitmap, strip) key = cv2.waitKey(15) if key == 27: # exit on ESC break end = time.time() print(end - start) # If you're using a static image for debugging if INPUT == 'image': if len(sys.argv) == 4: frame = cv2.imread(sys.argv[3]) else: frame = cv2.imread('bars.jpg') rval = True # while True: # For 1000 frames start = time.time() while True: flow(frame, bitmap, strip) key = cv2.waitKey(15) if key == 27: # exit on ESC break end = time.time() fps = 1000 / (end - start) print('fps:', fps) # If you're using a pre-recorded video for debugging set it here if INPUT == 'video': cv2.namedWindow('videoPreview', cv2.WINDOW_NORMAL) vc = cv2.VideoCapture('WaveCanon2.mp4') if vc.isOpened(): rval, frame = vc.read() else: rval = False while rval: rval, frame = vc.read() frame = shrink(frame) # 1080p video too big coming in cv2.imshow('videoPreview', frame) flow(frame, bitmap, strip) key = cv2.waitKey(15) if key == 27: # exit on ESC break return False
import time import random from dotstar import Adafruit_DotStar numpixels = 80 # Number of LEDs in strip # Here's how to control the strip from any two GPIO pins: datapins = [16, 13] clockpin = 27 a = Adafruit_DotStar(numpixels, datapins[0], clockpin, order='bgr') a.begin() a.setBrightness(100) b = Adafruit_DotStar(numpixels, datapins[1], clockpin, order='bgr') b.begin() b.setBrightness(100) a.setPixelColor(68, 0x0000FF) a.show() time.sleep(2) b.setPixelColor(68, 0x0000FF) b.show()
class povThread(threading.Thread): def __init__(self, FreqAverage=5, Path="pov/", start=""): threading.Thread.__init__(self) self.datapin = 2 # GPIO-Numbering! self.clockpin = 3 # GPIO-Numbering! self.strip = Adafruit_DotStar(0, self.datapin, self.clockpin) # Notice the number of LEDs is set to 0. This is on purpose...we're asking # the DotStar module to NOT allocate any memory for this strip...we'll handle # our own allocation and conversion and will feed it 'raw' data. self.strip.begin() # Initialize pins for output self.empty_array=bytearray(60*4) # prepare empty-flash for x in range(60): self.empty_array[x*4]=0xFF self.empty_array[x*4+1]=0x00 self.empty_array[x*4+2]=0x00 self.empty_array[x*4+3]=0x00 self.povPath=Path self.povFile = start self.size=[0,0] self.FAverage=FreqAverage self.actPeriod=0 self.freq = get_freq.freqThread(NAverage=self.FAverage) # initialize frequency-thread self.running=False # is Thread displaying a pov-File? self.NEWrunning=False # want to stop and start new? self.active=True # is Thread active? (& playing OR waiting to play) -> only False if quitting main. self.pause=False self.pos=0 self.loading=False # loading? --> main waits for finishing loading-process if start!="": self.running=True else: self.off() self.start() ####status data / show status function def off(self): self.running=False self.pause=False self.strip.show(self.empty_array) self.size=[0,0] self.pos=0 def doPause(self): if self.running==True: if self.pause: self.pause=False else: self.pause=True def stop(self): # only to quit!! else: use self.off() self.off() self.freq.running=False self.active=False # def restart(self): # self.off() # self.running=True def showPOV(self, File=""): self.off() if File!="": self.povFile=File self.NEWrunning=True self.loading=True def run(self): while self.active: if self.NEWrunning==True: self.running=True self.NEWrunning=False if self.running == True: if self.povFile == "": print "Error: No pov-file specified!" self.off() self.loading=False else: f=open(self.povPath+self.povFile,'r') self.size=map(int,(f.readline().rstrip("\n")).split(',')) width=self.size[0] print "\nLoading "+self.povPath+self.povFile + " into buffer; size: "+str(self.size[0])+"x"+str(self.size[1]) lines = f.read().splitlines() array=[0 for i in range(width)] for i in range(width): array[i]=bytearray(60*4) j=0 for LED in lines[i].split(','): array[i][j]=int(LED) j=j+1 if i%900==0: print "\r"+str(100*i/width)+"% loaded." print "\r100% loaded." print "\nwaiting for valid frequency-values..." while (self.freq.period>2.0) or (self.freq.period<0.05): pass print "Displaying... period="+ str(self.freq.period) + "\t frequency=" + str(self.freq.frequency) self.pos=0 # start at beginning; first rotation # for calculating needed time for one period timeB=time.time() timeDiff=0 timeDiff_new=0 self.loading=False period=.16 while self.running: # Loop period=self.freq.period pixel_time=(period-timeDiff)/60. if self.pause==False: # pause? for x in range(60): # For each column of image... self.strip.show(array[x+self.pos*60]) # Write raw data to strip time.sleep(pixel_time) self.pos=(self.pos+1)%(width/60) # next slice/rotation; modulo(%)=>endless loop else: time.sleep(period) self.actPeriod=time.time()-timeB timeDiff_new=self.actPeriod-period timeDiff=(timeDiff+timeDiff_new)%period timeB=time.time() self.off() time.sleep(0.1)