Skip to content

Commit f0f98cb

Browse files
committed
First commit of pilite.py and tes.py source
1 parent 2b32aa5 commit f0f98cb

2 files changed

Lines changed: 197 additions & 0 deletions

File tree

pilite.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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)

test.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/python2
2+
3+
import pilite
4+
import time
5+
import random
6+
7+
if __name__ == "__main__":
8+
p=pilite.PiLite()
9+
p.all_on()
10+
time.sleep(0.5)
11+
p.all_off()
12+
time.sleep(0.5)
13+
p.set_fb("000000000000000000000111000011111110011111110111111111111101111111101111011000110011000110000000000000000000000000000000000000")
14+
time.sleep(0.5)
15+
for i in range(2):
16+
for i in range(2):
17+
p.scroll_left(1)
18+
time.sleep(0.1)
19+
for i in range(5):
20+
p.scroll_right(1)
21+
time.sleep(0.1)
22+
for i in range(3):
23+
p.scroll_left(1)
24+
time.sleep(0.1)
25+
p.all_off()
26+
for i in range(33,126):
27+
p.display_char(1,1,chr(i))
28+
p.display_char(8,1,chr(i+1))
29+
time.sleep(0.1)
30+
p.all_off()
31+
time.sleep(0.5)
32+
p.send_wait("Hello")
33+
p.send_wait("World")
34+
for speed in [10, 25, 50, 100]:
35+
p.set_speed(speed)
36+
p.send_wait("SPEED "+str(speed))
37+
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14
38+
bars=[ [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90,100, 70, 40, 10 ],
39+
[ 10, 25, 40, 55, 70, 85,100, 85, 70, 55, 40, 25, 10, 0 ],
40+
[100, 0, 85, 15, 70, 30, 55, 45, 40, 60, 25, 75, 10, 90 ] ]
41+
for g in bars:
42+
for i,b in enumerate(g):
43+
p.set_bar(i,b)
44+
time.sleep(.5)
45+
p.all_off()
46+
vu=[100,100]
47+
for reps in range(100):
48+
for i,v in enumerate(vu):
49+
if vu[i]>0:
50+
vu[i]-=5
51+
if random.random()>0.8:
52+
vu[i]=100
53+
p.set_vu(i,vu[i])
54+
time.sleep(0.025)
55+
p.all_off()
56+
for x in range(14):
57+
for y in range(9):
58+
p.pixel_on(x,y)
59+
for reps in range(1000):
60+
p.pixel_off(random.randint(0,13),random.randint(0,8))
61+
for reps in range(1000):
62+
p.toggle_pixel(random.randint(0,13),random.randint(0,8))
63+
p.all_off()

0 commit comments

Comments
 (0)