-
Notifications
You must be signed in to change notification settings - Fork 2
/
Domotica.hh
319 lines (278 loc) · 8.42 KB
/
Domotica.hh
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
/**
* @file Domotica.hh
* @version 1.0
*
* @section License
* Copyright (C) 2015, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Arduino Che Cosa project.
*/
#ifndef COSA_DOMOTICA_HH
#define COSA_DOMOTICA_HH
#include "Cosa/Types.h"
#include "Cosa/Time.hh"
#include "Cosa/IOStream.hh"
#include "Cosa/Wireless.hh"
#include "Cosa/AnalogPin.hh"
#include "Cosa/ExternalInterrupt.hh"
/**
* Default Domotica network identity. The full node address is
* [NETWORK].[DEVICE]. A sensor address is [NETWORK].[DEVICE].[PORT].[ID].
* Typically the NETWORK is fixed. User interfaces will need to map
* [DEVICE].[PORT].[ID] to a human readable form.
*/
#ifndef NETWORK
#define NETWORK 0xD1CA
#endif
namespace Domotica {
/**
* Start Domotica with given wireless device. Returns true(1) if
* successful otherwise false(0).
* @param[in] dev device.
* @param[in] config configuration (default NULL).
* @return bool.
*/
bool begin(Wireless::Driver* rf, void* config = NULL);
/**
* Sleep in power down mode for given number of seconds.
* @param[in] s seconds (Default 5 seconds).
*/
void sleep(uint16_t s = 5);
/**
* Await external interrupt pin. Sleep in power down mode until the
* pin is low.
* @param[in] pin external interrupt pin (default EXT0).
* @param[in] mode level change (default ON_LOW_LEVEL_MODE).
*/
void await(Board::ExternalInterruptPin pin = Board::EXT0,
ExternalInterrupt::InterruptMode mode = ExternalInterrupt::ON_LOW_LEVEL_MODE);
/**
* Print local address of sensor; DEVICE.ID.
* @param[in] outs output stream.
* @param[in] device address.
* @param[in] port sensor type.
* @param[in] id identity.
*/
void print(IOStream& outs, uint8_t device, uint8_t port, uint8_t id);
/**
* Print full address of sensor; NETWORK.DEVICE.ID.
* @param[in] outs output stream.
* @param[in] network address.
* @param[in] device address.
* @param[in] port sensor type.
* @param[in] id identity.
*/
void print(IOStream& outs, uint16_t network, uint8_t device, uint8_t port, uint8_t id);
/**
* Domotic device message header.
*/
struct header_t {
/**
* Set local source identity, sequence number and sample battery
* level.
* @param[in] nr message sequence number.
* @param[in] id source identity.
*/
void set(uint8_t &nr, uint8_t id)
{
this->id = id;
this->nr = nr++;
this->battery = AnalogPin::bandgap();
}
/** Local sensor/device identity. */
uint8_t id;
/** Message sequence number. */
uint8_t nr;
/** Battery level (mV). */
uint16_t battery;
};
/** Message types. */
enum {
INFO_STRING_MSG = 0,
DIGITAL_PIN_MSG = 1,
ANALOG_PIN_MSG = 2,
DIGITAL_PINS_MSG = 3,
THERMOMETER_MSG = 4,
HUMIDITY_TEMPERATURE_SENSOR_MSG = 5,
REALTIME_CLOCK_MSG = 6,
ACCELEROMETER_MSG = 7
};
/** Message payload size (26 bytes). */
static const size_t PAYLOAD_MAX = 30 - sizeof(header_t);
/** Payload Message (26 bytes max). */
struct msg_t : header_t {
uint8_t payload[PAYLOAD_MAX];
};
/**
* Print message from given port.
* @param[in] outs output stream.
* @param[in] port sensor type.
* @param[in] msg message.
*/
void print(IOStream& outs, uint8_t port, msg_t* msg);
/**
* Print message from given source sensor address prefix.
* @param[in] outs output stream.
* @param[in] src source device.
* @param[in] port sensor type.
* @param[in] msg message.
*/
void print(IOStream& outs, uint8_t src, uint8_t port, Domotica::msg_t* msg);
/** Information String Message (26 bytes max). */
namespace InfoString {
static const size_t MAX = PAYLOAD_MAX;
struct msg_t : header_t {
char info[MAX]; // Null terminated string.
};
};
/** Digital Pin Sample Message (5 bytes). */
namespace DigitalPin {
struct msg_t : header_t {
bool value; // Sample value; on(1)/off(0).
};
};
/** Digital Pins Sample Message (8 bytes). */
namespace DigitalPins {
static const uint8_t MAX = 32;
struct msg_t : header_t {
uint32_t value; // Sample value; bit[0..ID-1]
};
};
/** Analog Pin Sample Message (6 bytes). */
namespace AnalogPin {
struct msg_t : header_t {
uint16_t value; // Sample value (0..1023).
};
};
/** Digital Thermometer Message (8 bytes). */
namespace Thermometer {
struct msg_t : header_t {
float32_t temperature; // Temperature C.
};
};
/** Digital Humidity and Temperature Sensor Message (12 bytes). */
namespace HumidityTemperatureSensor {
struct msg_t : header_t {
float32_t humidity; // Humidity RH%.
float32_t temperature; // Temperature C.
};
};
/** Real-Time Clock Message (8 bytes). */
namespace RealTimeClock {
struct msg_t : header_t {
clock_t time; // Seconds since epoch.
};
};
/** Accelerometer Message (17 bytes). */
namespace Accelerometer {
enum { //!< Source
DATA_READY = 7, //!< Data ready interrupt enable/map/source.
SINGLE_TAP = 6, //!< Single tap.
DOUBLE_TAP = 5, //!< Double tap.
ACT = 4, //!< Activity.
INACT = 3, //!< Inactivity.
FREE_FALL = 2, //!< Free fall.
WATERMARK = 1, //!< Watermark.
OVERRUN = 0 //!< Overrun data.
} __attribute__((packed));
struct msg_t : header_t {
uint8_t source;
float32_t x;
float32_t y;
float32_t z;
};
};
class InterruptPin : public ExternalInterrupt {
public:
InterruptPin(Board::ExternalInterruptPin pin, InterruptMode mode) :
ExternalInterrupt(pin, mode, true)
{}
virtual void on_interrupt(uint16_t arg)
{
UNUSED(arg);
disable();
}
};
};
/**
* Print header to given output stream.
* @param[in] outs output stream.
* @param[in] header to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::header_t* header);
/**
* Print message payload in hex format to given output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::msg_t* msg);
/**
* Print information message to given output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::InfoString::msg_t* msg);
/**
* Print digital pin sample message to given output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::DigitalPin::msg_t* msg);
/**
* Print digital pins sample message to given output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::DigitalPins::msg_t* msg);
/**
* Print analog pin sample message to given output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::AnalogPin::msg_t* msg);
/**
* Print thermometer sample message to given output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::Thermometer::msg_t* msg);
/**
* Print humidity and temperature sensor sample message to given
* output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::HumidityTemperatureSensor::msg_t* msg);
/**
* Print real-time clock message to given output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::RealTimeClock::msg_t* msg);
/**
* Print accelerometer message to given output stream.
* @param[in] outs output stream.
* @param[in] msg to print.
* @return output stream.
*/
IOStream& operator<<(IOStream& outs, Domotica::Accelerometer::msg_t* msg);
#endif