From c37b20556b0d4875ae1fb2878373426ac0a0d6ed Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 6 Jun 2022 14:55:43 +0200 Subject: use 32bit for framebuffer. Reduces memory footprint to about /4 --- framebuffer.lua | 29 +++++++++++++++++------------ ssd1306.lua | 10 +++++++--- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/framebuffer.lua b/framebuffer.lua index 754a2bb..944c53c 100644 --- a/framebuffer.lua +++ b/framebuffer.lua @@ -16,10 +16,12 @@ end function fb.scroll() for x = 0, fb.w-1 do - for y = 1, fb.h/8-1 do - fb.buf[x*(fb.h/8) + y-1 + 1] = fb.buf[x*(fb.h/8) + y + 1] or nil + for y = 1, fb.h/32 do + fb.buf[x*(fb.h/32) + y-1 + 1] = bit.rshift(fb.buf[x*(fb.h/32) + y-1 + 1] or 0, 8) or nil + if y ~= fb.h/32 then + fb.buf[x*(fb.h/32) + y-1 + 1] = bit.bor(fb.buf[x*(fb.h/32) + y-1 + 1] or 0, bit.lshift(fb.buf[x*(fb.h/32) + y + 1] or 0, 24)) or nil + end end - fb.buf[x*(fb.h/8) + fb.h/8] = nil end fb.y = fb.y - 8 end @@ -41,7 +43,10 @@ function fb.put(font, c) for i = 1, string.len(glyph) do local x1 = (i-1) / fh local y8 = (i-1) % fh - fb.buf[fb.y/8+y8 + (fb.x+x1) * (fb.h/8) + 1] = string.byte(glyph, i) + local fb8_o = fb.y/8+y8 + (fb.x+x1) * (fb.h/8) + local fb32_o = fb8_o / 4 + 1 + local fb32_s = (fb8_o % 4) * 8 + fb.buf[fb32_o] = bit.bor(fb.buf[fb32_o] or 0, bit.lshift(string.byte(glyph, i), fb32_s)) end fb.x = fb.x + string.len(glyph) / fh + 2 if fb.x > fb.w then @@ -56,25 +61,25 @@ function fb.print(font, str) end function fb.draw_battery_8(x, y, p) - fb.buf[y/8 + x*fb.h/8 + 1] = 0xff + fb.buf[y/32 + x*fb.h/32 + 1] = 0xff for i = 1, 10 do if p*2 >= i*15 then - fb.buf[y/8 + (x+i)*fb.h/8 + 1] = 0xff + fb.buf[y/32 + (x+i)*fb.h/32 + 1] = 0xff else - fb.buf[y/8 + (x+i)*fb.h/8 + 1] = 0x81 + fb.buf[y/32 + (x+i)*fb.h/32 + 1] = 0x81 end end if p*2 >= 11*15 then - fb.buf[y/8 + (x+11)*fb.h/8 + 1] = 0xff + fb.buf[y/32 + (x+11)*fb.h/32 + 1] = 0xff else - fb.buf[y/8 + (x+11)*fb.h/8 + 1] = 0xe7 + fb.buf[y/32 + (x+11)*fb.h/32 + 1] = 0xe7 end if p*2 >= 12*15 then - fb.buf[y/8 + (x+12)*fb.h/8 + 1] = 0x3c + fb.buf[y/32 + (x+12)*fb.h/32 + 1] = 0x3c else - fb.buf[y/8 + (x+12)*fb.h/8 + 1] = 0x24 + fb.buf[y/32 + (x+12)*fb.h/32 + 1] = 0x24 end - fb.buf[y/8 + (x+13)*fb.h/8 + 1] = 0x3c + fb.buf[y/32 + (x+13)*fb.h/32 + 1] = 0x3c end return fb diff --git a/ssd1306.lua b/ssd1306.lua index 2ab3491..d54d28f 100644 --- a/ssd1306.lua +++ b/ssd1306.lua @@ -55,9 +55,13 @@ end function ssd1306.show(fb) local txbuf = {0x40} - for i = 1, ssd1306.w * ssd1306.h / 8, 128 do - for j = 0, 127 do - txbuf[j+2] = fb[i+j] or 0 + for i = 1, ssd1306.w * ssd1306.h / 32, 32 do + for j = 0, 31 do + local dw = fb[i+j] or 0 + for k = 2, 5 do + txbuf[j*4+k] = bit.band(dw, 0xff) + dw = bit.rshift(dw, 8) + end end ssd1306.wd(txbuf) end -- cgit v1.2.3