Created
April 20, 2024 11:58
-
-
Save cwalther/fb2b2ca00c59b0577089fbdc4d7e0a88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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.
You're perfectly right: there was a typo and now it works quite fine. Thanks for your patience and apologies for my bizarre code formatting. Have a nice evening
… Il 21/04/2024 18:43 CEST Christian Walther ***@***.***> ha scritto:
@cwalther commented on this gist.
---------------------------------------------
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 https://github.com/peterhinch/micropython-font-to-py#readme.
By the way, see micropython/micropython#9111 for how to post code in a more readable way.
—
Reply to this email directly, view it on GitHub https://gist.github.com/cwalther/fb2b2ca00c59b0577089fbdc4d7e0a88#gistcomment-5031336 or unsubscribe https://github.com/notifications/unsubscribe-auth/A5BIYK22DA27QHFDKZXMSK3Y6PUCDBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTEOJXGY2TAOJTU52HE2LHM5SXFJTDOJSWC5DF.
You are receiving this email because you commented on the thread.
Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
Glad to help!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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