As with any Python script in Thonny, Click on File > Save and save the file to your Raspberry Pi Pico as oled-test.py. TH = bytearray(b’.’)įb = framebuf.FrameBuffer(TH,64,64, framebuf.MONO_HLSB)Ħ. Copy the text from b’ … ‘ and paste it inside the parenthesis of our TH object. python3 img2bytearray.py YOUR-IMAGE.jpg 64 64ĥ. First is the name of the image file, the next two are the dimensions of the image, in this case 64 by 64 pixels. We need to provide three extra parameters. To convert the image we call the command img2bytearray. Open a Command Prompt / Terminal window and navigate to the img2bytearray folder.Ĥ. Copy your image to the img2bytearray folder.ģ. This creates a folder, img2bytearray which stores the Hui’s Python file.Ģ. Download and extract the ZIP archive to your machine. To convert the image to a bytearray we shall use Don Hui’s (Novaspirit) img2bytearray python script.ġ. The screen is 128 x 64 pixels in size, but an image 64 x 64 pixels will fit nicely into the center of the screen. To create a bytearray of an image we first need a suitable image. Our object, TH, currently has no image to display. Where 32 is the horizontal (x) position and 0 is the vertical (y) position. Blitting draws the image to the screen, in this case it places the 64 x 64 image dead center of the screen. Clear the screen, and then blit the image onto the screen. fb = framebuf.FrameBuffer(TH,64,64, framebuf.MONO_HLSB)Ĥ. We pass the name of the bytearray object, the dimensions of the image (64 x 64 pixels) and then configure the image to be a 1-bit monochrome image. Create an object, FB, which will load the image into the framebuffer. For now leave the contents of the array blank, we will fill in the blank later. Create a new object, TH which will store an array of bytes that make up our image. This library enables the code to create bitmap images and show them on the display. So if you haven’t already done so, copy and paste the final code above into Thonny.ġ. We are going to use the code above as a base to work from. Using a clever technique we can convert a JPEG image into a string of bytes. We can show much more than just text on the screen. Showing Graphics on the OLED Screen with Pico oled.fill_rect(x,y,w,h,c): Draw a filled rectangle starting at point x.y and for a set width (w) and height(h) use ( c ) to set the color of the pixels.Use ( c ) to set the color of the pixels. oled.rect(x,y,w,h,c): Draw a rectangle starting at point x.y and for a set width (w) and height(h).oled.line(x1,y1,x2,y2,1): Draw a diagonal line from points x1, y1 to x2, y2 with the color ( c ).oled.vline(x,y,h,c): Draw a vertical line from point x,y that has a set height (h) in pixels, and color ( c ).oled.hline(x,y,w,c): Draw a horizontal line from point x,y that has a set width (w) in pixels, and color ( c ).oled.pixel(x,y,c): Draw a pixel at position x,y and uses c to set the color of the pixel, with 1 being lit, 0 being off.Note that most of these methods have a color parameter but, with a monochrome screen, you’ll always put a color of “1” (0 means pixel off). Each of these commands will need oled.show() in order to be seen. Simple shapes and lines can be drawn to the display with just a single command. Drawing Simple Shapes on OLED Screens with Pico When ready click on the Green play button to start the code and your text will appear on the OLED screen. The final code should look like this from machine import Pin, I2C Finally use the show command to render the output to the screen. Write a line of text to the top left of the screen, position 0,0. It has three arguments, the width and height of the screen (128 x 64 pixels) and the I2C connection details. Create an object, oled, which will be used to communicate with the OLED screen. Create an object, i2c, which stores the I2C channel in use, in this case zero, the SDA and SCL pins that we are connected to, and finally the frequency at which we connect to the OLED screen. These are used to communicate with the OLED screen, attached to the GPIO of the Pico. From the machine library, import the Pin and I2C classes. To write a single line of text to the OLED screen we need just six lines of MicroPython.ġ. Programming an OLED Screen on Raspberry Pi Pico Click Close to return to the main interface.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |