Skip to content

Instantly share code, notes, and snippets.

@glennklockwood
Last active October 15, 2016 21:44
Show Gist options
  • Save glennklockwood/df3ff8776814aa96d69ed13a8861134c to your computer and use it in GitHub Desktop.
Save glennklockwood/df3ff8776814aa96d69ed13a8861134c to your computer and use it in GitHub Desktop.
Turn on/off individual LEDs on a 8x8 matrix driven by MAX7219
#!/usr/bin/env python
import spi
### Initialize an SPI connection using BCM-mode pins 21, 20, and 16
max7219 = spi.SPI(clk=21, cs=20, mosi=16, miso=None, verbose=True)
### Zero out all registers
for cmd in range(16):
packet = cmd << 8
max7219.put(packet,12)
### Enable all columns via the scan limit register
max7219.put(int("101100000111",2),12)
### Disable shutdown mode
max7219.put(int("110000000001",2),12)
### Illuminate column 2 with a specific pattern
max7219.put(int("001000100111",2),12)
try:
input("Press return to continue")
except:
pass
#!/usr/bin/env python
"""
Change the state of a single LED in an 8x8 matrix.
"""
import spi
### Initialize an SPI connection using BCM-mode pins 21, 20, and 16
max7219 = spi.SPI(clk=21, cs=20, mosi=16, miso=None, verbose=True)
### Zero out all registers
for cmd in range(16):
packet = cmd << 8
max7219.put(packet,12)
### Set the scan limit register and disable shutdown mode
max7219.put(int("101100000111",2),12)
max7219.put(int("110000000001",2),12)
### We zeroed out all registers, so all LEDs are off (0)
led_state = [0]*64
def set_led(row, column, state):
### update our saved state
led_state[column*8 + row] = state
### convert the new column into an SPI command
register = (column+1) << 8
value = 0
for row in range(8):
value <<= 1
if led_state[column*8+row]:
value |= 1
max7219.put(register|value,12)
while True:
row, col, state = input("Enter a row, col, state: ")
set_led(row, col, state)
#!/usr/bin/env python
import random
import time
import spi
### Initialize an SPI connection using BCM-mode pins 21, 20, and 16
max7219 = spi.SPI(clk=21, cs=20, mosi=16, miso=None, verbose=True)
### Zero out all registers
for cmd in range(16):
packet = cmd << 8
max7219.put(packet,12)
### Set the scan limit register and disable shutdown mode
max7219.put(int("101100000111",2),12)
max7219.put(int("110000000001",2),12)
### Illuminate column 2 with a specific pattern
max7219.put(int("001000100111",2),12)
### Fill columns 5-8 with a random pattern
while True:
for column in (5, 6, 7, 8):
register_addr = column << 8
value = random.randint(0, 2**8-1)
max7219.put(register_addr|value,12)
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment