ESP32学习记录 OLED & u8g2库

ESP32 OLED & u8g2库 学习

编程环境

  • ARDUINO环境
    ESP32学习记录 OLED & u8g2库_第1张图片

  • VS Code Platformio环境
    ESP32学习记录 OLED & u8g2库_第2张图片

1. OLED简单显示

显示效果

  • 就连学习也想着可爱囧菌呢!!!
    ESP32学习记录 OLED & u8g2库_第3张图片

main.c

#include 
#include  // 调用函数库
#include "display.h" // OLED 加载资源

U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0,SCL,SDA,U8X8_PIN_NONE);//设置OLED_128X64_I2C模式 

/**
 * @brief 初始化
 * 
 */
void setup(void) {
  u8g2.begin(); //开始使用屏驱动
  u8g2.setFont(u8g2_font_jinxedwizards_tr); //设置字体
}

/**
 * @brief 循环运行
 * 
 */
void loop(void) {
  u8g2.firstPage();//第一页
  do {
      // u8g2.drawStr(0,13,"Mandy Sa"); // 显示文字

      u8g2.drawXBMP(0, 0, imgWidth, imgHeight,gImage_FMJJ); // 显示图片 封茗囧菌
  } while ( u8g2.nextPage() );//循环下一页
}

display.h

/************************************************************************
 * 
 * File         :       display.h
 * Project      :       OLED_UI V1.0
 * Time         :       2022/07/27 
 * Author       :       YU.J.P
 * 
 * **********************************************************************
 */

#ifndef DISPLAY_HH
#define DISPLAY_HH

/* USER CODE INCLUDE BEGIN */
// #include  // 调用函数库

/* USER CODE INCLUDE END */

/* USER CODE PRIVATE DEFINE BEGIN */
#define imgWidth 128 // 图片的宽度
#define imgHeight 64 // 图片的高度

/* USER CODE PRIVATE DEFINE END */

/* USER CODE PRIVATE functions prototypes BEGIN */

/* USER CODE PRIVATE functions prototypes END */

/* USER CODE STATIC VALUE BEGIN */

/**
 * @brief 封茗囧菌 打CALL图
 * 
 */
const unsigned char gImage_FMJJ[1030] = { /* 0X20,0X01,0X80,0X00,0X40,0X00, */
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,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,0X80,0XFF,0XFF,0X1F,0XFC,0XFF,0XFF,0X0F,0XFF,0XFF,0XFF,0X03,
0XE0,0X03,0XC0,0X3F,0X80,0XFF,0XFF,0X1F,0XFC,0XFF,0XFF,0X0F,0XFF,0XFF,0XFF,0X03,
0X00,0X00,0XC0,0X1F,0X80,0X0F,0X00,0X1E,0X3C,0X00,0X00,0X0F,0X0F,0X00,0XC0,0X03,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X18,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X03,0X00,0X60,0X00,0X00,0X00,0X30,0X00,0X00,0XC0,0X00,0X00,0X00,
0X00,0X00,0X80,0X01,0X00,0X70,0X00,0X00,0X00,0X30,0X00,0X00,0XC0,0X01,0X00,0X00,
0X00,0X00,0XC0,0X01,0X00,0X30,0X00,0X00,0X00,0X30,0X00,0X00,0X80,0X01,0X00,0X00,
0X00,0X00,0XC0,0X00,0X00,0X30,0X00,0X00,0X00,0X60,0X00,0X00,0X00,0X03,0X00,0X00,
0X00,0X00,0XE0,0X00,0X00,0X18,0X00,0X00,0X00,0X60,0X00,0X00,0X00,0X03,0X00,0X00,
0X00,0X00,0X60,0X80,0X01,0X18,0X0C,0XC0,0X00,0X60,0X00,0X80,0X01,0X06,0X00,0X00,
0X00,0X00,0X60,0X00,0X01,0X0C,0X06,0XC0,0X01,0X60,0X00,0X80,0X01,0X06,0X00,0X00,
0X00,0X00,0X70,0X10,0X09,0X0C,0X06,0X80,0X01,0XC0,0X00,0X90,0X09,0X06,0X00,0X00,
0X00,0X00,0X70,0XF0,0X1F,0X0E,0X06,0X80,0X01,0XC0,0X00,0XF0,0X0F,0X0E,0X00,0X00,
0X00,0X00,0X30,0X80,0X03,0X06,0X06,0X83,0X01,0XC0,0X00,0X80,0X01,0X0E,0X00,0X00,
0X00,0X00,0X30,0XC0,0X03,0X06,0X07,0X83,0X01,0X80,0X01,0XC0,0X03,0X0E,0X00,0X00,
0X00,0X00,0X30,0X40,0X06,0X03,0X07,0X83,0X01,0X80,0X01,0X60,0X06,0X0E,0X00,0X00,
0X00,0X00,0X30,0X60,0X04,0X03,0X07,0X83,0X01,0X80,0X01,0X60,0X06,0X0E,0X00,0X00,
0X00,0X00,0X70,0X00,0X80,0X01,0X06,0X83,0X01,0X80,0X01,0X00,0X00,0X0E,0X00,0X00,
0X00,0X00,0X70,0X00,0X80,0X01,0X86,0XC7,0X01,0X00,0X03,0X00,0X00,0X06,0X00,0X00,
0X00,0X00,0X60,0X00,0XC0,0X01,0XCE,0XEC,0X00,0X00,0X03,0X00,0X00,0X06,0X00,0X00,
0X00,0X00,0X60,0X00,0XC0,0X00,0XFC,0XFC,0X00,0X00,0X03,0X00,0X00,0X06,0X00,0X00,
0X00,0X00,0XE0,0X00,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,0X00,
0X00,0X00,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X00,0X00,
0X00,0X00,0XC0,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0X01,0X00,0X00,
0X00,0X00,0X80,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0X01,0X00,0X00,
0X00,0X00,0X00,0X03,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0XC0,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

/* USER CODE STATIC VALUE END */

#endif // DISPLAY_HH

2. OLED 升级学习

  • 参考这位博主:四行代码制作你的esp8266天气时钟——基于NodeMCU、OLED模块
  • 只提取显示模块进行学习。因为其他看不懂ヾ(=・ω・=)o 诶。。

显示效果

ESP32学习记录 OLED & u8g2库_第4张图片

main.c

#include 
#include 

#ifdef U8X8_HAVE_HW_SPI
#include 
#endif

#ifdef U8X8_HAVE_HW_I2C
#include 
#endif

 
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0,SCL,SDA,U8X8_PIN_NONE);//设置OLED_128X64_I2C模式 

