From 66e4cb84de331420baed6e6938cfbe3cf1f7b169 Mon Sep 17 00:00:00 2001 From: Jimit Doshi Date: Tue, 3 Dec 2024 14:49:12 -0800 Subject: [PATCH] Config mux to git rid of chatter, add print for Serial terminal Abstract out wire interface, no all 16 cells are functional Patch to increase NUM_LEDS now that all cells are enabled Update LDO and Buck DACs to use appropriate wire as well --- firmware/src/cell.cpp | 41 ++++++++++++++++++++++------------------- firmware/src/cell.h | 3 ++- firmware/src/main.cpp | 43 ++++++++++++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 33 deletions(-) diff --git a/firmware/src/cell.cpp b/firmware/src/cell.cpp index 9ed17f2..e9adf3c 100644 --- a/firmware/src/cell.cpp +++ b/firmware/src/cell.cpp @@ -1,7 +1,10 @@ #include "Cell.h" // Constructor -Cell::Cell(uint8_t mux_channel) : mux_channel(mux_channel) {} +Cell::Cell(uint8_t mux_channel, TwoWire& wire_interface) : + mux_channel(mux_channel), wire(wire_interface) { + // ... +} // Implementation of public methods void Cell::init() @@ -10,23 +13,23 @@ void Cell::init() setMuxChannel(); // Initialize DACs - ldo_dac.begin(LDO_ADDRESS); - buck_dac.begin(BUCK_ADDRESS); + ldo_dac.begin(LDO_ADDRESS, &wire); + buck_dac.begin(BUCK_ADDRESS, &wire); // Initialize ADC - adc.begin(ADC_ADDRESS); + adc.begin(ADC_ADDRESS, &wire); // Configure GPIO expander - Wire.beginTransmission(0x20); - Wire.write(0x03); // Configuration register - Wire.write(0x00); // Set all pins as output - Wire.endTransmission(); + wire.beginTransmission(0x20); + wire.write(0x03); // Configuration register + wire.write(0x00); // Set all pins as output + wire.endTransmission(); // Set the outputs to 0 - Wire.beginTransmission(0x20); - Wire.write(0x01); // Configuration register - Wire.write(0x00); // Set all pins as output - Wire.endTransmission(); + wire.beginTransmission(0x20); + wire.write(0x01); // Configuration register + wire.write(0x00); // Set all pins as output + wire.endTransmission(); } void Cell::disable() @@ -153,10 +156,10 @@ void Cell::setGPIOState() { setMuxChannel(); // Set output relay on P7 - Wire.beginTransmission(TCA6408_ADDR); - Wire.write(0x01); // Output register - Wire.write(GPIO_STATE); - Wire.endTransmission(); + wire.beginTransmission(TCA6408_ADDR); + wire.write(0x01); // Output register + wire.write(GPIO_STATE); + wire.endTransmission(); } float Cell::readShuntCurrent() @@ -170,7 +173,7 @@ float Cell::readShuntCurrent() void Cell::setMuxChannel() { - Wire.beginTransmission(MUX_ADDRESS); - Wire.write(1 << mux_channel); - Wire.endTransmission(); + wire.beginTransmission(MUX_ADDRESS); + wire.write(1 << mux_channel); + wire.endTransmission(); } diff --git a/firmware/src/cell.h b/firmware/src/cell.h index a7015df..d6a3883 100644 --- a/firmware/src/cell.h +++ b/firmware/src/cell.h @@ -11,7 +11,7 @@ class Cell { public: // Constructor - Cell(uint8_t mux_channel); + Cell(uint8_t mux_channel, TwoWire& wire_interface); // Public methods void init(); @@ -54,6 +54,7 @@ class Cell // Mux channel const uint8_t mux_channel; + TwoWire& wire; // Device instances Adafruit_MCP4725 ldo_dac; diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 19fac42..1c2ad09 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -29,21 +29,29 @@ const int DMM_MUX_PINS[] = {1, 2, 3, 4}; const int DMM_MUX_ENABLE = 5; // Addressable LEDs -#define NUM_LEDS 16 +#define NUM_LEDS 32 CRGB leds[NUM_LEDS]; // Create cells -Cell cell1(0); -Cell cell2(1); -Cell cell3(2); -Cell cell4(3); -Cell cell5(4); -Cell cell6(5); -Cell cell7(6); -Cell cell8(7); +Cell cell1(0, Wire); +Cell cell2(1, Wire); +Cell cell3(2, Wire); +Cell cell4(3, Wire); +Cell cell5(4, Wire); +Cell cell6(5, Wire); +Cell cell7(6, Wire); +Cell cell8(7, Wire); +Cell cell9(0, Wire1); +Cell cell10(1, Wire1); +Cell cell11(2, Wire1); +Cell cell12(3, Wire1); +Cell cell13(4, Wire1); +Cell cell14(5, Wire1); +Cell cell15(6, Wire1); +Cell cell16(7, Wire1); // Cell array -Cell cells[] = {cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8}; +Cell cells[] = {cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, cell10, cell11, cell12, cell13, cell14, cell15, cell16}; void setupLEDs() @@ -87,6 +95,13 @@ void setup() // SPI.setBitOrder(MSBFIRST); // SPI.setDataMode(SPI_MODE0); + // Setup mux + pinMode(DMM_MUX_ENABLE, OUTPUT); + for (int i = 0; i < 4; i++) { + pinMode(DMM_MUX_PINS[i], OUTPUT); + digitalWrite(DMM_MUX_PINS[i], LOW); + } + delay(1000); // Initialize cells @@ -101,13 +116,15 @@ void setup() FastLED.setBrightness(255); } -float voltage = 2; -float sum = 0.01; +float voltage = 3.5; void loop() { for (Cell& cell : cells) { cell.setVoltage(voltage); + USBSerial.println(cell.getVoltage()); } - updateStatusLEDs(cells, 8); + updateStatusLEDs(cells, sizeof(cells) / sizeof(cells[0])); + + delay(1000); } \ No newline at end of file