Last active
January 22, 2016 18:37
-
-
Save godot11/7ca8839943afd923f704 to your computer and use it in GitHub Desktop.
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
# IMPORTANT! | |
# | |
# THIS CODE IS UNFINISHED AND SOME FUNCTIONS (I.E. fit_to_screen()) ARE BROKEN! | |
# I made this code for myself, not for others, never meant to be released, just shared it related to another problem. | |
# Of course I do not ban you from it's usage, but I warned you. | |
# | |
# Distributed (or not) under the Creative Commons 2.0 license... nah, nobody cares. | |
# | |
# :author: Nagy Gergely | |
import spidev | |
import time | |
from PIL import Image, ImageDraw | |
'''register adresses''' | |
NOOP = 0x0 # only needed if more devices are cascaded | |
DIGIT0 = 0x1 | |
DIGIT1 = 0x2 # the register vaules in binary are equvivalents of the | |
DIGIT2 = 0x3 # illuminated and non-illuminated pixels in the columb | |
DIGIT3 = 0x4 # from top to bottom (if decodemode is false) | |
DIGIT4 = 0x5 | |
DIGIT5 = 0x6 | |
DIGIT6 = 0x7 | |
DIGIT7 = 0x8 | |
DECODEMODE = 0x9 # 0x0 for LED screen | |
INTENSITY = 0xA # brightness, 0x0 - 0x0F | |
SCANLIMIT = 0xB # number of columbs to display | |
SHUTDOWN = 0xC # 0x0 turns the screen off (the chip remains avaible for programming) | |
DISPLAYTEST = 0xF # 0x1 turns test mode on (all pixels illuminated at max brightness | |
class LedDisplay(object): | |
def __init__(self, device, speed=1000000, image=None): | |
''' | |
Initializes the hardware SPI connection on CS pin `device` at the speed `speed`, and sets deafults. | |
The maximum serial connection speed for MAX7219 is 10MHz. Also initializes the picture: it loads | |
and fits the provided `picture` if there is, else creates a new one. | |
''' | |
assert device == 0 or device == 1, 'wrong device ID' | |
assert 3814 <= speed <= 10000000, 'invaild connection speed' | |
self.display = spidev.SpiDev() | |
self.display.open(0, device) | |
#display.speed_in_hz = speed | |
self.send(SCANLIMIT, 0x7) # show all 8 digits | |
self.send(DECODEMODE, 0x0) # use matrix (not digits) | |
self.send(SHUTDOWN, 0x1) # not shutdown mode | |
self.send(DISPLAYTEST, 0x0) # no display test | |
if image is None: | |
self.image = Image.new('1', (8, 8)) | |
else: | |
self.image = fit_to_screen(image) | |
self.send(0x1, 0xAA) | |
def send(self, *args): | |
''' | |
Defines a fancier way of sending data to the device. | |
''' | |
self.display.xfer2(list(args)) | |
def fit_to_screen(self, image): | |
''' | |
If the picture is not a 2-colored one, and/or not 8x8 sized, it will be converted to fit. | |
''' | |
if (image.mode != '1'): | |
#print 'Image is converted to 1-bit color.' | |
image = image.convert('1') | |
if (image.size != (8, 8)): | |
#print 'Image has been resized.' | |
x, y = image.size | |
if (x >= y): | |
width = 8 | |
height = int(8 * y/x) | |
else: | |
width = int(8 * x/y) | |
height = 8 | |
image = image.resize((width, height)) | |
return image | |
def show(self, image): | |
''' | |
Draws the given image on the LED display. | |
''' | |
# calculating the numbers to what we have to set the columbs' registers | |
width, height = image.size | |
assert 1 <= width < 9 , 1 <= height <= 9 | |
pixels = [] | |
for x in range(0, width): | |
num = 0 | |
for y in range(0, height): | |
pix = image.getpixel((x, y)) | |
num = num + pix/255 * 2**y | |
pixels.append(num) | |
# sending the data to the chip | |
for x in range(1, 9): | |
self.send(x, pixels[x-1]) | |
def set_brightness(self, percent): | |
if not 0 <= percent <= 100: | |
raise ValueError('Brightness value must be between 0 and 16') | |
if percent == 0: | |
self.send(SHUTDOWN, 0x0) | |
else: | |
value = int(15.0/100 * percent) | |
self.send(SHUTDOWN, 0x1) | |
self.send(INTENSITY, value) | |
def finish(self): | |
image = Image.new('1', (8,8)) | |
self.show(image) | |
self.display.close() | |
def main(): | |
disp = LedDisplay(device = 1) | |
im = Image.new('1', (8, 8)) | |
draw = ImageDraw.Draw(im, mode='1') | |
delay = 1 | |
disp.set_brightness(16) | |
for j in range(0, 1): | |
for i in range(0, 4): | |
draw.rectangle((i, i, 7-i, 7-i), 0, 255) | |
time.sleep(delay) | |
disp.show(im) | |
for x in range(0, 8): | |
for y in range(0, 8): | |
if((x+y)%2 == 0): | |
im.putpixel((x, y), 255) | |
for i in range(0, 11): | |
disp.set_brightness(10*i) | |
time.sleep(1) | |
disp.finish() | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment