Match WipperSnapper_Protobuf bed8b12f6f5faec6c5604e204b44a8528e35607a

This commit is contained in:
brentru 2025-01-22 15:36:32 -05:00
parent 6c96a6006c
commit 970bf4c705
5 changed files with 65 additions and 35 deletions

View file

@ -56,11 +56,11 @@ drvBase *createI2CDriverByName(const char *driver_name, TwoWire *i2c,
*/
/***********************************************************************/
I2cController::I2cController() {
_i2c_bus_alt = nullptr;
_i2c_model = new I2cModel();
// Initialize the *default* I2C bus
// TODO: Handle multiple buses, maybe not from here though!
_i2c_hardware = new I2cHardware();
_i2c_hardware->InitDefaultBus();
_i2c_bus_default = new I2cHardware();
_i2c_bus_default->InitDefaultBus();
}
/***********************************************************************/
@ -72,8 +72,8 @@ I2cController::~I2cController() {
if (_i2c_model)
delete _i2c_model;
if (_i2c_hardware)
delete _i2c_hardware;
if (_i2c_bus_default)
delete _i2c_bus_default;
}
/***********************************************************************/
@ -83,7 +83,7 @@ I2cController::~I2cController() {
*/
/***********************************************************************/
bool I2cController::IsBusStatusOK() {
if (!_i2c_hardware->GetBusStatus() ==
if (!_i2c_bus_default->GetBusStatus() ==
wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS)
return false;
return true;
@ -111,6 +111,17 @@ drvBase *I2cController::GetMuxDrv(uint32_t mux_address) {
return nullptr;
}
/***********************************************************************/
/*!
@brief Configures the MUX channel
@param mux_address
The I2C address of the MUX
@param mux_channel
The MUX channel to select
@returns True if the MUX channel was configured successfully, False
otherwise.
*/
/***********************************************************************/
bool I2cController::ConfigureMuxChannel(uint32_t mux_address,
uint32_t mux_channel) {
drvBase *muxDriver = GetMuxDrv(mux_address);
@ -120,11 +131,22 @@ bool I2cController::ConfigureMuxChannel(uint32_t mux_address,
return true;
}
/***********************************************************************/
/*!
@brief Publishes an I2cDeviceAddedorReplaced message to the broker
@param device_descriptor
The I2cDeviceDescriptor message.
@param device_status
The I2cDeviceStatus message.
@returns True if the I2cDeviceAddedorReplaced message was published
successfully, False otherwise.
*/
/***********************************************************************/
bool I2cController::PublishI2cDeviceAddedorReplaced(
const wippersnapper_i2c_I2cDeviceDescriptor &device_descriptor,
const wippersnapper_i2c_I2cDeviceStatus &device_status) {
if (!_i2c_model->encodeMsgI2cDeviceAddedorReplaced(
device_descriptor, _i2c_hardware->GetBusStatus(), device_status)) {
device_descriptor, _i2c_bus_default->GetBusStatus(), device_status)) {
WS_DEBUG_PRINTLN(
"[i2c] ERROR: Unable to encode I2cDeviceAddedorReplaced message!");
return false;
@ -185,10 +207,15 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
// TODO: Handle Replace messages by implementing a Remove handler first...then
// proceed to adding a new device
// NOTE: This is only using the default bus, for now
// TODO: Implement the ability to work with > 1 i2c hardware bus
WS_DEBUG_PRINTLN("[i2c] Initializing I2C driver...");
// Does the device's descriptor specify a different i2c bus?
if (device_descriptor.i2c_bus != 0) {
WS_DEBUG_PRINTLN("[i2c] Attempting to initialize a new i2c bus object!");
_i2c_bus_default = new I2cHardware();
_i2c_bus_default->InitDefaultBus();
}
// Does the device's descriptor have a mux channel?
// TODO: We will need to modify the sdconfig parser to reflect "no value" set
// == the 0xFFFF magic value
@ -207,7 +234,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
WS_DEBUG_PRINTLN("Creating a new I2C driver obj");
drvBase *drv = createI2CDriverByName(
_i2c_model->GetI2cDeviceAddOrReplaceMsg()->i2c_device_name,
_i2c_hardware->GetI2cBus(), device_descriptor.i2c_device_address,
_i2c_bus_default->GetI2cBus(), device_descriptor.i2c_device_address,
device_descriptor.i2c_mux_channel, device_status);
// TODO: Clean up for clarity - confusing checks and returns

View file

@ -41,15 +41,17 @@ public:
bool Handle_I2cBusScan(pb_istream_t *stream);
bool Handle_I2cDeviceAddOrReplace(pb_istream_t *stream);
bool Handle_I2cDeviceRemove(pb_istream_t *stream);
// Publishing
bool PublishI2cDeviceAddedorReplaced(const wippersnapper_i2c_I2cDeviceDescriptor& device_descriptor, const wippersnapper_i2c_I2cDeviceStatus& device_status);
// Helpers
bool IsBusStatusOK();
bool ConfigureMuxChannel(uint32_t mux_address, uint32_t mux_channel);
drvBase* GetMuxDrv(uint32_t mux_address);
bool PublishI2cDeviceAddedorReplaced(const wippersnapper_i2c_I2cDeviceDescriptor& device_descriptor, const wippersnapper_i2c_I2cDeviceStatus& device_status);
private:
I2cModel *_i2c_model; ///< I2c model
I2cHardware *_i2c_hardware; ///< I2c hardware
std::vector<drvBase *> _i2c_drivers;
I2cModel *_i2c_model; ///< Pointer to an I2C model object
I2cHardware *_i2c_bus_default; ///< Pointer to the default I2C bus
I2cHardware *_i2c_bus_alt; ///< Pointer to an alternative I2C bus
std::vector<drvBase *> _i2c_drivers; ///< Vector of ptrs to I2C device drivers
};
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
#endif // WS_I2C_CONTROLLER_H

View file

@ -30,11 +30,9 @@ class I2cHardware {
public:
I2cHardware();
~I2cHardware();
// TODO
void InitDefaultBus();
TwoWire *GetI2cBus() { return _i2c_bus; }
wippersnapper_i2c_I2cBusStatus GetBusStatus() { return _bus_status; }
private:
wippersnapper_i2c_I2cBusStatus _bus_status;
void ToggleI2CPowerPin();

View file

@ -12,7 +12,7 @@ PB_BIND(wippersnapper_i2c_I2cDeviceDescriptor, wippersnapper_i2c_I2cDeviceDescri
PB_BIND(wippersnapper_i2c_I2cBusScan, wippersnapper_i2c_I2cBusScan, AUTO)
PB_BIND(wippersnapper_i2c_I2cBusScanned, wippersnapper_i2c_I2cBusScanned, 2)
PB_BIND(wippersnapper_i2c_I2cBusScanned, wippersnapper_i2c_I2cBusScanned, 4)
PB_BIND(wippersnapper_i2c_I2cDeviceAddOrReplace, wippersnapper_i2c_I2cDeviceAddOrReplace, AUTO)

File diff suppressed because one or more lines are too long