Skip to content

Commit 49e110d

Browse files
committed
🚧 WIP, analogio - Create controller, hardware, model and hooks for AnalogIOAdd msg
1 parent c20e45b commit 49e110d

File tree

8 files changed

+290
-0
lines changed

8 files changed

+290
-0
lines changed

‎src/Wippersnapper_V2.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Wippersnapper_V2::Wippersnapper_V2() {
7373

7474
// Initialize controller classes
7575
WsV2.digital_io_controller = new DigitalIOController();
76+
WsV2.analogio_controller = new AnalogIOController();
7677
};
7778

7879
/**************************************************************************/
@@ -358,6 +359,13 @@ bool cbDecodeBrokerToDevice(pb_istream_t *stream, const pb_field_t *field,
358359
return false;
359360
}
360361
break;
362+
case wippersnapper_signal_BrokerToDevice_analogio_add_tag:
363+
WS_DEBUG_PRINTLN("-> AnalogIO Add Message Type");
364+
if (!WsV2.analogio_controller->Handle_AnalogIOAdd(stream)) {
365+
WS_DEBUG_PRINTLN("ERROR: Unable to add analogio pin!");
366+
return false;
367+
}
368+
break;
361369
default:
362370
WS_DEBUG_PRINTLN("ERROR: BrokerToDevice message type not found!");
363371
return false;

‎src/Wippersnapper_V2.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class ws_uart;
114114
class CheckinModel;
115115
class SensorModel;
116116
class DigitalIOController;
117+
class AnalogIOController;
117118

