/*
程序: ESP32 OLED 使用 U8G2库
大家可以尝试一下改写成为FREERTOS
公众号:孤独的二进制
*/
#include <U8g2lib.h>
#include <Wire.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
int frame = 0;
const int sprite_count = 10; // number of sprites
int sprite_x[sprite_count];
int sprite_y[sprite_count];
int sprite_img[sprite_count];
// images generated by image2cpp
// 'frame_01', 8x8px
const unsigned char epd_bitmap_frame_01 [] PROGMEM = {
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00
};
// 'frame_02', 8x8px
const unsigned char epd_bitmap_frame_02 [] PROGMEM = {
0x00, 0x00, 0x28, 0x10, 0x28, 0x00, 0x00, 0x00
};
// 'frame_03', 8x8px
const unsigned char epd_bitmap_frame_03 [] PROGMEM = {
0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00, 0x00
};
// 'frame_04', 8x8px
const unsigned char epd_bitmap_frame_04 [] PROGMEM = {
0x82, 0x44, 0x28, 0x00, 0x28, 0x44, 0x82, 0x00
};
// 'frame_05', 8x8px
const unsigned char epd_bitmap_frame_05 [] PROGMEM = {
0x82, 0x44, 0x00, 0x10, 0x00, 0x44, 0x82, 0x00
};
// 'frame_06', 8x8px
const unsigned char epd_bitmap_frame_06 [] PROGMEM = {
0x82, 0x00, 0x10, 0x38, 0x10, 0x00, 0x82, 0x00
};
// 'frame_07', 8x8px
const unsigned char epd_bitmap_frame_07 [] PROGMEM = {
0x00, 0x10, 0x10, 0x6c, 0x10, 0x10, 0x00, 0x00
};
// 'frame_08', 8x8px
const unsigned char epd_bitmap_frame_08 [] PROGMEM = {
0x10, 0x10, 0x00, 0xc6, 0x00, 0x10, 0x10, 0x00
};
// 'frame_09', 8x8px
const unsigned char epd_bitmap_frame_09 [] PROGMEM = {
0x10, 0x00, 0x00, 0x82, 0x00, 0x00, 0x10, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 288)
const int epd_bitmap_allArray_LEN = 9;
const unsigned char* epd_bitmap_allArray[9] = {
epd_bitmap_frame_01,
epd_bitmap_frame_02,
epd_bitmap_frame_03,
epd_bitmap_frame_04,
epd_bitmap_frame_05,
epd_bitmap_frame_06,
epd_bitmap_frame_07,
epd_bitmap_frame_08,
epd_bitmap_frame_09
};
const unsigned char upir_logo [] PROGMEM = { // this is another way how to define images, using binary notation
B00010101, B11010111,
B00010101, B01000101,
B00010101, B10010110,
B00011001, B00010101
};
void setup() {
u8g2.begin();
for (int i = 0; i < sprite_count; i++) {
sprite_x[i] = random(0, 128-8);
sprite_y[i] = random(0, 64-8);
sprite_img[i] = i % 8; // previously was sprite_img[i] = random(0, 8);, but this one looks better
}
}
void loop() {
//u8g2.clearBuffer(); // clear the internal memory
//u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
//u8g2.drawStr(15, 10, "LONELY BINARY"); // write something to the internal memory
//u8g2.sendBuffer(); // transfer internal memory to the display
//delay(1000);
u8g2.firstPage();
do {
// draw all sprites
for (int i = 0; i < sprite_count; i++) {
u8g2.drawBitmap( sprite_x[i], sprite_y[i], 8/8, 8, epd_bitmap_allArray[sprite_img[i]]);
}
// draw first line of text, centered
u8g2.setFont(u8g2_font_7x14_tf);
u8g2.drawStr(128/2-u8g2.getStrWidth("Thank you!")/2, 25, "Thank you!");
// draw second line of text, centered
u8g2.setFont(u8g2_font_6x13_tf);
u8g2.drawStr(128/2-u8g2.getStrWidth("For 100 Subscribers")/2, 45, "For 100 Subscribers");
// draw upir logo
u8g2.drawBitmap(128-16, 64-4, 16/8, 4, upir_logo);
} while ( u8g2.nextPage() );
// update sprites with next image
for (int i = 0; i < sprite_count; i++) {
sprite_img[i]++; // increase current image
if (sprite_img[i] > 8) {
// last image == set new random position and first image
sprite_x[i] = random(0, 128-8);
sprite_y[i] = random(0, 64-8);
sprite_img[i] = 0;
}
}
//delay(10); // this is no longer needed, perhaps for the emulator to slow down the drawing a little bit
}