Skip to content

Updated MAX7219 to be compatible with CircuitPython #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 19, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 171 additions & 62 deletions adafruit_max7219.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
# The MIT License (MIT)
#
# Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_max7219`
====================================================

Driver for MAX7219 LED matrix driver chip.

* Author(s): Radomir Dopieralski
"""
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
import time
import framebuf
import digitalio

from adafruit_bus_device import spi_device


# register definitions
_NOOP = const(0)
_DIGIT0 = const(1)
_DIGIT1 = const(2)
Expand All @@ -44,47 +23,177 @@
_DISPLAYTEST = const(15)


class Matrix8x8:
def __init__(self, spi, cs):
self.spi = spi
class MAX7219_SPI:

def __init__(self, width, height, spi, csPin,
baudrate=8000000, polarity=0, phase=0):

cs = digitalio.DigitalInOut(csPin)
self.cs = cs
self.cs.init(cs.OUT, True)
self.buffer = bytearray(8)
self.init()
self.cs.direction = digitalio.Direction.OUTPUT

self.spiDevice = spi_device.SPIDevice(spi, cs, baudrate=baudrate,
polarity=polarity, phase=phase)

def _register(self, command, data):
self.cs.low()
self.spi.write(bytearray([command, data]))
self.cs.high()

self.buffer = bytearray((height // 8) * width)
self.framebuf = framebuf.FrameBuffer1(self.buffer, width, height)

def init(self):
for command, data in (
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
(_SCANLIMIT, 7),
(_DECODEMODE, 0),
(_SHUTDOWN, 1),
):
self._register(command, data)
self.width = width
self.height = height

self.init_display()

def brightness(self, value):
"""
control the brightness of the display
param: value - 0->15 dimmest to brightest
"""
if not 0<= value <= 15:
raise ValueError("Brightness out of range")
self._register(_INTENSITY, value)
self.write_cmd(_INTENSITY, value)

def fill(self, color):
data = 0xff if color else 0x00
def show(self):
"""
update the display with recent changes in buffer
"""
for y in range(8):
self.buffer[y] = data
self.write_cmd(_DIGIT0 + y, self.buffer[y])

def pixel(self, x, y, color=None):
if color is None:
return bool(self.buffer[y] & 1 << x)
elif color:
self.buffer[y] |= 1 << x
else:
self.buffer[y] &= ~(1 << x)
def fill(self, col):
"""
set all buffer bits to a col
param: col - value > 0 set the buffer bit, else clears the buffer bit
"""
self.framebuf.fill(col)

def show(self):
for y in range(8):
self._register(_DIGIT0 + y, self.buffer[y])
def pixel(self, x, y, col=None):
"""
set one buffer bit
param: col - value > 0 set the buffer bit, else clears the buffer bit
"""
col = 0x01 if col else 0x00
self.framebuf.pixel(x, y, col)

def scroll(self, dx, dy):
self.framebuf.scroll(dx, dy)

def write_cmd(self, command, data):
print('command {} data {}'.format(command,data))
self.cs.value = False
with self.spiDevice as spiDevice:
spiDevice.write(bytearray([command, data]))


class Matrix8x8(MAX7219_SPI):
def __init__(self, spi, csPin):
super().__init__(8,8,spi,csPin)

def init_display(self):
for command, data in (
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
(_SCANLIMIT, 7),
(_DECODEMODE, 0),
(_SHUTDOWN, 1),
):
self.write_cmd(command, data)

self.fill(0)
self.show()

def text(self, string, x, y, col=1):
"""
draw text in the 8x8 matrix.
"""
self.framebuf.text(string, x, y, col)

def clearAll():
"""
unlights all matrix leds
"""
self.fill(0)

class BCDDigits(MAX7219_SPI):
"""
Basic support for display on a 7-Segment BCD display controlled
by a Max7219 chip using SPI.
"""
def __init__(self, spi, csPin, nDigits=1):
self.nDigits = nDigits
super().__init__(self.nDigits, 8 ,spi ,csPin)

def init_display(self):

for command, data in (
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
(_SCANLIMIT, 7),
(_DECODEMODE, (2**self.nDigits)-1),
(_SHUTDOWN, 1),
):
self.write_cmd(command, data)

self.clearAll()
self.show()

def setDigit(self, digit, digitValue):
"""
set one digit in the display
param: digit - the display digit zero-based
param: digitValue - integer ranging from 0->15
"""
for i in range(4):
print('digit {} pixel {} value {}'.format(digit,i+4,digitValue & 0x01))
self.pixel(digit,i,digitValue & 0x01)
digitValue >>= 1

def setDigits(self, start, digits):
"""
set the display from a list
param: start - digit to start display zero-based
param: digits - list of integer values ranging from 0->15
"""
for digit in digits:
print('set digit {} start {}'.format(digit,start))
self.setDigit(start,digit)
start += 1

def setIntDigits(self, start, wide, value):
"""
start is the start digit position zero based
param: wide is the number of digits to show/use
param: value is a number to display
"""
value = abs(int(value))
digits = []
# initialize all digits to blank
for i in range(wide):
digits.append(0x0f)
# initalize a zero digit
digits[wide-1] = 0x00
# fill the field with digits right to left
for i in range(wide):
if value != 0:
digits[wide-i-1] = value % 10
value //= 10
# now send the digits to the display
self.setDigits(start, digits)

def setDot(self,whichDigit, col=None):
"""
set the decimal point for a digit
param: whichDigit - the digit to set the decimal point zero-based
param: col - value > zero lights the decimal point, else unlights the point
"""
self.pixel(whichDigit,7,col)

def clearAll(self):
"""
clear all digits and decimal points
"""
self.fill(1)
for i in range(self.nDigits):
self.setDot(i)


132 changes: 132 additions & 0 deletions adafruit_max7219/bcddigits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# The MIT License (MIT)
#
# Copyright (c) 2017 Dan Halbert
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

"""
:mod:`adafruit_max7219.bcddigits.BCDDigits`
====================================================
"""
from adafruit_max7219 import max7219

_DECODEMODE = const(9)
_SCANLIMIT = const(11)
_SHUTDOWN = const(12)
_DISPLAYTEST = const(15)

class BCDDigits(max7219.MAX7219):
"""
Basic support for display on a 7-Segment BCD display controlled
by a Max7219 chip using SPI.
"""
def __init__(self, spi, csPin, nDigits=1):
"""
param: spi - an spi busio or spi bitbangio object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the sphinx format for this is :param <type> spi: such as :param ~busio.SPI spi:.

