STIMA  3
Classes | Enumerations | Functions | Variables
i2c-opc.h File Reference
#include "i2c-opc-config.h"
#include <debug.h>
#include <i2c_config.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <i2c_utility.h>
#include <rmap_utility.h>
#include <eeprom_utility.h>
#include <Wire.h>
#include <TimeLib.h>
#include <typedef.h>
#include <registers-opc.h>
#include <opcxx.h>
#include <debug_config.h>
#include <SdFat.h>
#include <StreamUtils.h>
#include <ArduinoLog.h>
Include dependency graph for i2c-opc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  configuration_t
 EEPROM saved configuration. More...
 
struct  pm_value_t
 
struct  bin_value_t
 
struct  readable_data_t
 Readable data through i2c bus. More...
 
struct  writable_data_t
 Writable data through i2c bus. More...
 
struct  sample_t
 Samples values for measured opc data. More...
 
struct  float_observation_t
 
struct  uint16_observation_t
 

Enumerations

enum  state_t {
  INIT , TASKS_EXECUTION , END , INIT ,
  TASKS_EXECUTION , END , INIT , TASKS_EXECUTION ,
  END , INIT , TASKS_EXECUTION , END ,
  INIT , TASKS_EXECUTION , END , INIT ,
  TASKS_EXECUTION , END , INIT , TASKS_EXECUTION ,
  END , INIT , TASKS_EXECUTION , END ,
  INIT , TASKS_EXECUTION , END , REBOOT ,
  INIT , TASKS_EXECUTION , END , INIT ,
  TASKS_EXECUTION , END
}
 Main loop finite state machine. More...
 
enum  opc_state_t {
  OPC_INIT , OPC_SWITCH_ON , OPC_SEND_COMMAND_FAN_DAC , OPC_WAIT_RESULT_FAN_DAC ,
  OPC_SEND_COMMAND_FAN_ON , OPC_WAIT_RESULT_FAN_ON , OPC_SEND_COMMAND_LASER_ON , OPC_WAIT_RESULT_LASER_ON ,
  OPC_SEND_COMMAND_READ_HISTOGRAM , OPC_WAIT_RESULT_READ_HISTOGRAM , OPC_READ_HISTOGRAM , OPC_END ,
  OPC_WAIT_STATE
}
 OPC setup and reading task finite state machine. More...
 

Functions

void init_logging ()
 Init logging system. More...
 
void init_power_down (uint32_t *time_ms, uint32_t debouncing_ms)
 Enter power down mode. More...
 
void init_wdt (uint8_t wdt_timer)
 Init watchdog. More...
 
void init_system (void)
 Init system. More...
 
void init_buffers (void)
 Init buffers. More...
 
void init_tasks (void)
 Init tasks variable and state. More...
 
void init_pins (void)
 Init hardware pins. More...
 
void init_wire (void)
 Init wire (i2c) library and performs checks on the bus. More...
 
void init_spi (void)
 Init SPI library. More...
 
void init_rtc (void)
 Init RTC module. More...
 
void init_sensors (void)
 Create and setup sensors. More...
 
void print_configuration (void)
 Print current configuration. More...
 
void load_configuration (void)
 Load configuration from EEPROM. More...
 
void save_configuration (bool)
 Save configuration to EEPROM. More...
 
void commands (void)
 Performs specific operations based on the received command. More...
 
void reset_samples_buffer (void)
 Reset samples buffers to default value. More...
 
void exchange_buffers (void)
 Exchange reader and writer pointer to buffer. More...
 
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)
 
template<typename observation_g , typename value_v >
value_v readCurrentObservation (observation_g *buffer)
 
template<typename observation_g , typename value_v >
void writeCurrentObservation (observation_g *buffer, value_v value)
 
template<typename observation_g , typename length_v >
void resetObservation (observation_g *buffer, length_v length)
 
template<typename observation_g , typename length_v >
void resetBackPmObservation (observation_g *buffer, length_v length)
 
template<typename observation_g , typename length_v >
void incrementObservation (observation_g *buffer, length_v length)
 
template<typename observation_g , typename length_v , typename value_v >
void addObservation (observation_g *buffer, length_v length, value_v value)
 
