Skip to content

Commit 11cee82

Browse files
committed
More intelligent approach for the config file rm issue
1 parent 7fd0d1a commit 11cee82

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/components/i2c/controller.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,11 +862,23 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
862862
*/
863863
/***********************************************************************/
864864
bool I2cController::ScanI2cBus(bool default_bus = true) {
865-
_i2c_bus_default->InitBus(default_bus);
865+
// zero-out the scan I2cBusScanned message before attempting a scan
866866
_scan_results = wippersnapper_i2c_I2cBusScanned_init_zero;
867-
if (!default_bus)
867+
868+
// Scan the desired i2c bus
869+
if (default_bus) {
870+
if (_i2c_bus_default->GetBusStatus() !=
871+
wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS) {
872+
_i2c_bus_default->InitBus(default_bus);
873+
}
874+
return _i2c_bus_default->ScanBus(&_scan_results);
875+
} else {
876+
if (_i2c_bus_alt->GetBusStatus() !=
877+
wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS) {
878+
_i2c_bus_alt->InitBus(default_bus);
879+
}
868880
return _i2c_bus_alt->ScanBus(&_scan_results);
869-
return _i2c_bus_default->ScanBus(&_scan_results);
881+
}
870882
}
871883

872884
/***********************************************************************/

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,17 @@ bool ws_sdcard::InitDS3231() {
165165
*/
166166
/**************************************************************************/
167167
bool ws_sdcard::InitPCF8523() {
168+
169+
// TODO: Check if we can see the PCF8523 after
170+
// the initial i2c scan
171+
WsV2._i2c_controller->ScanI2cBus(true);
172+
WS_DEBUG_PRINT("[sd] Scanned I2C Devices: ")
173+
WS_DEBUG_PRINTLN(WsV2._i2c_controller->GetScanDeviceCount());
174+
WS_DEBUG_PRINT("Was Device Found? ");
175+
WS_DEBUG_PRINTLN(WsV2._i2c_controller->WasDeviceScanned(0x68));
176+
168177
_rtc_pcf8523 = new RTC_PCF8523();
169-
if (!_rtc_pcf8523->begin(&Wire)) {
178+
if (!_rtc_pcf8523->begin()) {
170179
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize PCF8523 RTC on WIRE");
171180
if (!_rtc_pcf8523->begin(&Wire1)) {
172181
WS_DEBUG_PRINTLN("[SD] Error: Failed to initialize PCF8523 RTC on WIRE1");
@@ -701,6 +710,7 @@ bool ws_sdcard::ParseExportedFromDevice(JsonDocument &doc) {
701710
WS_DEBUG_PRINTLN("[SD] Error: Failed to to configure a RTC!");
702711
return false;
703712
}
713+
704714
return true;
705715
}
706716

@@ -715,6 +725,9 @@ bool ws_sdcard::ParseExportedFromDevice(JsonDocument &doc) {
715725
bool ws_sdcard::ParseFileConfig() {
716726
DeserializationError error;
717727

728+
// delay 6 seconds
729+
delay(6000);
730+
718731
#ifndef OFFLINE_MODE_DEBUG
719732
WS_DEBUG_PRINTLN("[SD] Deserializing config.json...");
720733
JsonDocument &doc = WsV2._fileSystemV2->GetDocCfg();

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,7 @@ void Wippersnapper_FS::CreateFileConfig() {
386386
HaltFilesystem(
387387
"[fs] Error: Unable to deserialize config.json, is it corrupted?");
388388
}
389-
// We are going to parse the in-memory object, _doc_cfg, rather
390-
// than the file. So, let's remove the ctg file since we'll replace
391-
// it with a new cfg file later on
392389
file_cfg.close();
393-
wipperFatFs_v2.remove("/config.json");
394-
flash_v2.syncBlocks();
395-
396390
// Check if the config.json file has the required keys
397391
if (!_doc_cfg.containsKey("exportedFromDevice")) {
398392
// Build exportedFromDevice object
@@ -412,6 +406,7 @@ void Wippersnapper_FS::CreateFileConfig() {
412406

413407
return;
414408
}
409+
file_cfg.close();
415410
}
416411

417412
// Create a default configConfig structure in a new doc
@@ -478,6 +473,14 @@ void Wippersnapper_FS::AddI2cDeviceToFileConfig(
478473
*/
479474
/**************************************************************************/
480475
bool Wippersnapper_FS::WriteFileConfig() {
476+
// If it exists, remove the existing config.json file
477+
// as we're about to write the new one into memory
478+
if (wipperFatFs_v2.exists("/config.json")) {
479+
wipperFatFs_v2.remove("/config.json");
480+
flash_v2.syncBlocks();
481+
delay(500);
482+
}
483+
481484
// Write the document to the filesystem
482485
File32 file_cfg = wipperFatFs_v2.open("/config.json", FILE_WRITE);
483486
if (!file_cfg) {

0 commit comments

Comments
 (0)