-
-
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 |
There are no integers on line 8 of this file, so it’s probably talking about some other file. What are you doing exactly to “run the code”?
I've checked more accurately the code and maybe the error I got is just due to the presence of a typo in the code I pasted where there was in line 8 an extra "c" after bg^1. Correcting it the error message disappeared. Thanks. However the level of the code is far far higher than my very modest capabilities.
OK, I didn’t look properly, obviously 1
is an integer. :) Are you pasting this on the REPL? I would suggest putting the file on the board and importing it instead.
Here is how you use it (substituting a nano-gui font for “myfont”):
import myfont
import simplewriter
simplewriter.write(display, 'Hello World!', x, y, myfont)
If you want to write in different colors, pass simplewriter.KeyedPalette(foreground)
(for a transparent background) or simplewriter.KeyedPalette(foreground, background)
as an additional argument.
Hi, thanks for the time you're spending to solve my problem. I tried this very simple code
from machine import Pin, SoftI2C
import ssd1306
import simplewriter
import freesans20 # Font
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
Initialize the Writer
simplewriter.write(oled,'Hello World!', 0.20, freesans20)
oled.show()
But I get an error message File "", line 12, in
TypeError: function takes 5 positional arguments but 4 were given I don't understand, simce I thought that in the line there were 5 arguments. What did I miss ? Thanks .
One more question. My initial problem - still unsolved - is to find a library with fonts that allow correct display for ° symbol with 1306 SSD, SH1106 and SH1107 OLEDs (these are the usual displays I use). By Using writer.py I was able to display correctly characters with freesans20, font6.py, font10.py, etc but they are still limited to some ASCII characters and ° symbol is not included. I uploaded font_to_py.py but I get error message since I do not know how to upload on ESP32 freetype ( I've installed it on my W11 OS by using pip command). Is there any workaround to display the ° symbol in micropython ? I tried also direct printing of Unicode character but it was unsuccessful
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.
Glad to help!
Hi, why if I run the code I get the error message File "", line 8
SyntaxError: invalid syntax for integer with base 10 ? Sorry for the maybe silly question but I am a newbie. Thanks