-
Notifications
You must be signed in to change notification settings - Fork 2
/
sensor_analog.cpp
61 lines (52 loc) · 1.66 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
58
59
60
61
/*
Sensor Analog
Read from a pin and return as sAnalog::Value\
*/
// TODO turn this into a template
// TODO add the _ARRAY parameters as used in sensor_sht85.cpp so will read multiple analog inputs.
#include "_settings.h" // Settings for what to include etc
#ifdef SENSOR_ANALOG_WANT
#include <Arduino.h>
#include "sensor_analog.h"
// TODO figure out how to handle multiple analog input pins
namespace sAnalog {
#ifdef SENSOR_ANALOG_MS
unsigned long nextLoopTime = 0;
#endif // SENSOR_ANALOG_MS
int value = 0;
#ifdef SENSOR_ANALOG_SMOOTH
unsigned long smoothedValue = 0;
#endif
void setup() {
// pinMode(SENSOR_ANALOG_PIN, INPUT); // I don't think this is needed
#ifdef ESP8266_D1_MINI
analogReference(SENSOR_ANALOG_REFERENCE); // TODO see TODO's in the sensor_analog.h
#else
#error analogReference is board specific, appears to be undefined on ESP32C3
#endif
//value = 0;
}
void readSensor() {
value = analogRead(SENSOR_ANALOG_PIN);
}
void loop() {
#ifdef SENSOR_ANALOG_MS
if (nextLoopTime <= millis()) {
#endif // SENSOR_ANALOG_MS
readSensor();
#ifdef SENSOR_ANALOG_SMOOTH // TODO maybe copy this to a system function
smoothedValue = smoothedValue - (smoothedValue >> SENSOR_ANALOG_SMOOTH) + value;
#endif // SENSOR_ANALOG_SMOOTH
#ifdef SENSOR_ANALOG_DEBUG
Serial.print(F("Analog:")); Serial.println(value);
#ifdef SENSOR_ANALOG_SMOOTH
Serial.print(F("Smoothed Analog:")); Serial.println(smoothedValue);
#endif // SENSOR_ANALOG_SMOOTH
#endif // SENSOR_ANALOG_DEBUG
#ifdef SENSOR_ANALOG_MS
nextLoopTime = millis() + SENSOR_ANALOG_MS;
}
#endif // SENSOR_ANALOG_MS
}
} //namespace sAnalog
#endif // SENSOR_ANALOG_WANT