-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patholed_i2c.h
138 lines (119 loc) · 3.67 KB
/
oled_i2c.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
/*oled_i2c*/
#ifndef oled_i2c_h
#define oled_i2c_h
#include <Wire.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// #define SH1106_128_64
#define SCREEN_WIDTH 32 // OLED display width, in pixels
#define SCREEN_HEIGHT 128 // OLED display height, in pixels
#define OLED_RESET -1
// Adafruit_SSD1306 lcd(OLED_RESET);
// Adafruit_SSD1306 lcd(SCREEN_WIDTH, SCREEN_HEIGHT);
Adafruit_SSD1306 lcd(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET,800000,800000);
int oledfpsmicros = 0;
int fontlineheight = 9;
void invertText(){
lcd.setTextColor(BLACK, WHITE); // 'inverted' text
}
void whiteText(){
lcd.setTextColor(WHITE); // 'inverted' text
}
void i2cpinswap(){
Wire.begin(SCL,SDA); // begin(sda, scl) SWAP!
}
// 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();
// }
void print_oled(String str,uint8_t size,bool flush){
lcd.clearDisplay();
lcd.setTextSize(size);
lcd.setTextColor(WHITE);
lcd.setCursor(0,0);
lcd.println(str);
if(flush) lcd.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 = 0;
if(size == 1) y = fontlineheight*no;
if(size == 2) y = (fontlineheight*2)*no;
lcd.setCursor(0,y);
lcd.setTextSize(size);
lcd.println(str);
}
void init_oled(bool preamble = true){
Serial.println("\nInitializing SSD1306 OLED");
Serial.println("SDA: "+(String)SDA);
Serial.println("SCL: "+(String)SCL);
// Wire.begin(SCL,SDA); // begin(sda, scl) SWAP!
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!lcd.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
}
// lcd.setRotation(2);
// 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...."); // show init
lcd.display();
delay(1000);
lcd.clearDisplay(); // clear display
lcd.display();
}
void oled_test(uint8_t num = 1){
// print_oled_line(msg, line, size);
for(uint8_t i=0;i<num;i++){
lcd.clearDisplay();
print_oled_line("millis",0);
print_oled_line((String)millis(),1);
lcd.display();
delay(1000);
lcd.clearDisplay();
print_oled_line("Line One",0);
print_oled_line("Line Two",1);
print_oled_line("Line Three",2);
lcd.display();
delay(1000);
lcd.clearDisplay();
// print_oled_line("Line One",0,2);
// print_oled_line("Line Two",2);
lcd.display();
}
}
void displayFPS(){
lcd.clearDisplay();
lcd.setTextSize(1); // Normal 1:1 pixepl scale
lcd.setCursor(0,0); // Start at top-left corner
lcd.print((String)(10000000/((micros()-oledfpsmicros))));
lcd.display();
// println(" FPS");
oledfpsmicros = micros();
}
#endif