Permalink
Browse files

added UDP simulator support to hexaservice

  • Loading branch information...
1 parent 747f7d3 commit 723d780be95cccb37b9ef6525daebe78a796cc46 @mhenstell mhenstell committed Jun 28, 2013
Showing with 60 additions and 13 deletions.
  1. +49 −12 hexaservice/led_panel.py
  2. +11 −1 hexaservice/service.py
View
@@ -3,6 +3,7 @@
import serial
import sys
import struct
+import random
CC_TEXT = 0xA1
CC_BITMAP = 0xA2
@@ -15,26 +16,51 @@ def compile_image(img, x=0, y=0):
bitmap = ''
width = min(img.size[0]-x,120)
height = min(7,img.size[1]-y)
+
for i in range(width):
b = 0
- for j in range(height):
+ for j in range(height): # vertical scanning? jerk
if img.getpixel(( i+x, j+y )):
b |= 1 << (7-j)
bitmap = bitmap + struct.pack("B",b)
return bitmap
class Panel:
- def __init__(self):
+ def __init__(self, debug=False):
self.serialPort = None
+
+ if debug is not False:
+ self.debug = True
+ self.id = debug
+ else:
+ self.debug = False
def open(self,portName,baud=9600):
- self.serialPort = serial.Serial(portName, baud, timeout=0.5)
- try:
- self.serialPort.open()
- except serial.SerialException as e:
- sys.stderr.write("Could not open serial port %s: %s\n" % (self.serialPort.portstr, e))
+ if self.debug:
+ import socket
+ print "Opening UDP socket to localhost : %s" % portName
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ self.port = portName
+
+ else:
+ self.serialPort = serial.Serial(portName, baud, timeout=0.5)
+ try:
+ self.serialPort.open()
+ except serial.SerialException as e:
+ sys.stderr.write("Could not open serial port %s: %s\n" % (self.serialPort.portstr, e))
def command(self,command,payload,expected):
+ if self.debug:
+ #print struct.unpack("B", payload)
+ # for y in range(0, 7):
+ # for byte in payload[y * 15 : (y * 15) + 15]:
+ # print hex(ord(byte)),
+ # print
+ # print
+ payload = chr(self.id) + payload
+ self.sock.sendto(payload, ("127.0.0.1", self.port))
+ return
+
l = len(payload)
self.serialPort.write(struct.pack("BB",command,l))
if l > 0:
@@ -48,6 +74,7 @@ def command(self,command,payload,expected):
return rpay
def close(self):
+ if self.debug: return
self.serialPort.close()
def setRelay(self, on):
@@ -69,18 +96,28 @@ def setCompiledImage(self, bitmap):
self.command(CC_BITMAP,bitmap,0)
def getID(self):
+ if self.debug: return self.id
+
v = self.command(CC_GET_ID,"",1)
id = ord(v[0])
return id
panels = [None]*3
-def init():
- for port in range(0,3):
- name = "/dev/ttyACM{0}".format(port)
- p = Panel()
- p.open(name)
+def init(debug = False):
+ for port_num in range(0,3):
+
+ if debug:
+ port = 9990 + port_num
+ p = Panel(port_num)
+ p.open(port)
+
+ else:
+ name = "/dev/ttyACM{0}".format(port_num)
+ p = Panel()
+ p.open(name)
+
panels[p.getID()] = p
def shutdown():
View
@@ -8,29 +8,39 @@
import sys
import threading
+debug = False
+
class PanelThread(threading.Thread):
def __init__(self, panel):
pass
class ServiceThread:
pass
+
if __name__=="__main__":
- led_panel.init()
+
+ if len(sys.argv) > 1 and sys.argv[1] == 'debug':
+ debug = True
+
+ led_panel.init(debug)
panels[0].setRelay(True)
+
def sigint_handler(signal,frame):
print("Caught ctrl-C; shutting down.")
panels[0].setRelay(False)
led_panel.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT,sigint_handler)
+
while True:
msg = time.strftime("%H:%M:%S")
txtimg = base_font.strImg(msg)
img = Image.new("1",(120,7))
img.paste(txtimg,(15,0))
img.paste(txtimg,(75,0))
bitmap = compile_image(img,0,0)
+
for j in range(3):
panels[j].setCompiledImage(bitmap)
time.sleep(0.1)

0 comments on commit 723d780

Please sign in to comment.