Created
September 29, 2011 18:04
-
-
Save leetrout/1251447 to your computer and use it in GitHub Desktop.
Matrix Orbital Python Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import serial | |
import sys | |
import thread | |
import time | |
class Display(): | |
''' | |
Display class for Matrix Orbital MX LCD Screens. | |
Essentially a wrapper for serial.Serial with Matrix Orbital functions. | |
>>> d = Display(2) | |
>>> d.clear() | |
>>> d.send("Hello World!") | |
''' | |
#constants | |
## basic utils | |
NULL = '\x00' | |
CLEAR = '\xfe\x58' | |
CMD = '\xfe' | |
BACKLIGHT_ON = '\xfe\x42\x00' | |
BACKLIGHT_OFF = '\xfe\x46' | |
## bar graphs | |
INIT_VBG_THIN = '\xfe\x73' | |
INIT_VBG_THICK = '\xfe\x76' | |
INIT_HBG = '\xfe\x68' | |
## input | |
KEYS_IN = [] | |
LISTEN_FOR_KEYS = True | |
def clear(self): | |
'''Clears the screen''' | |
self.send(self.CLEAR) | |
def __init__(self, port, baud=19200): | |
self._serial = serial.Serial(port, baud) | |
self.clear() | |
def send(self, data): | |
'''Wrapper for our serial instance's write method''' | |
self._serial.write(data) | |
def close(self): | |
'''Wrapper for our serial instance's close method''' | |
self._serial.close() | |
def draw_vbg(self, cols=[], kind="thin"): | |
'''Method to draw a vertical bar graph | |
cols is an array of two-tuples [(column, height)] | |
example usage: | |
my_display.draw_vbg([(1,2),(2,4),(3,6)]) | |
The example would draw a bar graph with bars on | |
columns 1, 2, & 3 with heights 2, 4, & 6 (respectively) | |
''' | |
if kind == "thin": | |
self.send(self.INIT_VBG_THIN) | |
else: | |
self.send(self.INIT_VBG_THICK) | |
for col in cols: | |
c = int(col[0]) # column to draw | |
h = int(col[1]) # height of bar (0-14?) | |
try: | |
self.send(self.CMD+'\x3d'+chr(c)+chr(h)) | |
except: | |
raise | |
def clear_keys(self): | |
'''Clears out the KEYS_IN array''' | |
self.KEYS_IN = [] | |
# should not be called outside of class! use key_listen() | |
def _thread_key_listen(self): | |
while self.LISTEN_FOR_KEYS: | |
key = self._serial.read() | |
self.KEYS_IN.append(key) | |
#clean up on fail or exit | |
self.key_listen_thread = None | |
def key_listen(self): | |
'''starts a new thread to listen for key presses and | |
inserts them into the KEYS_IN array''' | |
self.key_listen_thread = thread.start_new_thread(self._thread_key_listen, ()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment