-
Notifications
You must be signed in to change notification settings - Fork 71
/
HB-LC-Bl1-Velux.ino
270 lines (235 loc) · 7.24 KB
/
HB-LC-Bl1-Velux.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2020-05-02 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
// ci-test=yes board=328p aes=no
//- -----------------------------------------------------------------------------------------------------------------------
// define this to read the device id, serial and device type from bootloader section
// #define USE_OTA_BOOTLOADER
#define USE_WOR // set this if the KLI battery is used for power
#define EI_NOTEXTERNAL
#include <EnableInterrupt.h>
#include <AskSinPP.h>
#include <LowPower.h>
#include <Blind.h>
// we use a Pro Mini
// Arduino pin for the LED
// D4 == PIN 4 on Pro Mini
#define LED_PIN 4
// Arduino pin for the config button
// B0 == PIN 8 on Pro Mini
#define CONFIG_BUTTON_PIN 8
#ifdef USE_WOR
// pins used on 328Stamp
#define UP_PIN 5
#define STOP_PIN 6
#define DOWN_PIN 7
#else
// pins used on HMSensor
#define UP_PIN 19
#define STOP_PIN 3
#define DOWN_PIN 6
#endif
// number of available peers per channel
#define PEERS_PER_CHANNEL 12
// all library classes are placed in the namespace 'as'
using namespace as;
// define all device properties
const struct DeviceInfo PROGMEM devinfo = {
{0x59,0x32,0xee}, // Device ID
"papa5932ee", // Device Serial
#ifdef USE_WOR
{0xf2,0x0a}, // HB-LC-Bl1-Velux
#else
{0x00,0x05}, // HM-LC-Bl1-FM
#endif
0x24, // Firmware Version
as::DeviceType::BlindActuator, // Device Type
{0x01,0x00} // Info Bytes
};
/**
* Configure the used hardware
*/
typedef AvrSPI<10,11,12,13> RadioSPI;
#ifdef USE_WOR
typedef IrqInternalBatt BattSensType;
#else
typedef NoBattery BattSensType;
#endif
typedef AskSin<StatusLed<LED_PIN>,BattSensType,Radio<RadioSPI,2> > Hal;
DEFREGISTER(BlindReg0,MASTERID_REGS,DREG_INTKEY,DREG_CONFBUTTONTIME,DREG_LOCALRESETDISABLE)
class BlindList0 : public RegList0<BlindReg0> {
public:
BlindList0 (uint16_t addr) : RegList0<BlindReg0>(addr) {}
void defaults () {
clear();
// intKeyVisible(false);
confButtonTime(0xff);
// localResetDisable(false);
}
};
// we use this interface to press the buttons of the velux remote
// the methods will be implemented by the button class later
class PushPin : public Alarm {
public:
PushPin () { async(true); }
virtual ~PushPin () {}
virtual void press () {};
virtual void unpress () {}
virtual void trigger (__attribute__((unused)) AlarmClock& clock) {
unpress();
}
};
class BlChannel : public ActorChannel<Hal,BlindList1,BlindList3,PEERS_PER_CHANNEL,BlindList0,BlindStateMachine> {
bool m_Drive;
PushPin* m_PinUp;
PushPin* m_PinDown;
PushPin* m_PinStop;
public:
typedef ActorChannel<Hal,BlindList1,BlindList3,PEERS_PER_CHANNEL,BlindList0,BlindStateMachine> BaseChannel;
BlChannel () : m_Drive(false), m_PinUp(0), m_PinDown(0), m_PinStop(0) {}
virtual ~BlChannel () {}
void init (PushPin& up, PushPin& down, PushPin& stop) {
BaseChannel::init();
m_PinUp = &up;
m_PinDown = &down;
m_PinStop = &stop;
}
virtual void switchState(uint8_t oldstate,uint8_t newstate, uint32_t stateDelay) {
BaseChannel::switchState(oldstate, newstate, stateDelay);
if( newstate == AS_CM_JT_RAMPON && stateDelay > 0 ) {
motorUp();
}
else if( newstate == AS_CM_JT_RAMPOFF && stateDelay > 0 ) {
motorDown();
}
else if( m_Drive==true ){
motorStop();
DPRINT("Bat: ");DDECLN(device().battery().current());
}
}
void motorUp () {
m_PinUp->press();
m_Drive=true;
}
void motorDown () {
m_PinDown->press();
m_Drive=true;
}
void motorStop () {
// we need no stop for end positions
if( status() != 0 && status() != 200 ) {
m_PinStop->press();
}
m_Drive=false;
}
void init () {
motorStop();
BaseChannel::init();
}
};
// setup the device with channel type and number of channels
typedef MultiChannelDevice<Hal,BlChannel,1,BlindList0> BlindType;
Hal hal;
BlindType sdev(devinfo,0x20);
ConfigButton<BlindType> cfgBtn(sdev);
// this class handles a single button of the velux remote
class VeluxBtn : public StateButton<>, public PushPin {
public:
enum Mode {UP, DOWN, STOP};
private:
Mode m_Mode;
volatile bool m_IgnorePress;
public:
VeluxBtn (Mode m) : m_Mode(m), m_IgnorePress(false) {}
virtual ~VeluxBtn () {}
virtual void state(uint8_t s) {
uint8_t last = StateButton<>::state();
StateButton<>::state(s);
if( s == StateButton<>::released || (s == StateButton<>::longpressed && last != StateButton<>::longpressed) ) {
DPRINT("SETIGNORE: ");DDECLN(getPin());
m_IgnorePress = true;
switch( m_Mode ) {
case UP: sdev.channel(1).set(200,0,0); break;
case DOWN: sdev.channel(1).set(0,0,0); break;
case STOP: sdev.channel(1).stop(); break;
}
}
else if( s == StateButton<>::longreleased ) {
sdev.channel(1).stop();
}
}
void irq () {
// irq is only handled if the button is not pressed by software / PushPin
if( PushPin::active() == false ) {
StateButton<>::irq();
}
}
virtual void unpress () {
DPRINT("Unpress ");DDECLN(getPin());
// back to high
digitalWrite(getPin(),HIGH);
// setup for input again
StateButton<>::init(getPin());
}
virtual void press () {
if( getPin() != 0 && m_IgnorePress == false) {
DPRINT("Press ");DDECLN(getPin());
// start timer
PushPin::set(millis2ticks(250));
sysclock.add((PushPin&)*this);
// set mode to output
pinMode(getPin(),OUTPUT);
// set pin to low
digitalWrite(getPin(),LOW);
}
else {
DPRINT("Ignore ");DDECLN(getPin());
m_IgnorePress = false;
}
}
};
VeluxBtn upbtn(VeluxBtn::UP);
VeluxBtn downbtn(VeluxBtn::DOWN);
VeluxBtn stopbtn(VeluxBtn::STOP);
void initPeerings (bool first) {
// create internal peerings - CCU2 needs this
if( first == true ) {
}
}
void setup () {
DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER);
//storage().setByte(0,0);
bool first = sdev.init(hal);
sdev.channel(1).init(upbtn,downbtn,stopbtn);
#ifdef USE_WOR
hal.led.invert(true);
hal.battery.init(seconds2ticks(60UL*60),sysclock);
hal.battery.low(22);
hal.battery.critical(19);
#endif
buttonISR(cfgBtn,CONFIG_BUTTON_PIN);
buttonISR(upbtn,UP_PIN);
buttonISR(stopbtn,STOP_PIN);
buttonISR(downbtn,DOWN_PIN);
initPeerings(first);
sdev.initDone();
// wait for valid battery value
while( hal.battery.current() == 0 ) ;
}
void loop() {
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if( worked == false && poll == false ) {
// deep discharge protection
// if we drop below critical battery level - switch off all and sleep forever
if( hal.battery.critical() ) {
// this call will never return
hal.sleepForever();
}
#ifdef USE_WOR
hal.activity.savePower<Sleep<> >(hal);
#else
hal.activity.savePower<Idle<> >(hal);
#endif
}
}