changeset 7:aa3586d05684 draft

Add the pew.py library
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Thu, 22 Feb 2018 20:40:16 +0100
parents a029d8735882
children b1afbd7addb3
files lameboy.py pew.py
diffstat 2 files changed, 188 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lameboy.py	Thu Feb 22 10:53:46 2018 +0100
+++ b/lameboy.py	Thu Feb 22 20:40:16 2018 +0100
@@ -13,10 +13,9 @@
         self._rst = rst or (lambda x: x)
         self._dc = dc
         self._fn = 0x20
-        buffer = bytearray(504)
-        self.fb = framebuf.FrameBuffer(buffer, 84, 48, framebuf.MONO_VLSB)
-        self._buffer = memoryview(buffer)
-
+        self._buffer = bytearray(504)
+        self.fb = framebuf.FrameBuffer(self._buffer, 84, 48,
+                                       framebuf.MONO_VLSB)
         self.reset()
 
     def _command(self, command):
@@ -24,6 +23,7 @@
         self._cs(0)
         self._spi.write(command)
         self._cs(1)
+        self._dc(1)
 
     def reset(self):
         self._rst(0)
@@ -88,4 +88,5 @@
 backlight = Backlight(i2c)
 buttons = Buttons(i2c)
 spi = SPI(1, baudrate=10000000)
