Skip to content

Commit eb2cfa1

Browse files
committed
Writing working for everything but floats
1 parent c9a20b3 commit eb2cfa1

File tree

3 files changed

+109
-57
lines changed

3 files changed

+109
-57
lines changed

src/components/i2c/controller.cpp

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -455,42 +455,50 @@ I2cController::~I2cController() {
455455
@brief Removes an I2C driver from the controller and frees memory
456456
@param address
457457
The desired I2C device's address.
458+
@param is_output_device
459+
True if the driver is an output device, False otherwise.
458460
@returns True if the driver was removed, False otherwise.
459461
*/
460462
/***********************************************************************/
461-
bool I2cController::RemoveDriver(uint32_t address) {
462-
// Safely remove the i2c sensor driver from the vector and free memory
463-
for (drvBase *driver : _i2c_drivers) {
464-
if (driver == nullptr)
465-
continue;
463+
bool I2cController::RemoveDriver(uint32_t address, bool is_output_device) {
464+
if (!is_output_device) {
465+
// Safely remove the i2c sensor driver from the vector and free memory
466+
for (drvBase *driver : _i2c_drivers) {
467+
if (driver == nullptr)
468+
continue;
466469

467-
if (driver->GetAddress() != address)
468-
continue;
470+
if (driver->GetAddress() != address)
471+
continue;
469472

470-
auto it = std::find(_i2c_drivers.begin(), _i2c_drivers.end(), driver);
471-
if (it != _i2c_drivers.end()) {
472-
_i2c_drivers.erase(it);
473+
auto it = std::find(_i2c_drivers.begin(), _i2c_drivers.end(), driver);
474+
if (it != _i2c_drivers.end()) {
475+
_i2c_drivers.erase(it);
476+
}
477+
delete driver;
478+
return true;
473479
}
474-
delete driver;
475-
return true;
476-
}
477-
478-
// This was an output driver type, safely remove the i2c output driver from the vector and free memory
479-
for (drvOutputBase *driver : _i2c_drivers_output) {
480-
if (driver == nullptr)
481-
continue;
480+
} else {
481+
// This was an output driver type, safely remove the i2c output driver from
482+
// the vector and free memory
483+
for (drvOutputBase *driver : _i2c_drivers_output) {
484+
if (driver == nullptr)
485+
continue;
482486

483-
if (driver->GetAddress() != address)
484-
continue;
487+
if (driver->GetAddress() != address)
488+
continue;
485489

486-
auto it = std::find(_i2c_drivers_output.begin(), _i2c_drivers_output.end(),
487-
driver);
488-
if (it != _i2c_drivers_output.end()) {
489-
_i2c_drivers_output.erase(it);
490+
auto it = std::find(_i2c_drivers_output.begin(),
491+
_i2c_drivers_output.end(), driver);
492+
if (it != _i2c_drivers_output.end()) {
493+
_i2c_drivers_output.erase(it);
494+
}
495+
delete driver;
496+
return true;
490497
}
491-
delete driver;
492-
return true;
493498
}
499+
500+
// We didn't find the driver to remove
501+
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to find driver to remove!");
494502
return false;
495503
}
496504

@@ -585,8 +593,8 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
585593
strlen(msgRemove->i2c_device_description.i2c_bus_sda) == 0) {
586594
WS_DEBUG_PRINTLN("[i2c] Removing device from default bus...");
587595
if (!_i2c_bus_default->HasMux()) {
588-
// TODO: Implement remove, straightforward
589-
if (!RemoveDriver(msgRemove->i2c_device_description.i2c_device_address)) {
596+
if (!RemoveDriver(msgRemove->i2c_device_description.i2c_device_address,
597+
msgRemove->is_output_device)) {
590598
WS_DEBUG_PRINTLN(
591599
"[i2c] ERROR: Failed to remove i2c device from default bus!");
592600
did_remove = false;
@@ -596,11 +604,10 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
596604
// Case 1: Is the I2C device connected to a MUX?
597605
if (msgRemove->i2c_device_description.i2c_mux_address != 0xFFFF &&
598606
msgRemove->i2c_device_description.i2c_mux_channel >= 0) {
599-
// TODO: Remove the device from the mux's channel and delete the driver
600607
_i2c_bus_default->SelectMuxChannel(
601608
msgRemove->i2c_device_description.i2c_mux_channel);
602-
if (!RemoveDriver(
603-
msgRemove->i2c_device_description.i2c_device_address)) {
609+
if (!RemoveDriver(msgRemove->i2c_device_description.i2c_device_address,
610+
msgRemove->is_output_device)) {
604611
WS_DEBUG_PRINTLN(
605612
"[i2c] ERROR: Failed to remove i2c device from default bus!");
606613
did_remove = false;
@@ -609,16 +616,14 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
609616
// Case 2: Is the I2C device a MUX?
610617
if (msgRemove->i2c_device_description.i2c_device_address ==
611618
msgRemove->i2c_device_description.i2c_mux_address) {
612-
// TODO: Scan the mux to see what drivers are attached?
613619
wippersnapper_i2c_I2cBusScanned scan_results;
614620
_i2c_bus_default->ScanMux(&scan_results);
615621
for (int i = 0; i < scan_results.i2c_bus_found_devices_count; i++) {
616-
scan_results.i2c_bus_found_devices[i].i2c_mux_channel);
617622
// Select the channel and remove the device
618623
_i2c_bus_default->SelectMuxChannel(
619624
scan_results.i2c_bus_found_devices[i].i2c_mux_channel);
620-
RemoveDriver(
621-
scan_results.i2c_bus_found_devices[i].i2c_device_address);
625+
RemoveDriver(scan_results.i2c_bus_found_devices[i].i2c_device_address,
626+
msgRemove->is_output_device);
622627
}
623628
_i2c_bus_default->RemoveMux();
624629
}
@@ -786,7 +791,8 @@ bool I2cController::Handle_I2cDeviceOutputWrite(pb_istream_t *stream) {
786791
"message!");
787792
return false;
788793
}
789-
wippersnapper_i2c_I2cDeviceDescriptor descriptor = _i2c_model->GetI2cDeviceOutputWriteMsg()->i2c_device_description;
794+
wippersnapper_i2c_I2cDeviceDescriptor descriptor =
795+
_i2c_model->GetI2cDeviceOutputWriteMsg()->i2c_device_description;
790796

791797
// Attempt to find the driver
792798
drvOutputBase *driver = nullptr;
@@ -816,8 +822,9 @@ bool I2cController::Handle_I2cDeviceOutputWrite(pb_istream_t *stream) {
816822

817823
// Determine which driver cb function to use
818824
if (_i2c_model->GetI2cDeviceOutputWriteMsg()->has_led_backpack_write) {
819-
WS_DEBUG_PRINT("[i2c] Writing message to LED backpack...");
820-
if (!driver->LedBackpackWrite(&_i2c_model->GetI2cDeviceOutputWriteMsg()->led_backpack_write)) {
825+
WS_DEBUG_PRINTLN("[i2c] Writing message to LED backpack...");
826+
if (!driver->LedBackpackWrite(
827+
&_i2c_model->GetI2cDeviceOutputWriteMsg()->led_backpack_write)) {
821828
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to write to LED backpack!");
822829
return false;
823830
}
@@ -942,12 +949,14 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
942949
did_init = true;
943950
}
944951
} else {
952+
WS_DEBUG_PRINT("[i2c] Creating an I2C output driver...");
945953
drv_out = CreateI2cOutputDrv(
946954
device_name, bus, device_descriptor.i2c_device_address,
947955
device_descriptor.i2c_mux_channel, device_status);
948956
if (drv_out != nullptr) {
949957
did_init = true;
950958
}
959+
WS_DEBUG_PRINTLN("OK!");
951960
}
952961

953962
if (!did_init) {
@@ -966,6 +975,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
966975
if (!is_output) {
967976
drv->SetMuxAddress(device_descriptor.i2c_mux_address);
968977
} else {
978+
WS_DEBUG_PRINTLN("[i2c] Setting MUX address for output driver...");
969979
drv_out->SetMuxAddress(device_descriptor.i2c_mux_address);
970980
}
971981
WS_DEBUG_PRINTLN("[i2c] Set driver to use MUX");
@@ -977,6 +987,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
977987
_i2c_model->GetI2cDeviceAddOrReplaceMsg()
978988
->i2c_device_description.i2c_bus_sda);
979989
} else {
990+
WS_DEBUG_PRINTLN("[i2c] Setting alt. I2C bus for output driver...");
980991
drv_out->EnableAltI2CBus(_i2c_model->GetI2cDeviceAddOrReplaceMsg()
981992
->i2c_device_description.i2c_bus_scl,
982993
_i2c_model->GetI2cDeviceAddOrReplaceMsg()
@@ -995,37 +1006,54 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
9951006
drv->SetSensorPeriod(
9961007
_i2c_model->GetI2cDeviceAddOrReplaceMsg()->i2c_device_period);
9971008
} else {
1009+
WS_DEBUG_PRINTLN("[i2c] Configuring output driver...");
9981010
// Configure Output-driver settings
9991011
pb_size_t config =
10001012
_i2c_model->GetI2cDeviceAddOrReplaceMsg()->i2c_output_add.which_config;
10011013
if (config ==
10021014
wippersnapper_i2c_output_I2cOutputAdd_led_backpack_config_tag) {
1003-
// TODO: Configure LED Backpack
1015+
WS_DEBUG_PRINTLN("[i2c] Configuring LED backpack...");
10041016
wippersnapper_i2c_output_LedBackpackConfig cfg =
10051017
_i2c_model->GetI2cDeviceAddOrReplaceMsg()
10061018
->i2c_output_add.config.led_backpack_config;
1019+
WS_DEBUG_PRINT("[i2c] Got cfg, calling ConfigureLEDBackpack...");
10071020
drv_out->ConfigureLEDBackpack(cfg.brightness, cfg.alignment);
1021+
WS_DEBUG_PRINTLN("OK!");
10081022
} else if (config ==
10091023
wippersnapper_i2c_output_I2cOutputAdd_char_lcd_config_tag) {
1010-
// TODO: Configure Char LCD
1024+
WS_DEBUG_PRINTLN("[i2c] Configuring char LCD...");
10111025
} else {
10121026
WS_DEBUG_PRINTLN(
10131027
"[i2c] ERROR: Unknown config specified for output driver!");
10141028
return false;
10151029
}
10161030
}
10171031

1018-
if (!drv->begin()) {
1019-
if (WsV2._sdCardV2->isModeOffline()) {
1020-
WsV2.haltErrorV2("[i2c] Driver failed to initialize!\n\tDid you set "
1021-
"the correct value for i2cDeviceName?\n\tDid you set "
1022-
"the correct value for"
1023-
"i2cDeviceAddress?",
1024-
WS_LED_STATUS_ERROR_RUNTIME, false);
1032+
if (!is_output) {
1033+
if (!drv->begin()) {
1034+
if (WsV2._sdCardV2->isModeOffline()) {
1035+
WsV2.haltErrorV2("[i2c] Driver failed to initialize!\n\tDid you set "
1036+
"the correct value for i2cDeviceName?\n\tDid you set "
1037+
"the correct value for"
1038+
"i2cDeviceAddress?",
1039+
WS_LED_STATUS_ERROR_RUNTIME, false);
1040+
}
1041+
}
1042+
_i2c_drivers.push_back(drv);
1043+
} else {
1044+
if (!drv_out->begin()) {
1045+
if (WsV2._sdCardV2->isModeOffline()) {
1046+
WsV2.haltErrorV2("[i2c] Driver failed to initialize!\n\tDid you set "
1047+
"the correct value for i2cDeviceName?\n\tDid you set "
1048+
"the correct value for"
1049+
"i2cDeviceAddress?",
1050+
WS_LED_STATUS_ERROR_RUNTIME, false);
1051+
}
10251052
}
1053+
_i2c_drivers_output.push_back(drv_out);
10261054
}
10271055

1028-
_i2c_drivers.push_back(drv);
1056+
10291057
WS_DEBUG_PRINTLN("[i2c] Driver initialized and added to controller: ");
10301058
WS_DEBUG_PRINTLN(device_name);
10311059

src/components/i2c/controller.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// I2C Drivers
2121
#include "drivers/drvAdt7410.h"
2222
#include "drivers/drvAhtx0.h"
23-
#include "drivers/drvOutQuadAlphaNum.h"
2423
#include "drivers/drvBase.h" ///< Base i2c input driver class
2524
#include "drivers/drvBh1750.h"
2625
#include "drivers/drvBme280.h"
@@ -46,6 +45,7 @@
4645
#include "drivers/drvMprls.h"
4746
#include "drivers/drvMs8607.h"
4847
#include "drivers/drvNau7802.h"
48+
#include "drivers/drvOutQuadAlphaNum.h"
4949
#include "drivers/drvOutputBase.h" ///< Base i2c output driver class
5050
#include "drivers/drvPct2075.h"
5151
#include "drivers/drvPm25.h"
@@ -98,15 +98,17 @@ class I2cController {
9898
bool IsBusStatusOK(bool is_alt_bus = false);
9999
bool InitMux(const char *name, uint32_t address, bool is_alt_bus);
100100
void ConfigureMuxChannel(uint32_t mux_channel, bool is_alt_bus);
101-
bool RemoveDriver(uint32_t address);
101+
bool RemoveDriver(uint32_t address, bool is_output_device);
102102

103103
private:
104-
I2cModel *_i2c_model = nullptr; ///< Pointer to an I2C model object
105-
I2cOutputModel *_i2c_output_model = nullptr; ///< Pointer to an I2C output model object
106-
I2cHardware *_i2c_bus_default = nullptr; ///< Pointer to the default I2C bus
107-
I2cHardware *_i2c_bus_alt = nullptr; ///< Pointer to an alternative I2C bus
104+
I2cModel *_i2c_model = nullptr; ///< Pointer to an I2C model object
105+
I2cOutputModel *_i2c_output_model =
106+
nullptr; ///< Pointer to an I2C output model object
107+
I2cHardware *_i2c_bus_default = nullptr; ///< Pointer to the default I2C bus
108+
I2cHardware *_i2c_bus_alt = nullptr; ///< Pointer to an alternative I2C bus
108109
std::vector<drvBase *> _i2c_drivers; ///< Vector of ptrs to I2C input drivers
109-
std::vector<drvOutputBase *> _i2c_drivers_output; ///< Vector of ptrs to I2C output drivers
110+
std::vector<drvOutputBase *>
111+
_i2c_drivers_output; ///< Vector of ptrs to I2C output drivers
110112
};
111113
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
112114
#endif // WS_I2C_CONTROLLER_H

src/components/i2c/drivers/drvOutQuadAlphaNum.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ class drvOutQuadAlphaNum : public drvOutputBase {
7878
The alignment of the LED backpack.
7979
*/
8080
void ConfigureLEDBackpack(int32_t brightness, uint32_t alignment) {
81+
WS_DEBUG_PRINTLN("[i2c] drvOutQuadAlphaNum::ConfigureLEDBackpack() called");
8182
if (alignment == LED_BACKPACK_ALIGNMENT_RIGHT) {
8283
_alignment = LED_BACKPACK_ALIGNMENT_RIGHT;
8384
} else {
8485
_alignment = LED_BACKPACK_ALIGNMENT_DEFAULT;
8586
}
8687
_brightness = brightness;
88+
WS_DEBUG_PRINTLN("[i2c] drvOutQuadAlphaNum::configured");
8789
}
8890

8991
/*!
@@ -131,17 +133,37 @@ class drvOutQuadAlphaNum : public drvOutputBase {
131133
pos_start = LED_MAX_CHARS - seg_chars;
132134
}
133135

136+
WS_DEBUG_PRINT("Message to display: ");
137+
WS_DEBUG_PRINT(message);
138+
WS_DEBUG_PRINT(" with len_display: ");
139+
WS_DEBUG_PRINT(len_display);
140+
WS_DEBUG_PRINT(" at pos_start: ");
141+
WS_DEBUG_PRINT(pos_start);
142+
143+
// TODO FRIDAY
144+
// NOTE: If there's a ., increment len_display by 1 to account for the decimal
145+
134146
// Write to the display's buffer
135147
int cur_idx = pos_start;
136148
for (size_t i = 0; i < len_display; i++) {
149+
// Save the character because if there's a decimal, we need to skip it in the buffer
150+
char ch = message[i];
151+
137152
// Look-ahead for a decimal point to attach to the current character
138153
bool display_dot = false;
139154
if (i + 1 < len_display && message[i + 1] == '.') {
140155
display_dot = true;
141156
i++;
142157
}
158+
143159
// Write the character to the display buffer
144-
_alpha4->writeDigitAscii(cur_idx, message[i], display_dot);
160+
WS_DEBUG_PRINT("Writing char: ");
161+
WS_DEBUG_PRINT(message[i]);
162+
WS_DEBUG_PRINT(" at index: ");
163+
WS_DEBUG_PRINT(cur_idx);
164+
WS_DEBUG_PRINT(" with dot: ");
165+
WS_DEBUG_PRINTLN(display_dot);
166+
_alpha4->writeDigitAscii(cur_idx, ch, display_dot);
145167
cur_idx++;
146168
}
147169
// Issue the buffered data in RAM to the display
@@ -156,7 +178,7 @@ class drvOutQuadAlphaNum : public drvOutputBase {
156178
*/
157179
void WriteValue(float value) {
158180
char message[LED_MAX_CHARS + 1];
159-
snprintf(message, sizeof(message), "%.4f", value);
181+
snprintf(message, sizeof(message), "%.5f", value);
160182
WriteMessage(message);
161183
}
162184

0 commit comments

Comments
 (0)