STIMA  3
i2c-opc.h
Go to the documentation of this file.
1 
3 /*********************************************************************
4 Copyright (C) 2017 Marco Baldinetti <m.baldinetti@digiteco.it>
5 authors:
6 Paolo patruno <p.patruno@iperbole.bologna.it>
7 Marco Baldinetti <m.baldinetti@digiteco.it>
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 **********************************************************************/
22 
23 #ifndef _I2C_OPC_H
24 #define _I2C_OPC_H
25 
26 #include "i2c-opc-config.h"
27 #include <debug.h>
28 #include <i2c_config.h>
29 #include <avr/sleep.h>
30 #include <avr/power.h>
31 #include <avr/wdt.h>
32 #include <i2c_utility.h>
33 #include <rmap_utility.h>
34 #if (USE_JSON)
35 #include <json_utility.h>
36 #endif
37 #include <eeprom_utility.h>
38 #include <Wire.h>
39 #include <TimeLib.h>
40 #include <typedef.h>
41 #include <registers-opc.h>
42 #include <opcxx.h>
43 #include <debug_config.h>
44 #include <SdFat.h>
45 #include <StreamUtils.h>
46 #include <ArduinoLog.h>
47 
48 
49 /*********************************************************************
50 * TYPEDEF
51 *********************************************************************/
56 typedef struct {
57  uint8_t module_version;
58  uint8_t module_type;
59  uint8_t i2c_address;
60  bool is_oneshot;
62  // uint8_t configuration_variables[OPCXX_CONFIGURATION_VARIABLES_LENGTH];
63  // uint8_t configuration_variables_2[OPCXX_CONFIGURATION_VARIABLES_2_LENGTH];
65 
66 #if (USE_SENSOR_OE3)
71 typedef struct {
72  float sample;
73  // float med60; //!< last observation
74  float med;
75  // float max; //!< maximum values of observations
76  // float min; //!< minium values of observations
77  float sigma;
78 } temperature_value_t;
79 
80 typedef struct {
81  float sample;
82  // float med60; //!< last observation
83  float med;
84  // float max; //!< maximum values of observations
85  // float min; //!< minium values of observations
86  float sigma;
87 } humidity_value_t;
88 #endif
89 
90 typedef struct {
91  float sample;
92  // float med60; //!< last observation
93  float med;
94  // float max; //!< maximum values of observations
95  // float min; //!< minium values of observations
96  float sigma;
97 } pm_value_t;
98 
99 typedef struct {
100  uint16_t sample;
101  // uint16_t med60; //!< last observation
102  uint16_t med;
103  // uint16_t max; //!< maximum values of observations
104  // uint16_t min; //!< minium values of observations
105  uint16_t sigma;
106 } bin_value_t;
107 
112 typedef struct {
113  uint8_t module_type;
114  uint8_t module_version;
115 
116  float pm_sample[OPCXX_PM_LENGTH];
117  float pm_med[OPCXX_PM_LENGTH];
118  float pm_sigma[OPCXX_PM_LENGTH];
119 
120  uint16_t bins_med[OPCN3_BINS_LENGTH];
121  uint16_t bins_sigma[OPCN3_BINS_LENGTH];
122 
123  #if (USE_SENSOR_OE3)
124  float temperature_sample;
125  float humidity_sample;
126 
127  float temperature_med;
128  float humidity_med;
129  #endif
130 
134  bin_value_t bins[OPC_BINS_LENGTH];
135 
136  #if (USE_SENSOR_OE3)
137  temperature_value_t temperature;
138  humidity_value_t humidity;
139  #endif
141 
146 typedef struct {
147  uint8_t i2c_address;
148  bool is_oneshot;
151 
156 typedef struct {
157  float values;
158  uint8_t count;
159  uint8_t error_count;
160 } sample_t;
161 
166 typedef struct {
167  float med[OBSERVATION_COUNT];
168  uint16_t count;
169  float *read_ptr;
170  float *write_ptr;
172 
173 typedef struct {
174  uint16_t med[OBSERVATION_COUNT];
175  uint16_t count;
176  uint16_t *read_ptr;
177  uint16_t *write_ptr;
179 
180 /*********************************************************************
181 * TYPEDEF for Finite State Machine
182 *********************************************************************/
187 typedef enum {
189  #if (USE_POWER_DOWN)
190  ENTER_POWER_DOWN,
191  #endif
193  END
195 
200 typedef enum {
201  OPC_INIT,
202  OPC_SWITCH_ON,
203  OPC_SEND_COMMAND_FAN_DAC,
204  OPC_WAIT_RESULT_FAN_DAC,
205  OPC_SEND_COMMAND_FAN_ON,
206  OPC_WAIT_RESULT_FAN_ON,
207  OPC_SEND_COMMAND_LASER_ON,
208  OPC_WAIT_RESULT_LASER_ON,
209  OPC_SEND_COMMAND_READ_HISTOGRAM,
210  OPC_WAIT_RESULT_READ_HISTOGRAM,
211  OPC_READ_HISTOGRAM,
215 
216 /*********************************************************************
217 * GLOBAL VARIABLE
218 *********************************************************************/
219 
220 #if (ENABLE_SDCARD_LOGGING)
225 SdFat SD;
226 
231 File logFile;
232 
237 WriteLoggingStream loggingStream(logFile,Serial);
238 #endif
239 
245 
251 
257 
263 
269 
275 
281 
287 
292 volatile uint8_t readable_data_address;
293 
298 volatile uint8_t readable_data_length;
299 
304 volatile uint8_t i2c_rx_data[I2C_MAX_DATA_LENGTH];
305 
310 volatile uint8_t i2c_error;
311 
316 volatile uint8_t ready_tasks_count;
317 
323 
328 bool is_start;
329 
334 bool is_stop;
335 
341 
347 
352 // bool is_read_configuration_variable;
353 
358 // sample_t opc_samples;
359 sample_t pm1_samples;
360 sample_t pm25_samples;
361 sample_t pm10_samples;
362 sample_t bins_samples[OPC_BINS_LENGTH];
363 #if (USE_SENSOR_OE3)
364 sample_t temperature_samples;
365 sample_t humidity_samples;
366 #endif
367 
372 float_observation_t pm1_observations;
373 float_observation_t pm25_observations;
374 float_observation_t pm10_observations;
375 uint16_observation_t bins_observations[OPC_BINS_LENGTH];
376 #if (USE_SENSOR_OE3)
377 float_observation_t temperature_observations;
378 float_observation_t humidity_observations;
379 #endif
380 
385 uint8_t samples_count;
386 
391 bool do_buffers_reset;
392 
397 volatile uint16_t timer_counter;
398 
403 state_t state;
404 
410 
411 bool is_opc_setted;
412 bool is_opc_first_read;
413 uint8_t histogram_error_count;
414 
419 #if (USE_SENSOR_OA2 || USE_SENSOR_OB2 || USE_SENSOR_OC2 || USE_SENSOR_OD2)
420 Opcn2 opcn(OPC_CHIP_SELECT, OPC_POWER_PIN, OPC_SPI_POWER_PIN, SENSORS_SAMPLE_TIME_MS / 1000);
421 #endif
422 
423 #if (USE_SENSOR_OA3 || USE_SENSOR_OB3 || USE_SENSOR_OC3 || USE_SENSOR_OD3 || USE_SENSOR_OE3)
424 Opcn3 opcn(OPC_CHIP_SELECT, OPC_POWER_PIN, OPC_SPI_POWER_PIN, SENSORS_SAMPLE_TIME_MS / 1000);
425 #endif
426 
427 /*********************************************************************
428 * FUNCTIONS
429 *********************************************************************/
430 
436 void init_logging();
437 
445 void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms);
446 
453 void init_wdt(uint8_t wdt_timer);
454 
460 void init_system(void);
461 
467 void init_buffers(void);
468 
474 void init_tasks(void);
475 
481 void init_pins(void);
482 
488 void init_wire(void);
489 
495 void init_spi(void);
496 
502 void init_rtc(void);
503 
504 #if (USE_TIMER_1)
510 void init_timer1(void);
511 
517 void start_timer(void);
518 
524 void stop_timer(void);
525 #endif
526 
532 void init_sensors(void);
533 
539 void print_configuration(void);
540 
546 void load_configuration(void);
547 
554 void save_configuration(bool);
555 
561 void commands(void);
562 
568 void reset_samples_buffer(void);
569 
575 // void reset_observations_buffer(void);
576 
582 void exchange_buffers(void);
583 
584 template<typename sample_g, typename observation_g, typename values_v, typename value_v> void addSample(sample_g *sample, observation_g *observation, values_v *values, value_v value);
585 
586 template<typename observation_g, typename value_v> value_v readCurrentObservation(observation_g *buffer);
587 template<typename observation_g, typename value_v> void writeCurrentObservation(observation_g *buffer, value_v value);
588 template<typename observation_g, typename length_v> void resetObservation(observation_g *buffer, length_v length);
589 template<typename observation_g, typename length_v> void resetBackPmObservation(observation_g *buffer, length_v length);
590 template<typename observation_g, typename length_v> void incrementObservation(observation_g *buffer, length_v length);
591 template<typename observation_g, typename length_v, typename value_v> void addObservation(observation_g *buffer, length_v length, value_v value);
592 template<typename observation_g, typename length_v, typename value_v> value_v readBackObservation(observation_g *buffer, length_v length);
593 
594 
601 void samples_processing(bool is_force_processing);
602 
608 bool observations_processing(void);
609 
618 template<typename sample_g, typename observation_g> bool make_observation_from_samples(bool is_force_processing, sample_g *sample, observation_g *observation);
619 
620 
629 template<typename sample_g, typename observation_g, typename value_v, typename val_v> bool make_value_from_samples_and_observations(sample_g *sample, observation_g *observation, value_v *value);
630 
631 /*********************************************************************
632 * TASKS
633 *********************************************************************/
638 volatile bool is_event_opc_task;
639 
646 void opc_task(void);
647 
652 volatile bool is_event_command_task;
653 
660 void command_task(void);
661 
662 /*********************************************************************
663 * INTERRUPTS HANDLER
664 *********************************************************************/
671 
678 
679 #endif
#define SENSORS_SAMPLE_TIME_MS
Milliseconds for sampling sensors: 1000 - 60000 [ms] must be integer multiple of TIMER1_INTERRUPT_TIM...
Definition: i2c-leaf-config.h:168
#define I2C_MAX_DATA_LENGTH
Max length in bytes for i2c bus data buffer.
Definition: i2c_config.h:36
#define OBSERVATION_COUNT
Observations buffer length.
Definition: sensors_config.h:259
volatile bool is_event_command_task
Enable or disable the Command task.
Definition: i2c-leaf.h:502
volatile uint8_t readable_data_length
Number of bytes to read at readable_data_address.
Definition: i2c-leaf.h:202
bool is_start
Execute start command.
Definition: i2c-leaf.h:244
configuration_t configuration
Configuration data.
Definition: i2c-leaf.h:148
volatile uint8_t i2c_error
Number of i2c error.
Definition: i2c-leaf.h:220
writable_data_t writable_data
Writable i2c register.
Definition: i2c-leaf.h:184
bool do_buffers_reset
Force a buffers reset.
Definition: i2c-leaf.h:271
writable_data_t * writable_data_ptr
Pointer for read and write data in i2c writable register.
Definition: i2c-leaf.h:190
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
uint8_t samples_count
Number of samples to be acquired for make one observation.
Definition: i2c-leaf.h:265
state_t
Main loop finite state machine.
Definition: i2c-leaf.h:100
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
bool is_stop
Execute stop command.
Definition: i2c-leaf.h:250
volatile uint8_t ready_tasks_count
Number of tasks ready to execute.
Definition: i2c-leaf.h:232
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
#define OPC_CHIP_SELECT
Output pin for spi chip select opc sensor.
Definition: i2c-opc-config.h:86
#define OPC_POWER_PIN
Output pin for power on and power off opc sensor.
void load_configuration(void)
Load configuration from EEPROM.
Definition: i2c-leaf.ino:335
void init_tasks(void)
Init tasks variable and state.
Definition: i2c-leaf.ino:225
opc_state_t opc_state
Current sensors reading task state.
Definition: i2c-opc.h:409
void init_system(void)
Init system.
Definition: i2c-leaf.ino:290
bool make_value_from_samples_and_observations(sample_g *sample, observation_g *observation, value_v *value)
Definition: i2c-opc.ino:920
void init_spi(void)
Init SPI library.
Definition: i2c-leaf.ino:262
volatile bool is_event_opc_task
Enable or disable the OPC task.
Definition: i2c-opc.h:638
void opc_task(void)
Opc setup and reading Task. Read data from OPC.
Definition: i2c-opc.ino:1014
opc_state_t
OPC setup and reading task finite state machine.
Definition: i2c-opc.h:200
@ OPC_WAIT_STATE
non-blocking waiting time
Definition: i2c-opc.h:213
@ OPC_END
performs end operations and deactivate task
Definition: i2c-opc.h:212
void reset_samples_buffer(void)
Reset samples buffers to default value.
Definition: i2c-leaf.ino:549
bool is_oneshot
Received command is in oneshot mode.
Definition: i2c-opc.h:340
bool is_continuous
Received command is in continuous mode.
Definition: i2c-opc.h:346
void init_wire(void)
Init wire (i2c) library and performs checks on the bus.
Definition: i2c-leaf.ino:251
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
bool observations_processing(void)
Main routine for processing the observations to calculate a value for report.
Definition: i2c-opc.ino:892
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-opc.h:187
@ INIT
init tasks and sensors
Definition: i2c-opc.h:188
@ TASKS_EXECUTION
execute active tasks
Definition: i2c-opc.h:192
@ END
go to ENTER_POWER_DOWN or TASKS_EXECUTION
Definition: i2c-opc.h:193
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
void samples_processing(bool is_force_processing)
Main routine for processing the samples to calculate an observation.
Definition: i2c-opc.ino:560
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
void init_sensors(void)
Create and setup sensors.
Definition: i2c-leaf.ino:353
void init_logging()
Init logging system.
Definition: i2c-leaf.ino:141
bool make_observation_from_samples(bool is_force_processing, sample_g *sample, observation_g *observation)
Definition: i2c-opc.ino:857
volatile uint16_t timer_counter
Timer counter variable for execute timed task with time multiple of base Timer1 time.
Definition: i2c-opc.h:397
void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms)
Enter power down mode.
Definition: i2c-leaf.ino:173
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
Definition: i2c-opc.h:99
uint16_t sigma
standard deviation of observations
Definition: i2c-opc.h:105
uint16_t med
average values of observations
Definition: i2c-opc.h:102
uint16_t sample
last sample
Definition: i2c-opc.h:100
EEPROM saved configuration.
Definition: i2c-leaf.h:55
uint8_t module_version
module version
Definition: i2c-opc.h:57
bool is_continuous
enable or disable continuous mode
Definition: i2c-opc.h:61
Definition: i2c-opc.h:166
float * read_ptr
reader pointer to buffer (read observations for calculate report value)
Definition: i2c-opc.h:169
float * write_ptr
writer pointer to buffer (add new observation)
Definition: i2c-opc.h:170
uint16_t count
number of observations
Definition: i2c-opc.h:168
Definition: i2c-opc.h:90
float sample
last sample
Definition: i2c-opc.h:91
float med
average values of observations
Definition: i2c-opc.h:93
float sigma
standard deviation of observations
Definition: i2c-opc.h:96
Readable data through i2c bus.
Definition: i2c-leaf.h:76
uint8_t module_version
module type
Definition: i2c-opc.h:114
pm_value_t pm1
pm1 data for report
Definition: i2c-opc.h:131
pm_value_t pm10
pm10 data for report
Definition: i2c-opc.h:133
pm_value_t pm25
pm25 data for report
Definition: i2c-opc.h:132
Samples values for measured opc data.
Definition: i2c-opc.h:156
float values
buffer containing the measured samples
Definition: i2c-opc.h:157
uint8_t count
number of good samples
Definition: i2c-opc.h:158
uint8_t error_count
number of bad samples
Definition: i2c-opc.h:159
Definition: i2c-opc.h:173
uint16_t * write_ptr
writer pointer to buffer (add new observation)
Definition: i2c-opc.h:177
uint16_t * read_ptr
reader pointer to buffer (read observations for calculate report value)
Definition: i2c-opc.h:176
uint16_t count
number of observations
Definition: i2c-opc.h:175
Writable data through i2c bus.
Definition: i2c-leaf.h:87
bool is_continuous
enable or disable continuous mode
Definition: i2c-opc.h:149