Last active
December 26, 2015 08:00
-
-
Save thisismyrobot/c592cc9d85c21e04626c to your computer and use it in GitHub Desktop.
Very basic test code for controlling DFRobot 3-wire LCD with a Raspberry Pi
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
# For Raspberry Pi. | |
# Code based on: http://www.dfrobot.com/wiki/index.php?title=SPI_LED_Module_(SKU:DFR0090)#Sample_Code | |
import re | |
import spidev | |
# 0-9 | |
NUMS = (0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90) | |
# a-z | |
CHARS = (0xA0,0x83,0xa7,0xa1,0x86,0x8e,0xc2,0x8b,0xe6,0xe1, | |
0x89,0xc7,0xaa,0xc8,0xa3,0x8c,0x98,0xce,0x9b,0x87, | |
0xc1,0xe3,0xd5,0xb6,0x91,0xb8) | |
class LED(object): | |
def __init__(self): | |
self._spi = spidev.SpiDev() | |
self._spi.open(0, 0) | |
def send(self, chars, clear=True): | |
# Clear | |
if clear: | |
self._spi.xfer2([0xff]*8) | |
# Expects string of [a-z0-9. ] | |
out = [] | |
for c in chars.lower(): | |
if re.match('[0-9]', c) is not None: | |
idx = int(c) | |
out.append(NUMS[idx]) | |
elif re.match('[a-z]', c) is not None: | |
idx = ord(c) - 97 | |
out.append(CHARS[idx]) | |
elif c == '.': | |
# Try to efficiently include the '.' | |
# using AND with previous char | |
out[-1] = out[-1] & 0x7f | |
elif c == ' ': | |
out.append(0xff) | |
self._spi.xfer2(out[::-1]) | |
if __name__ == '__main__': | |
led = LED() | |
led.send('Raspbery') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment