/
_wip_HM-MOD-EM-8Bit.ino
292 lines (241 loc) · 8.4 KB
/
_wip_HM-MOD-EM-8Bit.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
// define this to read the device id, serial and device type from bootloader section
// #define USE_OTA_BOOTLOADER
#define EI_NOTEXTERNAL
#include <EnableInterrupt.h>
#include <AskSinPP.h>
#include <LowPower.h>
#include <Switch.h>
#include <MultiChannelDevice.h>
#define LED_PIN 4
#define CONFIG_BUTTON_PIN 8
#define BTN1_PIN 3
#define BTN2_PIN 5
#define EM8_PIN0 6
#define EM8_PIN1 7
#define EM8_PIN2 9
#define EM8_PIN3 14
#define EM8_PIN4 15
#define EM8_PIN5 16
#define EM8_PIN6 17
#define EM8_PIN7 18
// number of available peers per channel
#define PEERS_PER_RemoteChannel 4
#define PEERS_PER_EM8CHANNEL 4
#define remISR(device,chan,pin) class device##chan##ISRHandler { \
public: \
static void isr () { device.remoteChannel(chan).irq(); } \
}; \
device.remoteChannel(chan).button().init(pin); \
if( digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT ) \
enableInterrupt(pin,device##chan##ISRHandler::isr,CHANGE); \
else \
attachInterrupt(digitalPinToInterrupt(pin),device##chan##ISRHandler::isr,CHANGE);
#define em8ISR(device,pin) class device##pin##ISR8Handler { \
public: \
static void isr () { device.em8bitChannel().irq(); } \
}; \
device.em8bitChannel().button().init(pin); \
if( digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT ) \
enableInterrupt(pin,device##pin##ISR8Handler::isr,CHANGE); \
else \
attachInterrupt(digitalPinToInterrupt(pin),device##pin##ISR8Handler::isr,CHANGE);
// all library classes are placed in the namespace 'as'
using namespace as;
// define all device properties
const struct DeviceInfo PROGMEM devinfo = {
{0xf3, 0x32, 0x01}, // Device ID
"JPEM8b0001", // Device Serial
{0x01, 0x06}, // Device Model
0x10, // Firmware Version
as::DeviceType::Switch, // Device Type
{0x00, 0x00} // Info Bytes
};
/**
Configure the used hardware
*/
typedef AvrSPI<10, 11, 12, 13> SPIType;
typedef Radio<SPIType, 2> RadioType;
typedef StatusLed<LED_PIN> LedType;
typedef AskSin<LedType, BatterySensor, RadioType> BaseHal;
class Hal : public BaseHal {
public:
void init (const HMID& id) {
BaseHal::init(id);
// measure battery every 1h
battery.init(seconds2ticks(60UL * 60), sysclock);
battery.low(22);
battery.critical(19);
}
bool runready () {
return sysclock.runready() || BaseHal::runready();
}
} hal;
DEFREGISTER(Reg0, MASTERID_REGS, DREG_LEDMODE, DREG_TRANSMITTRYMAX, DREG_LOCALRESETDISABLE, DREG_LOWBATLIMIT)
class SwList0 : public RegList0<Reg0> {
public:
SwList0(uint16_t addr) : RegList0<Reg0>(addr) {}
void defaults() {
clear();
intKeyVisible(true);
cycleInfoMsg(true);
}
};
DEFREGISTER(RemoteReg1, CREG_LONGPRESSTIME, CREG_AES_ACTIVE, CREG_DOUBLEPRESSTIME)
class RemoteList1 : public RegList1<RemoteReg1> {
public:
RemoteList1 (uint16_t addr) : RegList1<RemoteReg1>(addr) {}
void defaults () {
clear();
longPressTime(1);
// aesActive(false);
// doublePressTime(0);
}
};
class RemoteChannel : public Channel<Hal, RemoteList1, EmptyList, DefList4, PEERS_PER_RemoteChannel, SwList0>, public Button {
private:
uint8_t repeatcnt;
public:
typedef Channel<Hal, RemoteList1, EmptyList, DefList4, PEERS_PER_RemoteChannel, SwList0> BaseChannel;
RemoteChannel () : BaseChannel() {}
virtual ~RemoteChannel () {}
Button& button () {
return *(Button*)this;
}
uint8_t status () const {
return 0;
}
uint8_t flags () const {
return 0;
}
virtual void state(uint8_t s) {
DHEX(BaseChannel::number());
Button::state(s);
RemoteEventMsg& msg = (RemoteEventMsg&)this->device().message();
msg.init(this->device().nextcount(), this->number(), repeatcnt, (s == longreleased || s == longpressed), this->device().battery().low());
if ( s == released || s == longreleased) {
// send the message to every peer
this->device().sendPeerEvent(msg, *this);
repeatcnt++;
}
else if (s == longpressed) {
// broadcast the message
this->device().broadcastPeerEvent(msg, *this);
}
}
uint8_t state() const {
return Button::state();
}
bool pressed () const {
uint8_t s = state();
return s == Button::pressed || s == Button::debounce || s == Button::longpressed;
}
};
DEFREGISTER(Em8Reg1, CREG_TRANSMITTRYMAX, CREG_DATA_TRANSMISSION_CONDITION, CREG_DATA_STABILITY_FILTER_TIME, CREG_DATA_INPUT_PROPERTIES)
class Em8List1 : public RegList1<Em8Reg1> {
public:
Em8List1 (uint16_t addr) : RegList1<Em8Reg1>(addr) {}
void defaults () {
clear();
}
};
class Em8EventMsg : public Message {
public:
void init(uint8_t msgcnt, uint8_t val, uint8_t ch) {
Message::init(0xc, msgcnt, 0x41, BIDI, ch, msgcnt);
pload[0] = val;
}
};
class EM8Channel : public Channel<Hal, Em8List1, EmptyList, List4, PEERS_PER_EM8CHANNEL, SwList0>, public Button {
Em8EventMsg msg;
uint16_t millis;
public:
EM8Channel () : Button() {}
virtual ~EM8Channel () {}
Button& button () {
return *(Button*)this;
}
virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
tick = delay();
clock.add(*this);
msg.init(device().nextcount(), 16, number());
device().sendPeerEvent(msg, *this);
}
uint32_t delay () {
return seconds2ticks(180);
}
void setup(Device<Hal, SwList0>* dev, uint8_t number, uint16_t addr) {
Channel::setup(dev, number, addr);
sysclock.add(*this);
}
uint8_t status () const {
return 0;
}
void irq() {
}
void configChanged() {
DPRINTLN(F("*List1 changed"));
DPRINT(F("*dataTransmissionCondtion ")); DDECLN(this->getList1().dataTransmissionCondtion());
DPRINT(F("*dataStabilityFilterTime ")); DDECLN(this->getList1().dataStabilityFilterTime());
DPRINT(F("*dataInputPropertieIn0 ")); DDECLN(this->getList1().dataInputPropertieIn0());
DPRINT(F("*dataInputPropertieIn1 ")); DDECLN(this->getList1().dataInputPropertieIn1());
DPRINT(F("*dataInputPropertieIn2 ")); DDECLN(this->getList1().dataInputPropertieIn2());
DPRINT(F("*dataInputPropertieIn3 ")); DDECLN(this->getList1().dataInputPropertieIn3());
DPRINT(F("*dataInputPropertieIn4 ")); DDECLN(this->getList1().dataInputPropertieIn4());
DPRINT(F("*dataInputPropertieIn5 ")); DDECLN(this->getList1().dataInputPropertieIn5());
DPRINT(F("*dataInputPropertieIn6 ")); DDECLN(this->getList1().dataInputPropertieIn6());
DPRINT(F("*dataInputPropertieIn7 ")); DDECLN(this->getList1().dataInputPropertieIn7());
}
uint8_t flags () const {
return 0;
}
};
class MixDevice : public ChannelDevice<Hal, VirtBaseChannel<Hal, SwList0>, 3, SwList0> {
public:
VirtChannel<Hal, RemoteChannel, SwList0> remChannel[2];
VirtChannel<Hal, EM8Channel, SwList0> em8Channel;
public:
typedef ChannelDevice<Hal, VirtBaseChannel<Hal, SwList0>, 3, SwList0> DeviceType;
MixDevice (const DeviceInfo& info, uint16_t addr) : DeviceType(info, addr) {
DeviceType::registerChannel(remChannel[0], 1);
DeviceType::registerChannel(remChannel[1], 2);
DeviceType::registerChannel(em8Channel, 3);
}
virtual ~MixDevice () {}
RemoteChannel& remoteChannel (uint8_t num) {
return remChannel[num - 1];
}
EM8Channel& em8bitChannel () {
return em8Channel;
}
virtual void configChanged () {
}
};
MixDevice sdev(devinfo, 0x20);
ConfigButton<MixDevice> cfgBtn(sdev);
void setup () {
DINIT(57600, ASKSIN_PLUS_PLUS_IDENTIFIER);
sdev.init(hal);
remISR(sdev, 1, BTN1_PIN);
remISR(sdev, 2, BTN2_PIN);
em8ISR(sdev, EM8_PIN0);
em8ISR(sdev, EM8_PIN1);
em8ISR(sdev,EM8_PIN2);
em8ISR(sdev,EM8_PIN3);
em8ISR(sdev,EM8_PIN4);
em8ISR(sdev,EM8_PIN5);
em8ISR(sdev,EM8_PIN6);
em8ISR(sdev,EM8_PIN7);
buttonISR(cfgBtn, CONFIG_BUTTON_PIN);
sdev.initDone();
}
void loop() {
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if ( worked == false && poll == false ) {
hal.activity.savePower<Sleep<>>(hal);
}
}