Skip to content

Commit

Permalink
QA fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Oct 14, 2024
1 parent f935375 commit a1636ae
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
8 changes: 5 additions & 3 deletions examples/4player.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import math

from machine import I2C
from qwstpad import QwSTPad, ADDRESSES
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_RGB565
from picographics import DISPLAY_PICO_DISPLAY_2, PEN_RGB565, PicoGraphics

from qwstpad import ADDRESSES, QwSTPad

# Constants
WHITE = const(65535)
Expand Down Expand Up @@ -164,4 +166,4 @@ def update(self):

while True:
g.update()
g.draw(display)
g.draw(display)
3 changes: 2 additions & 1 deletion examples/led_wave.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import time

from machine import I2C
from qwstpad import QwSTPad

from qwstpad import QwSTPad

"""
"""
Expand Down
10 changes: 6 additions & 4 deletions examples/maze.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import time

from machine import I2C
from qwstpad import QwSTPad, DEFAULT_ADDRESS
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_RGB565
from picographics import DISPLAY_PICO_DISPLAY_2, PEN_RGB565, PicoGraphics

from qwstpad import DEFAULT_ADDRESS, QwSTPad

# Constants
WHITE = const(65535)
BLACK = const(0)
GREEN = const(57351)

# Classes

# Classes
class Game(object):
def __init__(self, i2c):

Expand Down Expand Up @@ -158,4 +160,4 @@ def __init__(self, x, y, colour):
while True:
if not g.complete:
g.update()
g.draw(display)
g.draw(display)
2 changes: 2 additions & 0 deletions examples/read_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time

from machine import I2C

from qwstpad import QwSTPad

"""
Expand Down
25 changes: 13 additions & 12 deletions src/qwstpad.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import struct
from micropython import const
from collections import OrderedDict

from micropython import const

__version__ = "0.0.1"

# Constants
Expand All @@ -25,12 +26,12 @@ class QwSTPad:
POLARITY_PORT1 = const(0x05)
CONFIGURATION_PORT0 = const(0x06)
CONFIGURATION_PORT1 = const(0x07)

# Mappings
BUTTON_MAPPING = OrderedDict({'A': 0xE, 'B': 0xC, 'X': 0xF, 'Y': 0xD,
'U': 0x1, 'D': 0x4, 'L': 0x2, 'R': 0x3,
'+': 0xB, '-': 0x5
})
})
LED_MAPPING = (0x6, 0x7, 0x9, 0xA)

def __init__(self, i2c, address=DEFAULT_ADDRESS):
Expand All @@ -39,16 +40,16 @@ def __init__(self, i2c, address=DEFAULT_ADDRESS):

self.__i2c = i2c
self.__address = address

# Set up the TCA9555 with the correct input and output pins
self.__reg_write_uint16(self.__i2c, self.__address, self.CONFIGURATION_PORT0, 0b11111001_00111111)
self.__reg_write_uint16(self.__i2c, self.__address, self.POLARITY_PORT0, 0b11111000_00111111)
self.__reg_write_uint16(self.__i2c, self.__address, self.OUTPUT_PORT0, 0b00000110_11000000)
self.__reg_write_uint16(self.__i2c, self.__address, self.OUTPUT_PORT0, 0b00000110_11000000)

self.__button_states = OrderedDict({})
for key, _ in self.BUTTON_MAPPING.items():
self.__button_states[key] = False

self.__led_states = 0b0000
self.set_leds_from_addr()

Expand All @@ -61,18 +62,18 @@ def read_buttons(self):
def set_leds(self, states):
self.__led_states = states & 0b1111
self.__update_leds()

def set_leds_from_addr(self):
self.__led_states = self.__change_bit(0b0000, ADDRESSES.index(self.__address), True)
self.__update_leds()

def set_led(self, led, state):
if led < 1 or led > NUM_LEDS:
raise ValueError("'led' out of range. Expected 1 to 4")

self.__led_states = self.__change_bit(self.__led_states, led - 1, state)
self.__update_leds()

def clear_leds(self):
self.__led_states = 0b0000
self.__update_leds()
Expand All @@ -82,7 +83,7 @@ def __update_leds(self):
for i in range(NUM_LEDS):
output = self.__change_bit(output, self.LED_MAPPING[i], not self.__get_bit(self.__led_states, i))
self.__reg_write_uint16(self.__i2c, self.__address, self.OUTPUT_PORT0, output)

def __get_bit(self, num, bit_pos):
return (num & (1 << bit_pos)) != 0

Expand All @@ -92,7 +93,7 @@ def __change_bit(self, num, bit_pos, state):
def __reg_write_uint16(self, i2c, address, reg, value):
buffer = struct.pack("<H", value)
i2c.writeto_mem(address, reg, buffer)

def __reg_read_uint16(self, i2c, address, reg):
buffer = i2c.readfrom_mem(address, reg, 2)
return struct.unpack("<H", buffer)[0]

0 comments on commit a1636ae

Please sign in to comment.