Hello, I have an SSD1306 OLED module and BME280 atmospheric sensor connected and working with a RPi Pico. Currently to display the temperature I am using the text “deg C”. How do you display the degree symbol? Is there a library that needs to be loaded into MicroPython to do this or is it a specific character string that is needed? Thanks
Hey @Paul3251, welcome to the forums!
That’s a tricky one. It looks like the font pack used for the library only includes 128 basic characters and the degree symbol is not included in that.
You could try drawing a circle and positioning it where you want it on the display to give the same effect?
This is some quick example code I have written up to show how you could implement this:
# PiicoDev SSD1306 circle drawing example
import math
from PiicoDev_SSD1306 import *
from PiicoDev_Unified import sleep_ms
display = create_PiicoDev_SSD1306()
def draw_circle(x0, y0, r):
f = 1 - r
ddf_x = 1
ddf_y = -2 * r
x = 0
y = r
# Draw four initial points (top, bottom, left, right)
display.pixel(x0, y0 + r, 1)
display.pixel(x0, y0 - r, 1)
display.pixel(x0 + r, y0, 1)
display.pixel(x0 - r, y0, 1)
# While the x-coordinate is less than the y-coordinate
while x < y:
# If f is greater than or equal to 0, decrease y and update f and ddf_y
if f >= 0:
y -= 1
ddf_y += 2
f += ddf_y
# Increment x and update f and ddf_x
x += 1
ddf_x += 2
f += ddf_x
# Draw 8 pixels in each section of the circle
display.pixel(x0 + x, y0 + y, 1)
display.pixel(x0 - x, y0 + y, 1)
display.pixel(x0 + x, y0 - y, 1)
display.pixel(x0 - x, y0 - y, 1)
display.pixel(x0 + y, y0 + x, 1)
display.pixel(x0 - y, y0 + x, 1)
display.pixel(x0 + y, y0 - x, 1)
display.pixel(x0 - y, y0 - x, 1)
while True:
for counter in range(10,100):
display.fill(0)
display.text(str(counter),20,20, 1)
draw_circle(40, 20, 2)
display.text("Celsius",50,20, 1)
display.show()
sleep_ms(500)
Hope this helps!
Thanks @Samuel. I will give that a go
@Samuel thanks for your help. Confirmed it is working very well.
Not to hijack the thread, but when using the library and font pack do you see the source file with the fonts defined ?
I have only every developed my own libraries and then convert fonts into one of the common formats (don’t use Arduino and those libraries)
If you see the font definitions you could always change one set for a character you wont be using. e.g. ~ then in your code you would just need to use the re-mapped character.
Of course you need to work out the font definitions so you get it right, but once you learn that, you can now make all sorts of custom characters.
One of my projects i did that for the “signal strength” and Wifi symbols.
But if its pre-compiled, then most likely way too much work (?)
I also remember doing this sort of thing on some of the old LCD character displays. You could upload the bitmap to redefine the characters.
Looking at the library for the Piicodev OLED it appears that it would be a precompiled file that is used for store the font. In saying that you definitely could make your own file and change things around, for one off symbols it would look at either going with the solution that Sam did, being drawing the symbol itself, making a custom bitmap could also be a viable option.
Yeah, its just a different way; if easy enough with what you use.
To me the key difference between the two would be
Modify the font:
- Con : Hard to do as you need to learn how the font storage/structure works
- Pro : Once done, it will use less memory/code space
- Pro : It will be faster as its pre-calculated
Drawing:
- Pro: Do more complex things that may be bigger the the fixed font size.
- Con: well, no the pro’s from above.
So if your speed is ok and you have plenty of space, go with what you can do easily.
A quick note; I just ran across this thread while lurking and reading another threads.
I am the author of a font pack + writer system for MicroPython; compatible with any framebuffer display.
Fonts for it range from 5pt to 64pt… all have ‘redistribution allowed’ licences.
They are quite memory efficient and fast, on a pico you are unlikely to have any issues there. I did /all/ my development of this on a pico based system.
Specific to the above discussion, each font comes in several different character set packs; eg ‘full ascii’, ‘uppercase only’, ‘time’; and there is a ‘numeric’ pack type that includes the degrees symbol when the source font supports it (and some other useful numeric symbols, plus all numerics).
Hey @Owen288028, welcome to the forums!
Thanks for sharing! this looks awesome and is a much cleaner solution than my solution of calling a function to draw a circle every time.
Hopefully, @Paul3251 can get some good use out of this!