19 #include <plog/Appenders/ConsoleAppender.h>
20 #include <plog/Formatters/TxtFormatter.h>
21 #include <plog/Init.h>
33 #define EEPROM_IDSTART 0xA0
34 #define MAXINSTANCES 10
37 #define EEPROM_ID 0xA5
38 #define VERSION_MAJOR 0
39 #define VERSION_MINOR 1
40 #define VERSION_PATCH 0
43 #define CRC32_TABLE_SIZE 256
46 #if !defined(US_ADC_CONVERSION_TIME)
47 #define US_ADC_CONVERSION_TIME 250
51 typedef enum : uint16_t {
59 typedef enum : uint16_t {
66 typedef enum : uint16_t {
123 SensorWLED(uint16_t analog_pin,
float mv_offset = 0.0,
float slope = 1.0,
124 uint16_t samples = 0);
169 0, 0x100, 0x200, 0x300, 0x400, 0x500,
170 0x600, 0x700, 0x800, 0x900, 0xA00};
181 void setAnalogPin(uint16_t a_pin, uint16_t mode = INPUT);
196 uint32_t initial,
const void *buf,
size_t len);
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