-
Notifications
You must be signed in to change notification settings - Fork 2
/
oled_i2c_ssd1306.h
337 lines (292 loc) · 10.4 KB
/
oled_i2c_ssd1306.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifndef old_i2c_sh1106_h
#define old_i2c_sh1106_h
/*oled_i2c*/
#include <log.h>
#include <Wire.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
// const char WIFI_SYMBOL[] = { 93,94,123,168, 176, 247, 253, 263, 277,278,279,280,281, '\0'};
#define GLYPH_BELL 93 // bell
#define GLYPH_BT 94 // bt
#define GLYPH_CLOCK 123 // clock
#define GLYPH_FLAME 168 // flame
#define GLYPH_CHART 176 // chart
#define GLYPH_SIGNAL 253 // bell
#define GLYPH_SPARK 96 // bell
#define GLYPH_GEAR 129 // bell
#define GLYPH_ALERT 0x118 // bell
#define GLYPH_SPKOFF 0x117 // bell
#define GLYPH_SPKHI 0x115 // bell
#define GLYPH_SPKLO 0x116 // bell
#define GLYPH_HASHF 177 // bell
#define GLYPH_HASHM 178 // bell
#define GLYPH_HASHC 179 // bell
#define GLYPH_HDOTS 155 // bell
#define GLYPH_HBAR 221 // bell
#define GLYPH_CROSS 218 // bell
#define GLYPH_CROSSB 234 // bell
#define GLYPH_EKG 238 // bell
#define GLYPH_WIFI 0x119 // bell
#define GLYPH_WRENCH 0x120 // bell
#define GLYPH_TARGET 0x107 // bell
#define GLYPH_LISTINV 0x101 // bell
#define GLYPH_STAR 0x102 // bell
#define GLYPH_SUN 0x103 // bell
#define GLYPH_STPWTCH 269 // bell
#define GLYPH_TERM 0x109 // bell
#define GLYPH_DEL 0x121 // bell
#define GLYPH_GOOD 120 // bell
#define GLYPH_BAD 121 // bell
#define GLYPH_HEART 183 // bell
#define GLYPH_HOUSE 184 // bell
#define GLYPH_SQUARE 217 // bell
#define GLYPH_SPACE 287 // bell
#define GLYPH_NO 87 // busted
#define GLYPH_COFFEE 2615 // busted
// const char* testing = u8"\u0263\u0BA3\u0B82";
// lcd.setFont(u8g2_font_open_iconic_embedded_1x_t);
// lcd.setFont(u8g2_font_open_iconic_all_1x_t);
// lcd.drawStr(0,y,WIFI_SYMBOL);
// lcd.drawUTF8(0,y,testing);
// lcd.drawGlyph(0,y, 93); // bell
// lcd.drawGlyph(10,y, 94); // bluetooth
// lcd.drawGlyph(20,y, 123); // clock
// lcd.drawGlyph(30,y, 168); // flame
// lcd.drawGlyph(40,y, 176); // chart
// lcd.drawGlyph(50,y, 253); // signal
// lcd.drawGlyph(60,y, 96); // terminus
// lcd.drawGlyph(70,y, 129); // gear
// lcd.drawGlyph(80,y, 0x118); // alert
// lcd.drawGlyph(90,y, 0x117); // speaker off
// lcd.drawGlyph(100,y, 0x115); // speaker high
// lcd.drawGlyph(110,y, 0x116); // speaker med
// lcd.drawGlyph(0,y, 177); // hash fine
// lcd.drawGlyph(10,y, 178); // hash med
// lcd.drawGlyph(20,y, 179); // hash low
// lcd.drawGlyph(30,y, 155); // 3 dots
// lcd.drawGlyph(40,y, 221); // mid bar
// lcd.drawGlyph(50,y, 218); // cross
// lcd.drawGlyph(60,y, 234); // cross
// lcd.drawGlyph(60,y, 238); // heartbeat
// lcd.drawGlyph(40,y,0x119); // wifi
// lcd.drawGlyph(50,y,0x120); // wrench not working
// lcd.drawGlyph(60,y,0x107); // target
// lcd.drawGlyph(70,y,0x101); // list inverted
// lcd.drawGlyph(80,y,0x102); // star
// lcd.drawGlyph(90,y,0x103); // sunshine
// lcd.drawGlyph(100,y,269); // stopwatch
// lcd.drawGlyph(110,y,0x109); // terminal
// lcd.drawGlyph(120,y,0x121); // delete
//
//
// lcd.drawGlyph(70,y, 120); // good
// lcd.drawGlyph(80,y, 121); // bad
// lcd.drawGlyph(90,y, 183); // heart
// lcd.drawGlyph(100,y, 184); // house
// lcd.drawGlyph(110,y, 217); // square
// // lcd.drawGlyph(110,y, 287); // space
// if(glyphanimstate==0){
// lcd.drawGlyph(120,y, 155); //
// glyphanimstate = 1;
// }
// else{
// lcd.drawGlyph(120,y, 287); //
// glyphanimstate = 0;
// }
// https://github.com/olikraus/u8g2/wiki/fntlistall#24-pixel-height
// U8g2 Font names
// <prefix> '_' <name> '_' <purpose> <char set>
// <purpose> Description
// t Transparent font, Do not use a background color.
// h All glyphs have common height.
// m All glyphs have common height and width (monospace).
// 8 All glyphs fit into a 8x8 pixel box.
// <char set> Description
// f The font includes up to 256 glyphs.
// r Only glyphs on the range of the ASCII codes 32 to 127 are included in the font.
// u Only glyphs on the range of the ASCII codes 32 to 95 (uppercase chars) are included in the font.
// n Only numbers and extra glyphs for writing date and time strings are included in the font.
// ... Other custom character list.
// #define WHITE SH110X_WHITE
// #define BLACK SH110X_BLACK
// #define SH1106_128_64
// #define SCREEN_WIDTH 128 // OLED display width, in pixels
// #define SCREEN_HEIGHT 64 // OLED display height, in pixels
// U8G2_SSD1306_128X64_NONAME_F_SW_I2C lcd(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
U8G2_SSD1306_128X64_NONAME_F_HW_I2C lcd(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
int oledfpsmicros = 0;
// #if (SSD1306_LCDHEIGHT != 64)
// #error("Height incorrect, please fix Adafruit_SSD1306.h!");
// #endif
void invertText(){
lcd.setDrawColor(2);
// lcd.setTextColor(BLACK, WHITE); // 'inverted' text
}
void whiteText(){
lcd.setDrawColor(1);
// lcd.setTextColor(WHITE); // 'inverted' text
}
// void init_oled(){
// init_oled(false);
// }
// void init_oled(){
// Wire.begin(SCL,SDA); // begin(sda, scl) SWAP!
// // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
// if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
// Serial.println(F("SSD1306 allocation failed"));
// }
// display.clearDisplay();
// display.setTextSize(1); // Normal 1:1 pixepl scale
// display.setTextColor(WHITE); // Draw white text
// display.setCursor(0,0); // Start at top-left corner
// display.display();
// }
/**
* print oled lines
* @param str string to print
* @param no line 0-3
* @param size text size 1-2
*/
void print_oled_line(String str,uint16_t no = 1,uint16_t size = 1){
uint16_t y = 10;
if(size == 1) y += 9*no;
if(size == 2) y += 18*no;
lcd.setCursor(10,y);
// lcd.setTextSize(size);
lcd.println(str);
}
void print_oled(String str,uint8_t size = 1,bool flush = true){
lcd.clearBuffer(); // clear the internal memory
// lcd.setFont(u8g2_font_ncenB08_tr);
// lcd.drawStr(0,10,String(micros()).c_str()); // write something to the internal memory
lcd.setFont(u8g2_font_inb30_mn);
lcd.drawStr(10,30,str.c_str());
// lcd.drawStr(0,10,micros().c_str(); // write something to the internal memory
if(flush)lcd.sendBuffer(); // transfer internal memory to the display
}
void init_oled(bool preamble = true,bool pinswap = false,uint16_t rotate = 2){
Logger.println("[OLED] Initializing SSD1106 OLED");
Logger.println("[OLED] SDA: "+(String)SDA);
Logger.println("[OLED] SCL: "+(String)SCL);
if(pinswap){
Wire.begin(SDA,SCL); // begin(sda, scl) SWAP!
Logger.println("[OLED] pinswapped");
Logger.println("[OLED] SDA: "+(String)SDA);
Logger.println("[OLED] SCL: "+(String)SCL);
}
// Wire.setClock(400000L);
if(rotate==2){
// #define U8G2_R0 (&u8g2_cb_r0)
// #define U8G2_R1 (&u8g2_cb_r1)
// #define U8G2_R2 (&u8g2_cb_r2)
// #define U8G2_R3 (&u8g2_cb_r3)
// #define U8G2_MIRROR (&u8g2_cb_mirror)
lcd.setDisplayRotation(U8G2_R2);
}
// lcd.setBusClock(100000L);
lcd.begin();
// lcd.setI2CAddress(0x78);
// if(!lcd.begin()) { // Address 0x3C for 128x32
// Serial.println(F("SSD1106 begin failed"));
// }
lcd.setFont(u8g2_font_ncenB08_tr);
// u8g2_font_inb30_mn
lcd.clearBuffer(); // clear the internal memory
int px = 10;
lcd.clearBuffer(); // clear the internal memory
lcd.drawStr(0,px,"Starting...");
// lcd.drawStr(60,px,String(micros()).c_str()); // write something to the internal memory
lcd.sendBuffer(); // transfer internal memory to the display
return;
// Wire.setClock(400000L); // set i2c speed 400khz
// Wire.setClock(100000L); // set i2c speed 400khz
// lcd.clearDisplay();
// lcd.setTextSize(1); // Normal 1:1 pixepl scale
// lcd.setCursor(0,0); // Start at top-left corner
// lcd.setTextColor(WHITE); // REQUIRED!
// lcd.display();
// delay(1000);
if(!preamble) return;
// lcd.setTextSize(1);
lcd.setCursor(0,0);
lcd.println("Booting....");
lcd.display();
delay(1000);
lcd.clearBuffer();
lcd.sendBuffer();
}
void displayFPS(){
String str = (String)((1000000/((micros()-oledfpsmicros))));
// String str = (String)((micros()-oledfpsmicros));
oledfpsmicros = micros();
lcd.clearBuffer(); // clear the internal memory
lcd.drawStr(10,10,String(str+ " FPS").c_str());
lcd.drawStr(30,10,String(micros()).c_str()); // write something to the internal memory
lcd.sendBuffer();
// println(" FPS");
// delay(300);
// delay(950);
}
// void printInverted(const char* str){
// lcd.setFontMode(1);
// lcd.print(str);
// lcd.setFontMode(0);
// }
int printInverted(const char* str,uint16_t posx,uint16_t posy){
int lh = lcd.getAscent()+abs(lcd.getDescent()); // font height ?
// int posy = px*2; // posy VAR
// int posx = 0; // posy VAR
// Serial.println(lcd.getStrWidth("OFF"));
// Serial.println(lcd.getStrWidth("ON"));
int padx = (lcd.getStrWidth("OFF")-lcd.getStrWidth("ON"))/2; // w padding
// int pady = 2; // h padding
int pady = 1; // h padding
// Serial.println(padx);
lcd.setDrawColor(1);
uint16_t strw = lcd.drawStr(posx+padx, posy, str); // x,y(NEGATIVE GOING!)
lcd.setFontMode(1);
lcd.setDrawColor(2);
lcd.drawBox(posx, (posy-lh)+pady, strw+(padx*2),lh); // x,y(POSITIVE GOING!),w,h
lcd.setDrawColor(1); // restore default
return strw+(padx*2);
}
void printInvertedStr(String str,uint16_t posx,uint16_t posy){
printInverted(str.c_str(),posx,posy);
}
int printValuePair(const char* str,String strb,int x,int y){
int xoff = printInverted(str,x,y);
lcd.setCursor(x+xoff+5, y);
lcd.print(strb.c_str());
return xoff; // get new cursor?
}
void oled_test(uint8_t num = 0){
// print_oled_line(msg, line, size);
for(uint8_t i=0;i<num;i++){
lcd.clearBuffer();
print_oled_line("millis",0);
print_oled_line((String)millis(),1);
lcd.sendBuffer();
delay(1000);
print_oled_line("Line One",0);
print_oled_line("Line Two",1);
print_oled_line("Line Three",2);
lcd.sendBuffer();
delay(1000);
lcd.clearBuffer();
print_oled_line("Line One",0,2);
print_oled_line("Line Two",2);
lcd.sendBuffer();
}
}
// lcd.setDrawColor(1);
// uint16_t strw = lcd.drawStr(posx+padx, posy, str); // x,y(NEGATIVE GOING!)
// lcd.setFontMode(1);
// lcd.setDrawColor(2);
// lcd.drawBox(posx, (posy-lh)+pady, strw+(padx*2),lh); // x,y(POSITIVE GOING!),w,h
// lcd.setDrawColor(1); // restore default
#endif