【微雪 RP2040双核开发板】四 汉子字库 汉子显示
<p>资料:</p><div></div>
<h2 >一、Pycharm开发环境配置</h2>
<p >1.安装micropython插件</p>
<p > </p>
<p > </p>
<p >2.关联pico</p>
<p > </p>
<p > </p>
<p >3.代码测试</p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<h2 >二、汉子字库显示参考资料</h2>
<p > </p>
<p >下面分享前辈的设计理念即步骤教程。。</p>
<p >1.1.micropython 汉子开发 (字库版)</p>
<p ><a href="https://www.bilibili.com/video/av599593694/" title="https://www.bilibili.com/video/av599593694/">https://www.bilibili.com/video/av599593694/</a> </p>
<p >1.2.micropython 字库生成</p>
<p ><a href="https://www.bilibili.com/video/BV1YD4y16739/?spm_id_from=333.788.video.desc.click" title="https://www.bilibili.com/video/BV1YD4y16739/?spm_id_from=333.788.video.desc.click">https://www.bilibili.com/video/BV1YD4y16739/?spm_id_from=333.788.video.desc.click</a> </p>
<p >1.3.MicroPython 中文字库</p>
<p >https://github.com/AntonVanke/MicroPython-Chinese-Font</p>
<p >1.4.点阵字体自定义工具</p>
<p ><a href="https://github.com/AntonVanke/MicroPython_BitMap_Tool">https://github.com/AntonVanke/MicroPython_BitMap_Tool</a></p>
<p > </p>
<p >2.ESP32 汉子显示 (取模版)</p>
<p >https://www.cnblogs.com/juwan/p/13198330.html</p>
<h1 >3.ssd1306OLED中文显示-MicroPython-<a href="https://so.csdn.net/so/search?q=ESP32&spm=1001.2101.3001.7020">ESP32</a>-利用GB2312字库(非手动取模)</h1>
<p >fb增强固件及字库.rar,这个很关键,正常的固件都不支持字库的方式显示字库。首先是没有字库对应的解析文件,就是根据汉子编码获取汉子的点阵信息。然后是microPython仅支持UTF-8</p>
<p ><a href="https://blog.csdn.net/hehedadaq/article/details/117596103"><u>https://blog.csdn.net/hehedadaq/article/details/117596103</u></a></p>
<p > </p>
<p > </p>
<p >以上三种方式,第二种是最容易实现的,方法思路都很简单方便,第三种受固件限制,扩平台几乎不太可能,所以我最终使用的第一种通用的方式实现的。</p>
<p > </p>
<ul>
<li >实际显示效果</li>
</ul>
<p > </p>
<p > </p>
<ol>
<li >实际效果如图所示,常用的3500个汉字以及英文字母和标点符号等都可以调用显示,字符的大小也可以切换。</li>
</ol>
<p align="justify" > </p>
<ul>
<li align="justify" >前期准备和大神的设计思路</li>
</ul>
<p align="justify" >1、大神写的实践步骤</p>
<p > </p>
<p ><b># MicroPython-Chinese</b></p>
<p > </p>
<p >MicroPython 的中文显示模块,使其可以在显示模块上显示汉字。此外也可以自定义字体集,使其能够满足不同存储空间、不同语言的要求。</p>
<p > </p>
<p ><b>### 资源</b></p>
<p > </p>
<p >](<u>https://www.bilibili.com/video/BV12B4y1B7Ff/</u>)</p>
<p > </p>
<p >(<u>./doc/MicroPython%20中文字库的使用演示文档.md</u>)</p>
<p > </p>
<p >[如何生成点阵字体文件](<u>https://github.com/AntonVanke/MicroPython_BitMap_Tools/blob/master/doc/如何生成点阵字体文件.md</u>)</p>
<p > </p>
<p ><b>### 简单上手</b></p>
<p > </p>
<p >所需</p>
<p > </p>
<p >1. 运行 `micropython` 的开发板</p>
<p >2. 使用 `SSD1306 驱动芯片`的 `OLED 128*64` 屏幕</p>
<p >3. 拥有至少 `512Kbyte` 的空闲 ROM 空间和 `20 Kbyte`的空闲内存</p>
<p > </p>
<p ><b>**两步操作**</b></p>
<p > </p>
<p >1. 将 `demo/ssd1306_demo.py`文件修改为你所连接的引脚</p>
<p > </p>
<p > ```python</p>
<p > i2c = I2C(scl=Pin(2), sda=Pin(3)) # line 16</p>
<p > ```</p>
<p > </p>
<p >2. 依次将`demo/ssd1306_demo.py`、`driver/ssd1306.py`、`ufont.py`、`unifont-14-12888-16.v3.bmf`上传到<b>**开发板根目录**</b>,运行`ssd1306_demo.py`即可</p>
<p > </p>
<p >图片</p>
<p > </p>
<p >如果你运行成功了,正常情况下是会显示</p>
<p > </p>
<p > <img src="https://s1.ax1x.com/2022/07/31/vFBplT.jpg" alt="运行效果" style="zoom: 33%;" /></p>
<p > </p>
<p ><b>### 使用方法</b></p>
<p > </p>
<p ><b>**项目文件**</b></p>
<p > </p>
<p >```shell</p>
<p >.</p>
<p >├── LICENSE</p>
<p >├── README.md</p>
<p >├── demo</p>
<p >│ ├── epaper1in54_demo.py # 1.54 英寸墨水屏例子</p>
<p >│ ├── ssd1306_demo.py # 0.96 英寸 OLED 例子</p>
<p >│ └── st7735_demo.py # 合宙 Air10x 系列屏幕扩展板驱动 例子(不完善)</p>
<p >├── doc</p>
<p >│ ├── MicroPython 中文字库的使用演示文档.md</p>
<p >│ └── 如何生成点阵字体文件.md</p>
<p >├── driver</p>
<p >│ ├── e1in54.py # 1.54 英寸墨水屏驱动</p>
<p >│ ├── ssd1306.py # 0.96 英寸 OLED 驱动</p>
<p >│ └── st7735.py # 合宙 Air10x 系列屏幕扩展板驱动</p>
<p >├── requirements.txt # python 库需求文件</p>
<p >├── ufont.py # ⭐库文件</p>
<p >└── unifont-14-12888-16.v3.bmf # ⭐生成的点阵字体</p>
<p > </p>
<p >```</p>
<p > </p>
<p >上传`ufont.py`、`unifont-14-12888-16.v3.bmf`到`MicroPython`根目录</p>
<p > </p>
<p ><b>**对于**</b>`SSD1306(OLED 128*64)`</p>
<p > </p>
<p >```python</p>
<p >from machine import Pin, I2C</p>
<p > </p>
<p >import ssd1306</p>
<p >import ufont</p>
<p > </p>
<p >i2c = I2C(scl=Pin(2), sda=Pin(3)) # 定义 I2C 管脚</p>
<p >display = ssd1306.SSD1306_I2C(128, 64, i2c) # 驱动对象</p>
<p > </p>
<p >f = ufont.BMFont("unifont-14-12888-16.v3.bmf") # 中文显示对象</p>
<p > </p>
<p >f.text(</p>
<p > display=display, # 显示对象 必要</p>
<p > string="", # 显示的文字 必要</p>
<p > x=0, # x 轴</p>
<p > y=0, # y 轴</p>
<p > color=1, # 颜色 默认是 1(黑白)</p>
<p > font_size=16, # 字号(像素)</p>
<p > reverse=False, # 逆置(墨水屏会用到)</p>
<p > clear=False, # 显示前清屏</p>
<p > show=False # 是否立即显示</p>
<p >)</p>
<p >```</p>
<p > </p>
<p ><b>**对于**</b>`st7735(80*160)[合宙 Air10x 系列屏幕扩展板]`</p>
<p > </p>
<p >详见(<u>/doc/MicroPython%20中文字库的使用演示文档.md</u>)</p>
<p > </p>
<p ><b>### 字体生成</b></p>
<p > </p>
<p ><b>#### 下载应用程序</b></p>
<p > </p>
<p >(<u>https://github.com/AntonVanke/MicroPython_BitMap_Tools/releases/tag/v0.0.1</u>)</p>
<p > </p>
<p ><b>#### 详见</b></p>
<p > </p>
<p >[如何生成点阵字体文件](<u>https://github.com/AntonVanke/MicroPython_BitMap_Tools/blob/master/doc/如何生成点阵字体文件.md</u>)</p>
<p > </p>
<p ><b>### 生成的字体文件格式</b></p>
<p > </p>
<p >![点阵字体文件格式](<u>https://s1.ax1x.com/2022/07/31/vkQ9u6.jpg</u>)</p>
<p > </p>
<p ><b>### 注意事项</b></p>
<p > </p>
<p >1. 采用`Framebuf`缓冲区的驱动才能够使用</p>
<p >2. 最好全程使用推荐字号(16px)否则会有各种各样的问题</p>
<p >3. 主要缓冲区内存不足的问题(尤其是TFT屏幕)</p>
<p > </p>
<p > </p>
<ol start="2">
<li >实际区别</li>
</ol>
<p >首先是芯片不同,固件不同,然后是显示屏的不同。相同的是字库文件相同,最后的实际字库代码略微调整。</p>
<p >所以在这个汉子的实践过程最主要的工作内容就是代码的移植。</p>
<p >3、准备文件</p>
<p >首先准备微雪的LCD显示列成代码(RP2040-LCD-1.28.py)和LCD用的固件(rp2-pico-20220117-v1.18.uf2),主要用做显示的驱动和主要函数代码。</p>
<p >然后准备使用的字库文件(unifont-14-12888-16.v3.bmf)和字库的解析程序(ufont),因为微雪官方提供的LCDDome是没有汉子显示的,其例程序可以画点、画线、字符显示等基础功能。</p>
<p > </p>
<p > </p>
<p > </p>
<ul>
<li >程序移植开发</li>
</ul>
<p >1.首先固件下载到pico当中,然后测试一下LCDDome工程。详细的操作可以查看此链接https://bbs.eeworld.com.cn/thread-1229748-1-1.html</p>
<p >2.将字库文件和字库解析文件下载到pico中,字库使用的是Unicode编码,并非GKB编码。如图所示:</p>
<p > </p>
<p >3.代码功能块和作用</p>
<p >3.1代码内容的概述在代码程序中已备注的形式体现。当前状态程序内汉子字符不能直接转为Unicode的十六进制,目前也不在想继续开发这块代码,对于我这初学者来说进坑有点多有点耗时。</p>
<p > </p>
<p ># encoding:utf-8</p>
<p >from machine import Pin,I2C,SPI,PWM,ADC</p>
<p >import framebuf</p>
<p > </p>
<p >import time</p>
<p >import random</p>
<p >import ufont #字库解码程序文件调用</p>
<p >import binascii</p>
<p > </p>
<p > </p>
<p >I2C_SDA = 6 # I2C引脚声明 陀螺仪用</p>
<p >I2C_SDL = 7</p>
<p > </p>
<p >DC = 8 # LCD引脚声明</p>
<p >CS = 9</p>
<p >SCK = 10</p>
<p >MOSI = 11</p>
<p >RST = 12</p>
<p > </p>
<p >BL = 25 #LCD背光</p>
<p > </p>
<p >Vbat_Pin = 29 #电池电压检测引脚</p>
<p > </p>
<p > </p>
<p > </p>
<p >class LCD_1inch28(framebuf.FrameBuffer): #LCD初始化和子函数</p>
<p > def __init__(self):</p>
<p > self.width = 240</p>
<p > self.height = 240</p>
<p > </p>
<p > self.cs = Pin(CS,Pin.OUT)</p>
<p > self.rst = Pin(RST,Pin.OUT)</p>
<p > </p>
<p > self.cs(1)</p>
<p > self.spi = SPI(1,100_000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)</p>
<p > self.dc = Pin(DC,Pin.OUT)</p>
<p > self.dc(1)</p>
<p > self.buffer = bytearray(self.height * self.width * 2)</p>
<p > super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)</p>
<p > self.init_display()</p>
<p > </p>
<p > self.red = 0x07E0</p>
<p > self.green = 0x001f</p>
<p > self.blue = 0xf800</p>
<p > self.white = 0xffff</p>
<p > self.colorData = bytearray(2)</p>
<p > self.windowLocData = bytearray(4)</p>
<p > self.fill(self.white)</p>
<p > self.show()</p>
<p > </p>
<p > self.pwm = PWM(Pin(BL))</p>
<p > self.pwm.freq(1000)</p>
<p > </p>
<p > def write_cmd(self, cmd):</p>
<p > self.cs(1)</p>
<p > self.dc(0)</p>
<p > self.cs(0)</p>
<p > self.spi.write(bytearray())</p>
<p > self.cs(1)</p>
<p > </p>
<p > def write_data(self, buf):</p>
<p > self.cs(1)</p>
<p > self.dc(1)</p>
<p > self.cs(0)</p>
<p > self.spi.write(bytearray())</p>
<p > self.cs(1)</p>
<p > def set_bl_pwm(self,duty):</p>
<p > self.pwm.duty_u16(duty)#max 65535</p>
<p > def init_display(self):</p>
<p > """Initialize dispaly""" </p>
<p > self.rst(1)</p>
<p > time.sleep(0.01)</p>
<p > self.rst(0)</p>
<p > time.sleep(0.01)</p>
<p > self.rst(1)</p>
<p > time.sleep(0.05)</p>
<p > </p>
<p > self.write_cmd(0xEF)</p>
<p > self.write_cmd(0xEB)</p>
<p > self.write_data(0x14)</p>
<p > </p>
<p > self.write_cmd(0xFE)</p>
<p > self.write_cmd(0xEF)</p>
<p > </p>
<p > self.write_cmd(0xEB)</p>
<p > self.write_data(0x14)</p>
<p > </p>
<p > self.write_cmd(0x84)</p>
<p > self.write_data(0x40)</p>
<p > </p>
<p > self.write_cmd(0x85)</p>
<p > self.write_data(0xFF)</p>
<p > </p>
<p > self.write_cmd(0x86)</p>
<p > self.write_data(0xFF)</p>
<p > </p>
<p > self.write_cmd(0x87)</p>
<p > self.write_data(0xFF)</p>
<p > </p>
<p > self.write_cmd(0x88)</p>
<p > self.write_data(0x0A)</p>
<p > </p>
<p > self.write_cmd(0x89)</p>
<p > self.write_data(0x21)</p>
<p > </p>
<p > self.write_cmd(0x8A)</p>
<p > self.write_data(0x00)</p>
<p > </p>
<p > self.write_cmd(0x8B)</p>
<p > self.write_data(0x80)</p>
<p > </p>
<p > self.write_cmd(0x8C)</p>
<p > self.write_data(0x01)</p>
<p > </p>
<p > self.write_cmd(0x8D)</p>
<p > self.write_data(0x01)</p>
<p > </p>
<p > self.write_cmd(0x8E)</p>
<p > self.write_data(0xFF)</p>
<p > </p>
<p > self.write_cmd(0x8F)</p>
<p > self.write_data(0xFF)</p>
<p > </p>
<p > </p>
<p > self.write_cmd(0xB6)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x20)</p>
<p > </p>
<p > self.write_cmd(0x36)</p>
<p > self.write_data(0x98)</p>
<p > </p>
<p > self.write_cmd(0x3A)</p>
<p > self.write_data(0x05)</p>
<p > </p>
<p > </p>
<p > self.write_cmd(0x90)</p>
<p > self.write_data(0x08)</p>
<p > self.write_data(0x08)</p>
<p > self.write_data(0x08)</p>
<p > self.write_data(0x08)</p>
<p > </p>
<p > self.write_cmd(0xBD)</p>
<p > self.write_data(0x06)</p>
<p > </p>
<p > self.write_cmd(0xBC)</p>
<p > self.write_data(0x00)</p>
<p > </p>
<p > self.write_cmd(0xFF)</p>
<p > self.write_data(0x60)</p>
<p > self.write_data(0x01)</p>
<p > self.write_data(0x04)</p>
<p > </p>
<p > self.write_cmd(0xC3)</p>
<p > self.write_data(0x13)</p>
<p > self.write_cmd(0xC4)</p>
<p > self.write_data(0x13)</p>
<p > </p>
<p > self.write_cmd(0xC9)</p>
<p > self.write_data(0x22)</p>
<p > </p>
<p > self.write_cmd(0xBE)</p>
<p > self.write_data(0x11)</p>
<p > </p>
<p > self.write_cmd(0xE1)</p>
<p > self.write_data(0x10)</p>
<p > self.write_data(0x0E)</p>
<p > </p>
<p > self.write_cmd(0xDF)</p>
<p > self.write_data(0x21)</p>
<p > self.write_data(0x0c)</p>
<p > self.write_data(0x02)</p>
<p > </p>
<p > self.write_cmd(0xF0) </p>
<p > self.write_data(0x45)</p>
<p > self.write_data(0x09)</p>
<p > self.write_data(0x08)</p>
<p > self.write_data(0x08)</p>
<p > self.write_data(0x26)</p>
<p > self.write_data(0x2A)</p>
<p > </p>
<p > self.write_cmd(0xF1) </p>
<p > self.write_data(0x43)</p>
<p > self.write_data(0x70)</p>
<p > self.write_data(0x72)</p>
<p > self.write_data(0x36)</p>
<p > self.write_data(0x37) </p>
<p > self.write_data(0x6F)</p>
<p > </p>
<p > </p>
<p > self.write_cmd(0xF2) </p>
<p > self.write_data(0x45)</p>
<p > self.write_data(0x09)</p>
<p > self.write_data(0x08)</p>
<p > self.write_data(0x08)</p>
<p > self.write_data(0x26)</p>
<p > self.write_data(0x2A)</p>
<p > </p>
<p > self.write_cmd(0xF3) </p>
<p > self.write_data(0x43)</p>
<p > self.write_data(0x70)</p>
<p > self.write_data(0x72)</p>
<p > self.write_data(0x36)</p>
<p > self.write_data(0x37)</p>
<p > self.write_data(0x6F)</p>
<p > </p>
<p > self.write_cmd(0xED)</p>
<p > self.write_data(0x1B)</p>
<p > self.write_data(0x0B)</p>
<p > </p>
<p > self.write_cmd(0xAE)</p>
<p > self.write_data(0x77)</p>
<p > </p>
<p > self.write_cmd(0xCD)</p>
<p > self.write_data(0x63)</p>
<p > </p>
<p > </p>
<p > self.write_cmd(0x70)</p>
<p > self.write_data(0x07)</p>
<p > self.write_data(0x07)</p>
<p > self.write_data(0x04)</p>
<p > self.write_data(0x0E)</p>
<p > self.write_data(0x0F)</p>
<p > self.write_data(0x09)</p>
<p > self.write_data(0x07)</p>
<p > self.write_data(0x08)</p>
<p > self.write_data(0x03)</p>
<p > </p>
<p > self.write_cmd(0xE8)</p>
<p > self.write_data(0x34)</p>
<p > </p>
<p > self.write_cmd(0x62)</p>
<p > self.write_data(0x18)</p>
<p > self.write_data(0x0D)</p>
<p > self.write_data(0x71)</p>
<p > self.write_data(0xED)</p>
<p > self.write_data(0x70)</p>
<p > self.write_data(0x70)</p>
<p > self.write_data(0x18)</p>
<p > self.write_data(0x0F)</p>
<p > self.write_data(0x71)</p>
<p > self.write_data(0xEF)</p>
<p > self.write_data(0x70)</p>
<p > self.write_data(0x70)</p>
<p > </p>
<p > self.write_cmd(0x63)</p>
<p > self.write_data(0x18)</p>
<p > self.write_data(0x11)</p>
<p > self.write_data(0x71)</p>
<p > self.write_data(0xF1)</p>
<p > self.write_data(0x70)</p>
<p > self.write_data(0x70)</p>
<p > self.write_data(0x18)</p>
<p > self.write_data(0x13)</p>
<p > self.write_data(0x71)</p>
<p > self.write_data(0xF3)</p>
<p > self.write_data(0x70)</p>
<p > self.write_data(0x70)</p>
<p > </p>
<p > self.write_cmd(0x64)</p>
<p > self.write_data(0x28)</p>
<p > self.write_data(0x29)</p>
<p > self.write_data(0xF1)</p>
<p > self.write_data(0x01)</p>
<p > self.write_data(0xF1)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x07)</p>
<p > </p>
<p > self.write_cmd(0x66)</p>
<p > self.write_data(0x3C)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0xCD)</p>
<p > self.write_data(0x67)</p>
<p > self.write_data(0x45)</p>
<p > self.write_data(0x45)</p>
<p > self.write_data(0x10)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > </p>
<p > self.write_cmd(0x67)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x3C)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x01)</p>
<p > self.write_data(0x54)</p>
<p > self.write_data(0x10)</p>
<p > self.write_data(0x32)</p>
<p > self.write_data(0x98)</p>
<p > </p>
<p > self.write_cmd(0x74)</p>
<p > self.write_data(0x10)</p>
<p > self.write_data(0x85)</p>
<p > self.write_data(0x80)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x4E)</p>
<p > self.write_data(0x00)</p>
<p > </p>
<p > self.write_cmd(0x98)</p>
<p > self.write_data(0x3e)</p>
<p > self.write_data(0x07)</p>
<p > </p>
<p > self.write_cmd(0x35)</p>
<p > self.write_cmd(0x21)</p>
<p > </p>
<p > self.write_cmd(0x11)</p>
<p > time.sleep(0.12)</p>
<p > self.write_cmd(0x29)</p>
<p > time.sleep(0.02)</p>
<p > </p>
<p > self.write_cmd(0x21)</p>
<p > </p>
<p > self.write_cmd(0x11)</p>
<p > </p>
<p > self.write_cmd(0x29)</p>
<p > </p>
<p > def show(self):</p>
<p > self.write_cmd(0x2A)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0xef)</p>
<p > </p>
<p > self.write_cmd(0x2B)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0x00)</p>
<p > self.write_data(0xEF)</p>
<p > </p>
<p > self.write_cmd(0x2C)</p>
<p > </p>
<p > self.cs(1)</p>
<p > self.dc(1)</p>
<p > self.cs(0)</p>
<p > self.spi.write(self.buffer)</p>
<p > self.cs(1)</p>
<p > </p>
<p > def _writedata( self, aData ) :</p>
<p > '''Write given data to the device. This may be</p>
<p > either a single int or a bytearray of values.'''</p>
<p > self.cs(1)</p>
<p > self.dc(1)</p>
<p > self.cs(0)</p>
<p > self.spi.write(aData)</p>
<p > self.cs(1)</p>
<p > </p>
<p > def _pushcolor( self, aColor ) :</p>
<p > self.colorData = aColor >> 8</p>
<p > self.colorData = aColor</p>
<p > self._writedata(self.colorData)</p>
<p > </p>
<p >class QMI8658(object): #陀螺仪初始化 以及陀螺仪函数</p>
<p > def __init__(self,address=0X6B):</p>
<p > self._address = address</p>
<p > self._bus = I2C(id=1,scl=Pin(I2C_SDL),sda=Pin(I2C_SDA),freq=100_000)</p>
<p > bRet=self.WhoAmI()</p>
<p > if bRet :</p>
<p > self.Read_Revision()</p>
<p > else :</p>
<p > return 0</p>
<p > self.Config_apply()</p>
<p > </p>
<p > def _read_byte(self,cmd):</p>
<p > rec=self._bus.readfrom_mem(int(self._address),int(cmd),1)</p>
<p > return rec</p>
<p > def _read_block(self, reg, length=1):</p>
<p > rec=self._bus.readfrom_mem(int(self._address),int(reg),length)</p>
<p > return rec</p>
<p > def _read_u16(self,cmd):</p>
<p > LSB = self._bus.readfrom_mem(int(self._address),int(cmd),1)</p>
<p > MSB = self._bus.readfrom_mem(int(self._address),int(cmd)+1,1)</p>
<p > return (MSB << 8) + LSB</p>
<p > def _write_byte(self,cmd,val):</p>
<p > self._bus.writeto_mem(int(self._address),int(cmd),bytes())</p>
<p > </p>
<p > def WhoAmI(self):</p>
<p > bRet=False</p>
<p > if (0x05) == self._read_byte(0x00):</p>
<p > bRet = True</p>
<p > return bRet</p>
<p > def Read_Revision(self):</p>
<p > return self._read_byte(0x01)</p>
<p > def Config_apply(self):</p>
<p > # REG CTRL1</p>
<p > self._write_byte(0x02,0x60)</p>
<p > # REG CTRL2 : QMI8658AccRange_8g and QMI8658AccOdr_1000Hz</p>
<p > self._write_byte(0x03,0x23)</p>
<p > # REG CTRL3 : QMI8658GyrRange_512dps and QMI8658GyrOdr_1000Hz</p>
<p > self._write_byte(0x04,0x53)</p>
<p > # REG CTRL4 : No</p>
<p > self._write_byte(0x05,0x00)</p>
<p > # REG CTRL5 : Enable Gyroscope And Accelerometer Low-Pass Filter</p>
<p > self._write_byte(0x06,0x11)</p>
<p > # REG CTRL6 : Disables Motion on Demand.</p>
<p > self._write_byte(0x07,0x00)</p>
<p > # REG CTRL7 : Enable Gyroscope And Accelerometer</p>
<p > self._write_byte(0x08,0x03)</p>
<p > </p>
<p > def Read_Raw_XYZ(self):</p>
<p > xyz=</p>
<p > raw_timestamp = self._read_block(0x30,3)</p>
<p > raw_acc_xyz=self._read_block(0x35,6)</p>
<p > raw_gyro_xyz=self._read_block(0x3b,6)</p>
<p > raw_xyz=self._read_block(0x35,12)</p>
<p > timestamp = (raw_timestamp<<16)|(raw_timestamp<<8)|(raw_timestamp)</p>
<p > for i in range(6):</p>
<p > # xyz=(raw_acc_xyz[(i*2)+1]<<8)|(raw_acc_xyz)</p>
<p > # xyz=(raw_gyro_xyz[((i+3)*2)+1]<<8)|(raw_gyro_xyz[(i+3)*2])</p>
<p > xyz = (raw_xyz[(i*2)+1]<<8)|(raw_xyz)</p>
<p > if xyz >= 32767:</p>
<p > xyz = xyz-65535</p>
<p > return xyz</p>
<p > def Read_XYZ(self):</p>
<p > xyz=</p>
<p > raw_xyz=self.Read_Raw_XYZ() </p>
<p > #QMI8658AccRange_8g</p>
<p > acc_lsb_div=(1<<12)</p>
<p > #QMI8658GyrRange_512dps</p>
<p > gyro_lsb_div = 64</p>
<p > for i in range(3):</p>
<p > xyz=raw_xyz/acc_lsb_div#(acc_lsb_div/1000.0)</p>
<p > xyz=raw_xyz*1.0/gyro_lsb_div</p>
<p > return xyz</p>
<p > </p>
<p >def TFTColor( aR, aG, aB ) :</p>
<p > '''Create a 16 bit rgb value from the given R,G,B from 0-255.</p>
<p > This assumes rgb 565 layout and will be incorrect for bgr.'''</p>
<p > return ((aR & 0xF8) << 8) | ((aG & 0xFC) << 3) | (aB >> 3)</p>
<p > </p>
<p > </p>
<p > </p>
<p >class utf8_gb2312(object): #utf-8 转 GB2312编码 在此没有用到</p>
<p > def __init__(self):</p>
<p > self.f = open('font.txt', 'r', encoding='utf-8')</p>
<p > </p>
<p > def b2i(self, byte): # bytes转int</p>
<p > r = 0</p>
<p > for i in range(len(byte)):</p>
<p > r = (r << 8) + byte</p>
<p > return r</p>
<p > </p>
<p > def i2b(self, num): # int转bytes</p>
<p > num = int(num, 16)</p>
<p > return num.to_bytes(2, 'big')</p>
<p > </p>
<p > def one_char(self, char): # 将一个字符转化成gb2312</p>
<p > utf_byte = char.encode('utf-8')</p>
<p > r = self.B_S(0, 7296, self.b2i(utf_byte))</p>
<p > gb2312_byte = self.i2b(r)</p>
<p > # print(gb2312_byte)</p>
<p > return gb2312_byte</p>
<p > </p>
<p > def str(self, st): # 将字符串转化成gb2312</p>
<p > r = b''</p>
<p > for s in st:</p>
<p > # print(s.encode('utf-8'))</p>
<p > if len(s.encode('utf-8')) <= 1:</p>
<p > r += s.encode('utf-8')</p>
<p > else:</p>
<p > r += self.one_char(s)</p>
<p > return r</p>
<p > </p>
<p > def B_S(self, low, high, m): # 二分查找</p>
<p > if 0 <= low <= high <= 7296:</p>
<p > mid = (low + high) // 2</p>
<p > self.f.seek(mid * 12)</p>
<p > data = self.f.read(12)</p>
<p > utf = data</p>
<p > if int(utf, 16) < m:</p>
<p > return self.B_S(mid + 1, high, m)</p>
<p > elif int(utf, 16) > m:</p>
<p > return self.B_S(low, mid - 1, m)</p>
<p > else:</p>
<p > return data</p>
<p > </p>
<p > def __del__(self):</p>
<p > self.f.close()</p>
<p > </p>
<p >'''</p>
<p >if __name__ == '__main__':</p>
<p > font = utf8_gb2312()</p>
<p > </p>
<p > r = font.one_char('中')</p>
<p > print(r)</p>
<p > print(r.decode('gb2312'))</p>
<p > r = font.str("起风了Abc123-")</p>
<p > print(r)</p>
<p > print(r.decode('gb2312'))</p>
<p > </p>
<p >'''</p>
<p > </p>
<p >if __name__=='__main__':</p>
<p > </p>
<p > LCD = LCD_1inch28()#初始化LCD</p>
<p > LCD.set_bl_pwm(65535)#初始化背光</p>
<p > qmi8658=QMI8658()#初始化陀螺仪</p>
<p > Vbat= ADC(Pin(Vbat_Pin)) #初始化ADC电压采集</p>
<p > print(issubclass(LCD_1inch28, framebuf.FrameBuffer))</p>
<p > f = ufont.BMFont("unifont-14-12888-16.v3.bmf") # 加载字库 Unicode字库 不仅有汉子还有字符串</p>
<p > #f = ufont.BMFont("GB2312.DZK")</p>
<p > f.text(LCD,"0123456",0,80,120,LCD.white,font_size=16,reverse=True,show=True) #文字显示函数</p>
<p > '''</p>
<p > 文字显示函数的内容介绍</p>
<p > display=display, # 显示对象 必要</p>
<p > string="", # 显示的文字字符串 必要</p>
<p > hz=, # 汉子显示 如果汉子显示 设置string为'?' 如果显示字符hz=0</p>
<p > x=0, # x 轴</p>
<p > y=0, # y 轴</p>
<p > color=1, # 颜色 默认是 1(黑白)</p>
<p > font_size=16, # 字号(像素)</p>
<p > reverse=False, # 逆置(墨水屏会用到)</p>
<p > clear=False, # 显示前清屏</p>
<p > show=False # 是否立即显示</p>
<p > '''</p>
<p > </p>
<p > #取模方式显示汉子 start </p>
<p > pattern = [0x81,0x00,0x41,0x00,0x21,0x00,0x11,0x00,0x09,0x00,0x05,0x00,0x02,0x80,0x03,0x80,</p>
<p >0x04,0xC0,0x04,0x40,0x08,0x20,0x08,0x30,0x10,0x18,0x20,0x0C,0x40,0x06,0x80,0x03]</p>
<p > buf = framebuf.FrameBuffer(bytearray(pattern), 16, 16, framebuf.MONO_HLSB)</p>
<p > #LCD.blit(buf, 140, 140,LCD.white)</p>
<p > #取模方式显示汉子 end</p>
<p > while(True):</p>
<p > #read QMI8658</p>
<p > '''</p>
<p > 隐藏部分为 微雪Dome列出内容</p>
<p > xyz=qmi8658.Read_XYZ()</p>
<p > </p>
<p > LCD.fill(LCD.white)</p>
<p > </p>
<p > LCD.fill_rect(0,0,240,40,LCD.red)</p>
<p > LCD.text("RP2040-LCD-1.28",60,25,LCD.white)</p>
<p > </p>
<p > LCD.fill_rect(0,40,240,40,LCD.blue)</p>
<p > #LCD.text(display,"浣犲ソ寰�杞�闆呴粦",80,57,LCD.white)</p>
<p > </p>
<p > LCD.fill_rect(0,80,120,120,0x1805)</p>
<p > LCD.text("ACC_X={:+.2f}".format(xyz),20,100-3,LCD.white)</p>
<p > LCD.text("ACC_Y={:+.2f}".format(xyz),20,140-3,LCD.white)</p>
<p > LCD.text("ACC_Z={:+.2f}".format(xyz),20,180-3,LCD.white)</p>
<p > </p>
<p > LCD.fill_rect(120,80,120,120,0xF073)</p>
<p > LCD.text("GYR_X={:+3.2f}".format(xyz),125,100-3,LCD.white)</p>
<p > LCD.text("GYR_Y={:+3.2f}".format(xyz),125,140-3,LCD.white)</p>
<p > LCD.text("GYR_Z={:+3.2f}".format(xyz),125,180-3,LCD.white)</p>
<p > </p>
<p > LCD.fill_rect(0,200,240,40,0x180f)</p>
<p > reading = Vbat.read_u16()*3.3/65535*2</p>
<p > LCD.text("Vbat={:.2f}".format(reading),80,215,LCD.white)</p>
<p > </p>
<p > LCD.show()</p>
<p > #''' </p>
<p > # 循环内循环的扫描显示内容</p>
<p > f.text(LCD,"chinese",0,80,57,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > #utf-8 转 GB2312编码 因为开始的时候以为使用的是GBk编码 在网上找到了此段代码</p>
<p > font = utf8_gb2312()</p>
<p > </p>
<p > r = font.one_char('撑')</p>
<p > #print(r)</p>
<p > #utf-8 转 GB2312编码</p>
<p > </p>
<p > #f.char(LCD,"我", reverse=True, color=0xffff, font_size=16)</p>
<p > #汉子显示初步完成</p>
<p > hzst = </p>
<p > </p>
<p > #LCD显示 汉子显示目前是逐个字的显示,还有待优化</p>
<p > f.text(LCD,'?',hzst,20,90,LCD.white,font_size=16,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,40,90,LCD.white,font_size=16,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,60,90,LCD.white,font_size=16,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,80,90,LCD.white,font_size=16,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,100,90,LCD.white,font_size=16,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,120,90,LCD.white,font_size=16,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,140,90,LCD.white,font_size=16,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,160,90,LCD.white,font_size=16,reverse=True,show=True)</p>
<p > </p>
<p > f.text(LCD,'?',hzst,20,150,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,44,150,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,68,150,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,92,150,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,116,150,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,120,150,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,144,150,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > f.text(LCD,'?',hzst,168,150,LCD.white,font_size=24,reverse=True,show=True)</p>
<p > '''</p>
<p > 隐藏代码为微雪Dome程序</p>
<p > RED = TFTColor(0xFF, 0x00, 0x00)</p>
<p > f=open('spaceman.bmp', 'rb')</p>
<p > if f.read(2) == b'BM': #header</p>
<p > print('Hello MicroPython.bmp')</p>
<p > dummy = f.read(8) #file size(4), creator bytes(4)</p>
<p > print('dummy',dummy)</p>
<p > offset = int.from_bytes(f.read(4), 'little')</p>
<p > print('offset',offset).</p>
<p > hdrsize = int.from_bytes(f.read(4), 'little')</p>
<p > print('hdrsize',hdrsize)</p>
<p > width = int.from_bytes(f.read(4), 'little')</p>
<p > </p>
<p > height = int.from_bytes(f.read(4), 'little')</p>
<p > #RGB = bytearray(172800)</p>
<p > #RGB = f.read(57600)</p>
<p > #print('RGB',RGB)</p>
<p > if int.from_bytes(f.read(2), 'little') == 1: #planes must be 1</p>
<p > print('fsit MicroPython.bmp')</p>
<p > depth = int.from_bytes(f.read(2), 'little')</p>
<p > if depth == 24 and int.from_bytes(f.read(4), 'little') == 0:#compress method == uncompressed</p>
<p > print("Image size:", width, "x", height)</p>
<p > rowsize = (width * 3 + 3) & ~3</p>
<p > if height < 0:</p>
<p > height = -height</p>
<p > flip = False</p>
<p > else:</p>
<p > flip = True</p>
<p > w, h = width, height</p>
<p > </p>
<p > </p>
<p > for row in range(h):</p>
<p > if flip:</p>
<p > pos = offset + (height - 1 - row) * rowsize</p>
<p > else:</p>
<p > pos = offset + row * rowsize</p>
<p > if f.tell() != pos:</p>
<p > dummy = f.seek(pos)</p>
<p > for col in range(w):</p>
<p > bgr = bytearray(4)</p>
<p > bgr = f.read(3)</p>
<p > </p>
<p > </p>
<p > #print('bgr',bgr)</p>
<p > LCD._pushcolor(TFTColor(0xFF, 0x00, 0x00))</p>
<p > </p>
<p > LCD.show()</p>
<p > '''</p>
<p > </p>
<p > time.sleep(0.1) # 延时</p>
<p > </p>
<p > </p>
<p > </p>
<p >3.2 .ufont.Py 部分内容说明,大神代码在文件前声明版本为Python __version__ = 3 但我的环境下运行的仍然是microPython V1.13 所以代码功能是有缺失的。</p>
<p >(1)word_code = ord(word) 将字符转换成十六进制,但是不能转换汉子。Python3 可以使用hz = hex(ord('中'))函数将汉子编码进行转换,但micropython不能直接转。</p>
<p >(2)如图所示此函数用于输入字符或汉子编码,根据输入得到点阵数据</p>
<p >(3)struct.unpack(">H", self.font.read(2)) 解包邴杜初字库内的十六进制点阵数据</p>
<p > </p>
<p > </p>
<ol start="4">
<li >其余功能在代码中有注释,详情查看代码</li>
</ol>
<p > </p>
<p > </p>
<ul>
<li >心酸路程</li>
</ul>
<p > </p>
<h2 >micropython编码</h2>
<p >计划使用字库的方案做个汉子显示,如此可以兼容简体中文常用的3500多个汉子,要想使用此方案,就需要制作字库,需要转码,程序内的汉子字符需要转成十六进制用来调取字库内的点阵数据。</p>
<p >1.简述ASCII、Unicode、GBK、UTF-8编码的区别</p>
<p >ASCII:使用一个字节编码,所以它的范围基本是只有英文字母、数字和一些特殊符号 ,只有256个字符,使用8位二进制表示信息。</p>
<p >Unicode:使用32位表示信息,Unicode一般用于计算机内存中做计算。</p>
<p >GBK:只用来编码汉字的,GBK全称《汉字内码扩展规范》。使用双字节编码,一个中文占2个字节。</p>
<p >UTF-8:压缩Unicode,使用8位二进制表示信息,用尽量少的位数来表示信息,节省存储空间。UTF-8中一个中文占3个字节,UTF-8一般可用于网络传输和数据存储</p>
<p > </p>
<ol start="2">
<li >MicroPython</li>
</ol>
<p >MicroPython个人用的也不是很多,对其不是完全的了解,在此阶段的开发学习中遇到了很多的困难。</p>
<ol>
<li >MicroPython是Python3编程语言的精简高效实现,包括Python标准款的一小部分,并且经过优化,可在微控制器和受限环境中运行。</li>
<li >MicroPython的编码格式是UTF-8,不支持GBK等其他格式,Python3的编码格式是以Unicode为主并且支持utf-8的格式。Python3以下也需要注意。</li>
<li >下面是我测试的过程</li>
</ol>
<p align="justify" > </p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p >如图所示:Thonny开发工具,运行的解释器是Raspberry Pi Pico</p>
<p > </p>
<p >如图所示:thonny默认的解释器如图所示,即可以正确的执行,一个汉字两个字节,因为默认的解释器是Python3.版本</p>
<p > </p>
<p > </p>
<p >如图所示:PyCharm开发工具汉子转码为正确的,但是运行代码的是软件自带仿真,而不是实际的开发板上。此时的运行环境实际是Python3,多以转换是正确的的。</p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p>幸亏有这么多汉子字库显示资料参考。</p>
<p>MicroPython经过优化,可在微控制器和受限环境中运行,这个好</p>
可惜我是个硬件工程师,软件没学好,看天书一样,不懂啊!
页:
[1]