Skip to content

Commit 4ca038c

Browse files
committed
🚧 WIP, analogio - Fix compiler errors and build pass
1 parent b9f7c12 commit 4ca038c

File tree

8 files changed

+91
-40
lines changed

8 files changed

+91
-40
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,10 @@ bool handleCheckinResponse(pb_istream_t *stream) {
300300
// Configure GPIO classes based on checkin response message
301301
WsV2.digital_io_controller->SetMaxDigitalPins(
302302
WsV2.CheckInModel->getTotalGPIOPins());
303-
WsV2._analogIOV2 =
304-
new Wippersnapper_AnalogIO(WsV2.CheckInModel->getTotalAnalogPins(),
305-
WsV2.CheckInModel->getReferenceVoltage());
303+
304+
WsV2.analogio_controller->SetRefVoltage(WsV2.CheckInModel->getReferenceVoltage());
305+
WsV2.analogio_controller->SetTotalAnalogPins(
306+
WsV2.CheckInModel->getTotalAnalogPins());
306307

307308
// set glob flag so we don't keep the polling loop open
308309
WsV2.got_checkin_response = true;

src/Wippersnapper_V2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "components/checkin/model.h"
5252
#include "components/sensor/model.h"
5353
#include "components/digitalIO/controller.h"
54+
#include "components/analogio/controller.h"
5455

5556

5657
// Components (API v1)

src/components/analogIO/controller.cpp

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@
1515
#include "controller.h"
1616

1717
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
2222
}
2323

2424
AnalogIOController::~AnalogIOController() {}
2525

26-
2726
void 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

3536
bool 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
}

src/components/analogIO/controller.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class AnalogIOHardware; ///< Forward declaration
2727
* @brief Structure repesenting an analogio pin on the device.
2828
*/
2929
struct analogioPin {
30-
uint8_t name; ///< The pin's name.
31-
float period; ///< The pin's period, in milliseconds.
32-
float prv_period; ///< The pin's previous period, in milliseconds.
33-
wippersnapper_sensor_SensorType read_mode; ///< Type of analog read to perform
34-
}
30+
uint8_t name; ///< The pin's name.
31+
float period; ///< The pin's period, in milliseconds.
32+
float prv_period; ///< The pin's previous period, in milliseconds.
33+
wippersnapper_sensor_SensorType read_mode; ///< Type of analog read to perform
34+
};
3535

