MicroPython example of TPYBoard development board control OLED display Chinese

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
Jackli
Posts: 80
Joined: Thu Apr 29, 2021 9:11 am

MicroPython example of TPYBoard development board control OLED display Chinese

Post by Jackli » Fri Jul 09, 2021 5:57 am

0x00 Preface

I saw an article about TPYBoard v102 controlling OLED screen display, and after seeing it, I wanted to try to use OLED screen to display Chinese. Recently used the free time to fix this experiment, hereby share the experimental process and source code for future use.

0x01 Experimental equipment

TPYBoard v102 development board 1 piece (a treasure can be bought, the price is not expensive)
0.96 inch OLED display (ssd1306) 1 piece / or LCD
Some DuPont wires

0x02 Preliminary preparation

1、Add Chinese font in font.py.

There are already English, numeric and symbolic characters in font.py, and we need to do Chinese character model to add to font.py.

2.1 First download the font extraction tool.

Address: http://tpyboard.com/download/tool/187.html
Unzip it, double click to run PCtoLCD2002.exe.

Here we recommend a better font generator.
https://www.stoneitech.com/media/upload ... %20TS3.rar

2.2 Go back to the main screen, enter "I" in the input box and click [Generate Character Template].

Image

The word model data obtained is as follows.

Image

2.3 Add the fetched word model data to font.py.

Image

The green box is the hexadecimal utf-8 encoding of "me".
Following the above method, I added the five Chinese characters "I love your country" in order.

Code: Select all

byte2 = {

0xe68891:
    [
    0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10,
    0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02,
    ],#我
0xe788b1:
    [
    0x00,0x01,0x7E,0x22,0x11,0x7F,0x42,0x82,0x7F,0x04,0x07,0x0A,0x11,0x20,0x43,0x1C,
    0x08,0xFC,0x10,0x10,0x20,0xFE,0x02,0x04,0xF8,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E,
    ],#爱
0xe4bda0:
    [
    0x08,0x08,0x08,0x11,0x11,0x32,0x34,0x50,0x91,0x11,0x12,0x12,0x14,0x10,0x10,0x10,
    0x80,0x80,0x80,0xFE,0x02,0x04,0x20,0x20,0x28,0x24,0x24,0x22,0x22,0x20,0xA0,0x40,
    ],#你
0xe7a596:
    [
    0x20,0x11,0x11,0xF9,0x09,0x11,0x11,0x39,0x55,0x95,0x11,0x11,0x11,0x11,0x17,0x10,
    0x00,0xF8,0x08,0x08,0x08,0xF8,0x08,0x08,0x08,0xF8,0x08,0x08,0x08,0x08,0xFE,0x00
    ],#祖
0xe59bbd:
    [
    0x00,0x7F,0x40,0x40,0x5F,0x41,0x41,0x4F,0x41,0x41,0x41,0x5F,0x40,0x40,0x7F,0x40,
    0x00,0xFC,0x04,0x04,0xF4,0x04,0x04,0xE4,0x04,0x44,0x24,0xF4,0x04,0x04,0xFC,0x04
    ],#国
}
1. Add the method of draw_chinese to display Chinese in ssd1306.py file.

Code: Select all

 def draw_chinese(self,ch_str,x_axis,y_axis):
    offset_=0
    y_axis=y_axis*8#中文高度一行占8个
    x_axis=127-(x_axis*16)#中文宽度占16个
    for k in ch_str:
        code = 0x00#将中文转成16进制编码
        data_code = k.encode("utf-8")
        code |= data_code[0]<<16
        code |= data_code[1]<<8
        code |= data_code[2]
        byte_data=font.byte2[code]
        for y in range(0,16):
            a_=bin(byte_data[y]).replace('0b','')
            while len(a_)<8:
                a_='0'+a_
               
            b_=bin(byte_data[y+16]).replace('0b','')
            while len(b_)<8:
                b_='0'+b_
            for x in range(0,8):
                self.set_pixel(x_axis-x-offset_,y+y_axis,int(a_[x]))#文字的上半部分
                self.set_pixel(x_axis-x-8-offset_,y+y_axis,int(b_[x]))#文字的下半部分
        offset_+=16
0x03 Hardware Connection

This experiment uses the SPI communication method of OLED, TPYBoard v102 with 2 SPI interfaces, I used SPI1.

0x04 Effect demonstration

After the hardware wiring is OK, copy all the source code to the disk loaded by TPYBaord v102, press the RST button to reset or use Putty software Ctrl+D to soft reset, and re-run the effect as follows.

Image

Post Reply