/
HM-LC-Bl1-FM.ino
179 lines (151 loc) · 4.73 KB
/
HM-LC-Bl1-FM.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2017-12-14 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 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
// define L298N when using a H-Bridge motor driver
// #define L298N
#ifdef L298N
// define up and down pins for L298N driver
#define UP_PIN 16
#define DOWN_PIN 17
#else
// define relay pins
#define ON_RELAY_PIN 15
#define DIR_RELAY_PIN 14
#endif
#define UP_BUTTON_PIN 6
#define DOWN_BUTTON_PIN 3
// 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,0xaf}, // Device ID
"papa5932af", // Device Serial
{0x00,0x05}, // Device Model
0x24, // Firmware Version
as::DeviceType::BlindActuator, // Device Type
{0x01,0x00} // Info Bytes
};
/**
* Configure the used hardware
*/
typedef AvrSPI<10,11,12,13> RadioSPI;
typedef AskSin<StatusLed<LED_PIN>,NoBattery,Radio<RadioSPI,2> > Hal;
//typedef AskSin<StatusLed<4>,NoBattery,NoRadio> 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);
}
};
class BlChannel : public ActorChannel<Hal,BlindList1,BlindList3,PEERS_PER_CHANNEL,BlindList0,BlindStateMachine> {
public:
typedef ActorChannel<Hal,BlindList1,BlindList3,PEERS_PER_CHANNEL,BlindList0,BlindStateMachine> BaseChannel;
BlChannel () {}
virtual ~BlChannel () {}
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 {
motorStop();
}
}
void motorUp () {
#ifdef L298N
digitalWrite(DOWN_PIN,LOW);
digitalWrite(UP_PIN,HIGH);
#else
digitalWrite(DIR_RELAY_PIN,HIGH);
digitalWrite(ON_RELAY_PIN,HIGH);
#endif
}
void motorDown () {
#ifdef L298N
digitalWrite(UP_PIN,LOW);
digitalWrite(DOWN_PIN,HIGH);
#else
digitalWrite(DIR_RELAY_PIN,LOW);
digitalWrite(ON_RELAY_PIN,HIGH);
#endif
}
void motorStop () {
#ifdef L298N
digitalWrite(UP_PIN,LOW);
digitalWrite(DOWN_PIN,LOW);
#else
digitalWrite(DIR_RELAY_PIN,LOW);
digitalWrite(ON_RELAY_PIN,LOW);
#endif
}
void init () {
#ifdef L298N
pinMode(UP_PIN,OUTPUT);
pinMode(DOWN_PIN,OUTPUT);
#else
pinMode(ON_RELAY_PIN,OUTPUT);
pinMode(DIR_RELAY_PIN,OUTPUT);
#endif
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);
InternalButton<BlindType> btnup(sdev,1);
InternalButton<BlindType> btndown(sdev,2);
void initPeerings (bool first) {
// create internal peerings - CCU2 needs this
if( first == true ) {
sdev.channel(1).peer(btnup.peer(),btndown.peer());
}
}
void setup () {
DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER);
//storage().setByte(0,0);
bool first = sdev.init(hal);
sdev.channel(1).init();
// sdev.channel(1).getList1().refRunningTimeBottomTop(270);
// sdev.channel(1).getList1().refRunningTimeTopBottom(270);
buttonISR(cfgBtn,CONFIG_BUTTON_PIN);
buttonISR(btnup,UP_BUTTON_PIN);
buttonISR(btndown,DOWN_BUTTON_PIN);
initPeerings(first);
sdev.initDone();
}
void loop() {
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if( worked == false && poll == false ) {
hal.activity.savePower<Idle<> >(hal);
}
}