It took him a month to install himself into the OLED using micro python

Keywords: micropython OLED

Introduction: The complete open source code and migration instructions are being sorted out. Establish a common ecosystem to enable more people to join the application module, enrich their wings and strengthen their muscles and bones. Let more non professionals experience the happiness of programming.

Keywords: MM32, micropathon, OLED

 

§ 01 boast

▲ Figure 1. In that year, the sixth smart car competition, eating chicken legs

  this man (MindMotion, Su Yong, was in a hot pot shop in Shanghai on October 17 While eating, I helped you remember when you participated in the smart car competition I boasted that this year I can let the students develop car model works using microphoton on MindMotion MCU.

  more than a month has passed. What functions has he realized for MM32F3277 single chip microcomputer on the original empty micro Python kernel?

   the following are the main functions seen by using dir(machine). Although there is still a distance from plump wings, it is still possible to install yourself into OLED for display.

MicroPython v1.16 on 2021-11-25; MB_F3270 with MM32F3277G7P
>>> import machine
>>> dir(machine)
['__name__', 'ADC', 'DAC', 'PWM', 'Pin', 'SDCard', 'SPI', 'SoftI2C', 'SoftSPI', 'UART', 'freq', 'mem16', 'mem32', 'mem8']
>>> 

 

§ 02 refrigerated elephant
How many steps are there altogether?

1, Mm32f3277 micro Python

   with I2C interface, OLED access to MCU is very simple. It only needs two signal lines MindMotion MM32F3277 SoftI2C function test The OLED is connected to the SoftI2C control port of MM32F3277 through I2C, and the character information is displayed through microphoton.

▲ figure 1.1.1 OLED is connected to MM32F3277 through I2C bus

1. OLED display picture

  OLED display pixels are 128 × 64. A total of 1024 bytes of data represent the displayed picture. Each pixel has only two states, so only the edge information of the picture can be displayed.

  you need to convert the picture into edge information before displaying the picture.

▲ figure 1.1.2 1024 byte encoding corresponding to OLED pixels

2, Convert picture

  for the displayed picture conversion, there are two processes:

  • Convert the picture into a given edge picture;
  • Cut the size of the picture to 128 × 64;

   after the conversion, it can be encoded into 1024 bytes and transferred to microphoton to refresh into OLED, or the data can be stored in a file, pre stored in the SD card and read and displayed by microphoton.

1. Extract edge

  use Canny operator in cv2 to extract edges in the picture.

filename = os.path.join(gifdir, '%04d.BMP'%i)
img = cv2.imread(filename)[:,:,::-1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edge = cv2.Canny(gray, 100, 200)

▲ figure 1.2.1 edge extraction results

2. Extraction center 256 × 128 pictures

imgshape = shape(img)
imgcrop = img[imgshape[0]//2-64:imgshape[0]//2+64,
              0:255,:]

▲ figure 1.2.2 center 256 × 128 pictures

3. The picture is compressed to 128 × sixty-four

imgcrop = cv2.resize(imgcrop, dsize=(128, 64), interpolation=cv2.INTER_LINEAR)

▲ figure 1.2.3 converted to 128 × 64 dot matrix

4. Convert to bytes

   convert the edge picture into 1024 bytes according to the OLED data coding method in [1.1.1: OLED display picture].

def oleddata(d):
    outd = []

    d[d!=0] = 1

    h,w = shape(d)
    line = h//8
    c12 = [2**i for i in range(8)]
    printf(sum(d))

    for i in range(line):
        startline = i*8
        endline = startline+8
        for j in range(w):
            data = d[startline:endline, j]
            printf(data)
            num = sum([x*y for x,y in zip(data, c12)])
            outd.append(num)

   return outd

   the following are the bytes after the conversion of the clothes picture.

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 224, 56, 12, 6, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 3, 4, 9, 11, 18, 50, 18, 4, 84, 84, 148, 20, 20, 36, 32, 40, 40, 40, 40, 40, 72, 64, 80, 208, 16, 224, 16, 80, 80, 80, 80, 80, 64, 15, 136, 143, 129, 129, 129, 2, 65, 129, 129, 128, 128, 128, 0, 0, 0, 0, 15, 0, 15, 16, 31, 16, 31, 0, 4, 7, 0, 255, 0, 176, 31, 16, 16, 31, 16, 16, 7, 4, 248, 0, 0, 0, 248, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 124, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 6, 28, 112, 192, 0, 0, 0, 0, 0, 0, 0, 255, 0, 254, 0, 2, 1, 13, 35, 221, 0, 129, 126, 0, 20, 58, 3, 1, 1, 238, 50, 14, 112, 216, 73, 197, 89, 17, 225, 2, 2, 254, 1, 2, 2, 252, 4, 8, 8, 9, 9, 1, 248, 8, 8, 8, 224, 32, 32, 32, 33, 33, 33, 33, 33, 32, 96, 64, 64, 64, 64, 0, 0, 0, 0, 0, 0, 0, 248, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 124, 0, 0, 192, 96, 32, 223, 64, 255, 0, 128, 238, 138, 136, 255, 138, 139, 170, 168, 138, 250, 128, 128, 128, 179, 8, 8, 8, 11, 8, 8, 57, 192, 15, 0, 254, 1, 0, 0, 196, 187, 0, 0, 0, 0, 0, 0, 255, 0, 28, 226, 1, 251, 134, 48, 80, 136, 136, 64, 112, 24, 8, 8, 24, 160, 32, 0, 0, 0, 0, 0, 0, 192, 103, 56, 80, 80, 48, 32, 64, 192, 160, 160, 64, 64, 64, 128, 0, 0, 128, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 18, 18, 11, 73, 152, 113, 134, 56, 136, 56, 64, 80, 160, 50, 24, 14, 16, 56, 32, 68, 52, 36, 201, 121, 130, 113, 27, 17, 255, 128, 30, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 1, 246, 8, 161, 48, 28, 7, 1, 128, 128, 128, 128, 143, 137, 153, 180, 161, 128, 128, 128, 128, 128, 143, 240, 0, 0, 248, 136, 134, 132, 254, 64, 64, 1, 3, 6, 4, 4, 73, 99, 22, 36, 104, 72, 80, 144, 160, 64, 64, 128, 128, 0, 0, 0, 0, 3, 2, 2, 2, 2, 98, 178, 20, 36, 100, 132, 4, 12, 8, 216, 48, 0, 192, 120, 72, 68, 244, 146, 210, 2, 168, 41, 43, 2, 36, 38, 34, 34, 39, 44, 51, 32, 21, 38, 37, 36, 36, 208, 80, 156, 167, 160, 160, 160, 76, 48, 13, 9, 139, 46, 32, 32, 31, 32, 255, 0, 0, 0, 1, 254, 0, 0, 0, 0, 192, 120, 7, 128, 160, 119, 72, 73, 120, 74, 74, 105, 59, 72, 8, 248, 136, 122, 170, 8, 238, 194, 98, 58, 14, 0, 0, 0, 127, 192, 4, 8, 17, 16, 17, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 2, 2, 4, 5, 8, 16, 240, 16, 252, 68, 36, 32, 48, 21, 28, 10, 65, 243, 10, 98, 31, 64, 143, 243, 140, 118, 131, 137, 133, 134, 0, 128, 195, 162, 12, 20, 20, 20, 20, 20, 20, 20, 36, 36, 36, 36, 36, 36, 36, 39, 32, 35, 36, 40, 40, 47, 36, 68, 68, 70, 85, 85, 85, 85, 87, 81, 87, 80, 80, 64, 160, 191, 160, 160, 160, 160, 161, 175, 136, 144, 144, 159, 144, 16, 80, 80, 80, 80, 86, 81, 81, 82, 84, 80, 93, 81, 17, 0, 0, 0, 0, 0, 0, 0, 0, 3, 14, 24, 112, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 7, 4, 4, 4, 4, 64, 188, 6, 66, 59, 104, 78, 65, 0, 97, 142, 48, 14, 0, 32, 192, 112, 14, 225, 24, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 64, 64, 64, 64, 64, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 12, 8, 24, 48, 96, 192, 0, 0, 0, 0, 0, 1, 6, 28, 112, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 16, 16, 8, 15, 3, 2, 0, 0, 14, 176, 104, 15, 0, 0, 0, 0, 0, 255, 0, 128, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 44, 0, 184, 196, 114, 10, 57, 99, 193, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

3, Display picture

   write a micro Python program to display the previous data in OLED.

1. Write data to SD card file

  write the above data to the data file: GIF.TXT.

  then store GIF.TXT into SD card.

pltgif = PlotGIF()
outfile = open(r'd:\temp\gif.txt', 'w')

#------------------------------------------------------------
for i in range(gifpage):

    filename = os.path.join(gifdir, '%04d.BMP'%i)
    printf(filename)
    img = cv2.imread(filename)[:,:,::-1]

    imgshape = shape(img)
    imgcrop = img[imgshape[0]//2-64:imgshape[0]//2+64,
                  0:255,:]

    imgcrop = cv2.resize(imgcrop, dsize=(128, 64), interpolation=cv2.INTER_LINEAR)
    gray = cv2.cvtColor(imgcrop, cv2.COLOR_BGR2GRAY)
    edge = cv2.Canny(gray, 80, 200)

    data = oleddata(edge)

    strdata = ''
    for d in data:
        strdata = strdata + '%02x'%d

    strdata = strdata + '\n'
    outfile.write(strdata)

2. Display on OLED

   in micropathon, read the data in GIF.TXT and display it on OLED.

from machine                import SoftI2C,Pin
import utime
import i2coled

scl = Pin('PA0')
sda = Pin('PA1')

i2c = SoftI2C(scl, sda, freq=400000)

oled = i2coled.SSD1306_I2C(128, 64, i2c, addr=0x3c)
oled.setstring(0, 0, 'HELLO')
oled.show()

print("Receive OLED and show...")
count = 0

while True:
    count = 0
    with open('gif.txt', 'r') as f:
        for l in f:
            print(len(l))
            for i in range(1024):
                num = int(l[i*2:i*2+2], 16)
                oled.buffer[count] = num
                count += 1

            count = 0
            oled.show()

▲ figure 2.2.1 display animation

 

§ 03 open source soon

  the complete open source code and transplant instructions are being sorted out. A common ecosystem will be established to enable more people to join the application module, enrich their wings and strengthen their muscles and bones. More non professionals will experience the happiness of programming.

■ links to relevant literature:

● relevant chart links:

Posted by thegame261 on Tue, 30 Nov 2021 15:32:42 -0800