int h = 0;      // 小时
int m = 0;      // 分钟
int s = 0;      // 秒钟
int start_x=0;  // 位置坐标

/* 读取屏幕大小 */
u8g2_uint_t U8X8_PROGMEM screenWidth;
u8g2_uint_t U8X8_PROGMEM screenHeight;
int day_now=0;

/* 天气信息结构体 */
struct DAY_INF{
  int day_weather;
  int night_weather;
  int high;
  int low;  
 }day_inf[3]={{0,0,0,0},{3,4,5,6},{10,20,15,25}};

int count = 0; // 显示测试计数器

/* 天气显示编码 */

const unsigned char BMP_qing_24X24[] U8X8_PROGMEM= { 
0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x30,0x00,0x0c,0x60,
0x00,0x0e,0x00,0x7e,0x04,0x00,0xff,0x01,0x80,0xff,0x01,0xc0,0xff,0x03,0xc0,0xff,
0x03,0xce,0xff,0x73,0xce,0xff,0x73,0xc0,0xff,0x03,0xc0,0xff,0x03,0x80,0xff,0x01,
0x80,0xff,0x01,0x00,0xfe,0x00,0x60,0x18,0x0e,0x30,0x00,0x0c,0x00,0x18,0x00,0x00,
0x18,0x00,0x00,0x18,0x00,0x00,0x00,0x00,};

const unsigned char BMP_xue_24X24[] U8X8_PROGMEM= { 
0x00,0x00,0x00,0x00,0x18,0x00,0x00,0xd9,0x00,0x00,0xfb,0x00,0x20,0x7e,0x04,0x60,
0x3c,0x06,0x6c,0x18,0x36,0x78,0x18,0x1e,0xf0,0x18,0x0e,0xfc,0x99,0x1f,0x0c,0xff,
0x30,0x00,0x3c,0x00,0x00,0x3c,0x00,0x0c,0xff,0x30,0xf8,0x99,0x3f,0xf0,0x18,0x0f,
0x78,0x18,0x1e,0x6c,0x18,0x36,0x60,0x3c,0x06,0x60,0x7e,0x04,0x00,0xff,0x00,0x00,
0xdb,0x00,0x00,0x18,0x00,0x00,0x00,0x00,};

