|
3 | 3 | #include <Adafruit_GFX.h> |
4 | 4 | #include <Adafruit_SSD1306.h> |
5 | 5 | #include <Arduino.h> |
| 6 | +#include <Wire.h> |
6 | 7 |
|
7 | 8 | #include "../Config.h" |
8 | | -#include "../ui/DisplayData.h" |
| 9 | +#include "../ui/GraphicsContext.h" |
9 | 10 |
|
10 | 11 | namespace drivers { |
11 | 12 |
|
12 | | -class OLED { |
| 13 | +class OLED : public ui::GraphicsContext { |
13 | 14 | private: |
14 | | - Adafruit_SSD1306 display; |
15 | | - TwoWire &wire; |
16 | | - int batteryLevel = 85; |
17 | | - int satelliteCount = 5; |
| 15 | + Adafruit_SSD1306 ssd1306; |
18 | 16 |
|
19 | 17 | public: |
20 | | - OLED(TwoWire &i2c) : display(Config::OLED::WIDTH, Config::OLED::HEIGHT, &i2c, -1), wire(i2c) {} |
| 18 | + OLED() : ssd1306(Config::OLED::WIDTH, Config::OLED::HEIGHT, &Wire, -1) {} |
21 | 19 |
|
22 | 20 | void begin() { |
23 | | - if (!display.begin(SSD1306_SWITCHCAPVCC, Config::OLED::ADDRESS)) |
| 21 | + if (!ssd1306.begin(SSD1306_SWITCHCAPVCC, Config::OLED::ADDRESS)) |
24 | 22 | for (;;); |
25 | | - display.clearDisplay(); |
26 | | - display.display(); |
| 23 | + ssd1306.clearDisplay(); |
| 24 | + ssd1306.display(); |
27 | 25 | } |
28 | 26 |
|
29 | | - void show(ui::DisplayDataType type, const char *value) { |
30 | | - display.clearDisplay(); |
| 27 | + // GraphicsContext implementation |
| 28 | + void clear() override { |
| 29 | + ssd1306.clearDisplay(); |
| 30 | + } |
31 | 31 |
|
32 | | - drawHeader(); |
33 | | - ui::DisplayMetadata meta = ui::getDisplayMetadata(type); |
34 | | - drawMainArea(meta.title.c_str(), value, meta.unit.c_str()); |
35 | | - drawFooter(); |
| 32 | + void display() override { |
| 33 | + ssd1306.display(); |
| 34 | + } |
36 | 35 |
|
37 | | - display.display(); |
| 36 | + void setTextSize(int size) override { |
| 37 | + ssd1306.setTextSize(size); |
38 | 38 | } |
39 | 39 |
|
40 | | -private: |
41 | | - void drawHeader() { |
42 | | - display.setTextSize(1); |
43 | | - display.setTextColor(SSD1306_WHITE); |
44 | | - display.setCursor(0, 0); |
45 | | - display.print("GNSS ON"); |
| 40 | + void setTextColor(int color) override { |
| 41 | + ssd1306.setTextColor(color); |
| 42 | + } |
46 | 43 |
|
47 | | - drawSatelliteIcon(100, 0, satelliteCount); |
48 | | - drawBatteryIcon(115, 0, batteryLevel); |
| 44 | + void setCursor(int x, int y) override { |
| 45 | + ssd1306.setCursor(x, y); |
| 46 | + } |
49 | 47 |
|
50 | | - display.drawLine(0, 10, Config::OLED::WIDTH, 10, SSD1306_WHITE); |
| 48 | + void print(const char *text) override { |
| 49 | + ssd1306.print(text); |
51 | 50 | } |
52 | 51 |
|
53 | | - void drawFooter() { |
54 | | - display.drawLine(0, Config::OLED::HEIGHT - 10, Config::OLED::WIDTH, Config::OLED::HEIGHT - 10, SSD1306_WHITE); |
| 52 | + void drawLine(int x0, int y0, int x1, int y1, int color) override { |
| 53 | + ssd1306.drawLine(x0, y0, x1, y1, color); |
| 54 | + } |
| 55 | + |
| 56 | + void drawRect(int x, int y, int w, int h, int color) override { |
| 57 | + ssd1306.drawRect(x, y, w, h, color); |
| 58 | + } |
55 | 59 |
|
56 | | - display.setTextSize(1); |
57 | | - display.setCursor(0, Config::OLED::HEIGHT - 8); |
58 | | - display.print("Ready"); // Placeholder for status |
| 60 | + void fillRect(int x, int y, int w, int h, int color) override { |
| 61 | + ssd1306.fillRect(x, y, w, h, color); |
59 | 62 | } |
60 | 63 |
|
61 | | - void drawMainArea(const char *title, const char *value, const char *unit) { |
62 | | - // Title |
63 | | - display.setTextSize(1); |
64 | | - display.setCursor(0, 14); |
65 | | - display.print(title); |
66 | | - |
67 | | - // Value (Large) |
68 | | - display.setTextSize(2); // Make value bigger |
69 | | - int16_t x1, y1; |
70 | | - uint16_t w, h; |
71 | | - display.getTextBounds(value, 0, 0, &x1, &y1, &w, &h); |
72 | | - display.setCursor((Config::OLED::WIDTH - w) / 2, 28); |
73 | | - display.print(value); |
74 | | - |
75 | | - if (strlen(unit) == 0) return; |
76 | | - |
77 | | - // Unit |
78 | | - display.setTextSize(1); |
79 | | - display.setCursor(Config::OLED::WIDTH - 24, 45); // Bottom right of main area |
80 | | - display.print(unit); |
| 64 | + void drawCircle(int x0, int y0, int r, int color) override { |
| 65 | + ssd1306.drawCircle(x0, y0, r, color); |
81 | 66 | } |
82 | 67 |
|
83 | | - void drawBatteryIcon(int x, int y, int percentage) { |
84 | | - display.drawRect(x, y, 12, 6, SSD1306_WHITE); |
85 | | - display.fillRect(x + 12, y + 2, 2, 2, SSD1306_WHITE); // Battery positive terminal |
| 68 | + void getTextBounds(const char *string, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) override { |
| 69 | + ssd1306.getTextBounds(string, x, y, x1, y1, w, h); |
| 70 | + } |
86 | 71 |
|
87 | | - int width = map(percentage, 0, 100, 0, 10); |
88 | | - display.fillRect(x + 1, y + 1, width, 4, SSD1306_WHITE); |
| 72 | + int getWidth() const override { |
| 73 | + return Config::OLED::WIDTH; |
89 | 74 | } |
90 | 75 |
|
91 | | - void drawSatelliteIcon(int x, int y, int count) { |
92 | | - display.drawCircle(x + 3, y + 3, 2, SSD1306_WHITE); // Placeholder for satellite icon |
| 76 | + int getHeight() const override { |
| 77 | + return Config::OLED::HEIGHT; |
93 | 78 | } |
94 | 79 | }; |
95 | 80 |
|
|
0 commit comments