diff --git a/Adafruit_LEDBackpack/Adafruit_8x8.py b/Adafruit_LEDBackpack/Adafruit_8x8.py index f1446d79..5da731d0 100644 --- a/Adafruit_LEDBackpack/Adafruit_8x8.py +++ b/Adafruit_LEDBackpack/Adafruit_8x8.py @@ -98,3 +98,33 @@ def setPixel(self, x, y, color=1): self.disp.setBufferRow(y, buffer[y] | (1 << (x+8)) | (1 << x) ) else: self.disp.setBufferRow(y, buffer[y] & ~(1 << x) & ~(1 << (x+8)) ) + def drawPixel(self, x, y, color=1): + #"Sets a single pixel" + if ((y < 0) or (y >= 8)): + return + if ((x < 0) or (x >= 8)): + return + + # Added rotation code 4/16/13 - RH + if self.rotation == 1: + x, y = self.swap(x, y) + x = 8 - x - 1 + elif self.rotation == 2: + x = 8 - x - 1 + y = 8 - y - 1 + elif self.rotation == 3: + x, y = self.swap(x, y) + y = 8 - y - 1 + + x += 7 # ATTN: This might be a bug? On the color matrix, this causes x=0 to draw on the last line instead of the first. + x %= 8 + # Set the appropriate pixel + buffer = self.getBuffer() + if (color == 1): + self.setBufferRow(y, (buffer[y] | (1 << x)) & ~(1 << (x+8)) ) + elif (color == 2): + self.setBufferRow(y, (buffer[y] | 1 << (x+8)) & ~(1 << x) ) + elif (color == 3): + self.setBufferRow(y, buffer[y] | (1 << (x+8)) | (1 << x) ) + else: + self.setBufferRow(y, buffer[y] & ~(1 << x) & ~(1 << (x+8)) ) diff --git a/Adafruit_LEDBackpack/Adafruit_8x8.pyc b/Adafruit_LEDBackpack/Adafruit_8x8.pyc new file mode 100644 index 00000000..4d893811 Binary files /dev/null and b/Adafruit_LEDBackpack/Adafruit_8x8.pyc differ diff --git a/Adafruit_LEDBackpack/Adafruit_GFX.py b/Adafruit_LEDBackpack/Adafruit_GFX.py index 15e267a8..ce1096bb 100644 --- a/Adafruit_LEDBackpack/Adafruit_GFX.py +++ b/Adafruit_LEDBackpack/Adafruit_GFX.py @@ -397,4 +397,4 @@ def width(self): def height(self): return self._height - \ No newline at end of file + diff --git a/Adafruit_LEDBackpack/Adafruit_GFX.pyc b/Adafruit_LEDBackpack/Adafruit_GFX.pyc new file mode 100644 index 00000000..ec2c507e Binary files /dev/null and b/Adafruit_LEDBackpack/Adafruit_GFX.pyc differ diff --git a/Adafruit_LEDBackpack/Adafruit_I2C.pyc b/Adafruit_LEDBackpack/Adafruit_I2C.pyc new file mode 100644 index 00000000..b241fbb7 Binary files /dev/null and b/Adafruit_LEDBackpack/Adafruit_I2C.pyc differ diff --git a/Adafruit_LEDBackpack/Adafruit_LEDBackpack.pyc b/Adafruit_LEDBackpack/Adafruit_LEDBackpack.pyc new file mode 100644 index 00000000..8959759e Binary files /dev/null and b/Adafruit_LEDBackpack/Adafruit_LEDBackpack.pyc differ diff --git a/Adafruit_LEDBackpack/AlarmClock.py b/Adafruit_LEDBackpack/AlarmClock.py new file mode 100644 index 00000000..2bfc0674 --- /dev/null +++ b/Adafruit_LEDBackpack/AlarmClock.py @@ -0,0 +1,150 @@ +#!/usr/bin/python + +import time +import pygame + +class AlarmClock(object): + COLOR_TRANSPARENT=0 + COLOR_GREEN=1 + COLOR_RED=2 + COLOR_YELLOW=3 + current_color=1 + bg_color=0 + alarm_time='06:30' + org_alarm_time='06:30' + alarm_text='ALARM!!' + snooze_time=1 + org_snooze_time=0 + snooze_count=0 + debug=False + flash=True + sound='/home/pi/alarm.mp3' + + def __init__(self, debug=False): + self.debug=debug + self.alarm_time=self.get_time() + #copy over originals + self.org_alarm_time=self.alarm_time + self.org_snooze_time=self.snooze_time + + pygame.mixer.init() + pygame.mixer.music.load(self.sound) + #Debug Set Alarm for 2 minutes from now + if(self.debug): + self.set_snooze() + + def set_alarm(self, time=False): + " Set alarm time in Hours : Minutes (12:59) " + if(time): + self.alarm_time=time + else: + self.alarm_time=self.get_time() + self.org_alarm_time=self.alarm_time + + def set_snooze_time(self, mins=10): + " Set Snooze time, format in minutes " + self.snooze_time=mins + self.org_snooze_time=self.snooze_time + + def set_snooze(self): + " Set snooze " + if(self.debug): + print("Setting snooze") + ++self.snooze_count + # Increment Alarm Time + hrs = int(time.strftime('%I')) + mins = int(time.strftime('%M')) + self.snooze_time + if(mins > 59): + r = mins % 60 + hrs += (mins - r) / 60 + mins = r + if(hrs > 12): + hrs = hrs - 12 + if(hrs < 10): + hrs = "0" + str(hrs) + if(mins < 10): + mins = "0" + str(mins) + #Set alarm time + self.alarm_time="%s:%s"%(hrs, mins) + if(self.debug): + print("New Alarm Time %s" % self.alarm_time) + if(self.snooze_time > 3): + --self.snooze_time + #Stop Sound + self.tigger_sound(False) + + def stop_alarm(self): + " Stop Alarm and reset object " + if(self.debug): + print("Stopping alarm") + self.snooze_count=0 + self.alarm_time=self.org_alarm_time + self.snooze_time=self.org_snooze_time + self.tigger_sound(False) + + def tigger_sound(self, on): + " Trigger Sound " + if(on): + if(pygame.mixer.music.get_busy()): + return + if(self.debug): + print("Playing sound") + # Play sound up to 25 times + pygame.mixer.music.play(25) + else: + if(self.debug): + print("Pausing sound") + # Pause sound + pygame.mixer.music.pause() + + def get_time(self): + " Get Time " + return time.strftime("%I:%M") + + def get_dsp_time(self): + " GetDisplay Time " + return time.strftime("%I:%M%p")[:-1] + + def is_am(self): + " Returns true if it is AM " + return time.strftime("%p") == 'AM' + + def check_alarm(self): + " Check to see if the alarm should be triggered " + return self.get_time() == self.alarm_time + + def display_msg(self): + " Get the Current Display MSG" + self.set_colors() + if(self.check_alarm()): + self.tigger_sound(True) + return self.alarm_text + else: + return self.get_dsp_time() + + def set_colors(self): + " Get the Current Colors " + #Set the default BG to transparent + self.bg_color=self.COLOR_TRANSPARENT + if(self.is_am()): + self.current_color=self.COLOR_GREEN + else: + self.current_color=self.COLOR_YELLOW + #Check if alarm triggered + if(self.check_alarm()): + if(self.flash): + self.current_color=self.COLOR_TRANSPARENT + self.bg_color=self.COLOR_RED + self.flash=False + else: + self.current_color=self.COLOR_RED + self.bg_color=self.COLOR_TRANSPARENT + self.flash=True + + def get_color(self): + " Get the Current Color " + return self.current_color + + def get_bg(self): + " Get the Current BG " + return self.bg_color diff --git a/Adafruit_LEDBackpack/AlarmClock.pyc b/Adafruit_LEDBackpack/AlarmClock.pyc new file mode 100644 index 00000000..5d305461 Binary files /dev/null and b/Adafruit_LEDBackpack/AlarmClock.pyc differ diff --git a/Adafruit_LEDBackpack/alarm.py b/Adafruit_LEDBackpack/alarm.py new file mode 100644 index 00000000..ff1099df --- /dev/null +++ b/Adafruit_LEDBackpack/alarm.py @@ -0,0 +1,99 @@ +#!/usr/bin/python +# Example code to test scrolling text across multiple 8x8 matrices. +# WARNING! Make sure to use a level shifter between the Raspberry Pi +# and matrices running at 5v to avoid overpowering the Pi. + +import RPi.GPIO as GPIO +import time +import random +from Adafruit_8x8 import ColorEightByEight +from AlarmClock import AlarmClock + +GPIO.setmode(GPIO.BOARD) + +ledbtns = [] +#Green +ledbtns.append({'led' : 11, 'button' : 12}) +#Yello +ledbtns.append({'led' : 13, 'button' : 16}) +#Red +ledbtns.append({'led' : 15, 'button' : 18}) + +for elm in ledbtns: + GPIO.setup(elm['led'], GPIO.OUT) + GPIO.output(elm['led'], GPIO.HIGH) + GPIO.setup(elm['button'], GPIO.IN) + +#GPIO.setup(11, GPIO.OUT) +#GPIO.output(11, GPIO.LOW) +#GPIO.output(11, GPIO.HIGH) + +MATRICES = 5 +matrix = [] + +alarm = AlarmClock(debug=True) + +message = alarm.display_msg() + +for i in range(0,MATRICES): + matrix.append(ColorEightByEight(address=0x70+i)) + matrix[i].setTextWrap(False) # Allow text to run off edges + matrix[i].setRotation(3) + matrix[i].setBrightness(1) + matrix[i].setTextSize(1) + matrix[i].setTextColor(alarm.get_color(), alarm.get_bg()) + +# Defintions for Alarm Hardware + +def update_matrixes(): + message = alarm.display_msg() + + for i in range(0,MATRICES): + # Draw message in each matrix buffer, offseting each by 8 pixels + #matrix[i].clear() + matrix[i].setCursor(x - i * 8, 1) + matrix[i].printMessage(message) + matrix[i].setTextColor(alarm.get_color(), alarm.get_bg()) + + # Write data to matrices in separate loop so it's less jumpy + for i in range(0,MATRICES): + matrix[i].writeDisplay() + +# Horiz. position of text -- starts off right edge +x = 2 +iter = 0 + +while True: + iter += 1 + + # Update Display + update_matrixes() + + if alarm.check_alarm(): + #Start Game + for i in range(0, 6): + #Get Random LEDBUTTON + current = random.choice(ledbtns); + #Turn it on + GPIO.output(current['led'], GPIO.LOW); + not_pressed = True + #Wait until it is pressed + s = 0 + while not_pressed: + if not GPIO.input(current['button']): + not_pressed = False + #TURN LIGHT OFF + GPIO.output(current['led'], GPIO.HIGH) + update_matrixes() + time.sleep(1) + #Update Display + if not s % 100: + update_matrixes() + s += 1 + #Sleep + time.sleep(0.01) + + #Stop Alarm + alarm.stop_alarm() + #Sleep for second before updating the display + time.sleep(1) diff --git a/Adafruit_LEDBackpack/ex_8x8_color_alarm.py b/Adafruit_LEDBackpack/ex_8x8_color_alarm.py new file mode 100644 index 00000000..4dc0c825 --- /dev/null +++ b/Adafruit_LEDBackpack/ex_8x8_color_alarm.py @@ -0,0 +1,40 @@ +#!/usr/bin/python +# Example code to test scrolling text across multiple 8x8 matrices. +# WARNING! Make sure to use a level shifter between the Raspberry Pi +# and matrices running at 5v to avoid overpowering the Pi. + +import time +from Adafruit_8x8 import ColorEightByEight + +def get_time(): + return time.strftime("%I:%M %p") + +MATRICES = 4 +matrix = [] +color = 2 + +for i in range(0,MATRICES): + matrix.append(ColorEightByEight(address=0x70+i)) + matrix[i].setTextWrap(False) # Allow text to run off edges + matrix[i].setRotation(3) + matrix[i].setBrightness(4) + matrix[i].setTextSize(1) + +message = get_time() + +# Horiz. position of text -- starts off right edge +x = 1 +while True: + message = get_time() + + for i in range(0,MATRICES): + # Draw message in each matrix buffer, offseting each by 8 pixels + matrix[i].clear() + matrix[i].setCursor(x - i * 8, 1) + matrix[i].printMessage(message) + + # Write data to matrices in separate loop so it's less jumpy + for i in range(0,MATRICES): + matrix[i].writeDisplay() + + time.sleep(60) diff --git a/Adafruit_LEDBackpack/ex_8x8_color_alarm2.py b/Adafruit_LEDBackpack/ex_8x8_color_alarm2.py new file mode 100644 index 00000000..3e57677c --- /dev/null +++ b/Adafruit_LEDBackpack/ex_8x8_color_alarm2.py @@ -0,0 +1,61 @@ +#!/usr/bin/python +# Example code to test scrolling text across multiple 8x8 matrices. +# WARNING! Make sure to use a level shifter between the Raspberry Pi +# and matrices running at 5v to avoid overpowering the Pi. + +import time +from Adafruit_8x8 import ColorEightByEight + +def get_time(): + s = time.strftime("%I:%M") + if(s[0] == '0'): + s=s[1:] + if(time.strftime('%p') == 'AM'): + s=s+'A' + else: + s=s+'P' + return s + +MATRICES = 3 +matrix = [] +color=2 + +info_matrix = ColorEightByEight(address=0x73) +info_matrix.setTextWrap(False) # Allow text to run off edges +info_matrix.setRotation(3) +info_matrix.setBrightness(4) +info_matrix.setTextSize(1) + +for i in range(0,MATRICES): + matrix.append(ColorEightByEight(address=0x70+i)) + matrix[i].setTextWrap(False) # Allow text to run off edges + matrix[i].setRotation(3) + matrix[i].setBrightness(4) + matrix[i].setTextSize(1) + +#message = 'Hello World!!!' +message = get_time() + +# Horiz. position of text -- starts off right edge +x = 0 +while True: + + for i in range(0,MATRICES): + # Draw message in each matrix buffer, offseting each by 8 pixels + matrix[i].clear() + matrix[i].setCursor(x - i * 8, 1) + matrix[i].printMessage(message) + + # Write data to matrices in separate loop so it's less jumpy + for i in range(0,MATRICES): + matrix[i].writeDisplay() + + #print('Test') + + info_matrix.clear() + info_matrix.setCursor(2,1) + info_matrix.printMessage(time.strftime('%p')[0]) + info_matrix.writeDisplay() + + time.sleep(60) + message = get_time() diff --git a/Adafruit_LEDBackpack/ex_8x8_color_text.py b/Adafruit_LEDBackpack/ex_8x8_color_text.py new file mode 100644 index 00000000..34d51026 --- /dev/null +++ b/Adafruit_LEDBackpack/ex_8x8_color_text.py @@ -0,0 +1,45 @@ +#!/usr/bin/python +# Example code to test scrolling text across multiple 8x8 matrices. +# WARNING! Make sure to use a level shifter between the Raspberry Pi +# and matrices running at 5v to avoid overpowering the Pi. + +import time +from Adafruit_8x8 import ColorEightByEight + +MATRICES = 4 +matrix = [] + +for i in range(0,MATRICES): + matrix.append(ColorEightByEight(address=0x70+i)) + matrix[i].setTextWrap(False) # Allow text to run off edges + matrix[i].setRotation(3) + matrix[i].setBrightness(4) + matrix[i].setTextSize(1) + +#message = 'Hello World!!!' +message = 'Super Sweet!!!' + +# Horiz. position of text -- starts off right edge +x = 8 * MATRICES + +while True: + for i in range(0,MATRICES): + # Draw message in each matrix buffer, offseting each by 8 pixels + matrix[i].clear() + matrix[i].setCursor(x - i * 8, 1) + matrix[i].printMessage(message) + + # Write data to matrices in separate loop so it's less jumpy + for i in range(0,MATRICES): + matrix[i].writeDisplay() + + #print('Test') + + # Move text position left by 1 pixel. + # When it's completely gone off left edge, start over off right. + length = len(message) * 6 * matrix[i].getTextSize() + x -= 1 + if(x < -(length)): + x = 8 * MATRICES + + time.sleep(0.15) diff --git a/Adafruit_LEDBackpack/glcdfont.pyc b/Adafruit_LEDBackpack/glcdfont.pyc new file mode 100644 index 00000000..cf8db3e7 Binary files /dev/null and b/Adafruit_LEDBackpack/glcdfont.pyc differ