-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsensor_analog.cpp
58 lines (49 loc) · 2.06 KB
/
sensor_analog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Sensor Analog
* Read from a pin and send message
*
* See https://docs.espressif.com/projects/arduino-esp32/en/latest/api/adc.html for lots more on ESP ADCs
*
* Configuration options.
* Required: SENSOR_XYZ_WANT - compiled based on any of its subclasses
* Optional: SENSOR_ANALOG_REFERENCE for ESP8266 only
*
*/
#include "_settings.h" // Settings for what to include etc
#include "sensor_analog.h" // defines SENSOR_ANALOG_WANT if needed
#ifdef SENSOR_ANALOG_WANT
#include <Arduino.h>
#include "system_mqtt.h"
#include "system_discovery.h" //
//TO_ADD_BOARD
// https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/
// TODO what are the values on ESP8266 or ESP32
// TODO map between one set of REFERENCE values and the board specfic ones from the docs
// See https://github.com/mitra42/frugal-iot/issues/60
// TODO Note this is not going to make it to the place its used in sensor_analog
#ifndef SENSOR_ANALOG_REFERENCE
#ifdef ESP8266_D1_MINI
#define SENSOR_ANALOG_REFERENCE DEFAULT // TODO not clear if / where this is used
#else
#ifndef LOLIN_C3_PICO
#error analogReference() is board dependent, review the docs and online and define
#endif
#endif
#endif // SENSOR_ANALOG_REFERENCE
Sensor_Analog::Sensor_Analog(const uint8_t p, const uint8_t smooth_init, const char* topic_init, const unsigned long ms_init) : Sensor_Uint16(smooth_init, topic_init, ms_init), pin(p) { };
// Sensor_Uint16_t::act is good - sends with retain=false; qos=0;
// Sensor_Uint16_t::set is good - does optional smooth, compares and calls act
// Sensor_Uint16_t::loop is good - does periodic read and set
// Note this is virtual, and subclassed in Sensor_Battery
uint16_t Sensor_Analog::read() {
return analogRead(pin);
}
void Sensor_Analog::setup() {
// initialize the analog pin as an input.
pinMode(pin, INPUT); // I don't think this is needed ?
#ifdef SENSOR_ANALOG_REFERENCE
analogReference(SENSOR_ANALOG_REFERENCE); // TODO see TODO's in the sensor_analog.h
#endif
}
#endif // SENSOR_ANALOG_WANT
// TODO-57 need to do discovery