13
13
#include " printf.h"
14
14
15
15
16
- /* * Globals and sketch configuration */
16
+ /* *** Globals and sketch configuration *** */
17
17
#define DEBUG 1 /* Enable trace statements */
18
18
#define SERIAL 1 /* Enable serial */
19
19
20
- #define MEASURE_PERIOD 600 // how often to measure, in tenths of seconds
21
- #define REPORT_EVERY 5 // report every N measurement cycles
22
- #define SMOOTH 5 // smoothing factor used for running averages
23
- #define MAXLENLINE 79
24
- #define SRAM_SIZE 0x800 // atmega328, for debugging
25
-
26
20
#define SHT11_PORT 0 // defined if SHT11 is connected to a port
27
21
#define LDR_PORT 0 // defined if LDR is connected to a port's AIO pin
28
22
#define PIR_PORT 0 // defined if PIR is connected to a port's DIO pin
29
-
30
23
#define _MEM 1 // Report free memory
31
24
#define _RFM12B 0
32
25
#define _RFM12BLOBAT 0
33
26
#define _NRF24 1
34
27
28
+ #define MEASURE_PERIOD 600 // how often to measure, in tenths of seconds
29
+ #define REPORT_EVERY 5 // report every N measurement cycles
30
+ #define SMOOTH 5 // smoothing factor used for running averages
31
+
32
+ #define MAXLENLINE 79
33
+ #if defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny85__)
34
+ #define SRAM_SIZE 512
35
+ #define EEPROM_SIZE 512
36
+ #elif defined(__AVR_ATmega168__)
37
+ #define SRAM_SIZE 1024
38
+ #define EEPROM_SIZE 512
39
+ #elif defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
40
+ #define SRAM_SIZE 2048
41
+ // #define SRAM_SIZE 0x800 // atmega328, for debugging
42
+ #define EEPROM_SIZE 1024
43
+ #endif
44
+
35
45
36
- String sketch = " RF24Test" ;
37
- String version = " 0 " ;
46
+ const String sketch = " RF24Test" ;
47
+ const int version = 0 ;
38
48
39
49
String node_id = " rf24tst-1" ;
40
50
@@ -51,9 +61,12 @@ const byte rf24_csn = 8;
51
61
MpeSerial mpeser (57600 );
52
62
53
63
54
- // The scheduler makes it easy to perform various tasks at various times:
55
- enum { MEASURE, REPORT, STDBY };
56
-
64
+ /* Scheduled tasks */
65
+ enum {
66
+ MEASURE,
67
+ REPORT,
68
+ STDBY
69
+ };
57
70
// Scheduler.pollWaiting returns -1 or -2
58
71
static const char WAITING = 0xFF ; // -1: waiting to run
59
72
static const char IDLE = 0xFE ; // -2: no tasks running
@@ -64,6 +77,11 @@ Scheduler scheduler (schedbuf, STDBY);
64
77
// has to be defined because we're using the watchdog for low-power waiting
65
78
ISR (WDT_vect) { Sleepy::watchdogEvent (); }
66
79
80
+
81
+ /* *** EEPROM config *** {{{ */
82
+
83
+ /* }}} *** */
84
+
67
85
// Other variables used in various places in the code:
68
86
#if SHT11_PORT
69
87
SHT11 sht11 (SHT11_PORT);
@@ -72,7 +90,17 @@ SHT11 sht11 (SHT11_PORT);
72
90
#if LDR_PORT
73
91
Port ldr (LDR_PORT);
74
92
#endif
93
+ #if _DHT
94
+ /* DHT temp/rh sensor
95
+ - AdafruitDHT
96
+ */
75
97
98
+ #endif // _DHT
99
+
100
+ #if _DS
101
+ /* Dallas OneWire bus with registration for DS18B20 temperature sensors */
102
+
103
+ #endif // _DS
76
104
#if _NRF24
77
105
/* nRF24L01+: nordic 2.4Ghz digital radio */
78
106
@@ -84,6 +112,7 @@ const uint64_t pipes[2] = {
84
112
0xF0F0F0F0E1LL , /* dest id: central link node */
85
113
0xF0F0F0F0D2LL /* src id: local node */
86
114
};
115
+
87
116
#endif // _NRF24
88
117
89
118
@@ -104,8 +133,7 @@ struct {
104
133
#endif
105
134
} payload;
106
135
107
-
108
- /* * AVR routines */
136
+ /* *** AVR routines *** {{{ */
109
137
110
138
int freeRam () {
111
139
extern int __heap_start, *__brkval;
@@ -117,8 +145,9 @@ int usedRam () {
117
145
return SRAM_SIZE - freeRam ();
118
146
}
119
147
148
+ /* }}} *** */
120
149
121
- /* * ATmega routines */
150
+ /* *** ATmega routines *** {{{ */
122
151
123
152
double internalTemp (void )
124
153
{
@@ -151,8 +180,9 @@ double internalTemp(void)
151
180
return (t);
152
181
}
153
182
183
+ /* }}} *** */
154
184
155
- /* * Generic routines */
185
+ /* *** Generic routines *** {{{ */
156
186
157
187
static void serialFlush () {
158
188
#if SERIAL
@@ -175,7 +205,7 @@ void blink(int led, int count, int length, int length_off=0) {
175
205
176
206
void debug_ticks (void )
177
207
{
178
- #if DEBUG
208
+ #if SERIAL && DEBUG
179
209
tick++;
180
210
// a bit less for non-waiting loops or always..?
181
211
if ((tick % 20 ) == 0 ) {
@@ -190,6 +220,7 @@ void debug_ticks(void)
190
220
pos = 0 ;
191
221
Serial.println ();
192
222
}
223
+ serialFlush ();
193
224
#endif
194
225
#endif
195
226
}
@@ -201,12 +232,15 @@ static int smoothedAverage(int prev, int next, byte firstTime =0) {
201
232
return ((SMOOTH - 1 ) * prev + next + SMOOTH / 2 ) / SMOOTH;
202
233
}
203
234
204
- void debug (String msg) {
235
+ void debugline (String msg) {
205
236
#if DEBUG
206
237
Serial.println (msg);
207
238
#endif
208
239
}
209
240
241
+ /* }}} *** */
242
+
243
+ /* *** Peripheral hardware routines *** {{{ */
210
244
211
245
#if PIR_PORT
212
246
@@ -265,8 +299,9 @@ void rf24_run()
265
299
}
266
300
#endif // _NRF24
267
301
302
+ /* }}} *** */
268
303
269
- /* Initialization routines */
304
+ /* *** Initialization routines *** {{{ */
270
305
271
306
void doConfig (void )
272
307
{
@@ -283,8 +318,9 @@ void initLibs()
283
318
#endif
284
319
}
285
320
321
+ /* }}} *** */
286
322
287
- /* Run-time handlers */
323
+ /* *** Run-time handlers *** {{{ */
288
324
289
325
bool doAnnounce ()
290
326
{
@@ -298,6 +334,7 @@ bool doAnnounce()
298
334
Serial.print (F (" memfree" ));
299
335
#endif
300
336
#endif
337
+ return false ;
301
338
}
302
339
303
340
void doReset (void )
@@ -375,14 +412,13 @@ bool doReport(void)
375
412
return ok;
376
413
}
377
414
378
- void runScheduler (char task)
415
+ void runScheduler (byte task)
379
416
{
380
417
switch (task) {
381
418
382
419
case MEASURE:
383
420
// reschedule these measurements periodically
384
- debug (" MEASURE" );
385
- scheduler.timer (MEASURE, MEASURE_PERIOD);
421
+ debugline (" MEASURE" );
386
422
387
423
doMeasure ();
388
424
@@ -395,7 +431,7 @@ void runScheduler(char task)
395
431
break ;
396
432
397
433
case REPORT:
398
- debug (" REPORT" );
434
+ debugline (" REPORT" );
399
435
// payload.msgtype = REPORT_MSG;
400
436
if (doReport ()) {
401
437
// XXX report again?
@@ -415,7 +451,6 @@ void runScheduler(char task)
415
451
}
416
452
}
417
453
418
-
419
454
#if PIR_PORT
420
455
421
456
// send packet and wait for ack when there is a motion trigger
@@ -426,20 +461,21 @@ void doTrigger()
426
461
427
462
428
463
429
- /* Main */
464
+ /* }}} *** */
465
+
466
+ /* *** Main *** {{{ */
430
467
431
468
void setup (void )
432
469
{
433
470
#if SERIAL
434
471
mpeser.begin ();
435
- mpeser.startAnnounce (sketch, version);
472
+ mpeser.startAnnounce (sketch, String ( version) );
436
473
doAnnounce ();
437
- serialFlush ();
438
-
439
- #if DEBUG
440
- Serial.print (F (" SRAM used: " ));
441
- Serial.println (usedRam ());
474
+ #if DEBUG || _MEM
475
+ Serial.print (F (" Free RAM: " ));
476
+ Serial.println (freeRam ());
442
477
#endif
478
+ serialFlush ();
443
479
#endif
444
480
445
481
initLibs ();
@@ -450,10 +486,12 @@ void setup(void)
450
486
void loop (void )
451
487
{
452
488
debug_ticks ();
453
-
454
- char task = scheduler.pollWaiting ();
455
489
serialFlush ();
456
- if (task == 0xFF ) return ; // -1
457
- if (task == 0xFE ) return ; // -2
458
- runScheduler (task);
490
+ byte task = scheduler.pollWaiting ();
491
+ if (task == 0xFF ) {} // -1
492
+ else if (task == 0xFE ) {} // -2
493
+ else runScheduler (task);
459
494
}
495
+
496
+ /* }}} *** */
497
+
0 commit comments