|
| 1 | +#!/usr/bin/python2 |
| 2 | + |
| 3 | +""" |
| 4 | +This python module provides an interface to the Ciseco Pi-LITE LED matrix |
| 5 | +through the Raspberry Pi's serial port. |
| 6 | +Before use, you will need to follow the instructions at |
| 7 | +http://tinyurl.com/omk9pby to disable the Pi using the serial port for login. |
| 8 | +
|
| 9 | +Stephen Blythe 2014 |
| 10 | +""" |
| 11 | + |
| 12 | +import serial |
| 13 | +import sys |
| 14 | +import time |
| 15 | + |
| 16 | +class PiLite: |
| 17 | + """Functions to control the PiLite. Instantiate the class to initialise |
| 18 | + the serial port using, for example: |
| 19 | + pilite=PiLite() |
| 20 | + Then call the other methods using the pilite object. |
| 21 | + """ |
| 22 | + |
| 23 | + BAUDRATE=9600 |
| 24 | + TIMEOUT=0 |
| 25 | + PORT="/dev/ttyAMA0" |
| 26 | + SPEED=50 |
| 27 | + COLS_PER_CHAR=6 #inc. space |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + self.s=serial.Serial() |
| 31 | + self.s.baudrate=PiLite.BAUDRATE |
| 32 | + self.s.timeout=PiLite.TIMEOUT |
| 33 | + self.s.port=PiLite.PORT |
| 34 | + try: |
| 35 | + self.s.open() |
| 36 | + except serial.SerialException, e: |
| 37 | + sys.stderr.write("could not open port %r: %s\n"%(s.port,e)) |
| 38 | + sys.exit(1) |
| 39 | + self.set_speed(PiLite.SPEED) |
| 40 | + |
| 41 | + def send(self,text): |
| 42 | + """Send a string to the PiLite, can be simple text or a $$$ command""" |
| 43 | + #print text |
| 44 | + self.s.write(text) |
| 45 | + time.sleep(0.001*len(text)) |
| 46 | + |
| 47 | + def send_wait(self,text): |
| 48 | + """Send a string to the PiLite, sleep until the message has been |
| 49 | + displayed (based on an estimate of the speed of the display. |
| 50 | + Due to the font not being monotype, this will wait too long in most |
| 51 | + cases""" |
| 52 | + self.send(text) |
| 53 | + time.sleep(len(text)*PiLite.COLS_PER_CHAR*self.speed/1000.0) |
| 54 | + |
| 55 | + def send_cmd(self,cmd): |
| 56 | + """Send a $$$ command - just pass the command itself to this function |
| 57 | + without the $$$ at the beginning or the CR at the end""" |
| 58 | + self.send("$$$"+cmd+"\r") |
| 59 | + |
| 60 | + def all_on(self): |
| 61 | + """Switch on all LEDs""" |
| 62 | + self.send_cmd("ALL,ON") |
| 63 | + |
| 64 | + def all_off(self): |
| 65 | + """Switch off all LEDs""" |
| 66 | + self.send_cmd("ALL,OFF") |
| 67 | + |
| 68 | + def set_speed(self,speed): |
| 69 | + """Set the display speed. The parameters is the number of milliseconds |
| 70 | + between each column scrolling off the display""" |
| 71 | + self.speed=speed |
| 72 | + self.send_cmd("SPEED"+str(speed)) |
| 73 | + |
| 74 | + def set_fb(self,fb): |
| 75 | + """Set the "frame buffer". fb is a string of "1" and "0" for each pixel |
| 76 | + """ |
| 77 | + self.send_cmd("F"+fb) |
| 78 | + |
| 79 | + def _set_indexed_value(self,cmd,index,value): |
| 80 | + self.send_cmd(cmd+str(index+1)+","+str(value)) |
| 81 | + |
| 82 | + def set_bar(self,index,value): |
| 83 | + """Assuming a vertical bar graph using each column of the display, set |
| 84 | + column "index" to percentage "value". Columns are indexed 0-13 from |
| 85 | + left to right""" |
| 86 | + self._set_indexed_value("B",index,value) |
| 87 | + |
| 88 | + def set_vu(self,index,value): |
| 89 | + """Assuming a horizontal bar graph with each bar using 5 rows of the |
| 90 | + display, set row "index" to percentage "value". Rows are indexed 0-1 |
| 91 | + from top to bottom""" |
| 92 | + self._set_indexed_value("V",index,value) |
| 93 | + |
| 94 | + def set_pixel(self,x,y,state): |
| 95 | + """Set pixel at "x,y" to "state" where state can be one of "ON", "OFF" |
| 96 | + or "TOGGLE" |
| 97 | + """ |
| 98 | + self.send_cmd("P"+str(x+1)+","+str(y+1)+","+state) |
| 99 | + |
| 100 | + def pixel_on(self,x,y): |
| 101 | + """Switch on pixel at "x,y" |
| 102 | + """ |
| 103 | + self.set_pixel(x,y,"ON") |
| 104 | + |
| 105 | + def pixel_off(self,x,y): |
| 106 | + """Switch off pixel at "x,y" |
| 107 | + """ |
| 108 | + self.set_pixel(x,y,"OFF") |
| 109 | + |
| 110 | + def toggle_pixel(self,x,y): |
| 111 | + """Toggle pixel at "x,y" |
| 112 | + """ |
| 113 | + self.set_pixel(x,y,"TOGGLE") |
| 114 | + |
| 115 | + def scroll(self,value): |
| 116 | + """Scroll the whole display "value" columns to the left. Use negative |
| 117 | + values to scroll to the right. |
| 118 | + """ |
| 119 | + self.send_cmd("SCROLL"+str(value)) |
| 120 | + |
| 121 | + def scroll_left(self,value): |
| 122 | + """Scroll the whole display "value" columns to the left. |
| 123 | + """ |
| 124 | + self.scroll(value) |
| 125 | + |
| 126 | + def scroll_right(self,value): |
| 127 | + """Scroll the whole display "value" columns to the right. |
| 128 | + """ |
| 129 | + self.scroll(-value) |
| 130 | + |
| 131 | + def display_char(self,x,y,char): |
| 132 | + """Display character "char" with its top left at "x,y" |
| 133 | + """ |
| 134 | + self.send_cmd("T"+str(x+1)+","+str(y+1)+","+char) |
0 commit comments