118119
/**************************************************************************/
119120
/*!
@@ -220,6 +221,7 @@ class Wippersnapper_V2 {
220221
SensorModel *sensorModel; ///< Instance of SensorModel class
221222
DigitalIOController
222223
*digital_io_controller; ///< Instance of DigitalIO controller class
224+
AnalogIOController *analogio_controller; ///< Instance of AnalogIO controller
223225

224226
// TODO: does this really need to be global?
225227
uint8_t _macAddrV2[6]; /*!< Unique network iface identifier */
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*!
2+
* @file controller.cpp
3+
*
4+
* Controller for the analogio.proto API
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
11+
*
12+
* BSD license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#include "controller.h"
16+
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
22+
}
23+
24+
AnalogIOController::~AnalogIOController() {}
25+
26+
27+
void AnalogIOController::SetRefVoltage(float voltage) {
28+
_ref_voltage = voltage;
29+
}
30+
31+
float AnalogIOController::GetRefVoltage(void) {
32+
return _ref_voltage;
33+
}
34+
35+
bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
36+
// Attempt to decode the incoming message into an AnalogIOAdd object
37+
if (!_analogio_model->DecodeAnalogIOAdd(stream)) {
38+
WS_DEBUG_PRINTLN("ERROR: Unable to decode AnalogIOAdd message");
39+
return false;
40+
}
41+
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
45+
int pin_name = atoi(_analogio_model->GetAnalogIOAddMsg()->pin_name + 1);
46+
// Create a new analogioPin object
47+
// TODO: Replicate this within the digitalio controller, much cleaner way to assign!
48+
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+
};
54+
55+
// TODO: Initialize the pin in the hardware layer
56+
_analogio_hardware->InitPin(pin_name);
57+
58+
// Add the new pin to the vector
59+
_analogio_pins.push_back(new_pin);
60+
61+
return true;
62+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*!
2+
* @file controller.h
3+
*
4+
* Controller for the AnalogIO API
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
11+
*
12+
* BSD license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WS_ANALOGIO_CONTROLLER_H
16+
#define WS_ANALOGIO_CONTROLLER_H
17+
#include "Wippersnapper_V2.h"
18+
#include "hardware.h"
19+
#include "model.h"
20+
21+
class Wippersnapper_V2; ///< Forward declaration
22+
class AnalogIOModel; ///< Forward declaration
23+
class AnalogIOHardware; ///< Forward declaration
24+
25+
/**
26+
* @struct analogioPin
27+
* @brief Structure repesenting an analogio pin on the device.
28+
*/
29+
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+
}
35+
36+
/**************************************************************************/
37+
/*!
38+
@brief Routes messages using the analogio.proto API to the
39+
appropriate hardware and model classes, controls and tracks
40+
the state of the hardware's digital I/O pins.
41+
*/
42+
/**************************************************************************/
43+
class AnalogIOController {
44+
public:
45+
AnalogIOController();
46+
~AnalogIOController();
47+
bool Handle_AnalogIOAdd(pb_istream_t *stream);
48+
49+
void SetRefVoltage(float voltage);
50+
float GetRefVoltage(void);
51+
private:
52+
AnalogIOModel *_analogio_model; ///< AnalogIO model
53+
AnalogIOHardware *_analogio_hardware; ///< AnalogIO hardware
54+
std::vector<analogioPin> _analogio_pins; ///< Vector of analogio pins
55+
uint8_t _total_analogio_pins; ///< Total number of analogio pins
56+
float _ref_voltage; ///< The device's reference voltage
57+
};
58+
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
59+
#endif // WS_ANALOGIO_CONTROLLER_H
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*!
2+
* @file hardware.cpp
3+
*
4+
* Hardware interface for the analogio.proto API
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
11+
*
12+
* BSD license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#include "hardware.h"
16+
17+
AnalogIOHardware::AnalogIOHardware() {
18+
SetNativeADCResolution(); // Configure the device's native ADC resolution
19+
}
20+
21+
AnalogIOHardware::~AnalogIOHardware() {}
22+
23+
void AnalogIOHardware::InitPin(uint8_t pin) {
24+
pinMode(pin, INPUT);
25+
}
26+
27+
void AnalogIOHardware::SetNativeADCResolution() {
28+
_is_adc_resolution_scaled = false;
29+
#if defined(ARDUINO_ARCH_SAMD)
30+
analogReadResolution(16);
31+
_native_adc_resolution = 12;
32+
#elif defined(ARDUINO_ARCH_ESP32)
33+
_is_adc_resolution_scaled = true;
34+
_native_adc_resolution = 13;
35+
#elif defined(ARDUINO_ARCH_RP2040)
36+
_is_adc_resolution_scaled = true;
37+
_native_adc_resolution = 10;
38+
#else
39+
_is_adc_resolution_scaled = true;
40+
_native_adc_resolution = 10;
41+
#endif
42+
}
43+
44+
void AnalogIOHardware::SetResolution(uint8_t resolution) {
45+
_adcResolution = resolution;
46+
}
47+
48+
49+
uint8_t AnalogIOHardware::GetResolution(void) {
50+
return _adc_resolution;
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*!
2+
* @file model.h
3+
*
4+
* Hardware implementation for the analogio.proto message.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
11+
*
12+
* BSD license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WS_ANALOGIO_HARDWARE_H
16+
#define WS_ANALOGIO_HARDWARE_H
17+
#include "Wippersnapper_V2.h"
18+
19+
/**************************************************************************/
20+
/*!
21+
@brief Interface for interacting with hardware's analog i/o pin API.
22+
*/
23+
/**************************************************************************/
24+
class AnalogIOHardware {
25+
public:
26+
AnalogIOHardware();
27+
~AnalogIOHardware();
28+
void SetNativeADCResolution();
29+
void SetResolution(uint8_t resolution);
30+
uint8_t GetResolution(void);
31+
32+
void InitPin(uint8_t pin);
33+
private:
34+
uint8_t _native_adc_resolution;
35+
uint8_t _adc_resolution;
36+
bool _is_adc_resolution_scaled;
37+
};
38+
#endif // WS_ANALOGIO_HARDWARE_H
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*!
2+
* @file model.cpp
3+
*
4+
* Interfaces for the analogio.proto API
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
11+
*
12+
* BSD license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#include "model.h"
16+
17+
// TODO! Implement the AnalogIOModel class
18+
19+
AnalogIOModel::AnalogIOModel() {
20+
_msg_AnalogioAdd = wippersnapper_analogio_AnalogIOAdd_init_default;
21+
}
22+
23+
AnalogIOModel::~AnalogIOModel() {}
24+
25+
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;
28+
29+
// Decode the stream into a AnalogIOAdd message
30+
return pb_decode(stream, wippersnapper_analogio_AnalogIOAdd_fields, &_msg_AnalogioAdd);
31+
}
32+
33+
wippersnapper_analogio_AnalogIOAdd *AnalogIOModel::GetAnalogIOAddMsg() {
34+
return &_msg_AnalogioAdd;
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*!
2+
* @file model.h
3+
*
4+
* Model interface for the analogio.proto message.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
11+
*
12+
* BSD license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WS_ANALOGIO_MODEL_H
16+
#define WS_ANALOGIO_MODEL_H
17+
#include "Wippersnapper_V2.h"
18+
19+
/**************************************************************************/
20+
/*!
21+
@brief Provides an interface for creating, encoding, and parsing
22+
messages from analogio.proto.
23+
*/
24+
/**************************************************************************/
25+
class AnalogIOModel {
26+
public:
27+
AnalogIOModel();
28+
~AnalogIOModel();
29+
// AnalogIOAdd
30+
bool DecodeAnalogIOAdd(pb_istream_t *stream);
31+
wippersnapper_analogio_AnalogIOAdd *GetAnalogIOAddMsg();
32+
private:
33+
wippersnapper_analogio_AnalogIOAdd _msg_AnalogioAdd;
34+
};
35+
#endif // WS_DIGITALIO_MODEL_H

0 commit comments

Comments
 (0)