const unsigned char BMP_duoyun_24X24[] U8X8_PROGMEM= { 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x01,0x00,
0x08,0x30,0x00,0x90,0x01,0x00,0xe0,0x07,0x00,0xc0,0x0f,0x80,0x87,0x0f,0xe0,0x8f,
0xcf,0xe0,0x1f,0x0f,0xf0,0x3f,0x00,0xf8,0x3f,0x00,0xfe,0xbf,0x01,0xfe,0xff,0x07,
0xff,0xff,0x07,0xff,0xff,0x0f,0xfe,0xff,0x0f,0xfe,0xff,0x07,0xf8,0xff,0x03,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};

const unsigned char BMP_yin_24X24[] U8X8_PROGMEM= { 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x70,0x00,0x00,0xfc,0x01,0x00,0xfc,0x03,0x00,0xe3,0x03,0xe0,0xcf,0x07,0xf0,0xdf,
0x3f,0xf0,0xbf,0x7f,0xf8,0x3f,0xfe,0xfe,0x7f,0xf8,0xfe,0xff,0xf7,0xff,0xff,0xef,
0xff,0xff,0x6f,0xff,0xff,0x0f,0xff,0xff,0x0f,0xfe,0xff,0x0f,0xfe,0xff,0x07,0xf8,
0xff,0x03,0x00,0x00,0x00,0x00,0x00,0x00,};

const unsigned char BMP_xiaoyu_24X24[] U8X8_PROGMEM= { 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x3f,0x00,0x80,0x7f,0x00,0x80,0x7f,0x00,0xc0,0xff,0x00,0xf0,0xff,0x07,0xf0,0xff,
0x0f,0xf0,0xff,0x0f,0xf0,0xff,0x1f,0xf0,0xff,0x0f,0xf0,0xff,0x0f,0xe0,0xff,0x07,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};

const unsigned char BMP_dayu_24X24[] U8X8_PROGMEM= { 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x1f,0x00,0x80,0x7f,0x00,0xc0,0x7f,0x00,0xe0,0xff,0x00,0xf0,0xff,0x07,0xf8,0xff,
0x0f,0xf8,0xff,0x1f,0xf8,0xff,0x1f,0xf8,0xff,0x1f,0xf0,0xff,0x0f,0xe0,0xff,0x0f,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x02,0xc0,0x18,0x03,0x40,0x1c,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};

const unsigned char BMP_wifi_24X18[] U8X8_PROGMEM= { 
0x00,0xff,0x00,0xe0,0xff,0x07,0xf8,0xff,0x1f,0xfc,0xc3,0x3f,0x7e,0x00,0x7e,0x1f,
0x00,0xf8,0x0e,0x00,0x60,0x00,0xff,0x00,0xc0,0xff,0x03,0xe0,0xff,0x07,0xe0,0x81,
0x07,0xc0,0x00,0x02,0x00,0x00,0x00,0x00,0x3c,0x00,0x00,0x7e,0x00,0x00,0x7c,0x00,
0x00,0x3c,0x00,0x00,0x18,0x00,};
 
const unsigned char BMP_mai_24X24[] U8X8_PROGMEM= { 
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xe0,0x81,0x07,0xf0,0xc7,0x1f,0x38,0xef,0x3d,0x1c,0x7c,
0x30,0x0c,0x38,0x30,0x0c,0x38,0x30,0x1c,0x7c,0x30,0x38,0xef,0x3c,0xf0,0xc7,0x1f,
0xe0,0x81,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};

const unsigned char BMP_wu_20X20[] U8X8_PROGMEM= { 
0xc0,0x01,0x00,0xf0,0x07,0x00,0x1c,0x0c,0x00,0x0c,0x18,0x00,0x06,0x70,0x00,0x02,
0xf0,0x01,0x03,0x00,0x02,0x03,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
0x07,0xfe,0xff,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x0f,0xfe,0xff,0x0f,
0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x07,0xfe,0xff,0x07,};

/**
 * @brief 初始化设置
 * 
 */
void setup(){
  /* 串口初始化 */
  Serial.begin(115200);

  /* u8g2 初始化 */  
  u8g2.begin();  
  u8g2.enableUTF8Print();
  screenWidth = u8g2.getDisplayWidth();
  screenHeight = u8g2.getDisplayHeight();

  /* WIFI连接等待 */

  // u8g2.firstPage();
  // do {
  //   u8g2.drawXBM(48, 1, 24, 18, BMP_wifi_24X18);
  //   u8g2.setFont(u8g2_font_ncenB14_tr);
  //   u8g2.setCursor(0,50);
  //   u8g2.print("wait for WiFi");
  // } while ( u8g2.nextPage() );   

}

