Mercurial > circuitpython-lameboy
changeset 9:e273cabfa0c1 draft
Port to CircuitPython
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Thu, 22 Feb 2018 21:35:17 +0100 |
| parents | b1afbd7addb3 |
| children | 08c0c8e701be |
| files | lameboy.py pew.py |
| diffstat | 2 files changed, 39 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/lameboy.py Thu Feb 22 21:09:53 2018 +0100 +++ b/lameboy.py Thu Feb 22 21:35:17 2018 +0100 @@ -1,16 +1,18 @@ import framebuf import time -from machine import SPI, I2C, Pin +import busio +import digitalio +import board class Display: width = 84 height = 48 - def __init__(self, spi, dc, rst=None, cs=None): + def __init__(self, spi, dc, rst, cs): self._spi = spi - self._cs = cs or (lambda x: x) - self._rst = rst or (lambda x: x) + self._cs = cs + self._rst = rst self._dc = dc self._fn = 0x20 self._buffer = bytearray(504) @@ -19,17 +21,17 @@ self.reset() def _command(self, command): - self._dc(0) - self._cs(0) + self._dc.value = False + self._cs.value = False self._spi.write(command) - self._cs(1) - self._dc(1) + self._cs.value = True + self._dc.value = True def reset(self): - self._rst(0) - time.sleep_us(100) - self._rst(1) - time.sleep_us(100) + self._rst.value = False + time.sleep(0.1) + self._rst.value = True + time.sleep(0.1) self.contrast() self.inverse(0) @@ -49,12 +51,12 @@ self._command(bytes((self._fn | 1, temp, bias, 0x80 | val, self._fn))) def update(self): - self._dc(0) - self._cs(0) + self._dc.value = False + self._cs.value = False self._spi.write(b'\x40\x80') - self._dc(1) + self._dc.value = True self._spi.write(self._buffer) - self._cs(1) + self._cs.value = True class Backlight: @@ -75,18 +77,28 @@ A = 0x08 B = 0x04 C = 0x01 + _byte = bytearray(1) def __init__(self, i2c, address=0x38): self._i2c = i2c self._address = address def get_pressed(self): - return self._i2c.readfrom(self._address, 1)[0] ^ 0xff + self._i2c.readfrom_into(self._address, self._byte) + return self._byte[0] ^ 0xff -i2c = I2C(-1, sda=Pin(4), scl=Pin(5)) +i2c = busio.I2C(sda=board.SDA, scl=board.SCL) +i2c.try_lock() backlight = Backlight(i2c) buttons = Buttons(i2c) -spi = SPI(1, baudrate=10000000) -display = Display(spi, Pin(2, Pin.OUT, value=1), Pin(0, Pin.OUT, value=0), - Pin(15, Pin.OUT, value=1)) +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +spi.try_lock() +spi.configure(baudrate=10000000) +_dc = digitalio.DigitalInOut(board.GPIO2) +_dc.switch_to_output(value=True) +_rst = digitalio.DigitalInOut(board.GPIO0) +_rst.switch_to_output(value=True) +_cs = digitalio.DigitalInOut(board.GPIO15) +_cs.switch_to_output(value=True) +display = Display(spi, _dc, _rst, _cs)
--- a/pew.py Thu Feb 22 21:09:53 2018 +0100 +++ b/pew.py Thu Feb 22 21:35:17 2018 +0100 @@ -16,10 +16,9 @@ ) _SALT = const(132) -_BUFFER = memoryview(bytearray(b'\x00\x00\x00\x00\x00\x00' - b'\x44\x11\x44\x11\x44\x11' - b'\xee\xbb\xee\xbb\xee\xbb' - b'\xff\xff\xff\xff\xff\xff')) +_BUFFER = memoryview(bytearray(b'\x00\x00\x00\x00\x00\x00\xaa\x00\xaa\x00\xaa' + b'\x00\xff\x55\xff\x55\xff\x55\xff\xff\xff\xff' + b'\xff\xff')) _TILES = tuple(framebuf.FrameBuffer(_BUFFER[6 * c:6 * c + 6], 8, 6, framebuf.MONO_HLSB) for c in range(4)) @@ -52,8 +51,8 @@ def tick(delay): global _tick - _tick += int(delay * 1000) - time.sleep_ms(max(0, _tick - time.ticks_ms())) + _tick += delay + time.sleep(max(0, _tick - time.monotonic())) class GameOver(Exception): @@ -180,4 +179,4 @@ if _tick is not None: return - _tick = time.ticks_ms() + _tick = time.monotonic()
