#!/usr/bin/python ######################################################################## # # Matrix3: Library of Pi Matrix display routines # # This Python script contains a bunch of routines for driving # the Pi Matrix & creating interesting displays. # Online References: w8bh.net & mypishop.com # YouTube video at http://youtu.be/VbPBNmlGy34 # # Author: Bruce E. Hall # Date : 20 Feb 2013 # ######################################################################## import smbus #gives us a connection to the I2C bus import time #for timing delays import random #random number generator #Definitions for the MCP23017 chip ADDR = 0x20 #The I2C address of our chip DIRA = 0x00 #PortA I/O direction, by pin. 0=output, 1=input DIRB = 0x01 #PortB I/O direction, by pin. 0=output, 1=input PORTA = 0x12 #Register address for PortA PORTB = 0x13 #Register address for PortB ORIENTATION = 180 #default viewing angle for the pi & matrix #set this value according to your viewing angle #values are 0 (power jack on left) # 90 (power jack on bottom) # 180 (power jack on right) # 270 (power jack on top) rows = 0x00 #starting pattern is (0,0) = all LEDS off columns= 0x00 delay = 0.08 #time delay between LED display transitions #smaller values = faster animation ######################################################################## # # Lower level routines for bit-manipulation. # Nothing here relates to our snazzy Pi Matrix. # def ReverseBits (byte): #reverse the bit order in the byte: bit0 <->bit 7, bit1 <-> bit6, etc. value = 0 currentBit = 7 for i in range(0,8): if byte & (1<>i) currentBit -= 1 return value def ROR (byte): #perform a 'rotate right' command on byte #bit 1 is rotated into bit 7; everything else shifted right bit1 = byte & 0x01 #get right-most bit byte >>= 1 #shift right 1 bit if bit1: #was right-most bit a 1? byte |= 0x80 #if so, rotate it into bit 7 return byte def ROL (byte): #perform a 'rotate left' command on byte #bit 7 is rotated into bit 1; everything else shifted left bit7 = byte & 0x080 #get bit7 byte <<= 1 #shift left 1 bit byte &= 0xFF #only keep 8 bits if bit7: #was bit7 a 1? byte |= 0x01 #if so, rotate it into bit 1 return byte ######################################################################## # # Lower Level LED Display routines. # These write data directly to the Pi Matrix board # def Write (register, value): #Abstraction of I2C bus and the MCP23017 chip #Call with the chip data register & value pair #The chip address is constant ADDR (0x20). bus.write_byte_data(ADDR,register,value) def EnableLEDS (): #Set up the 23017 for 16 output pins Write(DIRA, 0x00) #all zeros = all outputs on PortA Write(DIRB, 0x00) #all zeros = all outputs on PortB def DisableLEDS (): #Set all outputs to high-impedance by making them inputs Write(DIRA, 0xFF); #all ones = all inputs on PortA Write(DIRB, 0xFF); #all ones = all inputs on PortB def TurnOffLEDS (): #Clear the matrix display Write(PORTA, 0x00) #set all columns low Write(PORTB, 0x00) #set all rows low def TurnOnAllLEDS (): #Turn on all 64 LEDs Write(PORTA, 0xFF) #set all columns high Write(PORTB, 0x00) #set all rows low def WriteToLED (rowPins,colPins): #set logic state of LED matrix pins Write(PORTA, 0x00) #turn off all columns; prevent ghosting Write(PORTB, rowPins) #set rows first Write(PORTA, colPins) #now turn on columns ######################################################################## # # More low-level routines, saved for learning purposes. # # def FastSetLED (row,col): #turn on an individual LED at (row,col). All other LEDS off. #ignores orientation. Unless speed is required, use SetLED instead bus.write_byte_data(ADDR,PORTA,0x80>>col) bus.write_byte_data(ADDR,PORTB,~(1<>col) def FastSetRow (row): #turn on all LEDs in the specified row. Expects input of 0-7. #ignores orientation. Unless speed is required, use SetRow. bus.write_byte_data(ADDR,PORTA,0xFF) bus.write_byte_data(ADDR,PORTB,~(1<>1,columns) def MoveRight(): #shifts entire display one pixel to the right SetPattern(rows,columns<<1) def MoveLeft(): #shifts entire display one pixel to the left SetPattern(rows,columns>>1) ######################################################################## # # Higher-level routines for playing with the Pi Matrix board. # Each routine does a simple display animation, calling # one or more of the low-level routines above. # Set global variable DELAY to change animation speed (default 0.08) # def ShowRoutine (name): #display the routine name to console, file, or wherever you want. #this is useful for creating a log of your display. print " ",name def FlashLEDS (delay): #Flash all of the LEDS on/off for the specified time TurnOnAllLEDS() time.sleep(delay) TurnOffLEDS() time.sleep(delay) def MultiRowCylon (pattern=0x55,numCycles=4): #Do a side-to-side LED chaser using multiple rows ShowRoutine("%d Row Cylon %x" % (numCycles,pattern)) for count in range(0,numCycles): for col in range(0,8): SetPattern(pattern,1<