ESP32 OLED & u8g2库 学习
编程环境
-
ARDUINO环境
-
VS Code Platformio环境
1. OLED简单显示
显示效果
- 就连学习也想着可爱囧菌呢!!!
main.c
#include
#include
#include "display.h"
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0,SCL,SDA,U8X8_PIN_NONE);
void setup(void) {
u8g2.begin();
u8g2.setFont(u8g2_font_jinxedwizards_tr);
}
void loop(void) {
u8g2.firstPage();
do {
u8g2.drawXBMP(0, 0, imgWidth, imgHeight,gImage_FMJJ);
} while ( u8g2.nextPage() );
}
display.h
#ifndef DISPLAY_HH
#define DISPLAY_HH
#define imgWidth 128
#define imgHeight 64
const unsigned char gImage_FMJJ[1030] = {
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,0X3C,0XF0,0X00,0X00,0X00,0X00,0X00,0XC0,0X03,0X0F,0X00,
0X00,0X3C,0X00,0X78,0X00,0X3C,0XF0,0X00,0XFC,0XFF,0XFF,0X0F,0XC0,0X03,0X0F,0X00,
0X00,0X3C,0X00,0X78,0XF8,0XFF,0XFF,0X7F,0XFC,0XFF,0XFF,0X8F,0XFF,0XFF,0XFF,0X07,
0X00,0X3C,0X00,0X78,0XF8,0XFF,0XFF,0X7F,0XFC,0XFF,0XFF,0X8F,0XFF,0XFF,0XFF,0X07,
0XE0,0XFF,0X07,0X78,0XF8,0XFF,0XFF,0X7F,0XFC,0XFF,0XFF,0X8F,0XFF,0XFF,0XFF,0X07,
0XE0,0XFF,0X07,0X78,0X00,0X3C,0XF0,0X00,0X3C,0X08,0X02,0X0F,0XC0,0X03,0X0F,0X00,
0XE0,0XFF,0XF7,0XFF,0X03,0X00,0XF0,0X00,0X3C,0X1C,0X0E,0X0F,0XC0,0X03,0X0F,0X00,
0XE0,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F,0X1F,0X3C,0X1E,0X1E,0X0F,0XCF,0XFF,0XE7,0X03,
0X00,0X3C,0X00,0X78,0XF0,0X0F,0X00,0X1E,0X3C,0X1E,0X1E,0X0F,0XEF,0X7B,0XFF,0X03,
0X00,0X3C,0X00,0X78,0X80,0X0F,0X00,0X1E,0X3C,0X1E,0X1E,0X0F,0XFF,0X79,0XDC,0X03,
0X00,0XFC,0X0F,0X78,0X80,0X0F,0X00,0X1E,0X3C,0X1E,0X1E,0X0F,0X6F,0X78,0XC8,0X03,
0XF0,0XFF,0X0F,0X78,0X80,0X0F,0X00,0X1E,0XFC,0XFF,0XFF,0X0F,0X0F,0X00,0XC0,0X03,
0XE0,0XFF,0XEF,0X7F,0X80,0XFF,0XFF,0X1F,0XFC,0XFF,0XFF,0X0F,0XFF,0XFF,0XFF,0X03,
0XE0,0XFF,0XCF,0X7F,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,
};
#endif
2. OLED 升级学习
- 参考这位博主:四行代码制作你的esp8266天气时钟——基于NodeMCU、OLED模块
- 只提取显示模块进行学习。因为其他看不懂ヾ(=・ω・=)o 诶。。
显示效果
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);
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,};
void setup(){
Serial.begin(115200);
u8g2.begin();
u8g2.enableUTF8Print();
screenWidth = u8g2.getDisplayWidth();
screenHeight = u8g2.getDisplayHeight();
}
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);
}
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);
}
}
void shaoping(){
static int dir = 1;
static int flag = 1;
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;
}
}
}
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
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);
}
有时间再继续深入学习啦!!