save friday progress

This commit is contained in:
brentru 2025-03-21 16:45:19 -04:00
parent c4eba79970
commit cef1bfc76f
4 changed files with 56 additions and 21 deletions

View file

@ -384,7 +384,6 @@ I2cController::I2cController() {
_i2c_model = new I2cModel();
// Initialize the default I2C bus
_i2c_bus_default = new I2cHardware();
_i2c_bus_default->InitBus(true);
}
/***********************************************************************/
@ -686,6 +685,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
*/
/***********************************************************************/
bool I2cController::ScanI2cBus(bool default_bus = true) {
_i2c_bus_default->InitBus(default_bus);
_scan_results = wippersnapper_i2c_I2cBusScanned_init_zero;
if (!default_bus)
return _i2c_bus_alt->ScanBus(&_scan_results);
@ -726,18 +726,27 @@ void I2cController::PrintScanResults() {
}
bool I2cController::AddScanResultsToConfig() {
WS_DEBUG_PRINT("[i2c] # of Scanned Devices: ");
WS_DEBUG_PRINTLN(_scan_results.i2c_bus_found_devices_count);
if (_scan_results.i2c_bus_found_devices_count == 0)
return false;
for (pb_size_t i = 0; i < _scan_results.i2c_bus_found_devices_count; i++) {
WS_DEBUG_PRINT("[i2c] Adding device to config: ");
WS_DEBUG_PRINTLN(_scan_results.i2c_bus_found_devices[i].i2c_device_address,
HEX);
WsV2._fileSystemV2->AddI2CDeviceToConfig(_scan_results.i2c_bus_found_devices[i].i2c_device_address);
WS_DEBUG_PRINTLN("[i2c] ...added!");
}
// TODO: Not entirely sure we need this
// AND the callback, to-test!
WsV2._fileSystemV2->USBAttach();
delay(500);
/* WS_DEBUG_PRINTLN("[i2c] Detaching FS...");
WsV2._fileSystemV2->USBDetach();
delay(500);
WS_DEBUG_PRINTLN("[i2c] Attaching FS...");
WsV2._fileSystemV2->USBAttach();
delay(500);
WS_DEBUG_PRINTLN("[i2c] FS Attached..."); */
return true;
}

View file

@ -63,10 +63,12 @@ void I2cHardware::TogglePowerPin() {
/***********************************************************************/
void I2cHardware::InitBus(bool is_default, const char *sda, const char *scl) {
uint8_t pin_sda, pin_scl;
/* if (!is_default && (sda == nullptr || scl == nullptr)) {
WS_DEBUG_PRINT("is_default: ");
WS_DEBUG_PRINTLN(is_default);
if (!is_default && (sda == nullptr || scl == nullptr)) {
_bus_status = wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_UNSPECIFIED;
return;
} */
}
// Some development boards define a pin that controls power
// to the i2c bus. If the pin is defined, turn the power to the i2c bus on.
#if defined(PIN_I2C_POWER) || defined(TFT_I2C_POWER) || \
@ -81,6 +83,7 @@ void I2cHardware::InitBus(bool is_default, const char *sda, const char *scl) {
pin_scl = SCL;
#else
// RP2040 BSP uses a different naming scheme than Espressif for I2C pins
WS_DEBUG_PRINTLN("[i2c] Using RP2040 I2C pins...");
pin_sda = PIN_WIRE0_SDA;
pin_scl = PIN_WIRE0_SCL;
#endif
@ -127,6 +130,7 @@ void I2cHardware::InitBus(bool is_default, const char *sda, const char *scl) {
_bus->setSDA(pin_sda);
_bus->setSCL(pin_scl);
_bus->begin();
WS_DEBUG_PRINTLN("[i2c] RP2040 I2C bus initialized!");
#elif defined(ARDUINO_ARCH_SAM)
_bus = new TwoWire(&PERIPH_WIRE, pin_sda, pin_scl);
_bus->begin();
@ -134,6 +138,7 @@ void I2cHardware::InitBus(bool is_default, const char *sda, const char *scl) {
#error "I2C bus implementation not supported by this platform!"
#endif
WS_DEBUG_PRINTLN("[i2c] I2C bus initialized!");
_bus_status = wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS;
}
@ -157,6 +162,11 @@ bool I2cHardware::ScanBus(wippersnapper_i2c_I2cBusScanned *scan_results) {
if (!scan_results)
return false;
if (! _bus) {
WS_DEBUG_PRINTLN("[i2c] ERROR: I2C bus not initialized!");
return false;
}
// TODO: WS object needs to be added for this to work?
/* #ifndef ARDUINO_ARCH_ESP32
// Set I2C WDT timeout to catch I2C hangs, SAMD-specific
@ -165,24 +175,24 @@ bool I2cHardware::ScanBus(wippersnapper_i2c_I2cBusScanned *scan_results) {
#endif */
WS_DEBUG_PRINT("Bus Status: ");
WS_DEBUG_PRINTLN(_bus_status);
InitBus(true);
WS_DEBUG_PRINT("L169 Bus Status: ");
WS_DEBUG_PRINTLN(_bus_status);
// Perform a bus scan
WS_DEBUG_PRINTLN("[i2c]: Scanning I2C Bus for Devices...");
for (uint8_t address = 1; address < 127; ++address) {
for (uint8_t address = 1; address < 127; address++) {
WS_DEBUG_PRINT("[i2c] 0x");
WS_DEBUG_PRINTLN(address, HEX);
_bus->beginTransmission(address);
uint8_t endTransmissionRC = _bus->endTransmission();
WS_DEBUG_PRINT("[i2c] endTransmissionRC: ");
WS_DEBUG_PRINTLN(endTransmissionRC);
if (endTransmissionRC == 0) {
WS_DEBUG_PRINTLN("[i2c] Found Device!");
scan_results
->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count]
.i2c_device_address = address;
strcpy(
// NOTE: This is disabled because _sda and _scl are not saved, we should be doing this!
/* strcpy(
scan_results
->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count]
.i2c_bus_sda,
@ -191,7 +201,7 @@ bool I2cHardware::ScanBus(wippersnapper_i2c_I2cBusScanned *scan_results) {
scan_results
->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count]
.i2c_bus_scl,
_scl);
_scl); */
scan_results->i2c_bus_found_devices_count++;
}
#if defined(ARDUINO_ARCH_ESP32)

View file

@ -718,13 +718,9 @@ bool ws_sdcard::ParseFileConfig() {
} else {
WS_DEBUG_PRINTLN("[SD] Device not found during I2C scan: " +
String(addr_device));
// TODO: Do not add it to the components list, remove it from the JSON
// doc Print out the scan results
// TODO: Waiting on L's feedback around if I should remove it
// totally...or just skip it
// TODO: Just log this, do not remove from config or anything!
WS_DEBUG_PRINTLN("[SD] I2C scan results:");
WsV2._i2c_controller->PrintScanResults();
WsV2._i2c_controller->AddScanResultsToConfig();
}
}
} else {

View file

@ -424,6 +424,7 @@ bool Wippersnapper_FS::AddSDCSPinToFileConfig(uint8_t pin) {
// Modify sd_cs_pin
doc["exportedFromDevice"]["sd_cs_pin"] = pin;
doc.shrinkToFit();
// Write the updated JSON back to the file
file_cfg = wipperFatFs_v2.open("/config.json", FILE_WRITE);
if (!file_cfg) {
@ -442,11 +443,13 @@ bool Wippersnapper_FS::AddI2CDeviceToConfig(uint32_t address) {
JsonDocument doc;
DeserializationError error;
// Load the config.json file into memory
WS_DEBUG_PRINTLN("Opening config.json file for reading...");
File32 file_cfg = wipperFatFs_v2.open("/config.json", FILE_READ);
if (!file_cfg) {
HaltFilesystem("ERROR: Could not open the config.json file for reading!");
return false;
}
WS_DEBUG_PRINTLN("Parsing config.json file...");
error = deserializeJson(doc, file_cfg);
file_cfg.close();
if (error) {
@ -456,7 +459,9 @@ bool Wippersnapper_FS::AddI2CDeviceToConfig(uint32_t address) {
}
// Append the new JSON object to the config.json file
JsonObject components = doc["components"].add<JsonObject>();
WS_DEBUG_PRINTLN("Appending new I2C device to config.json...");
// TODO: we are experiencing a crash when returning, is this due to writing the file?
/* JsonObject components = doc["components"].add<JsonObject>();
components["name"] = "UNKNOWN";
components["componentAPI"] = "i2c";
components["i2cDeviceName"] = "UNKNOWN";
@ -465,13 +470,28 @@ bool Wippersnapper_FS::AddI2CDeviceToConfig(uint32_t address) {
sprintf(buffer, "0x%02X", (unsigned int)address);
components["i2cDeviceAddress"] = buffer;
JsonArray components_i2cDeviceSensorTypes =
components["i2cDeviceSensorTypes"].to<JsonArray>();
components["i2cDeviceSensorTypes"].to<JsonArray>(); */
// Serialize and write it back to the file!
WS_DEBUG_PRINTLN("Writing new I2C device to config.json...");
file_cfg = wipperFatFs_v2.open("/config.json", FILE_WRITE);
serializeJsonPretty(doc, file_cfg);
// Add error checking for serialization
size_t bytesWritten = serializeJsonPretty(doc, file_cfg);
if (bytesWritten == 0) {
WS_DEBUG_PRINTLN("ERROR - Failed to write to config.json");
file_cfg.close();
return false;
}
file_cfg.flush();
file_cfg.close();
delay(500); // arbitrary delay TODO remove
WS_DEBUG_PRINTLN("New I2C device added to config.json!");
WS_DEBUG_PRINTLN("Detaching TinyUSB...");
TinyUSBDevice.detach();
delay(500);
WS_DEBUG_PRINTLN("Re-attaching TinyUSB...");
TinyUSBDevice.attach();
delay(500);
return true;
}