为OLED显示图片生成Bytearray

OLED 0.96

缘起

自从使用Raspberry Pi Pico以后,就觉得MicroPython真的太方便了。各种应用想实现就分分钟搞定。
这里面不得不说OLED屏幕中0.96inch的存在,真是小巧又可爱。
在刷新OLED的屏幕时,常用到ssd1306的库,那都是底层,最重要的还是使用framebuf库的功能。
其中,我最喜欢就是用bytearray去做为输入的buf。
例如我就常常:

from machine import Pin, ADC, I2C
from time import sleep
import framebuf
from ssd1306 import SSD1306_I2C

WIDTH = 128
HEIGHT = 64

buf = bytearray(b'') # 这里定义buf。 后面讲这里的buf怎么实现。

fb = framebuf.FrameBuffer(buf, framebuf.MONO_VLSB)

bus = I2C(1, scl=Pin(15), sda=Pin(14), freq=2000000)
oled = SSD1306_I2C(WIDTH, HEIGHT, bus)

while True:
    oled.fill(0)
    oled.show()
    sleep(0.01)
    oled.blit(fb, 0, 0) 
    oled.show()
    sleep(0.5)

buf的实现方法

  1. 打开windows的绘图板,生成一个128x64的画布
    然后自己随便画个图像并保存。
  2. 然后打开pycharm编写如下代码:

from io import BytesIO
from PIL import Image
import sys

if len(sys.argv) > 1:
    path_to_image = str(sys.argv[1])
    x = int(sys.argv[2])
    y = int(sys.argv[3])
    im = Image.open(path_to_image).convert('1')
    im_resize = im.resize((x,y))
    buf = BytesIO()
    im_resize.save(buf, 'ppm')
    byte_im = buf.getvalue()
    temp = len(str(x) + ' ' + str(y)) + 4
    print(byte_im[temp::])
else:
    print("please specify the location of image i.e img2bytearray.py /path/to/image width heigh")

然后执行这个代码并提供图片的位置,这样就将图片生成了一段bytearray数组,拷贝到buf的位置。

红框部分的内容复制到bytearray的位置。上传到pico,就搞定了。
非常简单。

MPU6050计算三个姿态使用的公式

这两天折腾MPU6050, 仔细看了看文档。

下面内容就是不同的角度的算法。

Pitch angle 俯仰角

  • To find Pitch angle should be the angle between X and Z axis which will be
    atan2(accelZ,accelX)*180/PI

    Roll angle 滚动角

  • For Roll angle should be the angle between Y and Z axis which will be
    atan2(accelZ,accelY)*180/PI

    yaw angle 偏航角

    And to find yaw it should be the angle between X and VectorY+Z which will be

    atan2(sqrt(accelY*accelY+accelZ*accelZ),accelX)*180/PI

    总结

    1. 校准IMU
    2. 读取原始数据
    3. 处理数据信息