3636
/**************************************************************************/
3737
/*!
@@ -44,10 +44,14 @@ class AnalogIOController {
4444
public:
4545
AnalogIOController();
4646
~AnalogIOController();
47+
// Routing functions
4748
bool Handle_AnalogIOAdd(pb_istream_t *stream);
49+
bool Handle_AnalogIORemove(pb_istream_t *stream);
4850

51+
void SetTotalAnalogPins(uint8_t total_pins);
4952
void SetRefVoltage(float voltage);
5053
float GetRefVoltage(void);
54+
5155
private:
5256
AnalogIOModel *_analogio_model; ///< AnalogIO model
5357
AnalogIOHardware *_analogio_hardware; ///< AnalogIO hardware

src/components/analogIO/hardware.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ AnalogIOHardware::AnalogIOHardware() {
2020

2121
AnalogIOHardware::~AnalogIOHardware() {}
2222

23-
void AnalogIOHardware::InitPin(uint8_t pin) {
24-
pinMode(pin, INPUT);
23+
void AnalogIOHardware::InitPin(uint8_t pin) { pinMode(pin, INPUT); }
24+
25+
void AnalogIOHardware::DeinitPin(uint8_t pin) {
26+
pinMode(pin, INPUT); // set to a hi-z floating pin
2527
}
2628

2729
void AnalogIOHardware::SetNativeADCResolution() {
28-
_is_adc_resolution_scaled = false;
30+
_is_adc_resolution_scaled = false;
2931
#if defined(ARDUINO_ARCH_SAMD)
3032
analogReadResolution(16);
3133
_native_adc_resolution = 12;
@@ -42,10 +44,7 @@ _is_adc_resolution_scaled = false;
4244
}
4345

4446
void AnalogIOHardware::SetResolution(uint8_t resolution) {
45-
_adcResolution = resolution;
47+
_adc_resolution = resolution;
4648
}
4749

48-
49-
uint8_t AnalogIOHardware::GetResolution(void) {
50-
return _adc_resolution;
51-
}
50+
uint8_t AnalogIOHardware::GetResolution(void) { return _adc_resolution; }

src/components/analogIO/hardware.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class AnalogIOHardware {
3030
uint8_t GetResolution(void);
3131

3232
void InitPin(uint8_t pin);
33+
void DeinitPin(uint8_t pin);
34+
3335
private:
3436
uint8_t _native_adc_resolution;
3537
uint8_t _adc_resolution;

src/components/analogIO/model.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,35 @@
1717
// TODO! Implement the AnalogIOModel class
1818

1919
AnalogIOModel::AnalogIOModel() {
20-
_msg_AnalogioAdd = wippersnapper_analogio_AnalogIOAdd_init_default;
20+
_msg_AnalogioAdd = wippersnapper_analogio_AnalogIOAdd_init_default;
2121
}
2222

2323
AnalogIOModel::~AnalogIOModel() {}
2424

2525
bool AnalogIOModel::DecodeAnalogIOAdd(pb_istream_t *stream) {
26-
// Zero-out the AnalogIOAdd message struct. to ensure we don't have any old data
27-
_msg_AnalogioAdd = wippersnapper_analogio_AnalogIOAdd_init_default;
26+
// Zero-out the AnalogIOAdd message struct. to ensure we don't have any old
27+
// data
28+
_msg_AnalogioAdd = wippersnapper_analogio_AnalogIOAdd_init_default;
2829

29-
// Decode the stream into a AnalogIOAdd message
30-
return pb_decode(stream, wippersnapper_analogio_AnalogIOAdd_fields, &_msg_AnalogioAdd);
30+
// Decode the stream into a AnalogIOAdd message
31+
return pb_decode(stream, wippersnapper_analogio_AnalogIOAdd_fields,
32+
&_msg_AnalogioAdd);
3133
}
3234

3335
wippersnapper_analogio_AnalogIOAdd *AnalogIOModel::GetAnalogIOAddMsg() {
34-
return &_msg_AnalogioAdd;
36+
return &_msg_AnalogioAdd;
37+
}
38+
39+
bool AnalogIOModel::DecodeAnalogIORemove(pb_istream_t *stream) {
40+
// Zero-out the AnalogIORemove message struct. to ensure we don't have any old
41+
// data
42+
_msg_AnalogioRemove = wippersnapper_analogio_AnalogIORemove_init_default;
43+
44+
// Decode the stream into a AnalogIORemove message
45+
return pb_decode(stream, wippersnapper_analogio_AnalogIORemove_fields,
46+
&_msg_AnalogioRemove);
47+
}
48+
49+
wippersnapper_analogio_AnalogIORemove *AnalogIOModel::GetAnalogIORemoveMsg() {
50+
return &_msg_AnalogioRemove;
3551
}

src/components/analogIO/model.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ class AnalogIOModel {
2929
// AnalogIOAdd
3030
bool DecodeAnalogIOAdd(pb_istream_t *stream);
3131
wippersnapper_analogio_AnalogIOAdd *GetAnalogIOAddMsg();
32+
// AnalogIORemove
33+
bool DecodeAnalogIORemove(pb_istream_t *stream);
34+
wippersnapper_analogio_AnalogIORemove *GetAnalogIORemoveMsg();
35+
3236
private:
3337
wippersnapper_analogio_AnalogIOAdd _msg_AnalogioAdd;
38+
wippersnapper_analogio_AnalogIORemove _msg_AnalogioRemove;
3439
};
3540
#endif // WS_DIGITALIO_MODEL_H

0 commit comments

Comments
 (0)