Display ====================================== The mPython Board has a built-in 1.3-inch OLED display with a resolution of 128x64. It uses the `Google Noto Sans CJK `_ open-source sans-serif font. The font height is 16 pixels. It supports Simplified Chinese, Traditional Chinese, Japanese, and Korean languages. .. Hint:: oled is a derived class of ``machine.framebuf``, so it inherits framebuf methods. For detailed usage, refer to :meth:`framebuf`. Text Display ------------ Before use, you must import the mpython module:: from mpython import * `hello,world` text display:: # To see how characters are displayed on the screen, select the pixel inversion mode oled.DispChar('hello,world!',0,0,mode=TextMode.rev) # In default mode, character background pixels are off oled.DispChar('hello,world!',0,16,mode=TextMode.normal) oled.show() .. figure:: /_static/products/ledong-v2/_static/image/tutorials/display_char_pixel.png :width: 500px :align: center How character pixels are displayed on the mPython Board screen - The DispChar(str,x,y) function writes text with the top-left corner as the coordinate into the FrameBuffer. ``str`` is the display text content, supporting Simplified Chinese, Traditional Chinese, Japanese, and Korean languages. ``x`` ``y`` are the starting xy coordinates for oled display. `oled.show()` sends the FrameBuffer to the oled for refresh and display. - Uses Noto Sans CJK 16-pixel equal-height, variable-width font. Different characters have different widths, as shown in the figure above. .. literalinclude:: /_static/products/ledong-v2/_static/examples/display/helloworld.py :caption: Displaying hello world text in Chinese or other languages on the OLED screen: :linenos: .. figure:: /_static/products/ledong-v2/_static/image/mpython_front.png :align: center Multi-language hello,world display :: oled.fill(0) oled.show() In addition to clearing the display, you can also light up all pixels on the screen:: oled.fill(1) oled.show() .. Note:: fill() fills the entire FrameBuffer area. The OLED display also supports setting screen brightness:: oled.contrast(brightness) .. Note:: brightness represents brightness, with a value range of 0~255. Basic Shape Drawing ------------------- .. literalinclude:: /_static/products/ledong-v2/_static/examples/display/drawline.py :caption: Example: Drawing lines. :linenos: .. image:: /_static/products/ledong-v2/_static/image/tutorials/drawline.gif :scale: 100 % :align: center The OLED can draw shapes such as points, lines, and rectangles. Pixel display:: oled.pixel(50,0,1) #Set pixel (50,0) to 1, turning it on oled.show() #Refresh the display .. Note:: oled.pixel(x, y, [c]) can display a pixel. ``x``, ``y`` are the point coordinates (x,y). ``c`` is the color value; when 1, the pixel is lit; when 0, it is off. If c is not given, the color value of the specified pixel is returned. If c is given, the specified pixel is set to the given color. Drawing lines:: oled.hline(0,1,20,1) #Draw a horizontal line, starting at (0,1), length 20 oled.show() oled.vline(10,10,20,1) #Draw a vertical line, starting at (10,10), length 20 oled.show() oled.line(15,15,80,20,1) #Draw a line from (15,15) to (80,20) oled.show() .. Note:: * oled.hline(x, y, w, c) draws a horizontal line. ``x``, ``y`` are the point coordinates (x,y), ``w`` is the line width, ``c`` is the color value. * oled.vline(x, y, l, c) draws a vertical line, same as above. * oled.line(x1, y1, x2, y2, c) draws a line in any direction, from start coordinates (x1, y1) to end coordinates (x2, y2), ``c`` is the color value. Drawing hollow/filled rectangles:: oled.rect(60,25,30,25,1) #Draw a rectangle starting at (60, 25), width 30, height 25 oled.show() oled.fill_rect(100,25,20,25,1) #Draw a filled rectangle starting at (100, 25), width 20, height 25 oled.show() .. Note:: * oled.rect(x, y, w, h, c) draws a rectangle outline. Starting coordinates (x, y), width ``w``, height ``h``, ``c`` is the color value. * oled.fill_rect(x, y, w, h, c) draws a filled rectangle, same method as rect(). Unlike rect() which only draws the outline. Drawing rounded rectangles:: oled.RoundRect(40, 20, 50, 30, 5, 1) #Draw a rounded rectangle starting at (40, 25), width 50, height 30, corner radius 5 oled.show() .. Note:: oled.RoundRect(x, y, w, h, r, c) draws a rounded rectangle. Starting coordinates (x, y), width ``w``, height ``h``, corner radius ``r``, ``c`` is the color value. For more OLED display operations and shape drawing, refer to :ref:`oled object`. Displaying Images ----------------- First, we need to process the image to a size of 128*64, with a color depth of 1 (black and white mode) in bmp format. You can use Photoshop or other image processing software. Next, use an image extraction tool to extract the image data. There are tools such as PCtoLCD and LCD image converter available online. The following uses the :download:`Img2Lcd tool `. * Step 1. Import a 128x64 bmp format image * Step 2. Select parameters: output data type [C array], scan mode [horizontal scan], output grayscale [monochrome], width and height [128*64] * Step 3. Click save to automatically generate the extraction data. .. image:: /_static/products/ledong-v2/_static/image/tutorials/image2lcd.png Assign the extraction data to the bmp array, then display it on the OLED screen. .. literalinclude:: /_static/products/ledong-v2/_static/examples/display/chinamap.py :linenos: .. image:: /_static/products/ledong-v2/_static/image/tutorials/earth.png :scale: 50 % :align: center After assigning the extraction data to the bmp array, draw the image on the OLED display:: oled.bitmap(0, 0, bmp, 128, 64, 1) oled.show() .. Note:: oled.bitmap(x, y, bitmap, w, h, c) can draw a bitmap image. ``x``, ``y`` are the top-left starting coordinates. ``bitmap`` is the bytearray name of the image data. ``w`` is the image width, ``h`` is the image height, ``c`` is the color value; when ``1``, pixels are lit; when ``0``, pixels are off. Dynamic Display --------------- Combining the static frame display above, you can split the dynamic image into individual frames and send them to the OLED display frame by frame for an animation effect! Unlike the bmp format images used above, this time we use pbm (Portable BitMap) format images. You can use Photoshop to convert to pbm format. pbm data format:: P4 #CREATOR: GIMP PNM filter version 1.1 128 64 The first three lines of the pbm data format are defined as image annotations, followed by the image data. The first line indicates the image format, the second line is a comment (usually the program that created it), and the third line is the image size. The remaining data is what we need. Data is stored as a bit stream per pixel, where ``1`` means the pixel is on and ``0`` means the pixel is off. :download:`Dynamic display assets download ` First, upload the pre-prepared pbm images for each frame to the root directory of the mPython Board's file system. .. literalinclude:: /_static/products/ledong-v2/_static/examples/display/scatman.py :caption: Reading image data streams frame by frame and displaying them on the OLED screen: :linenos: .. image:: /_static/products/ledong-v2/_static/image/tutorials/scatman.gif :align: center Import the mpython and framebuf modules:: from mpython import * import framebuf Open each frame image in binary read-only format:: with open('scatman.%s.pbm' % n, 'rb') as f: f.readline() # Image format f.readline() # Comment f.readline() # Image size data = bytearray(f.read()) fbuf = framebuf.FrameBuffer(data, 128, 64, framebuf.MONO_HLSB) images.append(fbuf) #Assign each frame's data to the list In the program, ``file.read()`` is used to read image data streams frame by frame. Note that the first three lines are not the data we need, so ``readlines()`` is used to discard them. A FrameBuffer object is created for each frame's data stream, and all frames are cached in the images list. .. Note:: open(file, mode) is used to open a file and return a file object. ``file`` is the filename, ``mode`` is the file open mode. ``rb`` opens a file in binary format for read-only access, generally used for non-text files such as images. .. Note:: framebuf.FrameBuffer(buffer, width, height, format) can construct a frame buffer object. ``buffer`` is the buffer data, ``width`` is the image width, ``height`` is the image height, ``format`` is the FrameBuffer format, corresponding to the scan mode of the image extraction data output: ``framebuf.MONO_HLSB`` for horizontal direction; ``framebuf.MONO_VLSB`` for vertical direction. Display the stored frame buffers frame by frame on the OLED screen:: oled.blit(i, 0, 0) oled.show() .. Note:: oled.blit(fbuf, x, y) displays an image frame on the OLED. ``fbuf`` is the FrameBuffer object, ``x``, ``y`` are the starting point coordinates.