Skip to content

Instantly share code, notes, and snippets.

@cwalther
Created April 20, 2024 11:58
Show Gist options
  • Save cwalther/fb2b2ca00c59b0577089fbdc4d7e0a88 to your computer and use it in GitHub Desktop.
Save cwalther/fb2b2ca00c59b0577089fbdc4d7e0a88 to your computer and use it in GitHub Desktop.
from framebuf import FrameBuffer, MONO_HMSB, MONO_HLSB
# currently only works for monochrome (fg, bg in {-1, 0, 1})
class KeyedPalette(FrameBuffer):
def __init__(self, fg=0, bg=-1):
buf = bytearray(1)
if fg == -1:
self.key = fg = bg^1
elif bg == -1:
self.key = bg = fg^1
else:
self.key = -1
buf[0] = (fg << 1) | bg
super().__init__(buf, 2, 1, MONO_HMSB)
def write(fb, s, x, y, font, palette=None):
for char in s:
glyph, char_height, char_width = font.get_ch(char)
if glyph:
fbc = FrameBuffer(bytearray(glyph), char_width, char_height, MONO_HMSB if font.reverse() else MONO_HLSB)
fb.blit(fbc, x, y, getattr(palette, 'key', -1), palette)
x += char_width
def measure(s, font):
w = 0
for char in s:
glyph, h, cw = font.get_ch(char)
w += cw
return w, h
@cwalther
Copy link
Author

There is a period . where there should be a comma , in this line:

simplewriter.write(oled,'Hello World!', 0.20, freesans20)

If the fonts that come with nano-gui don’t include the ° glyph, you’ll have to make your own that does using font-to-py.py. This utility is not meant for running on the microcontroller, but on your PC. See the instructions.

By the way, see https://github.com/orgs/micropython/discussions/9111 for how to post code in a more readable way.

@bruno1950
Copy link

bruno1950 commented Apr 21, 2024 via email

@cwalther
Copy link
Author

Glad to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment