Debinix Team Library SensorWLED
Provides methods to retrieve instant and peak values from the ADC input.
All Classes Files Functions Variables Enumerations Enumerator Macros Pages
SensorWLED.h
Go to the documentation of this file.
1 
10 #ifndef SENSORWLED_H_
11 #define SENSORWLED_H_
12 
13 // Other includes needed by the contained implementations
14 #ifndef ARDUINO
15 #include <cmath>
16 #include <cstdint>
17 
18 // Add the plog logging framework
19 #include <plog/Appenders/ConsoleAppender.h>
20 #include <plog/Formatters/TxtFormatter.h>
21 #include <plog/Init.h>
22 #include <plog/Log.h>
23 
24 // <-- statement line will be removed
25 // <-- statement line will be removed
26 ; // <-- statement line will be removed
27 
28 #else // The SensorWLED library is dependent on the Arduino EEPROM library
29 #include <EEPROM.h>
30 #endif
31 
33 #define EEPROM_IDSTART 0xA0
34 #define MAXINSTANCES 10
35 
37 #define EEPROM_ID 0xA5
38 #define VERSION_MAJOR 0
39 #define VERSION_MINOR 1
40 #define VERSION_PATCH 0
41 
43 #define CRC32_TABLE_SIZE 256
44 
46 #if !defined(US_ADC_CONVERSION_TIME)
47 #define US_ADC_CONVERSION_TIME 250
48 #endif
49 
51 typedef enum : uint16_t {
52  bits10 = 1023,
53  bits12 = 4095,
54  bits13 = 8191,
55  bits16 = 65535,
57 
59 typedef enum : uint16_t {
60  mv_vcc_3v3 = 3300,
61  mv_vcc_5v = 5000,
63 
64 //-----------------------------------------------------------------------------
66 typedef enum : uint16_t {
70 
71 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 typedef struct {
77  uint16_t magic_id;
78  uint16_t major_version;
79  uint16_t minor_version;
80  uint16_t patch_version;
82 
83 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 typedef struct {
89  uint16_t analog_pin;
90  uint16_t sample_count;
91  uint16_t sample_period;
93  float cal_slope;
95 
96 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
101 typedef struct {
104  uint16_t ms_poll_time;
105  uint16_t ms_hold_time;
107  float decay_rate;
109 
110 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
119 class SensorWLED {
120 
121 public:
122  // Constructor, default: no calibration or averaging smooting are applied
123  SensorWLED(uint16_t analog_pin, float mv_offset = 0.0, float slope = 1.0,
124  uint16_t samples = 0);
125 
126  // Destructor: Restore pinMode to default
127  ~SensorWLED(void);
128 
129  void begin(DynamicDataType_t const &rDynamicParams);
130 
131  // Call continously (in the loop()) for updated ADC values.
132  bool updateAnalogRead(void);
133 
134  // Maps the input analog value to a value optimized
135  // for ADC resolution and max supply voltage.
136  double getMappedValue(void);
137  double getMappedPeakValue(void);
138 
139  // Read stored EEPROM Id and program version.
141 
142  bool writeCalibrationEEPROM(uint16_t instance, uint32_t crc32);
143  CalibrationDataType_t readCalibrationEEPROM(uint16_t instance);
144 
145  bool writeDynamicEEPROM(uint16_t instance, uint32_t crc32);
146  DynamicDataType_t readDynamicEEPROM(uint16_t instance);
147 
148  uint32_t
151 
152  uint32_t cal_crc32;
153  uint32_t dyn_crc32;
154 
158 
159  // -------------------------------------------------------
160  // Common for all class instances (i.e., ADC channels)
161  // -------------------------------------------------------
162  static uint16_t getInstanceNumber(void);
163 
164  // Each instance has its own 'emulated eeprom' flash area
165  // storing calibration, static, and dynamic parameters.
166  // Note: 0x100 is for the first instance, 0x200 for the second,
167  // 0x0 is an unused area (reseved for the future).
168  inline static uint16_t eeprom_area[MAXINSTANCES + 1] = {
169  0, 0x100, 0x200, 0x300, 0x400, 0x500,
170  0x600, 0x700, 0x800, 0x900, 0xA00};
172 
173  // Id and program version is stored at location 'EEPROM_IDSTART'.
177 
178  inline static uint16_t instance_counter = 0;
179 
180 private:
181  void setAnalogPin(uint16_t a_pin, uint16_t mode = INPUT);
182  uint32_t applyDecay(uint32_t peak_value);
183 
186 
187  uint32_t raw_input_value;
189 
192 
193  // EEPROM and CRC32 methods
194  static void generateTableCRC32(uint32_t (&table)[CRC32_TABLE_SIZE]);
195  static uint32_t updateCRC32(uint32_t (&table)[CRC32_TABLE_SIZE],
196  uint32_t initial, const void *buf, size_t len);
197  static bool writeVersionEEPROM(void);
198 
199  static bool inline eeprom_version_written_flag =
200  false;
201 };
202 /* class SensorWLED */
203 
204 #endif /* SENSORWLED_H_ */
AdcResolutionType_e
Definition: SensorWLED.h:51
@ bits10
ADC max resolution is 10 bits.
Definition: SensorWLED.h:52
@ bits13
ADC max resolution is 13 bits.
Definition: SensorWLED.h:54
@ bits16
ADC max resolution is 16 bits.
Definition: SensorWLED.h:55
@ bits12
ADC max resolution is 12 bits.
Definition: SensorWLED.h:53
#define MAXINSTANCES
Max number of instantiated EEPROM areas.
Definition: SensorWLED.h:34
#define VERSION_MAJOR
Semantic versioning (M.m.p)
Definition: SensorWLED.h:38
VoltageVccType_e
Definition: SensorWLED.h:59
@ mv_vcc_3v3
Microcontroller VCC: 3V3.
Definition: SensorWLED.h:60
@ mv_vcc_5v
Microcontroller VCC: 5V.
Definition: SensorWLED.h:61
DecayModelType_e
Definition: SensorWLED.h:66
@ exponential_decay
Peak value decay - exponentially.
Definition: SensorWLED.h:68
@ linear_decay
Peak value decay - linear.
Definition: SensorWLED.h:67
#define VERSION_PATCH
Semantic versioning (M.m.p)
Definition: SensorWLED.h:40
#define EEPROM_ID
EEPROM id marker (never touch)
Definition: SensorWLED.h:37
#define CRC32_TABLE_SIZE
The size of table.
Definition: SensorWLED.h:43
#define VERSION_MINOR
Semantic versioning (M.m.p)
Definition: SensorWLED.h:39
Track instant and peak DC ADC input readings.
Definition: SensorWLED.h:119
uint32_t raw_input_value
ADC raw input at bits capability.
Definition: SensorWLED.h:187
static bool eeprom_version_written_flag
EEPROM write flag.
Definition: SensorWLED.h:199
bool writeCalibrationEEPROM(uint16_t instance, uint32_t crc32)
Saves static 'CalibrationData' struct to EEPROM.
Definition: SensorWLED.cpp:348
double pk_mapped_input_value
ADC peak mapped to VCC range.
Definition: SensorWLED.h:191
CalibrationDataType_t readCalibrationEEPROM(uint16_t instance)
Memory load of EEPROM 'CalibrationData', and returns the struct.
Definition: SensorWLED.cpp:395
VersionType_t readVersionEEPROM(void)
Memory load of EEPROM Id and Pgm version, and returns the information. This use a fixed EEPROM addres...
Definition: SensorWLED.cpp:322
uint32_t cal_crc32
CRC32 sum of stored EEPROM (begin) calibration data.
Definition: SensorWLED.h:152
SensorWLED(uint16_t analog_pin, float mv_offset=0.0, float slope=1.0, uint16_t samples=0)
Sets up the static 'eeprom_offset'-array which hold each instance start address for configuration loc...
Definition: SensorWLED.cpp:47
static uint16_t getInstanceNumber(void)
Gets the total number of created instances.
Definition: SensorWLED.cpp:501
DynamicDataType_t DynamicParams
Static and dynamic setup parameters.
Definition: SensorWLED.h:157
uint32_t previous_poll_millis_tm
Holds previous ADC poll time.
Definition: SensorWLED.h:184
uint32_t previous_hold_millis_tm
Holds previous ADC hold time.
Definition: SensorWLED.h:185
static uint32_t updateCRC32(uint32_t(&table)[CRC32_TABLE_SIZE], uint32_t initial, const void *buf, size_t len)
Calculate 32 bit CRC.
Definition: SensorWLED.cpp:542
void setAnalogPin(uint16_t a_pin, uint16_t mode=INPUT)
Sets the analog pin for ADC input on the microcontroller.
Definition: SensorWLED.cpp:490
static void generateTableCRC32(uint32_t(&table)[CRC32_TABLE_SIZE])
Generate 32 bit CRC table.
Definition: SensorWLED.cpp:511
static uint16_t instance_counter
Number of instances.
Definition: SensorWLED.h:178
double getMappedPeakValue(void)
Map the input analog value to a value optimized for actual capability of the controllers ADc and supp...
Definition: SensorWLED.cpp:261
double mapped_input_value
ADC values mapped to VCC range.
Definition: SensorWLED.h:188
static uint16_t eeprom_area[MAXINSTANCES+1]
Emulated EEPROM areas for each instance.
Definition: SensorWLED.h:168
static VersionType_t Version
Program version stored in emulated EEPROM.
Definition: SensorWLED.h:174
uint32_t calculateCalibrationDataCRC32(CalibrationDataType_t CalibrationData)
Calculates the CRC32 sum for 'CalibrationData'.
Definition: SensorWLED.cpp:564
uint32_t pk_raw_input_value
ADC peak input at bits capability.
Definition: SensorWLED.h:190
uint32_t calculateDynamicParamsCRC32(DynamicDataType_t DynamicParams)
Calculates the CRC32 sum for 'DynamicParams'.
Definition: SensorWLED.cpp:594
~SensorWLED(void)
Deallocate SensorWLED object, set data pin to INPUT.
Definition: SensorWLED.cpp:83
CalibrationDataType_t CalibrationData
ADC channel setup and calibration.
Definition: SensorWLED.h:156
uint32_t applyDecay(uint32_t peak_value)
Applies the decay model to peak value.
Definition: SensorWLED.cpp:273
bool writeDynamicEEPROM(uint16_t instance, uint32_t crc32)
Saves static 'DynamicData' struct to EEPROM.
Definition: SensorWLED.cpp:421
double getMappedValue(void)
Map the input analog value to a value optimized for actual capability of the controllers ADC and supp...
Definition: SensorWLED.cpp:250
void begin(DynamicDataType_t const &rDynamicParams)
Load and initilize the user preferences, and EEPROM CRC32 sums.
Definition: SensorWLED.cpp:102
bool updateAnalogRead(void)
Call continously (in loop()) for updated values.
Definition: SensorWLED.cpp:171
static bool writeVersionEEPROM(void)
Saves static 'Version' struct to EEPROM. Writes to fixed EEPROM address, common for all instances.
Definition: SensorWLED.cpp:294
DynamicDataType_t readDynamicEEPROM(uint16_t instance)
Memory load of EEPROM 'DynamicData', and returns the struct.
Definition: SensorWLED.cpp:466
uint32_t dyn_crc32
CRC32 sum of stored EEPROM (begin) dynamic data.
Definition: SensorWLED.h:153
ADC pin and ADC channel calibration data.
Definition: SensorWLED.h:88
float cal_zero_offset
ADC zero offset value (mV)
Definition: SensorWLED.h:92
uint16_t sample_period
Averaging time window.
Definition: SensorWLED.h:91
float cal_slope
Multiplication factor to adjust ADC reading.
Definition: SensorWLED.h:93
uint16_t analog_pin
ADC microcontroller input pin.
Definition: SensorWLED.h:89
uint16_t sample_count
Number of samples for averaging.
Definition: SensorWLED.h:90
Various static, instant and dynamic (peak) data.
Definition: SensorWLED.h:101
uint16_t ms_poll_time
Instant poll time (milliseconds)
Definition: SensorWLED.h:104
VoltageVccType_e mv_maxvoltage_adc
ADC maximum input voltage (mV)
Definition: SensorWLED.h:103
float decay_rate
Set sample decay factor.
Definition: SensorWLED.h:107
AdcResolutionType_e bits_resolution_adc
ADC resolution.
Definition: SensorWLED.h:102
DecayModelType_e decay_model
Linear or exponetial model.
Definition: SensorWLED.h:106
uint16_t ms_hold_time
Sample hold time (milliseconds)
Definition: SensorWLED.h:105
Unique EEPROM Id and code version.
Definition: SensorWLED.h:76
uint16_t magic_id
Unique magic number in EEPROM.
Definition: SensorWLED.h:77
uint16_t minor_version
Semantic version number (m.m.p)
Definition: SensorWLED.h:79
uint16_t patch_version
Semantic version number (m.m.p)
Definition: SensorWLED.h:80
uint16_t major_version
Semantic version number (m.m.p)
Definition: SensorWLED.h:78