Equipment List
esp8266 development board oled (0.96) DuPont Line
Equipment circuit diagram
Add library
u8g2 library file
Project 1 Hello World
/*
HelloWorld.ino
esp8266+oled project example
Forum: bbskali.cn
Blog: blog.bbskali.cn
*/
#include Arduino.h
#include U8g2lib.h
#ifdef U8X8_HAVE_HW_SPI
#include SPI.h
#endif
#ifdef U8X8_HAVE_HW_I2C
#include Wire.h
#endif
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/D2, /* data=*/D1, /* reset=*/U8X8_PIN_NONE); //Here D1 D2 is the corresponding welding pin
void setup(void) {
u8g2.begin();
}
void loop(void) {
u8g2.clearBuffer(); //clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); //choose a suitable font
u8g2.drawStr(0,10,'Hello World!'); //write something to the internal memory
u8g2.sendBuffer(); //transfer internal memory to the display
delay(1000);
}The effect is as follows
The code of
Item 2 Display Chinese characters
is as follows:
/*
esp8266+oled display
Show Chinese character items
Forum address: bbskali.cn
Blog: blog.bbskali.cn
*/
#include Arduino.h
#include U8g2lib.h
#ifdef U8X8_HAVE_HW_SPI
#include SPI.h
#endif
#ifdef U8X8_HAVE_HW_I2C
#include Wire.h
#endif
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/D2, /* data=*/D1, /* reset=*/U8X8_PIN_NONE); //All Boards without Reset of the Display
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 'Hello world'
u8g2.setFontDirection(0);
u8g2.clearBuffer();
u8g2.setCursor(0, 15);
u8g2.print('kali forum');
u8g2.setCursor(0, 30);
u8g2.print('bbskali.cn'); //Chinese 'Hello World'
u8g2.setCursor(0, 45);
u8g2.print('kali Hacker Teaching');
u8g2.sendBuffer();
delay(1000);
}The effects are as follows:
The code of
Item 3 Display multiple lines of text
is as follows:
/*
*/
#include Arduino.h
#include U8g2lib.h
#ifdef U8X8_HAVE_HW_SPI
#include SPI.h
#endif
#ifdef U8X8_HAVE_HW_I2C
#include Wire.h
#endif
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/D2, /* data=*/D1, /* reset=*/U8X8_PIN_NONE); //All Boards without Reset of the Display
#define FONT u8g2_font_wqy14_t_gb2312b
//#define FONT u8g2_font_wqy16_t_chinese1
//#define FONT u8g2_font_wqy16_t_gb2312b
//The next two macros define the scroll speed of the short story
#define SCROLL_DELTA 2
#define SCROLL_DELAY 200
const char c_str[]=
'Shen Nong\n\n'
'KALI Forum Introduction\n'
'kali forum was created at 20\n'
'17, Webmaster Priess\n'
'This forum is based on kali'
'Full Research and Cybersecurity\n'
'For the convenience of friends' learning\n'
'We built this forum\n'
'Forum study is completely free\n'
'Our URL :\n'
'bbskali,cn\n'
'WeChat official account:\n'
'【kali Hacker Teaching】\n'
'Thank you for your attention. \n';
char buf[48]; //there are at most 8 chinese glyphs per line, max buf size is 8*3=24
uint8_t total_lines; //the total number of lines in the story
uint8_t i; //loop variable for the lines
uint8_t line_cnt; //number of lines to draw, usually equal to lines_per_draw
uint8_t start_line; //topmost visible line, derived from top_window_pos
uint8_t lines_per_draw; //how many lines to draw on the screen, derived from font and display height
uint16_t glyph_height; //height of the glyphs
uint16_t top_window_pos; //defines the display position in pixel within the text
uint16_t total_height; //total height in pixel, derived from font height and total_lines
u8g2_uint_t top_offset; //offset between the first visible line and the display
void setup(void) {
/* U8g2 Project: SSD1306 Test Board */
pinMode(D2, OUTPUT);
pinMode(D1, OUTPUT);
digitalWrite(10, 0);
digitalWrite(9, 0);
/* U8g2 Project: T6963 Test Board */
//pinMode(18, OUTPUT);
//digitalWrite(18, 1);
/* U8g2 Project: KS0108 Test Board */
//pinMode(16, OUTPUT);
//digitalWrite(16, 0);
/* U8g2 Project: LC7981 Test Board, connect RW to GND */
//pinMode(17, OUTPUT);
//digitalWrite(17, 0);
/* U8g2 Project: Pax Instruments Shield: Enable Backlight */
//pinMode(6, OUTPUT);
//digitalWrite(6, 0);
u8g2.begin();
/* select a font */
//u8g2.setFont(u8g2_font_wqy12_t_chinese1); //two unknown glyphs
//u8g2.setFont(u8g2_font_wqy12_t_chinese3); //two unknown glyphs
//u8g2.setFont(u8g2_font_wqy12_t_gb2312a); //';' is missing
//u8g2.setFont(u8g2_font_wqy12_t_gb2312b); //all glyphs available
u8g2.setFont(FONT);
/* calculate the length of the text in lines */
total_lines=u8x8_GetStringLineCnt(c_str);
/* get the height of the glyphs */
glyph_height=u8g2.getMaxCharHeight();
/* calculate the height of the text in pixel */
total_height=(uint16_t)total_lines * (uint16_t)glyph_height;
/* calculate how many lines must be drawn on the screen */
lines_per_draw=u8g2.getDisplayHeight()/glyph_height;
lines_per_draw +=2;
/* start at the top of the text */
top_window_pos=0;
}
void loop(void) {
start_line=top_window_pos/glyph_height;
top_offset=top_window_pos %glyph_height;
line_cnt=total_lines - start_line;
if ( line_cnt lines_per_draw )
line_cnt=lines_per_draw;
u8g2.firstPage();
do {
for( i=0; i line_cnt; i++ )
{
/* copy a line of the text to the local buffer */
u8x8_CopyStringLine(buf, i+start_line, c_str);
/* draw the content of the local buffer */
u8g2.drawUTF8(0, i*glyph_height-top_offset +glyph_height, buf);
}
} while ( u8g2.nextPage() );
delay(SCROLL_DELAY);
top_window_pos +=SCROLL_DELTA;
}
Recommended Comments