15
15
#include " controller.h"
16
16
17
17
AnalogIOController::AnalogIOController () {
18
- _analogio_hardware = new AnalogIOHardware ();
19
- _analogio_model = new AnalogIOModel ();
20
- _analogio_hardware->SetResolution (16 ); // Default to 16-bit resolution
21
- SetRefVoltage (0.0 ); // Default to 0.0V
18
+ _analogio_hardware = new AnalogIOHardware ();
19
+ _analogio_model = new AnalogIOModel ();
20
+ _analogio_hardware->SetResolution (16 ); // Default to 16-bit resolution
21
+ SetRefVoltage (0.0 ); // Default to 0.0V
22
22
}
23
23
24
24
AnalogIOController::~AnalogIOController () {}
25
25
26
-
27
26
void AnalogIOController::SetRefVoltage (float voltage) {
28
- _ref_voltage = voltage;
27
+ _ref_voltage = voltage;
29
28
}
30
29
31
- float AnalogIOController::GetRefVoltage (void ) {
32
- return _ref_voltage;
30
+ float AnalogIOController::GetRefVoltage (void ) { return _ref_voltage; }
31
+
32
+ void AnalogIOController::SetTotalAnalogPins (uint8_t total_pins) {
33
+ _total_analogio_pins = total_pins;
33
34
}
34
35
35
36
bool AnalogIOController::Handle_AnalogIOAdd (pb_istream_t *stream) {
@@ -39,24 +40,46 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
39
40
return false ;
40
41
}
41
42
42
- // TODO TOMORROW: pin_name is a pb_callback_t, not an char, maybe an issue with
43
- // how options picks things up? First, check the newly compiled output bc may
44
- // have fixed
43
+ // Get the pin name
45
44
int pin_name = atoi (_analogio_model->GetAnalogIOAddMsg ()->pin_name + 1 );
45
+
46
46
// Create a new analogioPin object
47
- // TODO: Replicate this within the digitalio controller, much cleaner way to assign!
47
+ // TODO: Replicate this within the digitalio controller, much cleaner way to
48
+ // assign!
48
49
analogioPin new_pin = {
49
- .name = pin_name,
50
- .period = long (_analogio_model->GetAnalogIOAddMsg ()->period ) * 1000 ,
51
- .prv_period = 0 ,
52
- .read_mode = _analogio_model->GetAnalogIOAddMsg ()->read_mode
53
- };
50
+ .name = pin_name,
51
+ .period = long (_analogio_model->GetAnalogIOAddMsg ()->period ) * 1000 ,
52
+ .prv_period = 0 ,
53
+ .read_mode = _analogio_model->GetAnalogIOAddMsg ()->read_mode };
54
54
55
- // TODO: Initialize the pin in the hardware layer
55
+ // Initialize the pin
56
56
_analogio_hardware->InitPin (pin_name);
57
57
58
58
// Add the new pin to the vector
59
59
_analogio_pins.push_back (new_pin);
60
60
61
+ return true ;
62
+ }
63
+
64
+ bool AnalogIOController::Handle_AnalogIORemove (pb_istream_t *stream) {
65
+ // Attempt to decode the incoming message into an AnalogIORemove object
66
+ if (!_analogio_model->DecodeAnalogIORemove (stream)) {
67
+ WS_DEBUG_PRINTLN (" ERROR: Unable to decode AnalogIORemove message" );
68
+ return false ;
69
+ }
70
+
71
+ // Get the pin name
72
+ int pin_name = atoi (_analogio_model->GetAnalogIORemoveMsg ()->pin_name + 1 );
73
+
74
+ // Remove the pin from the hardware
75
+ _analogio_hardware->DeinitPin (pin_name);
76
+
77
+ // Remove the pin from the vector
78
+ for (int i = 0 ; i < _analogio_pins.size (); i++) {
79
+ if (_analogio_pins[i].name == pin_name) {
80
+ _analogio_pins.erase (_analogio_pins.begin () + i);
81
+ break ;
82
+ }
83
+ }
61
84
return true ;
62
85
}
0 commit comments