Skip to content

Update for pyb -> machine migration #3

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
35 changes: 10 additions & 25 deletions matrix8x8.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pyb
from machine import I2C, Pin


class Matrix8x8:
Expand Down Expand Up @@ -31,10 +31,11 @@ def __init__(self, i2c_bus=1, addr=0x70, brightness=15, i2c=None):
if i2c:
self.i2c = i2c
else:
self.i2c = pyb.I2C(i2c_bus, pyb.I2C.MASTER, baudrate=400000)
self.i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
self.i2c.start()

# set HT16K33 oscillator on
self._send(0x21)
self._send(b'\x21')

self.set_brightness(brightness)
self.clear()
Expand All @@ -44,25 +45,20 @@ def _send(self, data):
"""
Send data over I2C.
"""
self.i2c.send(data, self.addr)
self.i2c.writeto(self.addr, data)

def _send_row(self, row):
"""
Send single row over I2C.
"""
data = bytes((self.row_addr[row], rotate_right(self.buf[row])))
data = bytes((self.row_addr[row], self.buf[row]))
self._send(data)

def _send_buf(self):
"""
Send buffer over I2C.
"""
data = bytearray(16)
i = 1
for byte in self.buf:
data[i] = rotate_right(byte)
i += 2
self._send(data)
self._send(self.buf)

def _clear_column(self, column):
"""
Expand Down Expand Up @@ -94,21 +90,21 @@ def on(self):
Turn on display.
"""
self.is_on = True
self._send(0x81 | self._blinking << 1)
self._send(bytes([0x81 | self._blinking << 1]))

def off(self):
"""
Turn off display. You can controll display when it's off (change image,
brightness, blinking, ...).
"""
self.is_on = False
self._send(0x80)
self._send(b'\x80')

def set_brightness(self, value):
"""
Set display brightness. Value from 0 (min) to 15 (max).
"""
self._send(0xE0 | value)
self._send(bytes([0xE0 | value]))

def set_blinking(self, mode):
"""
Expand Down Expand Up @@ -179,14 +175,3 @@ def clear_pixel(self, row, column):
self.buf[row] &= ~(0x80 >> column)
self._send_row(row)


def rotate_right(byte):
"""
Rotate bits right.
"""
byte &= 0xFF
bit = byte & 0x01
byte >>= 1
if(bit):
byte |= 0x80
return byte