Ceci est une ancienne révision du document !
This month, we will continue playing with the SSD1302 OLED display. We’ve seen how to simply display text using the default font and how to use other fonts through the micropython-font-to-py library package. Let’s jump right in to some new things that we can do. Simple Graphics NOTE: The code presented here was developed using the ESP32/ESP8266. For the most part, the same code runs on the RPi Pico with a few changes in the import and I2C sections. In order to do simple graphics like lines, circles and so on, you need to download a graphics driver for the SSD1302. You can get it at https://github.com/adafruit/micropython-adafruit-gfx/blob/master/gfx.py. Be sure to copy it to your microcontroller device. I was able to gain a tremendous amount of information on using the SSD1302 from the web. One of the most useful websites was the Random Nerds Tutorials site. Their page for this section is https://randomnerdtutorials.com/micropython-ssd1306-oled-scroll-shapes-esp32-esp8266/ . This page also discusses how to do display scrolling, which we’ll discuss shortly.
We’ll start (right) with the normal import and I2C setup code… This is for the ESP32/ESP8266. Now the necessary preliminaries are out of the way, we can concentrate on how to draw our graphics. Simple Line We’ll start with drawing a line from position 0,0 to position 128, bottom line of the display. If you are using a 128×64 OLED, that (of course) would be 64, but if you are using a 128×32, then you would need to use 32. The function call parameters for the line function is as follows: line(x0, y0, x1, y1, color) So we would code it as follows… graphics.line(0, 0, 127, oled_height, 1) oled.show()
By using oled_height as our y1 parameter, we can ensure that this will work for both 128×64 and 128×32 displays. Now, sleep for 2 seconds, then clear the display and we will move on to the next demo. sleep(2) oled.fill(0) Rectangle Next, we will draw a rectangle. The parameters for the rectangle function are: rect(x0, y0, width, height, color) We will draw a rectangle from 0,0 to 50,20. This way, it will work on either of the display devices. Once we show the rectangle, then, like above, we’ll sleep for 2 seconds and clear the display. So our code would be… graphics.rect(0, 0, 50, 20, 1) oled.show() sleep(2) oled.fill(0)
Filled Rectangle To create a filled rectangle we’ll use a very similar function fill_rect with the same parameters as before. graphics.fill_rect(0, 0, 50, 20, 1) oled.show() sleep(2) oled.fill(0)
Circles Circles are just as easy as rectangles. The functions are circle and fill_circle. They both use the same parameter list. circle(x0, y0, radius, color) fill_circle(x0, y0, radius, color) Again, we’ll use parameters that will work on either of the two OLED displays. # Circle (x0,y0,radius,colour) graphics.circle(64, 10, 10, 1) oled.show() sleep(2) oled.fill(0) # Filled Circle graphics.fill_circle(64, 10, 10, 1) oled.show() sleep(2) oled.fill(0)
Triangles The triangle functions are similar to the rect and circle functions, but have a third set of coordinates. triangle(x0, y0, x1, y1, x2, y2, color) fill_triangle(x0, y0, x1, y1, x2, y2, color) As with the last functions, we’ll use coordinates that will fit either size display. graphics.triangle(0,0,55,20,5,32,1) oled.show() sleep(2) oled.fill(0) oled.show() # Filled Triangle graphics.fill_triangle(0,0,55,20,5,32,1) oled.show() sleep(2) oled.fill(0) oled.show()
Easy enough, right? The file on the repository for this section is called rnerd_ssd1306_esp32_graphics1.py for the ESP32/ESP8266 and pico_oled_graphics2.py for the Pico. Display Scrolling Sometimes, you want to make something nicer than a static display where the text simply overwrites itself. Having the text scroll in from the side or top and pause a few seconds, then scroll off the screen, gives your project a certain “WOW” factor. These days, we need a “WOW” factor to make our projects stand out. I originally tested this code on the ESP32, but I was able to get it to run on the RPi Pico. The only thing you have to make sure of is the I2C setup. Between the information from last month and the information in the code above, you should have no problems getting things to work. Just in case you have forgotten, I got this demo from Random Nerds Tutorials web site. You can find the link in the text above.
Getting Started As I do most of the time, we’ll start (top right) with the import section and, in this case, the I2C setup. As I said, if you were able to get the code above to work you can get this to work. Now that we have the text of the “screens” set up, we need to create a list of lists (middle right) that we will pass into the functions that we will be creating. Each entry in the list is set up as X (column), Y (row) and message. In the above code, we created three messages as “screen1”, two messages as “screen2” and 1 message as “screen3”. So, we just need to set up where we want each of the messages to show up. Each list shows up as a complete “screen”. At this point (bottom right), we can create our first support function. This one is called scroll_in_screen and will scroll the screen horizontally from left to right. Once everything has been passed into the display, the function is finished and you can call sleep for a period so the user can read the message. Now that we have all of our support functions coded (final ones shown top right), we can start using them. Each demo portion is fairly long, so we’ll just loop three times. The original code (next page, on the left) had this as a forever loop.
*Once the demo is finished, clear the display.
# Clear the OLED
oled.fill(0)
oled.show()
Now that you have seen the functions that work from left to right and top to bottom, you might want to try to create functions that scroll from right to left and bottom to top on your own.
I’ve taken up all the space that I should, so that’s all for this month. You can find the code for both projects (in both Pico and ESP formats) at my github repository at https://github.com/gregwa1953/FCM173_MicroThisMicroThat
Until next time, as always; stay safe, healthy, positive and creative!**