for y in range(8): for x in range(8): if (bMap[y] & (2**x)): grid.setPixel(x, y, 1) # Continually update the 8x8 display, one pixel at a time count = 0 iter = 0 while (count < 10): iter += 1 for x in range(8): for y in range(8): grid.setPixel(x, y, iter % 4) time.sleep(0.02) count += 1 sleep(3) grid.clear() # Draw an X for x in range(8): grid.setPixel(x, x, 1) x = 0 y = 7 while (x < 8):
#!/usr/bin/python import time import datetime from Adafruit_8x8 import ColorEightByEight # =========================================================================== # 8x8 Pixel Example # =========================================================================== grid = ColorEightByEight(address=0x70) print "Press CTRL+Z to exit" iter = 0 # Continually update the 8x8 display one pixel at a time while(True): iter += 1 for x in range(0, 8): for y in range(0, 8): grid.setPixel(x, y, iter % 4 ) time.sleep(0.02)
class LED_Matrix: #Variables for converting Pixel Colors ColorInt_To_String = ["Blank", "Green", "Red", "Yellow"] ColorString_To_Int = {"Blank": 0, "Green": 1, "Red": 2, "Yellow": 3} #These are used for easy use throughout the code for specific colors Blank_Pixel = 0 Green_Pixel = 1 Red_Pixel = 2 Yellow_Pixel = 3 #We need to track the state of each Grid LED. Grid_State = [[0 for Column in range(8)] for Row in range(8)] Pixel_Refresh_Rate = 0.0001 #Reference locations # x,y | References for Grid #---------------- # 0,0 = TOP RIGHT # 7,0 = TOP LEFT # 0,7 = BOTTOM RIGHT # 7,7 = BOTTOM LEFT #Construction function that is ran when class is initiated. #Here we initiate the Adafruit8x8 API def __init__(self): self.Grid = ColorEightByEight(address=0x70) #self.Connection = Database.connect('localhost', 'root', 'cool', 'gamedb') #=========================================== #General Use Functions that return a value== #=========================================== def get_Random_ColorString(self, boolean_Inc_Blank=True): if boolean_Inc_Blank is True: return LED_Matrix.ColorInt_To_String[randrange(0, 4)] else: return LED_Matrix.ColorInt_To_String[randrange(1, 3)] def get_Buffer_Value(self, i): return self.Grid.getBufferValue(i) def get_Random_ColorInt(self, boolean_Inc_Blank=True): if boolean_Inc_Blank is True: return randrange(0, 4) else: return randrange(1, 3) def get_Current_XY_Color(self, x, y): return LED_Matrix.ColorInt_To_String[LED_Matrix.Grid_State[x][y]] #=========================================== #End of General Use Functions=============== #=========================================== #=========================================== #Drawing Functions #=========================================== def draw_4px_Square(self, Bottom_Left_X, Bottom_Left_Y, string_Pixel_Color="Blank"): self.set_Pixel(Bottom_Left_X, Bottom_Left_Y, string_Pixel_Color) self.set_Pixel(Bottom_Left_X, Bottom_Left_Y-1, string_Pixel_Color) self.set_Pixel(Bottom_Left_X-1, Bottom_Left_Y-1, string_Pixel_Color) self.set_Pixel(Bottom_Left_X-1, Bottom_Left_Y, string_Pixel_Color) def draw_Row_Line(self, row_number, string_Row_Color="Blank"): y = 0 Random = False if(string_Row_Color=="Rainbow"): Random = True while(y < 8): if(Random==True): string_Row_Color = self.get_Random_ColorString(False) self.set_Pixel(row_number, y, string_Row_Color) y = y + 1 time.sleep(LED_Matrix.Pixel_Refresh_Rate) def draw_Column_Line(self, column_number, string_Column_Color="Blank"): x = 0 Random = False if(string_Column_Color=="Rainbow"): Random = True while(x < 8): if(Random==True): string_Column_Color = self.get_Random_ColorString(False) self.set_Pixel(x, column_number, string_Column_Color) x = x + 1 time.sleep(LED_Matrix.Pixel_Refresh_Rate) #=========================================== #End of Drawning Functions================== #=========================================== #=========================================== #I/O Functions #=========================================== def write_FlatFile(self): Config = ConfigParser.ConfigParser() with open ('Grid_Status.ini', 'w') as FlatFile: Config.read('Grid_Status') Config.add_section('Grid_Status') for x in range(0, 8): for y in range(0, 8): Config.set("Grid_Status", str(x) + "," + str(y), self.get_Current_XY_Color(x, y) ) Config.write(FlatFile) #=========================================== #End of I/O Functions======================= #=========================================== #=========================================== #Set Functions #=========================================== #Instant grid clear for prettier transitions. #We do it this way to bypass the pixel refresh interval. def set_Clear_Grid(self): self.Grid.clear() for x in range(0, 8): for y in range(0, 8): LED_Matrix.Grid_State[x][y] = 0 #Wrapper for setting a pixel so we can track it in our code and not need to reference the hardware. def set_Pixel(self, x, y, string_Pixel_Color="Blank"): int_Pixel_Color = LED_Matrix.ColorString_To_Int[string_Pixel_Color] #Here we update our instances's record of the grid screen LED_Matrix.Grid_State[x][y] = int_Pixel_Color #This is the function from our AdaFruit_8x8.py API self.Grid.setPixel(x, y, int_Pixel_Color) #If x and y are valid, update the database #//if (x >= 0 and y >= 0): #//self.write_FlatFile() #//self.set_Database_GridStatus_Update(x, y, int_Pixel_Color) #//thread.start_new_thread(self.set_Database_GridStatus_Update, (x, y, int_Pixel_Color)) #This function will display a color to every pixel of the matrix based on input string. #See Class Variables for acceptable input. Defaults to BLANK. def set_All_Pixels(self, string_Pixel_Color="Blank"): for x in range(0, 8): for y in range(0, 8): self.set_Pixel(x, y, string_Pixel_Color) time.sleep(LED_Matrix.Pixel_Refresh_Rate) def set_Random_Pixel(self, string_Pixel_Color="Blank"): x = randrange(0, 8) y = randrange(0, 8) self.set_Pixel(x, y, string_Pixel_Color) #Brightness is messured 0-15 def set_Matrix_Brightness(self, brightness): self.Grid.setBrightness(brightness) # Sets the displays Blink Rate, 0 = OFF , 1 = 2HZ , 2 = 1HZ , 3 = Half HZ def set_Matrix_BlinkRate(self, blinkRate): self.Grid.setBlinkRate(blinkRate) def set_Database_GridStatus_Update(self, x, y, int_Color): with self.Connection: Cursor = self.Connection.cursor() #SQL = "INSERT INTO Grid_Status(ID, Value) VALUES(%s, %s) ON DUPLICATE KEY UPDATE Value = %s" SQL = "UPDATE Grid_Status SET Value = %s WHERE ID = %s" DATA = ( str(int_Color), str(x) + "," + str(y), ) Cursor.execute(SQL, DATA) time.sleep(LED_Matrix.Pixel_Refresh_Rate)
# display buffer to represent the 8x8 LED matrix matrix = [ [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0] ] grid = ColorEightByEight(address=0x70) # clear matrix for x in range(0, 8): for y in range(0, 8): grid.setPixel(x, y, 0) ADC.setup() # continually scroll the matrix and plot new ADC sample while(True): for x in range(7): for y in range(8): matrix[x][y] = matrix[x+1][y] # read sample from ADC and convert to integer 0-8 adc_sample = ADC.read("P9_40") sample_flot = adc_sample*8.0 if sample_flot > 7.5: sample_flot = 8
#if(toggle): #currDeque0.reverse() #currDeque1.reverse() #currDeque2.reverse() #currDeque3.reverse() #currDeque4.reverse() #currDeque5.reverse() #currDeque6.reverse() #currDeque7.reverse() while(iter<10): iter += 1 for y in range(0, 8): grid.setPixel(0, y, currDeque0[ y ] ) grid.setPixel(1, y, currDeque1[ y ] ) grid.setPixel(2, y, currDeque2[ y ] ) grid.setPixel(3, y, currDeque3[ y ] ) grid.setPixel(4, y, currDeque4[ y ] ) grid.setPixel(5, y, currDeque5[ y ] ) grid.setPixel(6, y, currDeque6[ y ] ) grid.setPixel(7, y, currDeque7[ y ] ) #time.sleep(0.02) #print ":", iter if(toggle): currDeque0.rotate(1) currDeque1.rotate(1) currDeque2.rotate(1)
board = [[0,1,0,0,0,0,0,0], [0,0,1,0,0,0,0,0], [1,1,1,0,0,0,0,0], [0,0,0,0,0,1,1,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0]] grid = ColorEightByEight(address=0x70) iter =0; while(True): iter=iter+1 for i in range(0,8): for j in range(0,8): if (board[i][j]): grid.setPixel(i, j, i%4) else: grid.clearPixel(i-1,j-1) neighbors = [[0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0]] for i in range(0,8): for j in range(0,8): for k in [-1,0,1]: for m in [-1,0,1]: if(not((k==0) and (m==0))):
import serial from Adafruit_8x8 import ColorEightByEight # must have pyserial install sudo apt-get install python # commands for LCD found here //https://www.parallax.com/sites/default/files/downloads/27979-Parallax-Serial-LCDs-Product-Guide-v3.1.pdf # =========================================================================== # 8x8 Pixel Example # =========================================================================== grid = ColorEightByEight(address=0x70, debug=True) grid.disp.setBlinkRate(2) serialport = serial.Serial("/dev/ttyAMA0", 9600, timeout=0.5) print "Press CTRL+Z to exit" iter = 0 serialport.write("\x11") serialport.write("\x0C") # Continually update the 8x8 display one pixel at a time while(True): iter += 1 for x in range(-1, 8): for y in range(1, 7): grid.setPixel(x, y, iter % 8 ) serialport.write("\x0C") serialport.write("x="+str(x) + " y="+ str(y)) time.sleep(0.25)
#before outputting, reverse each of the deques since the screen is backwards #if(toggle): #currDeque0.reverse() #currDeque1.reverse() #currDeque2.reverse() #currDeque3.reverse() #currDeque4.reverse() #currDeque5.reverse() #currDeque6.reverse() #currDeque7.reverse() while (iter < 10): iter += 1 for y in range(0, 8): grid.setPixel(0, y, currDeque0[y]) grid.setPixel(1, y, currDeque1[y]) grid.setPixel(2, y, currDeque2[y]) grid.setPixel(3, y, currDeque3[y]) grid.setPixel(4, y, currDeque4[y]) grid.setPixel(5, y, currDeque5[y]) grid.setPixel(6, y, currDeque6[y]) grid.setPixel(7, y, currDeque7[y]) #time.sleep(0.02) #print ":", iter if (toggle): currDeque0.rotate(1) currDeque1.rotate(1) currDeque2.rotate(1) currDeque3.rotate(1)
'/home/pi/python-code/Adafruit-Raspberry-Pi-Python-Code/Adafruit_LEDBackpack' ) from Adafruit_8x8 import ColorEightByEight board = [[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] grid = ColorEightByEight(address=0x70) iter = 0 while (True): iter = iter + 1 for i in range(0, 8): for j in range(0, 8): if (board[i][j]): grid.setPixel(i, j, i % 4) else: grid.clearPixel(i - 1, j - 1) neighbors = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] for i in range(0, 8): for j in range(0, 8): for k in [-1, 0, 1]: for m in [-1, 0, 1]: if (not ((k == 0) and (m == 0))): neighbors[i][j] += board[(i + k) % 8][(j + m) % 8] for i in range(0, 8): for j in range(0, 8): if (((board[i][j] == 1) and
# Enable debug output DEBUG = True # display buffer to represent the 8x8 LED matrix matrix = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] grid = ColorEightByEight(address=0x70) # clear matrix for x in range(0, 8): for y in range(0, 8): grid.setPixel(x, y, 0) ADC.setup() # continually scroll the matrix and plot new ADC sample while (True): for x in range(7): for y in range(8): matrix[x][y] = matrix[x + 1][y] # read sample from ADC and convert to integer 0-8 adc_sample = ADC.read("P9_40") sample_flot = adc_sample * 8.0 if sample_flot > 7.5: sample_flot = 8