forked from jp112sdl/HB-RC-12-EP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HB-RC-12-EP.ino
421 lines (365 loc) · 14.1 KB
/
HB-RC-12-EP.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
// 2019-04-03 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
//#define NDISPLAY
#define DISPLAY_COLORED 1 // 1 = Farbdisplay; 0 = schwarz/weiß Display
//////////////////// DISPLAY DEFINITIONS /////////////////////////////////////
#include <GxEPD.h>
#if DISPLAY_COLORED == 1
#include <GxGDEW029Z10/GxGDEW029Z10.h> // 2.9" b/w/r
//#include <GxGDEW026Z39/GxGDEW026Z39.h> // 2.6" b/w/r
#else
#include <GxGDEW029T5/GxGDEW029T5.h> // 2.9" b/w
#endif
#include <GxIO/GxIO_SPI/GxIO_SPI.h>
#include <GxIO/GxIO.h>
#include "U8G2_FONTS_GFX.h"
#include "Icons.h"
#define GxRST_PIN 22 // PC6
#define GxBUSY_PIN 23 // PC7
#define GxDC_PIN 21 // PC5
#define GxCS_PIN 20 // PC4
#define DISPLAY_ROTATE 0 // 0 = 0° , 1 = 90°, 2 = 180°, 3 = 270°
GxIO_Class io(SPI, GxCS_PIN, GxDC_PIN, GxRST_PIN);
GxEPD_Class display(io, GxRST_PIN, GxBUSY_PIN);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
U8G2_FONTS_GFX u8g2Fonts(display);
//////////////////////////////////////////////////////////////////////////////
#define EI_NOTEXTERNAL
#include <EnableInterrupt.h>
#include <SPI.h>
#include <AskSinPP.h>
#include <LowPower.h>
#include <Register.h>
#include <MultiChannelDevice.h>
#include <Remote.h>
#define CC1101_CS_PIN 4 // PB4
#define CC1101_GDO0_PIN 2 // PB2
//#define CC1101_SCK_PIN 7 // PB7
//#define CC1101_MOSI_PIN 5 // PB5
//#define CC1101_MISO_PIN 6 // PB6
#define CONFIG_BUTTON_PIN 1 // PB1
#define LED_PIN_1 19 // PC3
//#define LED_PIN_2 NC // uncomment if you use DualStatusLed
#define BTN01_PIN A5 // PA5 Key Pattern
#define BTN02_PIN 18 // PC2 1 --- 2
#define BTN03_PIN A4 // PA4 3 --- 4
#define BTN04_PIN A6 // PA6 5 --- 6
#define BTN05_PIN A3 // PA3 7 --- 8
#define BTN06_PIN 15 // PD7 9 --- 10
#define BTN07_PIN A2 // PA2 11 --- 12
#define BTN08_PIN 14 // PD6
#define BTN09_PIN A1 // PA1
#define BTN10_PIN 13 // PD5
#define BTN11_PIN A0 // PA0
#define BTN12_PIN 12 // PD4
#define TEXT_LENGTH 10
#define CHANNEL_COUNT 12
#define PEERS_PER_CHANNEL 8
using namespace as;
struct {
bool Inverted = false;
uint16_t clFG = GxEPD_BLACK;
uint16_t clBG = GxEPD_WHITE;
} DisplayDeviceConfig;
typedef struct {
uint8_t IconNumber = 0x00;
#if DISPLAY_COLORED == 1
bool IconColored = false;
bool TextColored = false;
#endif
bool TextBold = false;
String TextContent = "";
bool ShowLine = false;
} DisplayLineConfig;
DisplayLineConfig DisplayLines[CHANNEL_COUNT];
const struct DeviceInfo PROGMEM devinfo = {
{0xf3, 0x46 - DISPLAY_COLORED, 0x00}, // Device ID
"JPRCEP0001", // Device Serial
{0xf3, 0x46 - DISPLAY_COLORED}, // Device Model
0x10, // Firmware Version
as::DeviceType::Remote, // Device Type
{0x00, 0x00} // Info Bytes
};
bool first = false;
bool mustRefreshDisplay = false;
/**
Configure the used hardware
*/
typedef LibSPI<CC1101_CS_PIN> SPIType;
typedef Radio<SPIType, CC1101_GDO0_PIN> RadioType;
#ifdef LED_PIN_2
typedef DualStatusLed<LED_PIN_1, LED_PIN_2> LedType;
#else
typedef StatusLed<LED_PIN_1> LedType;
#endif
typedef AskSin<LedType, BatterySensor, RadioType> BaseHal;
class Hal: public BaseHal {
AlarmClock btncounter;
public:
void init(const HMID& id) {
BaseHal::init(id);
battery.init(20, btncounter);
battery.low(24);
battery.critical(22);
}
void sendPeer () {
--btncounter;
}
bool runready () {
return sysclock.runready() || BaseHal::runready();
}
} hal;
void updateDisplay(bool doit) {
if (doit) {
u8g2Fonts.begin(display);
mustRefreshDisplay = false;
#ifndef NDISPLAY
display.drawPaged(updateDisplay);
#else
DPRINTLN(F("display.drawPaged(updateDisplay);"));
#endif
}
}
class RefreshDisplayAlarm : public Alarm {
public:
RefreshDisplayAlarm () : Alarm(0) {}
virtual ~RefreshDisplayAlarm () {}
void cancel (AlarmClock& clock) {
clock.cancel(*this);
}
void set (uint32_t t,AlarmClock& clock) {
clock.cancel(*this);
Alarm::set(t);
clock.add(*this);
}
virtual void trigger (__attribute__((unused)) AlarmClock& clock) {
updateDisplay(mustRefreshDisplay);
}
};
DEFREGISTER(Reg0, MASTERID_REGS, DREG_LEDMODE, DREG_LOWBATLIMIT, 0x06)
class RCEPList0 : public RegList0<Reg0> {
public:
RCEPList0(uint16_t addr) : RegList0<Reg0>(addr) {}
bool displayInvertingHb(bool v) const { return this->writeRegister(0x06, 0x01,0,v); }
bool displayInvertingHb() const { return this->readRegister(0x06, 0x01,0,false); }
void defaults () {
clear();
displayInvertingHb(false);
ledMode(1);
lowBatLimit(24);
}
};
DEFREGISTER(Reg1, CREG_LONGPRESSTIME, CREG_AES_ACTIVE, CREG_DOUBLEPRESSTIME, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x90, 0x92)
class RCEPList1 : public RegList1<Reg1> {
public:
RCEPList1 (uint16_t addr) : RegList1<Reg1>(addr) {}
bool showLine (uint8_t value) const { return this->writeRegister(0x90, 0x01, 0, value & 0xff); }
bool showLine () const { return this->readRegister(0x90, 0x01, 0, false); }
#if DISPLAY_COLORED == 1
bool textColored (uint8_t value) const { return this->writeRegister(0x90, 0x02, 0, value & 0xff); }
bool textColored () const { return this->readRegister(0x90, 0x02, 0, false); }
bool iconColored (uint8_t value) const { return this->writeRegister(0x90, 0x04, 0, value & 0xff); }
bool iconColored () const { return this->readRegister(0x90, 0x04, 0, false); }
#endif
bool textBold (uint8_t value) const { return this->writeRegister(0x90, 0x08, 0, value & 0xff); }
bool textBold () const { return this->readRegister(0x90, 0x08, 0, false); }
bool iconNumber (uint8_t value) const { return this->writeRegister(0x92, value & 0xff); }
uint8_t iconNumber () const { return this->readRegister(0x92, 0); }
bool textContent (uint8_t value[TEXT_LENGTH]) const { for (int i = 0; i < TEXT_LENGTH; i++) { this->writeRegister(0x36 + i, value[i] & 0xff); } return true; }
String textContent () const { String a = ""; for (int i = 0; i < TEXT_LENGTH; i++) { byte b = this->readRegister(0x36 + i, 0x20); if (b == 0x00) b = 0x20; a += char(b); } return a; }
void defaults () {
clear();
//aesActive(false);
uint8_t initValues[TEXT_LENGTH];
memset(initValues, 0x20, TEXT_LENGTH);
textContent(initValues);
showLine(false);
textBold(false);
iconNumber(0);
#if DISPLAY_COLORED == 1
textColored(false);
iconColored(false);
#endif
}
};
class ConfigChannel : public RemoteChannel<Hal,PEERS_PER_CHANNEL,RCEPList0, RCEPList1> {
public:
ConfigChannel () : RemoteChannel() {}
virtual ~ConfigChannel () {}
void configChanged() {
RemoteChannel::configChanged();
if (first == false) {
if (number() < 10) DPRINT("0"); DDEC(number());
DPRINT(" SHOWLINE: "); DDEC(this->getList1().showLine());
DPRINT(" ICON NUMBER: "); DDEC(this->getList1().iconNumber());
DPRINT(" TEXT BOLD: "); DDEC(this->getList1().textBold());
DPRINT(" TEXT CONTENT: "); DPRINT(this->getList1().textContent());
#if DISPLAY_COLORED == 1
DPRINT(" TEXT COLORED: "); DDEC(this->getList1().textColored());
DPRINT(" ICON COLORED: "); DDEC(this->getList1().iconColored());
#endif
DPRINTLN("");
mustRefreshDisplay = true;
}
uint8_t d = number() - 1;
DisplayLines[d].ShowLine = this->getList1().showLine();
DisplayLines[d].IconNumber = min(this->getList1().iconNumber(), ICON_COUNT + 2); // 0 = no icon, 1 = arrow, 2... icons from Icons.h
DisplayLines[d].TextBold = this->getList1().textBold();
DisplayLines[d].TextContent = this->getList1().textContent();
#if DISPLAY_COLORED == 1
DisplayLines[d].TextColored = this->getList1().textColored();
DisplayLines[d].IconColored = this->getList1().iconColored();
#endif
}
};
class RemoteType : public ChannelDevice<Hal, ConfigChannel, CHANNEL_COUNT, RCEPList0> {
public:
ConfigChannel cdata[CHANNEL_COUNT];
RefreshDisplayAlarm rda;
public:
typedef ChannelDevice<Hal, ConfigChannel, CHANNEL_COUNT, RCEPList0> DeviceType;
RemoteType (const DeviceInfo& info, uint16_t addr) : DeviceType(info, addr) ,rda() {
for (uint8_t i = 0; i < CHANNEL_COUNT; ++i) DeviceType::registerChannel(cdata[i], i + 1);
}
virtual ~RemoteType () {}
bool process(Message& msg) {
HMID devid;
this->getDeviceID(devid);
if (msg.to() == devid)
rda.set(millis2ticks(3000), sysclock);
return ChannelDevice::process(msg);
}
virtual void configChanged () {
uint8_t lowbat = getList0().lowBatLimit();
if( lowbat > 0 ) {
battery().low(lowbat);
}
bool inv = this->getList0().displayInvertingHb();
if (inv == true) {
DisplayDeviceConfig.clFG = GxEPD_WHITE;
DisplayDeviceConfig.clBG = GxEPD_BLACK;
} else {
DisplayDeviceConfig.clFG = GxEPD_BLACK;
DisplayDeviceConfig.clBG = GxEPD_WHITE;
}
if (first == false && DisplayDeviceConfig.Inverted != inv) mustRefreshDisplay = true;
DPRINT("DISP INV: "); DDECLN(this->getList0().displayInvertingHb());
DPRINT("LED MODE: "); DDECLN(this->getList0().ledMode());
DPRINT("LOWBAT : "); DDECLN(this->getList0().lowBatLimit());
DisplayDeviceConfig.Inverted = inv;
}
ConfigChannel& btnChannel (uint8_t c) {
return cdata[c - 1];
}
};
RemoteType sdev(devinfo, 0x20);
class ConfBtn : public ConfigButton<RemoteType> {
public:
RefreshDisplayAlarm rda;
ConfBtn (RemoteType& i) : ConfigButton(i), rda() {}
virtual ~ConfBtn () {}
virtual void state (uint8_t s) {
if( s == ButtonType::longreleased ) {
mustRefreshDisplay = true;
rda.set(millis2ticks(20), sysclock);
}
ConfigButton::state(s);
}
};
ConfBtn cfgBtn(sdev);
void setup() {
first = true;
initIcons();
DINIT(57600, ASKSIN_PLUS_PLUS_IDENTIFIER);
sdev.init(hal);
remoteChannelISR(sdev.btnChannel(1), BTN01_PIN);
remoteChannelISR(sdev.btnChannel(2), BTN02_PIN);
remoteChannelISR(sdev.btnChannel(3), BTN03_PIN);
remoteChannelISR(sdev.btnChannel(4), BTN04_PIN);
remoteChannelISR(sdev.btnChannel(5), BTN05_PIN);
remoteChannelISR(sdev.btnChannel(6), BTN06_PIN);
remoteChannelISR(sdev.btnChannel(7), BTN07_PIN);
remoteChannelISR(sdev.btnChannel(8), BTN08_PIN);
remoteChannelISR(sdev.btnChannel(9), BTN09_PIN);
remoteChannelISR(sdev.btnChannel(10), BTN10_PIN);
remoteChannelISR(sdev.btnChannel(11), BTN11_PIN);
remoteChannelISR(sdev.btnChannel(12), BTN12_PIN);
buttonISR(cfgBtn, CONFIG_BUTTON_PIN);
sdev.initDone();
display.init(57600);
first = false;
DPRINTLN("setup done.");
}
void loop() {
yield();
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if ( worked == false && poll == false ) {
if (hal.battery.critical()) {
hal.activity.sleepForever(hal);
}
hal.activity.savePower<Sleep<>>(hal);
}
}
uint16_t centerPosition(const char * text) {
return (display.width() / 2) - (u8g2Fonts.getUTF8Width(text) / 2);
}
void updateDisplay() {
u8g2Fonts.setFontMode(1);
u8g2Fonts.setBackgroundColor(DisplayDeviceConfig.clBG);
display.fillScreen(DisplayDeviceConfig.clBG);
u8g2Fonts.setFont(u8g2_font_helvB18_tf);
for (uint16_t i = 0; i < CHANNEL_COUNT; i++) {
//Text Font festlegen (B)old oder (R)egular
u8g2Fonts.setFont(DisplayLines[i].TextBold == true ? u8g2_font_helvB14_tf : u8g2_font_helvR14_tf );
//Text Farbe festlegen
#if DISPLAY_COLORED == 1
u8g2Fonts.setForegroundColor(DisplayLines[i].TextColored == true ? GxEPD_RED : DisplayDeviceConfig.clFG);
#else
u8g2Fonts.setForegroundColor(DisplayDeviceConfig.clFG);
#endif
//Text Zeichenersetzungen
String viewText = DisplayLines[i].TextContent;
viewText.trim();
viewText.replace("{", "ä");
viewText.replace("|", "ö");
viewText.replace("}", "ü");
viewText.replace("[", "Ä");
viewText.replace("#", "Ö");
viewText.replace("$", "Ü");
viewText.replace("~", "ß");
viewText.replace("'", "=");
if (DisplayLines[i].ShowLine == true)
display.drawLine(0, ((i / 2) + 1) * 49, display.width(), ((i / 2) + 1) * 49, DisplayDeviceConfig.clFG);
uint8_t icon_width = 0;
if (DisplayLines[i].IconNumber > 1) {
uint8_t icon_number = DisplayLines[i].IconNumber - 2;
icon_width = Icons[icon_number].width;
uint16_t icon_top = (Icons[icon_number].height - ( Icons[icon_number].height / 2)) + ((i / 2) * 49);
uint16_t icon_left = (i % 2 == 0) ? 0 : display.width() - Icons[icon_number].width;
#if DISPLAY_COLORED == 1
display.drawBitmap(Icons[icon_number].Icon, icon_left , icon_top, icon_width, Icons[icon_number].height, DisplayLines[i].IconColored == true ? GxEPD_RED : DisplayDeviceConfig.clFG, DisplayDeviceConfig.Inverted ? GxEPD::bm_normal : GxEPD::bm_default );
#else
display.drawBitmap(Icons[icon_number].Icon, icon_left , icon_top, icon_width, Icons[icon_number].height, DisplayDeviceConfig.clFG, DisplayDeviceConfig.Inverted ? GxEPD::bm_normal : GxEPD::bm_default );
#endif
} else {
if (DisplayLines[i].IconNumber == 1) {
uint16_t top = (i / 2) * 49;
if (i % 2 == 0) {
icon_width = u8g2Fonts.getUTF8Width("<");
u8g2Fonts.setCursor(0, top + 18); u8g2Fonts.print("<");
} else {
icon_width = u8g2Fonts.getUTF8Width(">");
u8g2Fonts.setCursor(display.width() - icon_width, top + 42); u8g2Fonts.print(">");
}
}
}
uint16_t leftTextPos = 0;
leftTextPos = centerPosition(viewText.c_str());
u8g2Fonts.setCursor(leftTextPos, ((i / 2) * 49) + ((i % 2 == 0) ? 21 : 43));
u8g2Fonts.print(viewText);
}
}