Skip to content

Commit fcd87d1

Browse files
committed
🚧 WIP, analogio - Add deinit, scale reads, update
1 parent 4ca038c commit fcd87d1

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

src/components/analogIO/controller.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,44 @@ bool AnalogIOController::Handle_AnalogIORemove(pb_istream_t *stream) {
7575
_analogio_hardware->DeinitPin(pin_name);
7676

7777
// Remove the pin from the vector
78+
// TODO: Refactor this out?
7879
for (int i = 0; i < _analogio_pins.size(); i++) {
7980
if (_analogio_pins[i].name == pin_name) {
8081
_analogio_pins.erase(_analogio_pins.begin() + i);
8182
break;
8283
}
8384
}
8485
return true;
86+
}
87+
88+
bool AnalogIOController::IsPinTimerExpired(analogioPin *pin, ulong cur_time) {
89+
return cur_time - pin->prv_pin_time > pin->pin_period;
90+
}
91+
92+
void AnalogIOController::update() {
93+
// Bail-out if the vector is empty
94+
if (_analogio_pins.empty())
95+
return;
96+
97+
// Process analog input pins
98+
for (int i = 0; i < _analogio_pins.size(); i++) {
99+
// Create a pin object for this iteration
100+
analogioPin &pin = _analogio_pins[i];
101+
// Go to the next pin if the period hasn't expired yet
102+
ulong cur_time = millis();
103+
if (!IsPinTimerExpired(&pin, cur_time))
104+
continue;
105+
106+
// Pins timer has expired, lets read the pin
107+
if (pin.read_mode == wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE) {
108+
// TODO: Read and store the pin's voltage
109+
} else if (pin.read_mode ==
110+
wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW) {
111+
// TODO: Read and store the pin's raw value
112+
} else {
113+
WS_DEBUG_PRINT("ERROR: Invalid read mode for analog pin: ");
114+
WS_DEBUG_PRINTLN(pin.name);
115+
continue;
116+
}
117+
}
85118
}

src/components/analogIO/controller.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ class AnalogIOController {
4747
// Routing functions
4848
bool Handle_AnalogIOAdd(pb_istream_t *stream);
4949
bool Handle_AnalogIORemove(pb_istream_t *stream);
50+
// Polling loop function
51+
void update();
5052

5153
void SetTotalAnalogPins(uint8_t total_pins);
5254
void SetRefVoltage(float voltage);
5355
float GetRefVoltage(void);
56+
bool IsPinTimerExpired(analogioPin *pin, ulong cur_time);
5457

5558
private:
5659
AnalogIOModel *_analogio_model; ///< AnalogIO model

src/components/analogIO/hardware.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
AnalogIOHardware::AnalogIOHardware() {
1818
SetNativeADCResolution(); // Configure the device's native ADC resolution
19+
_scale_factor = 0;
1920
}
2021

2122
AnalogIOHardware::~AnalogIOHardware() {}
@@ -45,6 +46,33 @@ void AnalogIOHardware::SetNativeADCResolution() {
4546

4647
void AnalogIOHardware::SetResolution(uint8_t resolution) {
4748
_adc_resolution = resolution;
49+
// Calculate (or re-calculate) the scale factor when we set the resolution
50+
CalculateScaleFactor();
4851
}
4952

50-
uint8_t AnalogIOHardware::GetResolution(void) { return _adc_resolution; }
53+
uint8_t AnalogIOHardware::GetResolution(void) { return _adc_resolution; }
54+
55+
void AnalogIOHardware::CalculateScaleFactor() {
56+
if (!_is_adc_resolution_scaled)
57+
return;
58+
59+
if (getADCresolution() > getNativeResolution()) {
60+
_scale_factor = getADCresolution() - getNativeResolution();
61+
} else {
62+
_scale_factor = getNativeResolution() - getADCresolution();
63+
}
64+
}
65+
66+
uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
67+
// Get the pin's value using Arduino API
68+
uint16_t value = analogRead(pin);
69+
// Scale the pins value
70+
if (_is_adc_resolution_scaled) {
71+
if (getADCresolution() > getNativeResolution()) {
72+
value = value << _scale_factor;
73+
} else {
74+
value = value >> _scale_factor;
75+
}
76+
}
77+
return value;
78+
}

src/components/analogIO/hardware.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ class AnalogIOHardware {
2828
void SetNativeADCResolution();
2929
void SetResolution(uint8_t resolution);
3030
uint8_t GetResolution(void);
31+
void CalculateScaleFactor();
3132

3233
void InitPin(uint8_t pin);
3334
void DeinitPin(uint8_t pin);
35+
uint16_t GetPinValue(uint8_t pin);
3436

3537
private:
3638
uint8_t _native_adc_resolution;
3739
uint8_t _adc_resolution;
40+
int _scale_factor;
3841
bool _is_adc_resolution_scaled;
3942
};
4043
#endif // WS_ANALOGIO_HARDWARE_H

0 commit comments

Comments
 (0)