File tree Expand file tree Collapse file tree 4 files changed +20
-6
lines changed Expand file tree Collapse file tree 4 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -18,17 +18,16 @@ AnalogIOController::AnalogIOController() {
18
18
_analogio_hardware = new AnalogIOHardware ();
19
19
_analogio_model = new AnalogIOModel ();
20
20
_analogio_hardware->SetResolution (16 ); // Default to 16-bit resolution
21
- SetRefVoltage (0.0 ); // Default to 0.0V
21
+ SetRefVoltage (3.3 ); // Default to 3.3V
22
22
}
23
23
24
24
AnalogIOController::~AnalogIOController () {}
25
25
26
26
void AnalogIOController::SetRefVoltage (float voltage) {
27
- _ref_voltage = voltage;
27
+ // To set the reference voltage, we call into the hardware
28
+ _analogio_hardware->SetReferenceVoltage (voltage);
28
29
}
29
30
30
- float AnalogIOController::GetRefVoltage (void ) { return _ref_voltage; }
31
-
32
31
void AnalogIOController::SetTotalAnalogPins (uint8_t total_pins) {
33
32
_total_analogio_pins = total_pins;
34
33
}
@@ -109,6 +108,7 @@ void AnalogIOController::update() {
109
108
} else if (pin.read_mode ==
110
109
wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW) {
111
110
// TODO: Read and store the pin's raw value
111
+ uint16_t value = _analogio_hardware->GetPinValue (pin.name );
112
112
} else {
113
113
WS_DEBUG_PRINT (" ERROR: Invalid read mode for analog pin: " );
114
114
WS_DEBUG_PRINTLN (pin.name );
Original file line number Diff line number Diff line change @@ -52,15 +52,13 @@ class AnalogIOController {
52
52
53
53
void SetTotalAnalogPins (uint8_t total_pins);
54
54
void SetRefVoltage (float voltage);
55
- float GetRefVoltage (void );
56
55
bool IsPinTimerExpired (analogioPin *pin, ulong cur_time);
57
56
58
57
private:
59
58
AnalogIOModel *_analogio_model; // /< AnalogIO model
60
59
AnalogIOHardware *_analogio_hardware; // /< AnalogIO hardware
61
60
std::vector<analogioPin> _analogio_pins; // /< Vector of analogio pins
62
61
uint8_t _total_analogio_pins; // /< Total number of analogio pins
63
- float _ref_voltage; // /< The device's reference voltage
64
62
};
65
63
extern Wippersnapper_V2 WsV2; // /< Wippersnapper V2 instance
66
64
#endif // WS_ANALOGIO_CONTROLLER_H
Original file line number Diff line number Diff line change @@ -27,6 +27,11 @@ void AnalogIOHardware::DeinitPin(uint8_t pin) {
27
27
pinMode (pin, INPUT); // set to a hi-z floating pin
28
28
}
29
29
30
+ void AnalogIOHardware::SetReferenceVoltage (float voltage) {
31
+ _ref_voltage = voltage;
32
+ _voltage_scale_factor = _ref_voltage / 65536 ;
33
+ }
34
+
30
35
void AnalogIOHardware::SetNativeADCResolution () {
31
36
_is_adc_resolution_scaled = false ;
32
37
#if defined(ARDUINO_ARCH_SAMD)
@@ -75,4 +80,10 @@ uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
75
80
}
76
81
}
77
82
return value;
83
+ }
84
+
85
+ float Wippersnapper_AnalogIO::GetPinVoltage (uint8_t pin) {
86
+ uint16_t raw_value = GetPinValue (pin);
87
+ float voltage = raw_value * _voltage_scale_factor;
88
+ return voltage
78
89
}
Original file line number Diff line number Diff line change @@ -28,16 +28,21 @@ class AnalogIOHardware {
28
28
void SetNativeADCResolution ();
29
29
void SetResolution (uint8_t resolution);
30
30
uint8_t GetResolution (void );
31
+ void SetReferenceVoltage (float voltage);
31
32
void CalculateScaleFactor ();
32
33
34
+ // Arduino/Wiring API
33
35
void InitPin (uint8_t pin);
34
36
void DeinitPin (uint8_t pin);
35
37
uint16_t GetPinValue (uint8_t pin);
38
+ float GetPinVoltage (uint8_t pin);
36
39
37
40
private:
38
41
uint8_t _native_adc_resolution;
39
42
uint8_t _adc_resolution;
40
43
int _scale_factor;
41
44
bool _is_adc_resolution_scaled;
45
+ float _ref_voltage;
46
+ float _voltage_scale_factor;
42
47
};
43
48
#endif // WS_ANALOGIO_HARDWARE_H
You can’t perform that action at this time.
0 commit comments