#include "dac.h" #include "src/interfaces/i2c_controller.h" #include "ti/driverlib/dl_i2c.h" #include "ti_msp_dl_config.h" #include "src/battery_data/battery.h" #include #include #include "src/config.h" bool DAC_SingleWrite(uint8_t slot, uint16_t channel_value) { if(channel_value > MAX_DAC_VALUE) { #ifdef DEBUG_DAC printf("DAC Error: channel_value out of range. Must be between 0 and %d\n", MAX_DAC_VALUE); #endif *battery_slots[slot].state = SLOT_WARN_DAC_INVALID_VALUE; return false; } controllerTxPackage.len = 3; controllerTxPackage.packet[0] = (0b01000000 | slot << 1); controllerTxPackage.packet[1] = (0x10) | ((channel_value >> 8) & 0x0F); controllerTxPackage.packet[2] = (channel_value & 0xFF); #ifdef DEBUG_DAC // Log data being sent printf("Sending to DAC: 0x%02X 0x%02X 0x%02X\n", controllerTxPackage.packet[0], controllerTxPackage.packet[1], controllerTxPackage.packet[2]); #endif // Write data to DAC if (!i2c_hal.write(DAC_TARGET_ADDRESS)) { #ifdef DEBUG_DAC printf("I2C DAC Write Error: Failed to write to DAC.\n"); #endif *battery_slots[slot].state = SLOT_ERR_DAC_WRITE_FAILED; return false; } return true; }