-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSM.h
More file actions
195 lines (162 loc) · 8.16 KB
/
Copy pathFSM.h
File metadata and controls
195 lines (162 loc) · 8.16 KB
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
/**
*******************************************************************************
*******************************************************************************
*
* License :
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*******************************************************************************
*******************************************************************************
*
*
* @file FSM.h
* @author gilou
* @date 19 févr. 2018
* @brief The FSM is the finish state machine mechanism.
*
* This is the Final State Machine organization
*
*/
#ifndef FSM_H_
#define FSM_H_
//#define DEBUG_FSM
#include "Arduino.h"
#include "Rtc_Pcf8563.h"
#include "SD.h"
#include "Power.h"
class FSM;
typedef void (FSM::*_nextState)(void); /**< This typedef define a method pointer type */
/**
* \brief The class FSM regroup hardware peripherals declaration, mechanic methods and useful parameters.
*/
class FSM {
public:
/******************************************************************************
* Constructor and destructor definition
*/
FSM();
virtual ~FSM();
/******************************************************************************
* State machine mechanic methods
*/
/**
* \brief This method initialize the state machine
*/
void init();
/**
* \brief This method is used to test each second and decide if the flag_measure need to be pull-up
*/
void timingControl();
/******************************************************************************
* State pointer
*/
_nextState nextState = NULL; /**< This method pointer is use to manipulate the next state function */
/******************************************************************************
* Public flags
*/
static bool flag_frequenciesReady; /**< Set when the timer3 overflow, data are ready*/
static bool flag_configRequest; /**< set when a string from usart0 finish by "\r" */
static bool flag_measure; /**< set when time come for a new measure */
/******************************************************************************
* transitions list
* Each transition for a next state is organize in simple setter method, that try to make more clean code
*/
inline void ev_isWaiting(){nextState = &FSM::st_SLEEP;} ///< The nextState is set to st_SLEEP */
inline void ev_testCounter(){nextState = &FSM::st_CALC_AVERAGES;} ///< The nextState is set to st_CALC_AVERAGES */
inline void ev_transmitting(){nextState = &FSM::st_OUTPUT;} ///< The nextState is set to st_OUTPUT */
inline void ev_configRequest(){nextState =&FSM::st_CONFIG;} ///< The nextState is set to st_CONFIG */
inline void ev_measure(){nextState = &FSM::st_MEASURE;} ///< The nextState is set to st_MEASURE */
inline void ev_frequenciesReady(){nextState = &FSM::st_READ_FREQUENCIES;} ///< The nextState is set to st_READ_FREQUENCIES */
/******************************************************************************
* State list declaration
*/
void st_SETUP(); ///< this state is used at the start time */
void st_CONFIG(); ///< this state is used to download or upload configuration */
void st_MEASURE(); ///< this state is used for measurement process */
void st_READ_FREQUENCIES(); ///< This state is used to read frequencies from anemometers or RPMs sensors */
void st_CALC_AVERAGES(); ///< This state is used to calc averages if the max measures is done */
void st_OUTPUT(); ///< this state is used to save or send data */
void st_SLEEP(); ///< When nothing is need to do, the micro-controller go to sleep */
/******************************************************************************
* Configuration management
******************************************************************************/
/// This method a to serialString the ch character
inline void addChar(char ch){ serialString[StrIndex]=ch; StrIndex++; }
/// This method print the main menu
void menu();
/**
* The config method permit to update private members
* @param stringConfig This string contain the ID parameter and the value as "id=value"
* @return If the id or value is wrong return 1 else 0.
*/
bool config(char *stringConfig);
/// The printConfig method print all parameters and values on Serial (uart0)
void printConfig();
/**
* The configDT method permit to update Date and Time
* @param stringConfig This string contain the ID parameter and the value as "id=value"
* string for time needs to be as : "*21=hh:mm:ss"
* string for date needs to be as : "*22=mm/dd/yyyy"
*/
void configDT(char *stringConfig);
/// This method print the date and the time on the serial(uart0)
void printDateTime();
/// Print output configuration
void printOutput();
void configOutput(char *stringConfig);
/******************************************************************************
* FSM parameters in EEPROM management
******************************************************************************/
/// Load saved parameters for the FSM from the eeprom
void load_param();
/// Update saved parameters for the FSM in the eeprom
void update_param();
/// \brief Initialize the eeprom memory
void initialize_param();
private:
/******************************************************************************
* params saved in eeprom
******************************************************************************/
const unsigned char DATA_STRUCTURE_VERSION = 201;
unsigned long structure_version; /**< This permit to improve the structure by auto reset eeprom data when the structure evolve to prevent data bad reading */
unsigned char node_id; /**< The node id, permit identify each datalogger (0 - 255)*/
unsigned char measureSampleConf; /**< Measurement sampling (0: no measure, 1 : 10 secs, 2: 1 min, 3: 10 min...) */
unsigned char measureMax; /**< Measure_max is the number of measure by measure_sample_conf (ex by minute or by 10 minutes...). !Be careful! is a new configuration is create, the Sensor::MAX_DATA_SAMPLE needs to be adjust to the highest value of measure_max! */
/******************************************************************************
* Private FSM members
******************************************************************************/
unsigned char m_eeprom_addr; /**< Adress were fsm data are stored in eeprom */
unsigned char isInConfig; /**< This is a flag to stay in config mode */
unsigned char measure; /**< This is a measure counter, to store data at the correct place and compare with the measureMax value */
unsigned char secondOld; /**< Store the old second value to made timing test only each second */
unsigned char second_counter; /**< The second_counter is used to compare to the measurePeriode interval */
unsigned char measurePeriode; /**< measurePeriode is the interval between two measures in seconds */
unsigned long timestamp; /**< save timestamp at each average to print on output */
char serialString[64]={'a'}; /**< This string is used for character test and is pass to the config state when a '\r' char comes */
unsigned char StrIndex=0; /**< This is serialString index */
// output config flags
bool serial_enable; /**< set to true, to write data on Serial */
bool sd_enable; /**< set to true, to write data on SD card */
/******************************************************************************
* Hardware interface
******************************************************************************/
Rtc_Pcf8563 rtc; /**< Real Time Clock instance */
SDClass sd;
Power power1 = Power(1);
Power power2 = Power(2);
bool sd_init;
const unsigned char SD_CS = 7;
};
#endif /* FSM_H_ */