-display = Display(spi, Pin(2, Pin.OUT), Pin(0, Pin.OUT), Pin(15, Pin.OUT))
+display = Display(spi, Pin(2, Pin.OUT, value=1), Pin(0, Pin.OUT, value=0),
+                  Pin(15, Pin.OUT, value=1))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pew.py	Thu Feb 22 20:40:16 2018 +0100
@@ -0,0 +1,182 @@
+from micropython import const
+import time
+import framebuf
+import lameboy
+
+
+_FONT = (
+    b'{{{{{{wws{w{HY{{{{YDYDY{sUtGUsH[wyH{uHgHE{ws{{{{vyxyv{g[K[g{{]f]{{{wDw{{'
+    b'{{{wy{{{D{{{{{{{w{K_w}x{VHLHe{wuwww{`KfyD{UKgKU{w}XDK{DxTKT{VxUHU{D[wyx{'
+    b'UHfHU{UHEKe{{w{w{{{w{wy{KwxwK{{D{D{{xwKwx{eKg{w{VIHyB{fYH@H{dHdHd{FyxyF{'
+    b'`XHX`{DxtxD{Dxtxx{FyxIF{HHDHH{wwwww{KKKHU{HXpXH{xxxxD{Y@DLH{IL@LX{fYHYf{'
+    b'`HH`x{fYHIF{`HH`H{UxUKU{Dwwww{HHHIR{HHH]w{HHLD@{HYsYH{HYbww{D[wyD{txxxt{'
+    b'x}w_K{GKKKG{wLY{{{{{{{{Dxs{{{{{BIIB{x`XX`{{ByyB{KBIIB{{WIpF{OwUwww{`YB[`'
+    b'x`XHH{w{vwc{K{OKHUxHpXH{vwws_{{dD@H{{`XHH{{fYYf{{`XX`x{bYIBK{Ipxx{{F}_d{'
+    b'wUws_{{HHIV{{HH]s{{HLD@{{HbbH{{HHV[a{D_}D{Cw|wC{wwwwwwpwOwp{WKfxu{@YYY@{'
+)
+_SALT = const(132)
+
+_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))
+
+K_X = lameboy.buttons.A
+K_DOWN = lameboy.buttons.DOWN
+K_LEFT = lameboy.buttons.LEFT
+K_RIGHT = lameboy.buttons.RIGHT
+K_UP = lameboy.buttons.UP
+K_O = lameboy.buttons.B
+
+_tick = None
+
+
+def brightness(level):
+    pass
+
+
+def show(pix):
+    for y in range(8):
+        for x in range(8):
+            color = pix.pixel(7 - x, 7 - y)
+            lameboy.display.fb.blit(_TILES[3 - color], 10 + x * 8, y * 6)
+    lameboy.display.update()
+
+
+def keys():
+    return lameboy.buttons.get_pressed()
+
+
+def tick(delay):
+    global _tick
+
+    _tick += int(delay * 1000)
+    time.sleep_ms(max(0, _tick - time.ticks_ms()))
+
+
+class GameOver(Exception):
+    pass
+
+
+class Pix:
+    __slots__ = ('buffer', 'width', 'height')
+
+    def __init__(self, width=8, height=8, buffer=None):
+        if buffer is None:
+            buffer = bytearray(width * height)
+        self.buffer = buffer
+        self.width = width
+        self.height = height
+
+    @classmethod
+    def from_text(cls, string, color=None, bgcolor=0, colors=None):
+        pix = cls(4 * len(string), 6)
+        font = memoryview(_FONT)
+        if colors is None:
+            if color is None:
+                colors = (3, 2, bgcolor, bgcolor)
+            else:
+                colors = (color, color, bgcolor, bgcolor)
+        x = 0
+        for c in string:
+            index = ord(c) - 0x20
+            if not 0 <= index <= 95:
+                continue
+            row = 0
+            for byte in font[index * 6:index * 6 + 6]:
+                unsalted = byte ^ _SALT
+                for col in range(4):
+                    pix.pixel(x + col, row, colors[unsalted & 0x03])
+                    unsalted >>= 2
+                row += 1
+            x += 4
+        return pix
+
+    @classmethod
+    def from_iter(cls, lines):
+        pix = cls(len(lines[0]), len(lines))
+        y = 0
+        for line in lines:
+            x = 0
+            for pixel in line:
+                pix.pixel(x, y, pixel)
+                x += 1
+            y += 1
+        return pix
+
+    def pixel(self, x, y, color=None):
+        if not 0 <= x < self.width or not 0 <= y < self.height:
+            return 0
+        if color is None:
+            return self.buffer[x + y * self.width]
+        self.buffer[x + y * self.width] = color
+
+    def box(self, color, x=0, y=0, width=None, height=None):
+        x = min(max(x, 0), self.width - 1)
+        y = min(max(y, 0), self.height - 1)
+        width = max(0, min(width or self.width, self.width - x))
+        height = max(0, min(height or self.height, self.height - y))
+        for y in range(y, y + height):
+            xx = y * self.width + x
+            for i in range(width):
+                self.buffer[xx] = color
+                xx += 1
+
+    def blit(self, source, dx=0, dy=0, x=0, y=0,
+             width=None, height=None, key=None):
+        if dx < 0:
+            x -= dx
+            dx = 0
+        if x < 0:
+            dx -= x
+            x = 0
+        if dy < 0:
+            y -= dy
+            dy = 0
+        if y < 0:
+            dy -= y
+            y = 0
+        width = min(min(width or source.width, source.width - x),
+                    self.width - dx)
+        height = min(min(height or source.height, source.height - y),
+                     self.height - dy)
+        source_buffer = memoryview(source.buffer)
+        self_buffer = self.buffer
+        if key is None:
+            for row in range(height):
+                xx = y * source.width + x
+                dxx = dy * self.width + dx
+                self_buffer[dxx:dxx + width] = source_buffer[xx:xx + width]
+                y += 1
+                dy += 1
+        else:
+            for row in range(height):
+                xx = y * source.width + x
+                dxx = dy * self.width + dx
+                for col in range(width):
+                    color = source_buffer[xx]
+                    if color != key:
+                        self_buffer[dxx] = color
+                    dxx += 1
+                    xx += 1
+                y += 1
+                dy += 1
+
+    def __str__(self):
+        return "\n".join(
+            "".join(
+                ('.', '+', '*', '@')[self.pixel(x, y)]
+                for x in range(self.width)
+            )
+            for y in range(self.height)
+        )
+
+
+def init():
+    global _tick
+
+    if _tick is not None:
+        return
+
+    _tick = time.ticks_ms()