STIMA  3
i2c-wind.h
1 /*********************************************************************
2 Copyright (C) 2017 Marco Baldinetti <m.baldinetti@digiteco.it>
3 authors:
4 Paolo patruno <p.patruno@iperbole.bologna.it>
5 Marco Baldinetti <m.baldinetti@digiteco.it>
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **********************************************************************/
20 
21 #ifndef _I2C_WIND_H
22 #define _I2C_WIND_H
23 
24 #include "i2c-wind-config.h"
25 #include <debug.h>
26 #include <i2c_config.h>
27 #include <avr/sleep.h>
28 #include <avr/power.h>
29 #include <avr/wdt.h>
30 #include <i2c_utility.h>
31 #include <rmap_utility.h>
32 #include <sdcard_utility.h>
33 #if (USE_JSON)
34 #include <json_utility.h>
35 #endif
36 #include <eeprom_utility.h>
37 #include <Wire.h>
38 #include <TimeLib.h>
39 #include <typedef.h>
40 #include <registers-wind.h>
41 #include <debug_config.h>
42 #include <SdFat.h>
43 #include <StreamUtils.h>
44 #include <ArduinoLog.h>
45 #include <CircularBuffer.h>
46 
47 /*********************************************************************
48 * TYPEDEF
49 *********************************************************************/
54 typedef struct {
55  uint8_t module_main_version;
56  uint8_t module_configuration_version;
57  uint8_t module_type;
58  uint8_t i2c_address;
59  bool is_oneshot;
60  float adc_voltage_offset_1;
61  float adc_voltage_offset_2;
62  float adc_voltage_min;
63  float adc_voltage_max;
65 
70 typedef struct {
71  uint16_t sample_speed; // B11002 254,0,0
72  uint16_t sample_direction; // B11001 254,0,0
73 
74  // DWA
75  uint16_t vavg10_speed; // B11002 254,0,0
76  uint16_t vavg10_direction; // B11001 254,0,0
77 
78  // DWB
79  uint16_t vavg_speed; // B11002 200,0,900
80  uint16_t vavg_direction; // B11001 200,0,900
81 
82  // DWC
83  uint16_t peak_gust_speed; // B11041 2,0,900
84  uint16_t long_gust_speed; // B11209 2,0,900
85 
86  // DWD
87  uint16_t avg_speed; // B11002 0,0,900
88 
89  // DWE
90  uint8_t class_1; // B11211 9,0,900
91  uint8_t class_2; // B11212 9,0,900
92  uint8_t class_3; // B11213 9,0,900
93  uint8_t class_4; // B11214 9,0,900
94  uint8_t class_5; // B11215 9,0,900
95  uint8_t class_6; // B11216 9,0,900
96  // dtable={"51":["B11211","B11212","B11213","B11214","B11215","B11216"]}
97 
98  // DWF
99  uint16_t peak_gust_direction; // B11043 205,0,900
100  uint16_t long_gust_direction; // B11210 205,0,900
101 
102 } report_t;
103 
108 typedef struct {
109  uint8_t module_type;
110  uint8_t module_main_version;
111  uint8_t module_minor_version;
112  report_t wind;
114 
119 typedef struct {
120  uint8_t i2c_address;
121  bool is_oneshot;
122  float adc_voltage_offset_1;
123  float adc_voltage_offset_2;
124  float adc_voltage_min;
125  float adc_voltage_max;
127 
128 /*********************************************************************
129 * TYPEDEF for Finite State Machine
130 *********************************************************************/
135 typedef enum {
136  INIT,
137  #if (USE_POWER_DOWN)
138  ENTER_POWER_DOWN,
139  #endif
141  END
142 } state_t;
143 
148 typedef enum {
149  WIND_INIT,
150  WIND_READING,
151  WIND_ELABORATE,
152  WIND_END,
153  WIND_WAIT_STATE
154 } wind_state_t;
155 
156 /*********************************************************************
157 * GLOBAL VARIABLE
158 *********************************************************************/
164 
170 
176 
182 
188 
194 
200 
206 
211 volatile uint8_t readable_data_address;
212 
217 volatile uint8_t readable_data_length;
218 
223 volatile uint8_t i2c_rx_data[I2C_MAX_DATA_LENGTH];
224 
229 volatile uint8_t lastcommand;
230 
235 volatile uint8_t i2c_error;
236 
241 volatile uint8_t i2c_time;
242 
247 volatile uint8_t ready_tasks_count;
248 
254 
259 volatile bool inside_transaction;
260 
265 volatile uint16_t transaction_time;
266 
267 
268 #if (USE_SENSOR_DES)
269 CircularBuffer<unsigned int, WMO_REPORT_SAMPLES_COUNT> cb_speed;
270 #endif
271 
272 #if (USE_SENSOR_DED)
273 CircularBuffer<unsigned int, WMO_REPORT_SAMPLES_COUNT> cb_direction;
274 #endif
275 
276 #if (USE_SENSOR_GWS)
277 CircularBuffer<unsigned int, WMO_REPORT_SAMPLES_COUNT> cb_speed;
278 CircularBuffer<unsigned int, WMO_REPORT_SAMPLES_COUNT> cb_direction;
279 #endif
280 
285 volatile uint16_t timer_counter_ms;
286 
287 #if (USE_SENSOR_DED || USE_SENSOR_DES || USE_SENSOR_GWS)
288 volatile bool is_wind_on;
289 #endif
290 
291 #if (USE_SENSOR_DES)
292 volatile uint32_t wind_speed_count;
293 volatile uint16_t wind_speed;
294 #endif
295 
300 state_t state;
301 
306 wind_state_t wind_state;
307 
308 
309 #if (ENABLE_SDCARD_LOGGING)
314 SdFat SD;
315 
320 File logFile;
321 
326 WriteLoggingStream loggingStream(logFile,Serial);
327 #endif
328 
329 /*********************************************************************
330 * FUNCTIONS
331 *********************************************************************/
332 
338 void init_logging();
339 
347 void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms);
348 
355 void init_wdt(uint8_t wdt_timer);
356 
362 void init_system(void);
363 
369 void init_buffers(void);
370 
376 void init_tasks(void);
377 
383 void init_pins(void);
384 
390 void init_wire(void);
391 
397 void init_spi(void);
398 
404 void init_rtc(void);
405 
406 #if (USE_TIMER_1)
412 void init_timer1(void);
413 
419 void start_timer(void);
420 
426 void stop_timer(void);
427 #endif
428 
434 void init_sensors(void);
435 
441 void print_configuration(void);
442 
448 void load_configuration(void);
449 
456 void save_configuration(bool);
457 
463 void commands(void);
464 
470 void tests(void);
471 
477 void reset_samples_buffer(void);
478 
484 // void reset_observations_buffer(void);
485 
486 void reset_report_buffer(void);
487 
493 void exchange_buffers(void);
494 
495 /*
496 template<typename buffer_g, typename length_v, typename value_v> value_v bufferRead(buffer_g *buffer, length_v length);
497 template<typename buffer_g, typename length_v, typename value_v> value_v bufferReadBack(buffer_g *buffer, length_v length);
498 template<typename buffer_g, typename value_v> void bufferWrite(buffer_g *buffer, value_v value);
499 template<typename buffer_g> void bufferPtrReset(buffer_g *buffer);
500 template<typename buffer_g, typename length_v> void bufferPtrResetBack(buffer_g *buffer, length_v length);
501 template<typename buffer_g, typename length_v> void incrementBuffer(buffer_g *buffer, length_v length);
502 template<typename buffer_g, typename length_v, typename value_v> void bufferReset(buffer_g *buffer, length_v length);
503 template<typename buffer_g, typename length_v, typename value_v> void addValue(buffer_g *buffer, length_v length, value_v value);
504 */
505 
511 void make_report (bool init=false);
512 
513 
519 void elaborate_circular_buffer(void);
520 
521 
522 void getSDFromUV (float, float, float *, float *);
523 
524 #if (USE_SENSOR_DED || USE_SENSOR_DES)
525 #define windDirectionRead() (analogRead(WIND_DIRECTION_ANALOG_PIN))
526 #define isWindOn() (is_wind_on)
527 #define isWindOff() (!is_wind_on)
528 
529 void windPowerOff(void);
530 void windPowerOn(void);
531 void wind_speed_interrupt_handler(void);
532 void calibrationOffset(uint8_t count, uint8_t delay_ms, float *offset, float ideal);
533 void calibrationValue(uint8_t count, uint8_t delay_ms, float *value);
534 float getWindMv(float adc_value);
535 float getWindDirection(float adc_value);
536 float getWindSpeed (float count);
537 #endif
538 
539 #if (USE_SENSOR_GWS)
540 #define isWindOn() (is_wind_on)
541 #define isWindOff() (!is_wind_on)
542 
543 void windPowerOff(void);
544 void windPowerOn(void);
545 void serial1_reset(void);
546 bool windsonic_interpreter (uint16_t *speed, uint16_t *direction);
547 
548 uint16_t uart_rx_buffer_length;
549 uint8_t uart_rx_buffer[UART_RX_BUFFER_LENGTH];
550 #endif
551 
552 /*********************************************************************
553 * TASKS
554 *********************************************************************/
555 
560 volatile bool is_event_activate_wind_task;
561 
566 volatile bool is_event_wind_task;
567 
574 void wind_task(void);
575 
580 volatile bool is_event_command_task;
581 
588 void command_task(void);
589 
590 /*********************************************************************
591 * INTERRUPTS HANDLER
592 *********************************************************************/
599 
606 
607 #endif
#define I2C_MAX_DATA_LENGTH
Max length in bytes for i2c bus data buffer.
Definition: i2c_config.h:36
volatile bool is_event_command_task
Enable or disable the Command task.
Definition: i2c-leaf.h:502
volatile bool inside_transaction
Status of command transaction.
Definition: i2c-leaf.h:295
void load_configuration(void)
Load configuration from EEPROM.
Definition: i2c-leaf.ino:335
volatile uint8_t readable_data_length
Number of bytes to read at readable_data_address.
Definition: i2c-leaf.h:202
volatile uint8_t lastcommand
last command received.
Definition: i2c-leaf.h:214
void init_tasks(void)
Init tasks variable and state.
Definition: i2c-leaf.ino:225
configuration_t configuration
Configuration data.
Definition: i2c-leaf.h:148
void init_system(void)
Init system.
Definition: i2c-leaf.ino:290
volatile uint8_t i2c_error
Number of i2c error.
Definition: i2c-leaf.h:220
void init_spi(void)
Init SPI library.
Definition: i2c-leaf.ino:262
writable_data_t writable_data
Writable i2c register.
Definition: i2c-leaf.h:184
volatile uint8_t i2c_time
Time in seconds from last I2C reset.
Definition: i2c-leaf.h:226
void reset_samples_buffer(void)
Reset samples buffers to default value.
Definition: i2c-leaf.ino:549
writable_data_t * writable_data_ptr
Pointer for read and write data in i2c writable register.
Definition: i2c-leaf.h:190
void init_wire(void)
Init wire (i2c) library and performs checks on the bus.
Definition: i2c-leaf.ino:251
void i2c_receive_interrupt_handler(void)
I2C receive interrupt handler.
uint32_t awakened_event_occurred_time_ms
System time (in millisecond) when the system has awakened from power down.
Definition: i2c-leaf.h:238
void save_configuration(bool)
Save configuration to EEPROM.
Definition: i2c-leaf.ino:312
void init_rtc(void)
Init RTC module.
Definition: i2c-leaf.ino:266
volatile uint16_t transaction_time
Timer counter variable for compute command transaction timeout.
Definition: i2c-leaf.h:301
void init_wdt(uint8_t wdt_timer)
Init watchdog.
Definition: i2c-leaf.ino:202
void command_task(void)
Command Task. Execute the command received on i2c bus by reading i2c received data buffer.
Definition: i2c-leaf.ino:557
state_t
Main loop finite state machine.
Definition: i2c-leaf.h:100
@ INIT
init tasks and sensors
Definition: i2c-leaf.h:101
@ TASKS_EXECUTION
execute active tasks
Definition: i2c-leaf.h:105
@ END
go to ENTER_POWER_DOWN or TASKS_EXECUTION
Definition: i2c-leaf.h:106
void init_pins(void)
Init hardware pins.
Definition: i2c-leaf.ino:244
void commands(void)
Performs specific operations based on the received command.
Definition: i2c-leaf.ino:649
void exchange_buffers(void)
Exchange reader and writer pointer to buffer.
Definition: i2c-leaf.ino:535
void print_configuration(void)
Print current configuration.
Definition: i2c-leaf.ino:301
volatile readable_data_t readable_data_1
First readable i2c register.
Definition: i2c-leaf.h:154
volatile readable_data_t * readable_data_write_ptr
Pointer for write data in i2c readable register.
Definition: i2c-leaf.h:172
volatile uint16_t timer_counter_ms
Timer counter variable for execute timed task with time multiple of base Timer1 time.
Definition: i2c-leaf.h:277
void i2c_request_interrupt_handler(void)
I2C request interrupt handler.
Definition: i2c-leaf.ino:395
void init_buffers(void)
Init buffers.
Definition: i2c-leaf.ino:208
volatile uint8_t ready_tasks_count
Number of tasks ready to execute.
Definition: i2c-leaf.h:232
void init_sensors(void)
Create and setup sensors.
Definition: i2c-leaf.ino:353
void init_logging()
Init logging system.
Definition: i2c-leaf.ino:141
state_t state
Current main loop state.
Definition: i2c-leaf.h:283
volatile uint8_t i2c_rx_data[I2C_MAX_DATA_LENGTH]
Buffer for i2c received data.
Definition: i2c-leaf.h:208
volatile uint8_t readable_data_address
Address of readable i2c register.
Definition: i2c-leaf.h:196
volatile readable_data_t * readable_data_temp_ptr
Temporary pointer for exchange read and write pointer for i2c readable register.
Definition: i2c-leaf.h:178
volatile readable_data_t * readable_data_read_ptr
Pointer for read data in i2c readable register.
Definition: i2c-leaf.h:166
volatile readable_data_t readable_data_2
Second readable i2c register.
Definition: i2c-leaf.h:160
void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms)
Enter power down mode.
Definition: i2c-leaf.ino:173
bool is_oneshot
Received command is in oneshot mode.
Definition: i2c-opc.h:340
void tests()
Performs specific operations based on the received command.
Definition: i2c-opc.ino:1400
void init_timer1(void)
Init Timer1 module.
Definition: stima.ino:737
void start_timer(void)
Start Timer1 module.
Definition: stima.ino:741
void stop_timer(void)
Stop Timer1 module.
Definition: stima.ino:749
EEPROM saved configuration.
Definition: i2c-leaf.h:55
Readable data through i2c bus.
Definition: i2c-leaf.h:76
report data.
Definition: i2c-thr.h:98
Writable data through i2c bus.
Definition: i2c-leaf.h:87