-
Notifications
You must be signed in to change notification settings - Fork 5
/
ssd1306.ino
329 lines (298 loc) · 11.3 KB
/
ssd1306.ino
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
/************************************************************************************
* All OLED SSD1306 related stuff
************************************************************************************/
#if defined(USE_SSD1306)
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DEFAULT 0
#define OLED_CHASE 1
#define OLED_GOD 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int oledMode = OLED_DEFAULT;
/************************************************************************************
* Initialize the SSD1306
************************************************************************************/
void setupSSD1306()
{
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
}
/************************************************************************************
* This function is called about once every 10 secs
************************************************************************************/
void timedOledUpdate()
{
// If time since last packet is one the screen, update the timing
switch(oledMode)
{
case OLED_GOD:
case OLED_CHASE:
// clear half of the first line
for (int y=0; y<=6; y++)
{
for (int x=0; x<64; x++)
{
display.drawPixel(x, y, BLACK);
}
}
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(getDuration(Telemetry.atmillis,true));
display.setCursor(97, 0);
display.print("#"); display.print(packetCounter);
display.display(); // Takes around 25ms
break;
}
}
/************************************************************************************
* display a Text message on the OLED SSD1306
************************************************************************************/
void displayOled(int X, int Y, const char* str)
{
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(X, Y);
// Display static text
display.print(str);
display.display();
}
/************************************************************************************
* clear the OLED display
************************************************************************************/
void displayClear()
{
// Clear the buffer
display.clearDisplay();
display.display();
}
/************************************************************************************
* clear the OLED mode
************************************************************************************/
bool changeOLEDMode(int aMode)
{
oledMode = aMode;
// displayUpdate();
oledUpdateNeeded = true;
return true;
}
/************************************************************************************
* display packet data on the OLED display
************************************************************************************/
void displayUpdate()
{
if (oledUpdateNeeded)
{
start = millis();
switch (oledMode)
{
case OLED_DEFAULT:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("IP: ");
display.println(WiFi.localIP().toString().c_str());
display.print("#"); display.println(packetCounter);
display.print(" ID: "); display.println(Telemetry.payload_callsign);
display.print("Frequency: "); display.println(Telemetry.frequency,3);
display.print(" Altitude: "); display.println(Telemetry.alt,0);
display.print(" Distance: "); display.println(Telemetry.distance,1);
display.print(" Chase: "); display.println(Telemetry.compass);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.print("TBTacker-RX "); display.print(TBTRACKER_VERSION);
#if defined(USE_GPS)
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); // Not inverse text
display.setCursor(122, 0);
if (gps_valid)
display.print("G");
else
display.print("X");
#endif
display.display();
break;
case OLED_CHASE:
drawCompass();
break;
case OLED_GOD:
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(getDuration(Telemetry.atmillis,true));
display.setCursor(97, 0);
display.print("#"); display.print(packetCounter);
display.drawLine(0, 10, 128, 10, WHITE);
display.setCursor(0, 13);
display.print(Telemetry.payload_callsign);
display.setCursor(64, 13);
display.print(Telemetry.frequency, 3); display.print("MHz");
display.setCursor(0, 23);
display.print(Telemetry.alt, 0); display.print("m");
display.setCursor(64, 23);
if (Telemetry.distance < 10)
display.print(Telemetry.distance, 1);
else
display.print(Telemetry.distance, 0);
display.print("km");
display.setCursor(105, 23);
display.print(Telemetry.compass);
display.setCursor(0, 33);
display.print(Telemetry.batt, 2); display.print("V");
display.setCursor(64, 33);
display.print(Telemetry.snr); display.print("dB");
display.setCursor(0, 43);
display.print(Telemetry.temp, 1); display.print((char)247); display.print("C");
display.setCursor(64, 43);
display.print(Telemetry.sats); display.print(" sats");
display.setCursor(0, 53);
display.print(Telemetry.lat, 6);
display.setCursor(64, 53);
display.print(Telemetry.lon, 6);
display.display();
break;
}
oledUpdateNeeded = false;
//Serial.print(F("\nTIME spent in displayUpdate():\t\t"));
//Serial.println(millis()- start);
}
}
/************************************************************************************
* flash the OLED screen to show packet was received
************************************************************************************/
void displayFlash()
{
//display.clearDisplay();
//display.setTextColor(WHITE);
//display.setTextSize(2);
//display.setCursor(10, 27);
//display.print("PACKET RX");
//display.display();
display.invertDisplay(true);
flashMillis = millis();
}
void disableFlash()
{
display.invertDisplay(false);;
}
/************************************************************************************
// Update the OLED with the correct frequency
************************************************************************************/
void updateOLEDforFrequency(void)
{
// Clear the display
display.clearDisplay();
// Set the Text size
display.setTextSize(1);
// Set the text color
display.setTextColor(WHITE);
// Set the cursor
display.setCursor(0, 0);
// Line 1
display.print("IP: "); display.println(WiFi.localIP().toString().c_str());
// Line 2
display.println();
// Line 3
display.println();
// Line 4
display.println("Waiting for packets");
// Line 5
display.print("on: ");
display.print(String(LoRaSettings.Frequency,3).c_str());
display.println(" MHz");
// Line 6
display.println();
// Line 7
display.println();
// Invert the screen color
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
// Line 8
display.print("TBTacker-RX "); display.println(TBTRACKER_VERSION);
// Display everything
display.display();
}
/************************************************************************************
// Draw a compass on the OLED
************************************************************************************/
void drawCompass()
{
static int armLength = 22;
static int arrowLength = 15;
static int cx = 86;
static int cy = 32;
int armX, armY, arrow1X, arrow1Y, arrow2X, arrow2Y;
int bearing = Telemetry.bearing;
//convert degree to radian
float bearingRad = bearing/57.2957795; // 1 Radian is 57.2957 degrees
float arm1Rad = (bearing-15)/57.2957795; // calculate for little arrow arms +/- a few degrees.
float arm2Rad = (bearing+15)/57.2957795;
armX = armLength*cos(bearingRad); // use trig to get x and y values. x=hypotenuse*cos(angle in Rads)
armY = -armLength*sin(bearingRad); // y = hypotenuse*sin(angle in Rads)
arrow1X = arrowLength*cos(arm1Rad); // x and y offsets to draw the arrow bits
arrow1Y = -arrowLength*sin(arm1Rad);
arrow2X = arrowLength*cos(arm2Rad); // x and y offsets to draw the rest of the arrow bits
arrow2Y = -arrowLength*sin(arm2Rad);
display.clearDisplay();
// draw line, circle, and arrows
display.drawLine(cx, cy, cx-armY, cy-armX, WHITE); // for some reason have to invert x and y to get correct compass heading
//u8g2.drawLine(cx-armY, cy-armX, cx-arrow1Y, cy-arrow1X); // draw 1/2 of arrowhead
//u8g2.drawLine(cx-armY, cy-armX, cx-arrow2Y, cy-arrow2X);
display.drawTriangle(cx-armY, cy-armX, cx-arrow1Y, cy-arrow1X, cx-arrow2Y, cy-arrow2X, WHITE);
//display.drawCircle(cx, cy, armLength, U8G2_DRAW_ALL, WHITE);
display.drawCircle(cx, cy, armLength, WHITE);
//display.drawCircle(cx, cy, 2, U8G2_DRAW_ALL);
display.drawCircle(cx, cy, 2, WHITE);
// Draw tick marks at each Compass point
display.drawLine(cx, cy-(armLength-2), cx, cy-(armLength +2),WHITE); // North tick mark
display.drawLine(cx, cy+(armLength-2), cx, cy+(armLength +2),WHITE); // South tick mark
display.drawLine(cx-(armLength-2), cy, cx-(armLength+2), cy, WHITE); // West tick mark
display.drawLine(cx+(armLength-2), cy, cx+(armLength+2), cy, WHITE); // East tick mark
//u8g2.setFont(u8g_font_unifont);
// display.setFont(u8g2_font_profont12_tf); //8 pixel font
display.setTextColor(WHITE);
// Label the Compass Directions
display.setTextSize(1);
display.setCursor(cx-2, cy-(armLength+9));
display.print("N");
display.setCursor(cx-2, cy+(armLength+3));
display.print("S");
display.setCursor(cx+(armLength+6), cy-3);
display.print("E");
display.setCursor(cx-(armLength+9), cy-3);
display.print("W");
// display time since last packet
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(getDuration(Telemetry.atmillis,true));
display.setCursor(97, 0);
display.print("#"); display.print(packetCounter);
display.display();
// Display altitude
display.setCursor(0, 34);
display.print(Telemetry.alt,0); display.print("m");
// Display the actual bearing in a larger font
display.setTextSize(2);
display.setCursor(0, 15);
display.print(bearing,0); display.print((char)247);
// display the distance
display.setCursor(0,48);
// Add a decimal if the distance is < 10km
if (Telemetry.distance < 10)
display.print(Telemetry.distance, 1);
else
display.print(Telemetry.distance, 0);
display.print("km");
display.display();
}
#endif