/**
 * @brief 对应天气函数
 * 
 * @param a 选择天气范围
 * @param y y坐标
 * @param x x坐标
 */
void duizhao(int a,int y,int x){
  if(a>=0 && a<=3)u8g2.drawXBM(y+start_x, x,  24, 24, BMP_qing_24X24);                      // 晴
  if(a>=4 && a<=8)u8g2.drawXBM(y+start_x, x,  24, 24, BMP_duoyun_24X24);                    // 多云
  if(a==9)u8g2.drawXBM(y+start_x, x,  24, 24, BMP_yin_24X24);                               // 阴
  if(a==13)u8g2.drawXBM(y+start_x, x,  24, 24, BMP_xiaoyu_24X24);                           // 小雨
  if((a>=10 && a<=12)||(a>=14&&a<=19))u8g2.drawXBM(y+start_x, x,  24, 24, BMP_dayu_24X24);  // 大雨
  if(a>=20 && a<=25)u8g2.drawXBM(y+start_x, x, 24, 24, BMP_xue_24X24);                      // 雪
  if(a>=26 && a<=31&&a!=30)u8g2.drawXBM(y+start_x, x, 24, 24, BMP_mai_24X24);               // 霾
  if(a==30)u8g2.drawXBM(y+start_x+2, x+1, 20, 20, BMP_wu_20X20);                            // 雾
}

/**
 * @brief 显示天气函数
 * 
 */
void DisplayWeather(){
  for(int i=day_now;i<=day_now+1&&i<3;i++){
    duizhao(day_inf[i].day_weather,60*(i-day_now),20);
    duizhao(day_inf[i].night_weather,60*(i-day_now)+30,20);
  }
}

/**
 * @brief 防止烧屏
 * 
 */
void shaoping(){
  static int dir = 1;
  static int flag = 1;
 // Serial.printf("防止烧屏\n");
  if(dir == 1){
    if(flag == 1){ // 减慢移动速度
      start_x ++;
      flag = 0;
    }
    else if(flag == 0){
      flag = 1;
    }
    if(start_x > 128){
      dir = 0;
    }
  }
  else{
    if(flag == 1){
      start_x --;
      flag = 0;
    }
    else if(flag == 0){
      flag = 1;
    }
    if(start_x <= -127){
      dir = 1;
    }
  }
}

/**
 * @brief 主循环函数
 * 
 */
void loop(){
  static int WiFi_status=1;

  u8g2.firstPage();
  do {
    if(count >= 2000){ // 随机测试函数
      day_inf[0].day_weather = random(30);
      day_inf[0].night_weather = random(30);
      day_inf[0].high = random(30);
      day_inf[0].low = random(30);

      day_inf[1].day_weather = random(30);
      day_inf[1].night_weather = random(30);
      day_inf[1].high = random(30);
      day_inf[1].low = random(30);

      day_inf[2].day_weather = random(30);
      day_inf[2].night_weather = random(30);
      day_inf[2].high = random(30);
      day_inf[2].low = random(30);

      h = random(24);
      m = random(60);
      s = random(60);
      count = 0;
    }
    count += 10;
    shaoping(); // 防止烧屏
    
    DisplayWeather();
    u8g2.setFont(u8g2_font_ncenB18_tr);
    u8g2.setCursor(start_x,18);

    if(h < 13)          // 显示小时
      u8g2.print(h);    
    else                // 0-12小时
      u8g2.print(h-12);
    u8g2.print(':');

    if(m < 10)          // 显示分钟
      u8g2.print('0');  // 单数补零
    u8g2.print(m);
    u8g2.setFont(u8g2_font_ncenB10_tr);
    u8g2.print(" ");
    
    if(s < 10)          // 显示秒钟
      u8g2.print('0');  // 单数补零
    u8g2.print(s);
    
    u8g2.setCursor(start_x, 60);
    u8g2.print(day_inf[day_now].high);
    u8g2.print(" / ");
    u8g2.print(day_inf[day_now].low);
  } while (u8g2.nextPage() );
  delay(20);
}

有时间再继续深入学习啦!!

你可能感兴趣的:(ESP32,学习,物联网,ESP32)