forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HM-WDS10-TH-O.ino
183 lines (149 loc) · 4.47 KB
/
HM-WDS10-TH-O.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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http:https://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#include <PinChangeInt.h>
#include <TimerOne.h>
#include <AskSinPP.h>
#include <Debug.h>
#include <Activity.h>
#include <Led.h>
#include <AlarmClock.h>
#include <MultiChannelDevice.h>
#include <Message.h>
#include <Button.h>
#include <Radio.h>
// define this to read the device id, serial and device type from bootloader section
// #define USE_OTA_BOOTLOADER
#ifdef USE_OTA_BOOTLOADER
#define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader
#define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader
#define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader
#else
// device ID
#define DEVICE_ID HMID(0x34,0x56,0x78)
// serial number
#define DEVICE_SERIAL "papa111111"
#endif
// 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
// number of available peers per channel
#define PEERS_PER_CHANNEL 6
// all library classes are placed in the namespace 'as'
using namespace as;
class WeatherEventMsg : public Message {
public:
void init(uint8_t msgcnt,int16_t temp,uint8_t humidity, bool batlow) {
uint8_t t1 = (temp >> 8) & 0x7f;
uint8_t t2 = temp & 0xff;
if( batlow == true ) {
t1 |= 0x80; // set bat low bit
}
Message::init(0xc,msgcnt,0x70,Message::BIDI,t1,t2);
pload[0] = humidity;
}
};
class WeatherChannel : public Channel<List1,EmptyList,List4,PEERS_PER_CHANNEL>, public Alarm {
WeatherEventMsg msg;
uint8_t msgcnt;
int16_t temp;
uint8_t humidity;
public:
WeatherChannel () : Channel(), Alarm(5), msgcnt(0), temp(0), humidity(50) {}
virtual ~WeatherChannel () {}
virtual void trigger (AlarmClock& clock) {
// reactivate for next measure
tick = delay();
clock.add(*this);
DPRINT("Measure...\n");
measure();
msg.init(msgcnt,temp,humidity,false);
device().sendPeerEvent(msg,*this);
}
// here we do the measurement
void measure () {
static int16_t tdx = -7;
static int8_t hdx = 1;
temp += tdx;
humidity += hdx;
if( temp >= 40*10 || temp <= -15*10 ) tdx = -tdx;
if( humidity == 99 || humidity == 5) hdx = -hdx;
}
// here we calc when to send next value
uint32_t delay () {
// for testing we use delay of 5sec
return seconds2ticks(5);
}
void setup(Device* dev,uint8_t number,uint16_t addr) {
Channel::setup(dev,number,addr);
aclock.add(*this);
}
uint8_t status () const {
return 0;
}
uint8_t flags () const {
return 0;
}
};
MultiChannelDevice<WeatherChannel,1> sdev(0x20);
class CfgButton : public Button {
public:
CfgButton () {
setLongPressTime(seconds2ticks(3));
}
virtual void state (uint8_t s) {
uint8_t old = Button::state();
Button::state(s);
if( s == Button::released ) {
sdev.startPairing();
}
else if( s == longpressed ) {
if( old == longpressed ) {
sdev.reset(); // long pressed again - reset
}
else {
sled.set(StatusLed::key_long);
}
}
}
};
CfgButton cfgBtn;
void cfgBtnISR () { cfgBtn.check(); }
void setup () {
#ifndef NDEBUG
Serial.begin(57600);
DPRINTLN(ASKSIN_PLUS_PLUS_IDENTIFIER);
#endif
sled.init(LED_PIN);
cfgBtn.init(CONFIG_BUTTON_PIN);
attachPinChangeInterrupt(CONFIG_BUTTON_PIN,cfgBtnISR,CHANGE);
radio.init();
if( eeprom.setup() == true ) {
sdev.firstinit();
}
#ifdef USE_OTA_BOOTLOADER
sdev.init(radio,OTA_HMID_START,OTA_SERIAL_START);
sdev.setModel(OTA_MODEL_START);
#else
sdev.init(radio,DEVICE_ID,DEVICE_SERIAL);
sdev.setModel(0x00,0x3d);
#endif
sdev.setFirmwareVersion(0x10);
sdev.setSubType(0x70);
sdev.setInfo(0x03,0x01,0x00);
radio.enableGDO0Int();
aclock.init();
sled.set(StatusLed::welcome);
}
void loop() {
bool worked = aclock.runready();
bool poll = sdev.pollRadio();
if( worked == false && poll == false ) {
activity.savePower<Sleep>();
}
}