Repository navigation
Expand file tree
/
Copy pathGS32Display1306.cpp
More file actions
146 lines (115 loc) · 5.32 KB
/
Copy pathGS32Display1306.cpp
File metadata and controls
146 lines (115 loc) · 5.32 KB
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
#include "GS32Display1306.h"
// Стандартные иконки 16x16
const unsigned char GS32_ICON_WIFI [] PROGMEM = {
0x03, 0xc0, 0x1f, 0xf8, 0x70, 0x0e, 0xe3, 0xc7, 0xc7, 0xe3, 0x0f, 0xf0, 0x1c, 0x38, 0x38, 0x1c,
0x31, 0x8c, 0x03, 0xc0, 0x07, 0xe0, 0x00, 0x00, 0x01, 0x80, 0x03, 0xc0, 0x03, 0xc0, 0x01, 0x80
};
const unsigned char GS32_ICON_BT [] PROGMEM = {
0x03, 0x00, 0x03, 0x80, 0x03, 0xc0, 0x13, 0x40, 0x13, 0x40, 0x0b, 0x80, 0x07, 0x00, 0x03, 0x00,
0x03, 0x00, 0x07, 0x00, 0x0b, 0x80, 0x13, 0x40, 0x13, 0x40, 0x03, 0xc0, 0x03, 0x80, 0x03, 0x00
};
const unsigned char GS32_ICON_TEMP [] PROGMEM = {
0x06, 0x00, 0x09, 0x00, 0x09, 0x00, 0x09, 0x00, 0x0b, 0x00, 0x09, 0x00, 0x0b, 0x00, 0x12, 0x00,
0x22, 0x40, 0x40, 0x20, 0x44, 0x20, 0x44, 0x20, 0x40, 0x20, 0x22, 0x40, 0x1c, 0x80, 0x07, 0x00
};
const unsigned char GS32_ICON_HUM [] PROGMEM = {
0x01, 0x80, 0x01, 0x80, 0x03, 0xc0, 0x07, 0xe0, 0x07, 0xe0, 0x0f, 0xf0, 0x1f, 0xf8, 0x1f, 0xf8,
0x3f, 0xfc, 0x3f, 0xfc, 0x3f, 0xfc, 0x3f, 0xfc, 0x1f, 0xf8, 0x1f, 0xf8, 0x0f, 0xf0, 0x03, 0xc0
};
GS32Display1306::GS32Display1306() {
_display = nullptr;
_isInitialized = false; // По умолчанию дисплей не готов
_globalOffsetX = 0;
_globalOffsetY = 0;
}
GS32Display1306::~GS32Display1306() {
if (_display != nullptr) {
delete _display;
}
}
bool GS32Display1306::begin(uint8_t mosi, uint8_t clk, uint8_t dc, uint8_t rst, uint8_t cs) {
_isInitialized = false; // Сбрасываем флаг при новой инициализации
if (_display != nullptr) {
delete _display;
_display = nullptr;
}
_display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, mosi, clk, dc, rst, cs);
// Если выделение памяти сорвалось или чип не ответил по шине
if(_display == nullptr || !_display->begin(SSD1306_SWITCHCAPVCC)) {
return false; // Просто выходим, _isInitialized остается false
}
_display->clearDisplay();
_display->display();
_isInitialized = true; // Успешно! Теперь функции рисования разблокированы
return true;
}
bool GS32Display1306::isReady() const {
return _isInitialized;
}
void GS32Display1306::setGlobalOffset(int offsetX, int offsetY) {
_globalOffsetX = offsetX;
_globalOffsetY = offsetY;
}
void GS32Display1306::clear() {
if (!_isInitialized || _display == nullptr) return; // 🛑 Блокировка, если не инициализирован
_display->clearDisplay();
}
void GS32Display1306::drawIconText(const unsigned char* bitmap, int iconW, int iconH, String text, int x, int y, uint8_t textSize, int textOffsetFromIcon) {
if (!_isInitialized || _display == nullptr) return; // 🛑 Блокировка, если не инициализирован
_display->setTextColor(SSD1306_WHITE);
_display->setTextSize(textSize);
int finalIconX = x + _globalOffsetX;
int finalIconY = y + _globalOffsetY;
if (bitmap != nullptr) {
_display->drawBitmap(finalIconX, finalIconY, bitmap, iconW, iconH, SSD1306_WHITE);
int finalTextX = finalIconX + iconW + textOffsetFromIcon;
int textHeight = textSize * 8;
int finalTextY = finalIconY + ((iconH - textHeight) / 2);
_display->setCursor(finalTextX, finalTextY);
} else {
_display->setCursor(finalIconX, finalIconY);
}
_display->print(text);
}
void GS32Display1306::drawText(String text, int x, int y, uint8_t textSize) {
if (!_isInitialized || _display == nullptr) return; // 🛑 Блокировка, если не инициализирован
_display->setTextColor(SSD1306_WHITE);
_display->setTextSize(textSize);
int finalX = x + _globalOffsetX;
int finalY = y + _globalOffsetY;
_display->setCursor(finalX, finalY);
_display->print(text);
}
void GS32Display1306::drawPixel(int x, int y, uint16_t color) {
if (!_isInitialized || _display == nullptr) return;
// Применяем глобальное смещение к координатам точки
_display->drawPixel(x + _globalOffsetX, y + _globalOffsetY, color);
}
void GS32Display1306::drawLine(int x0, int y0, int x1, int y1, uint16_t color) {
if (!_isInitialized || _display == nullptr) return;
_display->drawLine(x0 + _globalOffsetX, y0 + _globalOffsetY, x1 + _globalOffsetX, y1 + _globalOffsetY, color);
}
void GS32Display1306::drawRect(int x, int y, int w, int h, bool fill, uint16_t color) {
if (!_isInitialized || _display == nullptr) return;
int finalX = x + _globalOffsetX;
int finalY = y + _globalOffsetY;
if (fill) {
_display->fillRect(finalX, finalY, w, h, color);
} else {
_display->drawRect(finalX, finalY, w, h, color);
}
}
void GS32Display1306::drawCircle(int x, int y, int r, bool fill, uint16_t color) {
if (!_isInitialized || _display == nullptr) return;
int finalX = x + _globalOffsetX;
int finalY = y + _globalOffsetY;
if (fill) {
_display->fillCircle(finalX, finalY, r, color);
} else {
_display->drawCircle(finalX, finalY, r, color);
}
}
void GS32Display1306::send() {
if (!_isInitialized || _display == nullptr) return;
_display->display(); // Физически обновляем экран данными из буфера
}