1515#include " controller.h"
1616
1717AnalogIOController::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
2222}
2323
2424AnalogIOController::~AnalogIOController () {}
2525
26-
2726void AnalogIOController::SetRefVoltage (float voltage) {
28- _ref_voltage = voltage;
27+ _ref_voltage = voltage;
2928}
3029
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;
3334}
3435
3536bool AnalogIOController::Handle_AnalogIOAdd (pb_istream_t *stream) {
@@ -39,24 +40,46 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
3940 return false ;
4041 }
4142
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
4544 int pin_name = atoi (_analogio_model->GetAnalogIOAddMsg ()->pin_name + 1 );
45+
4646 // 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!
4849 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 };
5454
55- // TODO: Initialize the pin in the hardware layer
55+ // Initialize the pin
5656 _analogio_hardware->InitPin (pin_name);
5757
5858 // Add the new pin to the vector
5959 _analogio_pins.push_back (new_pin);
6060
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+ }
6184 return true ;
6285}
0 commit comments