ESP32 & SSD1306 - U8G2 中文显示

标准测试程序

#include <Arduino.h>
#include <U8g2lib.h>

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 26, /* data=*/ 25, /* reset=*/ U8X8_PIN_NONE);   // ESP32 Thing, pure SW emulated I2C

void setup(void) {
  u8g2.begin();
  u8g2.enableUTF8Print();		// enable UTF8 support for the Arduino print() function
}

void loop(void) {
  u8g2.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of "你好世界"
  u8g2.setFontDirection(0);
  u8g2.clearBuffer();
  u8g2.setCursor(0, 15);
  u8g2.print("Hello");
  u8g2.setCursor(0, 40);
  u8g2.print("你好");		// Chinese "Hello World" 
  u8g2.sendBuffer();
  delay(1000);
}

 

PY发送端

import serial
import time
import psutil
import serial.tools.list_ports

ports_list = list(serial.tools.list_ports.comports())
if len(ports_list) <= 0:
    print("无串口设备。")
else:
    print("可用的串口设备如下:")
    for comport in ports_list:
        print(list(comport)[0], list(comport)[1])

while True:
    text = ['蓝牙链接', '测试', 'TEST', '可用', '你好', '正在进行', 'COM6']
    for i in text:
        text = i
        ser = serial.Serial('com6', 115200, parity='E', stopbits=1, bytesize=8, timeout=0.5)
        ser.write(text.encode("utf-8"))
        time.sleep(1)
        ser.close()

测试程序1:

#include <Arduino.h>
#include <U8g2lib.h>
#include "BluetoothSerial.h"
 
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
 
String str="";
RTC_DATA_ATTR int bootCount = 0;
const int potpin = 34;
int potValue = 0;
BluetoothSerial SerialBT;
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 26, /* data=*/ 25, /* reset=*/ U8X8_PIN_NONE);   // ESP32 Thing, pure SW emulated I2C
 
void setup(void) {
  u8g2.begin();
  u8g2.enableUTF8Print();		// enable UTF8 support for the Arduino print() function
  pinMode(LED_BUILTIN, OUTPUT);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  SerialBT.printf("ESP32 is restart now! It's the %d time\r\n", ++bootCount);
  Serial.begin(115200); //set up serial library baud rate to 115200
}
 
void loop(void) {
  digitalWrite(LED_BUILTIN, HIGH);
  str = "";
  u8g2.setFont(u8g2_font_wqy12_t_gb2312);  // use chinese2 for all the glyphs of "你好世界"
  u8g2.setFontDirection(0);
  while (SerialBT.available() > 0)
  {
    u8g2.clearBuffer();
    str += char(SerialBT.read());   // read是剪切,而不是复制
    u8g2.setCursor(50, 40);
    u8g2.print(String(potValue));
    //display.drawString(15, 42 , "light:   " + String(potValue));
    delay(10);  // 延时2
  }
  potValue = analogRead(potpin);
  SerialBT.println(potValue);
  u8g2.setCursor(10, 60);
  u8g2.print(str);		// Chinese "Hello World" 
  u8g2.setCursor(10, 15);
  u8g2.print("CXR_PCB第二代");		// Chinese "Hello World" 
  u8g2.sendBuffer();
  digitalWrite(LED_BUILTIN, LOW);
  delay(100);

}

 

posted @ 2022-12-09 20:16  c/  阅读(686)  评论(0编辑  收藏  举报