template<typename observation_g , typename length_v , typename value_v >
value_v readBackObservation (observation_g *buffer, length_v length)
 
void samples_processing (bool is_force_processing)
 Main routine for processing the samples to calculate an observation. More...
 
bool observations_processing (void)
 Main routine for processing the observations to calculate a value for report. More...
 
template<typename sample_g , typename observation_g >
bool make_observation_from_samples (bool is_force_processing, sample_g *sample, observation_g *observation)
 
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)
 
void opc_task (void)
 Opc setup and reading Task. Read data from OPC. More...
 
void command_task (void)
 Command Task. Execute the command received on i2c bus by reading i2c received data buffer. More...
 
void i2c_request_interrupt_handler (void)
 I2C request interrupt handler. More...
 
void i2c_receive_interrupt_handler (void)
 

Variables

configuration_t configuration
 
volatile readable_data_t readable_data_1
 
volatile readable_data_t readable_data_2
 
volatile readable_data_treadable_data_read_ptr
 
volatile readable_data_treadable_data_write_ptr
 
volatile readable_data_treadable_data_temp_ptr
 
writable_data_t writable_data
 
writable_data_twritable_data_ptr
 
volatile uint8_t readable_data_address
 
volatile uint8_t readable_data_length
 
volatile uint8_t i2c_rx_data [I2C_MAX_DATA_LENGTH]
 
volatile uint8_t i2c_error
 
volatile uint8_t ready_tasks_count
 
uint32_t awakened_event_occurred_time_ms
 
bool is_start
 
bool is_stop
 
bool is_oneshot
 Received command is in oneshot mode.
 
bool is_continuous
 Received command is in continuous mode.
 
sample_t pm1_samples
 
sample_t pm25_samples
 
sample_t pm10_samples
 
sample_t bins_samples [OPC_BINS_LENGTH]
 
float_observation_t pm1_observations
 
float_observation_t pm25_observations
 
float_observation_t pm10_observations
 
uint16_observation_t bins_observations [OPC_BINS_LENGTH]
 
uint8_t samples_count
 
bool do_buffers_reset
 
volatile uint16_t timer_counter
 Timer counter variable for execute timed task with time multiple of base Timer1 time.
 
state_t state
 
opc_state_t opc_state
 Current sensors reading task state.
 
bool is_opc_setted
 
bool is_opc_first_read
 
uint8_t histogram_error_count
 
volatile bool is_event_opc_task
 Enable or disable the OPC task.
 
volatile bool is_event_command_task
 

Enumeration Type Documentation

◆ opc_state_t

OPC setup and reading task finite state machine.

Enumerator
OPC_END 

performs end operations and deactivate task

OPC_WAIT_STATE 

non-blocking waiting time

◆ state_t

enum state_t

Main loop finite state machine.

Enumerator
INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

REBOOT 

reboot the machine

Function Documentation

◆ command_task()

void command_task ( )

Command Task. Execute the command received on i2c bus by reading i2c received data buffer.

Execute the command received on i2c bus by reading i2c received data buffer.

Returns
void.

◆ commands()

void commands ( )

Performs specific operations based on the received command.

Returns
void.

CONTINUOUS TEST

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS TEST

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS TEST

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS TEST

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS TEST

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS TEST

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS TEST

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

CONTINUOUS START

CONTINUOUS STOP

CONTINUOUS START-STOP

ONESHOT START

ONESHOT STOP

ONESHOT START-STOP

◆ exchange_buffers()

void exchange_buffers ( )

Exchange reader and writer pointer to buffer.

Returns
void.

◆ i2c_request_interrupt_handler()

void i2c_request_interrupt_handler ( )

I2C request interrupt handler.

Returns
void.

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

write crc8

write readable_data_length bytes of data stored in readable_data_read_ptr (base) + readable_data_address (offset) on i2c bus

◆ init_buffers()

void init_buffers ( )

Init buffers.

Returns
void.

copy readable_data_2 in readable_data_1

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_2 in readable_data_1

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

copy readable_data_2 in readable_data_1

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

◆ init_logging()

void init_logging ( )

