3.57 KB
Test python sketch for Adafruit USB+Serial LCD backpack

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada  for Adafruit Industries.  
BSD license, check license.txt for more information
All text above must be included in any redistribution

*** Changed to a class by John Snowdon (, January 2016)


import serial
import sys
import time

# 20x4 LCD
#ROWS = 4
#COLS = 20

# 16x2 LCD:
ROWS = 2
COLS = 16

class Matrix():
	def __init__(self, device = "/dev/ttyACM0"):
		# initialise the class and attempt to open the serial device
		s = serial.Serial(device, 57600, timeout=1)
		self.device = s
		self.scroll = False
		self.cols = COLS
		self.rows = ROWS
		self.r = 0
		self.g = 0
		self.b = 0
	def matrixwritecommand(self, commandlist):
		# write a command to the lcd
		commandlist.insert(0, 0xFE)
		for i in range(0, len(commandlist)):
	def matrixNewline(self):
	def matrixWrite(self, text):
		# If we're not autoscrolling, then
		# pad the text to the length of the line.
		if self.scroll == False:
			if len(text) < self.cols:
				text = text.ljust(self.cols)
	def matrixSize(self, cols = COLS, rows = ROWS):
		# set size
		self.cols = cols
		self.rows = rows
		self.matrixwritecommand([0xD1, cols, rows]);
	def matrixAutoscrollOn(self):
		self.matrixwritecommand([0xFE, 0x51])
	def matrixAutoscrollOff(self):
		self.matrixwritecommand([0xFE, 0x52])
	def matrixContrast(self, contrast = 0x220):
		# set contrast
		self.matrixwritecommand([0x50, contrast]) 
	def matrixOff(self):
		# turn off display
	def matrixOn(self):
		# turn on display
		self.matrixwritecommand([0x42, 0]) 
	def matrixBrightness(self, brightness = 0x200):
		# set brightness
		self.matrixwritecommand([0x99, brightness]) 
	def matrixClear(self):
		# clear screen
	def matrixPulse(self, r = 0x0, g = 0x0, b = 0x0):
		# Pulse to the set RGB values, from the 
		# current values stored in self.r/g/b and back again
		self.matrixwritecommand([0xD0, r, g, b])
	def matrixRGB(self, r = 0x200, g = 0x0, b = 0x0):
		self.r = r
		self.g = g
		self.b = b
		self.matrixwritecommand([0xD0, r, g, b]) 
	def matrixScrollOn(self):
		# Turn on scrolling of lines longer than COLS
		self.scroll = True
	def matrixScrollOff(self):
		# Turn off scrolling of lines longer than COLS
		self.scroll = False
	def matrixCursorOn(self):
		#underline cursor on
	def matrixCursorOff(self):
		# cursor off
	def matrixBlockCursorOn(self):
		# block cursor on
	def matrixBlockCursorOff(self):
	def matrixSetSplash(self, splash = "Hello"):
		# Splashscreen change
		# loop over string, get the ordinal values, append to 0x40...
		self.matrixwritecommand([0x40, ord('H'),ord('e'),ord('l'),ord('l'),ord('o'),ord(' '),ord('W'),ord('o'),ord('r'),ord('l'),ord('d'),ord('!'),ord(' '),ord(' '),ord(' '),ord(' '),ord('T'),ord('e'),ord('s'),ord('t'),ord('i'),ord('n'),ord('g'),ord(' '),ord('1'),ord('6'),ord('x'),ord('2'),ord(' '),ord('L'),ord('C'),ord('D')])