-
Notifications
You must be signed in to change notification settings - Fork 1
/
Context.h
128 lines (100 loc) · 2.52 KB
/
Context.h
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
/* Context.h
*
* Keep all "dynamic" configuration
*/
#ifndef CONTEXT_H
#define CONTEXT_H
#include <KeepInRTC.h>
#include <OWBus.h> /* 1-wire */
#include "Parameters.h"
class Context : public KeepInRTC::KeepMe {
struct { // Data to be kept b/w run, saved in the RTC
unsigned long wakeuptime; // last time we go to sleep (in seconds : will round in 136 years ;) )
bool verbose; // Send informational messages
bool debug; // Debug mode
} data;
OneWire oneWire;
OWBus bus;
public:
Context(KeepInRTC &kir) : KeepInRTC::KeepMe( kir, (uint32_t *)&this->data, sizeof(this->data) ), oneWire(ONE_WIRE_BUS), bus(&this->oneWire) {}
/* Set default values if needed
* <- true if data have been reseted
*/
bool begin( void ){
if( !kir.isValid() ){
this->data.wakeuptime = 0;
this->data.verbose = DEF_VERBOSITY;
# ifdef DEV
this->data.debug = true;
# else
this->data.debug = false;
#endif
this->save();
return true;
}
return false;
}
/* Sleep management
* <- howlong to sleep (in secondes)
*/
void deepSleep( unsigned long howlong ){
this->data.wakeuptime += millis()/1e3; // Add the seconds already spent
this->data.wakeuptime += howlong; // Add sleep duration
this->save();
ESP.deepSleep( howlong * 1e6 );
}
/* Indicate if informational messages have to be send or not */
void beVerbose( bool arg ){
this->data.verbose = arg;
this->save();
}
bool getVerbose( void ){
return this->data.verbose;
}
/* Enable debugging message */
void setDebug(bool v){
this->data.debug = v;
this->save();
}
bool getDebug( void ){
return this->data.debug;
}
OWBus &getOWBus( void ){
return this->bus;
}
/********
* Helpers
********/
String toString( float number, uint8_t digits=2){ // Largely inspired by Print::printFloat()
String res;
if (isnan(number)) return "nan";
if (isinf(number)) return "inf";
bool negative = false;
if(number < 0.0){
number = -number;
negative = true;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
float rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;
unsigned long int_part = (unsigned long)number;
float remainder = number - (float)int_part;
do {
char c = int_part % 10;
int_part /= 10;
res = (char)(c+'0') + res;
} while( int_part );
if( negative )
res = '-' + res;
res += '.';
if( digits ) while(digits--){
remainder *= 10.0;
res += (char)((char)remainder + '0');
remainder -= (int)remainder;
}
return res;
}
};
#endif