Init logging system.

Returns
void.

◆ init_pins()

void init_pins ( )

Init hardware pins.

Returns
void.

◆ init_power_down()

void init_power_down ( uint32_t *  time_ms,
uint32_t  debouncing_ms 
)

Enter power down mode.

Parameters
time_mspointer to a variable to save the last instant you entered power down.
debouncing_msdelay to power down.
Returns
void.

◆ init_rtc()

void init_rtc ( )

Init RTC module.

Returns
void.

◆ init_sensors()

void init_sensors ( )

Create and setup sensors.

Returns
void.

◆ init_spi()

void init_spi ( )

Init SPI library.

Returns
void.

◆ init_system()

void init_system ( )

Init system.

Returns
void.

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

◆ init_tasks()

void init_tasks ( )

Init tasks variable and state.

Returns
void.

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

no tasks ready

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

no tasks ready

no tasks ready

◆ init_wdt()

void init_wdt ( uint8_t  wdt_timer)

Init watchdog.

Parameters
wdt_timera time value for init watchdog (WDTO_xxxx).
Returns
void.

◆ init_wire()

void init_wire ( )

Init wire (i2c) library and performs checks on the bus.

Returns
void.

clear the I2C bus first before calling Wire.begin()

clear the I2C bus first before calling Wire.begin()

◆ load_configuration()

void load_configuration ( )

Load configuration from EEPROM.

Returns
void.

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

◆ make_observation_from_samples()

template<typename sample_g , typename observation_g >
bool make_observation_from_samples ( bool  is_force_processing,
sample_g *  sample,
observation_g *  observation 
)

when true, indicates that sufficient samples have been acquired for the calculation of an observation

force processing when total samples count (sample->count + sample->error_count >= (SENSORS_SAMPLE_COUNT_MAX - SENSORS_SAMPLE_COUNT_TOLERANCE))

force processing when total samples count (sample->count + sample->error_count >= (SENSORS_SAMPLE_COUNT_MIN - SENSORS_SAMPLE_COUNT_TOLERANCE))

normal behavior: processing when correct sample count ((sample->count + sample->error_count) == samples_count)

add missing observation to observations buffer

increment ptr for set new calculted observation to observations buffer

◆ make_value_from_samples_and_observations()

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 
)

assign last sample to report value

reset value to default

if true, you can calculate the value for report (there are at least STATISTICAL_DATA_COUNT observations)

good observation counter

error observation counter

current observation's value

minimum value

average value

maximum value

standard deviation value

loop backwards in last STATISTICAL_DATA_COUNT observations array

if it is a good observation, calculate sum, minimum and maximum value. Otherwise increment error counter.

check and assing minimum value

check and assing maximum value

average calculation

standard deviation

calculate report value only if there are enough good observations

assign last observation to report value

average

assign maximum observation to report value

assign minimum observation to report value

calculate standard deviation: make sense for count >=2

assign standard deviation to standard deviation report value

◆ observations_processing()

void observations_processing ( void  )

Main routine for processing the observations to calculate a value for report.

Returns
void.

if true, a new pm1 data report was calculated

if true, a new pm2.5 data report was calculated

if true, a new pm10 data report was calculated

if true, a new bins data report was calculated

◆ opc_task()

void opc_task ( void  )

Opc setup and reading Task. Read data from OPC.

Returns
void.

success

retry

fail

success

retry

fail

success

retry

fail

success

◆ print_configuration()

void print_configuration ( )

Print current configuration.

Returns
void.

◆ reset_samples_buffer()

void reset_samples_buffer ( )

Reset samples buffers to default value.

Returns
void.

◆ samples_processing()

void samples_processing ( bool  is_force_processing)

Main routine for processing the samples to calculate an observation.

Parameters
is_force_processingif is true, force the calculation of the observation provided there is a minimum number of samples.
Returns
void.

if true, a new bins and pm observation was calculated

assign new value for samples_count

reset samples's buffer values

◆ save_configuration()

void save_configuration ( bool  is_default)

Save configuration to EEPROM.

Parameters
is_defaultif true save default configuration; if false save current configuration.
Returns
void.

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom