def make_loadflash(fn, fl): cd = zlib.compress(fl, 9) print('%s: flash image is %d bytes, compresses to %d bytes' % (fn, len(fl), len(cd))) assert len(fl) % 256 == 0, len(fl) gd = Gameduino() gd.cmd_inflate(LP) gd.cc(eve.align4(cd)) ecrc = crc(fl) gd.cmd_memwrite(0xffff8, 8) gd.c4(len(fl)) gd.c4(ecrc) gd.cmd_memcrc(LP, len(fl), 0) print('Expected CRC %x' % ecrc) b = gd.buf padw = (-len(b) & 0xff) // 4 b = (padw * struct.pack("I", 0xffffff5b)) + b with open(fn, "wb") as h: h.write(b) with open(fn.replace(".bin", ".h"), "wt") as f: for i in range(0, len(b), 100): f.write("".join(["%d," % x for x in b[i:i + 100]]) + "\n")
def make_sector_1(): gd = Gameduino() gd.setup_1280x720() gd.pack() print("Sector 1 CRC %08X" % crc(gd.buf)) cd = eve.align4(zlib.compress(gd.buf, 9)) with open("_sector1.h", "wt") as f: f.write(",".join([str(s) for s in cd])) f.write("\n")
def add(self, d): gd = self.gd self.a = (self.a + 1) & ~1 gd.cmd_memcrc(self.a, len(d), 0) if crc(d) == gd.result(): print('skipping loading', len(d), 'bytes') else: self.gd.cmd_inflate(self.a) self.gd.cc(eve.align4(zlib.compress(d))) self.a += len(d)
def make_flash(): gd = Gameduino() blob_addr = 0x8000 gd.cmd_inflate(blob_addr) img = open("assets/unified.blob", "rb").read() + sector_1() assert len(img) == 8192 c = eve.align4(zlib.compress(img, 9)) gd.cc(c) gd.cmd_flashupdate(0, blob_addr, len(img)) gd.cmd_flashfast() return gd.buf
def uadd(self, d): self.a = (self.a + 1) & ~1 self.gd.cmd_inflate(self.a) self.gd.cc(eve.align4(zlib.compress(d))) self.a += len(d)
def setlabel(n, s): gd.cmd_memwrite(32 * n, len(s) + 1) gd.cc(eve.align4(s + bytes([0])))
def make_textmode(): font = ImageFont.truetype("../../.fonts/IBMPlexMono-SemiBold.otf", 13) ch = [chr(i) for i in range(32, 255)] im = Image.new("L", (256, 256)) draw = ImageDraw.Draw(im) for c in ch: draw.text((128, 128), c, font=font, fill=255) (x0, y0, _, _) = im.getbbox() print(128 - x0, 128 - y0) im = im.crop(im.getbbox()) # im.save("out.png") (w, h) = im.size fim = Image.new("L", (w, h * len(ch))) draw = ImageDraw.Draw(fim) for i, c in enumerate(ch): draw.text((128 - x0, (128 - y0) + (h * i)), c, font=font, fill=255) # fim.save("out.png") gd = Gameduino() def size(a, b): gd.BitmapSize(eve.NEAREST, eve.BORDER, eve.BORDER, a, b) gd.BitmapSizeH(a >> 9, b >> 9) (sw, sh) = (720, 1280) w2 = w + 1 h2 = h * 28 // 22 (W, H) = (sw // w2, (sh // h2) + 1) ht = H * h2 y_bar = (sh - (H - 1) * h2) // 2 x_bar = (sw - (W * w2)) // 2 gx = w * ht # glyph x term gy = w * h2 # glyph y term cx = 2 * H # color x term cz = 2 * H * W # color z term wh = w * h sz = w * W * h2 * H cm = 0 fm = cm + 2 * cz fb = fm + len(fim.tobytes()) gd.Clear() gd.swap() gd.cmd_inflate(fm) c = eve.align4(zlib.compress(fim.tobytes())) gd.cc(c) print('font size', (w, h), (w2, h2), 'screen size', (W, H - 1)) print('font bytes', len(fim.tobytes())) print('bars:', (x_bar, y_bar)) gd.BitmapHandle(0) gd.cmd_setbitmap(fb, eve.L8, w, ht) gd.cmd_memset(fb, 0x00, W * w * ht) gd.BitmapHandle(1) gd.cmd_setbitmap(cm, eve.RGB565, 1, H) size(w2, ht) if 0: b = bytes([rr(256) for i in range(2 * 2 * W * H)]) gd.cmd_memwrite(cm, len(b)) gd.cc(eve.align4(b)) else: gd.cmd_memzero(cm, 2 * 2 * W * H) with open("_textmode.fs", "wt") as f: for v in ("fm", "fb", "cm", "sz"): f.write("$%x. 2constant %s\n" % (eval(v), v)) for v in ("H", "W", "gx", "gy", "cx", "cz", "wh", "y_bar", "h2"): f.write("$%x constant %s\n" % (eval(v), v)) def gaddr(x, y): return fb + w * ((y * h2) + (x * ht)) def caddr(x, y, z): return cm + 2 * (y + H * (x + z * W)) def drawch(x, y, c, color=0xffff, bg=0x0000): dst = gaddr(x, y) src = fm + (ord(c) - 0x20) * (w * h) gd.cmd_memcpy(dst, src, (w * h)) # gd.cmd_memset(dst, 0xff, (w * h)) gd.cmd_memwrite(caddr(x, y, 0), 2) gd.cc(struct.pack("<I", color)) gd.cmd_memwrite(caddr(x, y, 1), 2) gd.cc(struct.pack("<I", bg)) gd.setup_1280x720() gd.cmd_setrotate(2) offset = 0 vh = y_bar + ((H - offset) * h2) def drawtwice(x): gd.Macro(0) if x: yo = 0 else: yo = (h2 - h) // 2 for i in range(sw // w2): gd.Cell(i) gd.Vertex2f(x_bar + x + w2 * i, -ht + yo) gd.Vertex2f(x_bar + x + w2 * i, 0 + yo) def color_panel(z): gd.SaveContext() gd.BitmapHandle(1) gd.BitmapSource(caddr(0, 0, z)) gd.BitmapTransformA(0, 1) gd.BitmapTransformE(32768 // h2 + 1, 1) drawtwice(-1) gd.RestoreContext() gd.cmd_memwrite(eve.REG_MACRO_0, 4) gd.VertexTranslateY(vh << 4) gd.VertexFormat(0) gd.ClearColorA(0) # gd.ClearColorRGB(200, 200, 200) gd.Clear() gd.ScissorXY(x_bar, y_bar) gd.ScissorSize((W * w2), (H - 1) * h2) gd.Begin(eve.BITMAPS) gd.ColorMask(1, 1, 1, 0) color_panel(1) gd.ColorMask(0, 0, 0, 1) gd.BlendFunc(1, 0) gd.BitmapHandle(0) drawtwice(0) gd.ColorMask(1, 1, 1, 0) gd.BlendFunc(eve.DST_ALPHA, eve.ONE_MINUS_DST_ALPHA) color_panel(0) gd.swap() if 0: for i in range(H): s = "[{0}]".format(i) for j, c in enumerate(s): drawch(i + j, i, c, 0xffff, 0x1010) return gd.buf
def configure(self, mode = 'P', fontsize = 14): gd = self.gd if mode == 'L': gd.cmd_setrotate(0) (sw, sh) = (1280, 720) elif mode == 'P': gd.cmd_setrotate(2) (sw, sh) = (720, 1280) font = ImageFont.truetype("../fonts/IBMPlexMono-Medium.otf", fontsize) ch = [chr(i) for i in range(32, 255)] im = Image.new("L", (256, 256)) draw = ImageDraw.Draw(im) for c in ch: draw.text((128, 128), c, font=font, fill=255) (x0, y0, _, _) = im.getbbox() im = im.crop(im.getbbox()) # im.save("out.png") (w, h) = im.size w2 = w + 1 h2 = h * 28 // 22 (W, H) = (sw // w2, (sh // h2) + 1) ht = H * h2 y_bar = (sh - (H - 1) * h2) // 2 x_bar = (sw - (W * w2)) // 2 print('Screen size', (W, H - 1)) print('Bars', (x_bar, y_bar)) fim = Image.new("L", (w2 * len(ch), h)) draw = ImageDraw.Draw(fim) for i,c in enumerate(ch): draw.text(((128 - x0) + (w2 * i) + 1, (128 - y0)), c, font=font, fill=255) fim = fim.transpose(Image.TRANSPOSE) # fim.save("out.png") def size(a, b): gd.BitmapSize(eve.NEAREST, eve.BORDER, eve.BORDER, a, b) gd.BitmapSizeH(a >> 9, b >> 9) gx = w * ht # glyph x term gy = w * h2 # glyph y term cx = 2 * H # color x term cz = 2 * H * W # color z term wh = w * h sz = w * W * h2 * H cm = len(colorblk) + (4 * H) fm = cm + 2 * cz fb = fm + len(fim.tobytes()) self.gd = gd self.gx = gx self.gy = gy self.cx = cx self.cz = cz self.wh = wh self.sz = sz self.cm = cm self.fm = fm self.fb = fb self.w2 = w2 self.h2 = h2 self.w = w self.h = h self.W = W self.H = H gd.Clear() gd.swap() gd.cmd_memwrite(0, len(colorblk)) gd.cc(eve.align4(colorblk)) self.scrollblk = len(colorblk) gd.cmd_memwrite(len(colorblk), 4 * self.H) for i in range(self.H): gd.VertexTranslateY(-self.h2 * i) gd.cmd_inflate(fm) c = eve.align4(zlib.compress(fim.tobytes())) gd.cc(c) gd.BitmapHandle(0) gd.cmd_setbitmap(fb, eve.L8, h, w2 * W) size(w2 * W, h) gd.cmd_memzero(fb, H * h * w2 * W) gd.BitmapHandle(1) gd.cmd_setbitmap(cm, eve.RGB565, W, 1) size(w2 * W, h2) # Cursor box self.curh = h2 // 4 gd.BitmapHandle(2) gd.cmd_setbitmap(2 * (255 + 2), eve.RGB565, 1, 1) gd.BitmapSize(eve.NEAREST, eve.REPEAT, eve.REPEAT, w2, self.curh) if 0: random.seed(0) b = bytes([rr(256) for i in range(2 * 2 * W * H)]) gd.cmd_memwrite(cm, len(b)) gd.cc(eve.align4(b)) else: gd.cmd_memzero(cm, 2 * 2 * W * H) def gaddr(x, y): return fb + x * (w2 * h) + y * (w2 * h * W) def caddr(x, y, z): return cm + 2 * (x + W * (y + (z * H))) def drawch(x, y, c, color = 0xffff, bg = 0x0000): dst = gaddr(x, y) src = fm + (ord(c) - 0x20) * (w2 * h) gd.cmd_memcpy(dst, src, w2 * h) gd.cmd_memwrite(caddr(x, y, 0), 2) gd.cc(struct.pack("<I", color)) gd.cmd_memwrite(caddr(x, y, 1), 2) gd.cc(struct.pack("<I", bg)) if 0: for i in range(20): drawch(0, i, str(i % 10)) if i & 1: drawch(W - 1, i, str(i % 10)) def colorpass(z): gd.SaveContext() gd.BitmapHandle(1) gd.BitmapSource(caddr(0, 0, z)) gd.BitmapTransformA(0x8000 // w2, 1) gd.BitmapTransformC(0x10) gd.BitmapTransformE(0) for i in range(2 * H): gd.Cell(i % H) gd.Vertex2f(0, i * h2) gd.RestoreContext() gd.cmd_memwrite(eve.REG_MACRO_0, 4) gd.VertexTranslateY(0) gd.Macro(0) gd.VertexFormat(0) gd.ClearColorA(0) gd.Clear() gd.ScissorSize(1280, (self.H - 1) * self.h2) gd.Begin(eve.BITMAPS) gd.ColorMask(1, 1, 1, 0) colorpass(1) gd.SaveContext() gd.ColorMask(0, 0, 0, 1) gd.BlendFunc(1, 0) gd.BitmapTransformA(0) gd.BitmapTransformB(0x8000, 1) gd.BitmapTransformD(0x8000, 1) gd.BitmapTransformE(0) gd.BitmapHandle(0) for i in range(2 * H): gd.Cell(i % H) gd.Vertex2f(0, i * h2) gd.RestoreContext() gd.ColorMask(1, 1, 1, 0) gd.BlendFunc(eve.DST_ALPHA, eve.ONE_MINUS_DST_ALPHA) colorpass(0) # Cursor drawing gd.VertexTranslateY(0) gd.BlendFunc(eve.SRC_ALPHA, eve.ONE_MINUS_SRC_ALPHA) gd.Begin(eve.BITMAPS) gd.BitmapHandle(2) (cursorx, cursory) = (0, 0) gd.Macro(1) gd.cmd_memwrite(eve.REG_MACRO_1, 4) gd.Vertex2f(cursorx * w2, (cursory + 1) * h2 - self.curh) gd.swap() self.colors = (7, 0) self.yo = 0 self.append_config()