param: csPin - board pin to use as chip select signal
param: nDigits number of led 7-segment digits; default 1; max 8
"""
self.nD = nDigits
super().__init__(self.nD, 8 ,spi ,csPin)

def init_display(self):

for cmd, data in (
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
(_SCANLIMIT, 7),
(_DECODEMODE, (2**self.nD)-1),
(_SHUTDOWN, 1),
):
self.write_cmd(cmd, data)

self.clearAll()
self.show()

def setDigit(self, d, v):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change function name style to set_digit. Its a common Python style thing.

"""
set one digit in the display
param: d - the digit position; zero-based
param: v - integer ranging from 0->15
"""
d = self.nD - d - 1
for i in range(4):
#print('digit {} pixel {} value {}'.format(d,i+4,v & 0x01))
self.pixel(d,i,v & 0x01)
v >>= 1

def setDigits(self, s, ds):
"""
set the display from a list
param: s - digit to start display zero-based
param: ds - list of integer values ranging from 0->15
"""
for d in ds:
#print('set digit {} start {}'.format(d,start))
self.setDigit(s,d)
s += 1

def setDot(self,d, col=None):
"""
set the decimal point for a digit
param: d - the digit to set the decimal point zero-based
param: col - value > zero lights the decimal point, else unlights the point
"""
if d < self.nD and d >= 0:
#print('set dot {} = {}'.format((self.nD - d -1),col))
self.pixel(self.nD-d-1, 7,col)

def clearAll(self):
"""
clear all digits and decimal points
"""
self.fill(1)
for i in range(self.nD):
self.setDot(i)

def showStr(self,s,str):
"""
displays a numeric str in the display. shows digits 0-9, -, and .
param: s - start position to show the numeric string
param: str - the numeric string
"""
ci = s
for i in range (len(str)):
c = str[i]
# print('c {}'.format(c))
v = 0x0f # assume blank
if c >= '0' and c<='9':
v = int(c)
elif c == '-':
v = 10
elif c == '.':
self.setDot(ci-1,1)
continue
self.setDigit(ci,v)
ci += 1

def showHelp(self, s):
"""
display the word HELP in the display
param: s - start position to show HELP
"""
digits = [12,11,13,14]
self.setDigits(s,digits)

Loading