🚧 SD - refactor out sd func. to a new class
This commit is contained in:
parent
fbf9886d5a
commit
00530c1a7f
6 changed files with 169 additions and 100 deletions
|
|
@ -68,6 +68,9 @@ Wippersnapper_V2::Wippersnapper_V2() {
|
|||
// Initialize model classes
|
||||
WsV2.sensorModel = new SensorModel();
|
||||
|
||||
// SD Card
|
||||
WsV2._sdCardV2 = new ws_sdcard();
|
||||
|
||||
// Initialize controller classes
|
||||
WsV2.digital_io_controller = new DigitalIOController();
|
||||
WsV2.analogio_controller = new AnalogIOController();
|
||||
|
|
@ -94,6 +97,11 @@ void Wippersnapper_V2::provisionV2() {
|
|||
// Initialize the status LED for signaling FS errors
|
||||
initStatusLED();
|
||||
|
||||
// If an SD card is inserted and mounted, we do not need to provision
|
||||
// wifi/io credentials
|
||||
if (WsV2._sdCardV2->IsSDCardInserted())
|
||||
return;
|
||||
|
||||
// Initialize the filesystem
|
||||
#ifdef USE_TINYUSB
|
||||
_fileSystemV2 = new Wippersnapper_FS_V2();
|
||||
|
|
@ -122,38 +130,18 @@ void Wippersnapper_V2::provisionV2() {
|
|||
WsV2._ui_helper->set_label_status("Validating Credentials...");
|
||||
#endif
|
||||
|
||||
// TODO: This should be refactored, we don't want all esp32 platforms to default to False
|
||||
#ifdef USE_TINYUSB
|
||||
// Attempt to detect if a SD card is inserted
|
||||
if (_fileSystemV2->IsSDCardInserted()) {
|
||||
// a SD card is inserted, we're running in offline mode
|
||||
WsV2._is_offline_mode = true;
|
||||
} else {
|
||||
// no SD card inserted, we're running in online mode
|
||||
WsV2._is_offline_mode = false;
|
||||
}
|
||||
_fileSystemV2->parseSecrets();
|
||||
#elif defined(USE_LITTLEFS)
|
||||
WsV2._is_offline_mode = false;
|
||||
_littleFSV2->parseSecrets();
|
||||
#else
|
||||
check_valid_ssidV2(); // non-fs-backed, sets global credentials within network
|
||||
// iface
|
||||
#endif
|
||||
|
||||
|
||||
// If we are running in Online mode, parse the secrets.json file
|
||||
WsV2._is_offline_mode = false;
|
||||
if (WsV2._is_offline_mode == false){
|
||||
#ifdef USE_TINYUSB
|
||||
_fileSystemV2->parseSecrets();
|
||||
#elif defined(USE_LITTLEFS)
|
||||
_littleFSV2->parseSecrets();
|
||||
#else
|
||||
check_valid_ssidV2(); // non-fs-backed, sets global credentials within network
|
||||
// iface
|
||||
#endif
|
||||
// Set the status pixel's brightness
|
||||
setStatusLEDBrightness(WsV2._configV2.status_pixel_brightness);
|
||||
// Set device's wireless credentials
|
||||
set_ssid_passV2();
|
||||
}
|
||||
|
||||
// Set the status pixel's brightness
|
||||
setStatusLEDBrightness(WsV2._configV2.status_pixel_brightness);
|
||||
// Set device's wireless credentials
|
||||
set_ssid_passV2();
|
||||
|
||||
#ifdef USE_DISPLAY
|
||||
WsV2._ui_helper->set_label_status("");
|
||||
|
|
@ -318,7 +306,8 @@ bool handleCheckinResponse(pb_istream_t *stream) {
|
|||
WsV2.digital_io_controller->SetMaxDigitalPins(
|
||||
WsV2.CheckInModel->getTotalGPIOPins());
|
||||
|
||||
WsV2.analogio_controller->SetRefVoltage(WsV2.CheckInModel->getReferenceVoltage());
|
||||
WsV2.analogio_controller->SetRefVoltage(
|
||||
WsV2.CheckInModel->getReferenceVoltage());
|
||||
WsV2.analogio_controller->SetTotalAnalogPins(
|
||||
WsV2.CheckInModel->getTotalAnalogPins());
|
||||
|
||||
|
|
@ -889,9 +878,9 @@ void Wippersnapper_V2::haltErrorV2(String error,
|
|||
/**************************************************************************/
|
||||
bool Wippersnapper_V2::PublishSignal(pb_size_t which_payload, void *payload) {
|
||||
|
||||
#ifdef DEBUG_PROFILE
|
||||
#ifdef DEBUG_PROFILE
|
||||
unsigned long total_start_time = micros();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
size_t szMessageBuf;
|
||||
wippersnapper_signal_DeviceToBroker MsgSignal =
|
||||
|
|
@ -967,30 +956,30 @@ bool Wippersnapper_V2::PublishSignal(pb_size_t which_payload, void *payload) {
|
|||
runNetFSMV2();
|
||||
WsV2.feedWDTV2();
|
||||
|
||||
#ifdef DEBUG_PROFILE
|
||||
#ifdef DEBUG_PROFILE
|
||||
unsigned long publish_start_time = micros();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Attempt to publish the signal message to the broker
|
||||
WS_DEBUG_PRINT("Publishing signal message to broker...");
|
||||
#ifdef DEBUG_PROFILE
|
||||
#ifdef DEBUG_PROFILE
|
||||
WS_DEBUG_PRINT("Message buffer size: ");
|
||||
WS_DEBUG_PRINTLN(szMessageBuf);
|
||||
#endif
|
||||
#endif
|
||||
if (!WsV2._mqttV2->publish(WsV2._topicD2b, msgBuf, szMessageBuf, 1)) {
|
||||
WS_DEBUG_PRINTLN("ERROR: Failed to publish signal message to broker!");
|
||||
return false;
|
||||
}
|
||||
WS_DEBUG_PRINTLN("Published!");
|
||||
|
||||
#ifdef DEBUG_PROFILE
|
||||
#ifdef DEBUG_PROFILE
|
||||
unsigned long publish_end_time = micros();
|
||||
WS_DEBUG_PRINT("Publishing time: ");
|
||||
WS_DEBUG_PRINTLN(publish_end_time - publish_start_time);
|
||||
unsigned long total_end_time = micros();
|
||||
WS_DEBUG_PRINT("Total PublishSignal() execution time: ");
|
||||
WS_DEBUG_PRINTLN(total_end_time - total_start_time);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -1174,7 +1163,8 @@ void Wippersnapper_V2::connectV2() {
|
|||
if (WsV2._is_offline_mode) {
|
||||
WS_DEBUG_PRINTLN("[Offline Mode] Running device configuration...");
|
||||
// TODO: Implement configuration function for offline mode
|
||||
WS_DEBUG_PRINTLN("[Offline Mode] Hardware configured, skipping network setup...");
|
||||
WS_DEBUG_PRINTLN(
|
||||
"[Offline Mode] Hardware configured, skipping network setup...");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,8 +31,8 @@
|
|||
// Include Signal Proto
|
||||
#include "protos/checkin.pb.h"
|
||||
#include "protos/digitalio.pb.h"
|
||||
#include "protos/signal.pb.h"
|
||||
#include "protos/ds18x20.pb.h"
|
||||
#include "protos/signal.pb.h"
|
||||
|
||||
// External libraries
|
||||
#include "Adafruit_MQTT.h" // MQTT Client
|
||||
|
|
@ -49,12 +49,11 @@
|
|||
#endif
|
||||
|
||||
// Components (API v2)
|
||||
#include "components/sensor/model.h"
|
||||
#include "components/analogio/controller.h"
|
||||
#include "components/checkin/model.h"
|
||||
#include "components/digitalIO/controller.h"
|
||||
#include "components/analogio/controller.h"
|
||||
#include "components/ds18x20/controller.h"
|
||||
|
||||
#include "components/sensor/model.h"
|
||||
|
||||
// Components (API v1)
|
||||
#include "components/analogIO/Wippersnapper_AnalogIO.h"
|
||||
|
|
@ -73,6 +72,7 @@
|
|||
#endif
|
||||
|
||||
#include "provisioning/ConfigJson.h"
|
||||
#include "provisioning/sdcard/ws_sdcard.h"
|
||||
#if defined(USE_TINYUSB)
|
||||
#include "provisioning/tinyusb/Wippersnapper_FS_V2.h"
|
||||
#endif
|
||||
|
|
@ -97,6 +97,7 @@
|
|||
class Wippersnapper_AnalogIO;
|
||||
class Wippersnapper_FS_V2;
|
||||
class WipperSnapper_LittleFS;
|
||||
class ws_sdcard;
|
||||
#ifdef USE_DISPLAY
|
||||
class ws_display_driver;
|
||||
class ws_display_ui_helper;
|
||||
|
|
@ -208,7 +209,8 @@ public:
|
|||
Wippersnapper_AnalogIO *_analogIOV2; ///< Instance of analog io class
|
||||
Wippersnapper_FS_V2 *_fileSystemV2; ///< Instance of Filesystem (native USB)
|
||||
WipperSnapper_LittleFS
|
||||
*_littleFSV2; ///< Instance of LittleFS Filesystem (non-native USB)
|
||||
*_littleFSV2; ///< Instance of LittleFS Filesystem (non-native USB)
|
||||
ws_sdcard *_sdCardV2; ///< Instance of SD card class
|
||||
#ifdef USE_DISPLAY
|
||||
ws_display_driver *_displayV2 = nullptr; ///< Instance of display driver class
|
||||
ws_display_ui_helper *_ui_helperV2 =
|
||||
|
|
@ -220,12 +222,12 @@ public:
|
|||
ws_uart *_uartComponentV2; ///< Instance of UART class
|
||||
|
||||
// API v2 Components
|
||||
CheckinModel *CheckInModel; ///< Instance of CheckinModel class
|
||||
SensorModel *sensorModel; ///< Instance of SensorModel class
|
||||
CheckinModel *CheckInModel; ///< Instance of CheckinModel class
|
||||
SensorModel *sensorModel; ///< Instance of SensorModel class
|
||||
DigitalIOController
|
||||
*digital_io_controller; ///< Instance of DigitalIO controller class
|
||||
AnalogIOController *analogio_controller; ///< Instance of AnalogIO controller
|
||||
DS18X20Controller *_ds18x20_controller; ///< Instance of DS18X20 controller
|
||||
DS18X20Controller *_ds18x20_controller; ///< Instance of DS18X20 controller
|
||||
|
||||
// TODO: does this really need to be global?
|
||||
uint8_t _macAddrV2[6]; /*!< Unique network iface identifier */
|
||||
|
|
@ -250,7 +252,8 @@ public:
|
|||
#ifdef ARDUINO_ARCH_ESP32
|
||||
ws_ledc *_ledcV2 = nullptr; ///< Pointer to LEDC object
|
||||
#endif
|
||||
bool got_checkin_response; ///< True if a checkin response was received, False otherwise.
|
||||
bool got_checkin_response; ///< True if a checkin response was received, False
|
||||
///< otherwise.
|
||||
|
||||
private:
|
||||
void _initV2();
|
||||
|
|
|
|||
82
src/provisioning/sdcard/ws_sdcard.cpp
Normal file
82
src/provisioning/sdcard/ws_sdcard.cpp
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*!
|
||||
* @file ws_sdcard.cpp
|
||||
*
|
||||
* Interface for Wippersnapper's SD card filesystem.
|
||||
*
|
||||
* Adafruit invests time and resources providing this open source code,
|
||||
* please support Adafruit and open-source hardware by purchasing
|
||||
* products from Adafruit!
|
||||
*
|
||||
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
|
||||
*
|
||||
* BSD license, all text here must be included in any redistribution.
|
||||
*
|
||||
*/
|
||||
#include "ws_sdcard.h"
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Constructs an instance of the Wippersnapper SD card class.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
ws_sdcard::ws_sdcard() {
|
||||
#ifndef SD_CS_PIN
|
||||
_is_sd_card_inserted = false;
|
||||
return;
|
||||
#endif
|
||||
|
||||
// Attempt to initialize the SD card
|
||||
if (!_sd.begin(SD_CS_PIN)) {
|
||||
_is_sd_card_inserted = false;
|
||||
}
|
||||
_is_sd_card_inserted = true;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Destructs an instance of the Wippersnapper SD card class.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
ws_sdcard::~ws_sdcard() {
|
||||
// TODO: Close any open files
|
||||
// Then, end the SD card (ends SPI transaction)
|
||||
_sd.end();
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Checks if an SD card is inserted and mounted.
|
||||
@returns True if an SD card is inserted and mounted, False otherwise.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool ws_sdcard::IsSDCardInserted() { return _is_sd_card_inserted; }
|
||||
|
||||
void ws_sdcard::parseConfigFile() {
|
||||
File32 file_config; // TODO: MAke this global?
|
||||
file_config = _sd.open("config.json", FILE_READ);
|
||||
|
||||
JsonDocument doc;
|
||||
// TODO: Re-enable this
|
||||
// TODO: Change max input length to fit an expected/max json size
|
||||
int max_input_len = 512;
|
||||
|
||||
// Attempt to de-serialize the JSON document
|
||||
// DeserializationError error = deserializeJson(doc, file_config,
|
||||
// max_input_len);
|
||||
// If the JSON document failed to deserialize - halt the running device and
|
||||
// print the error because it is not possible to continue running in offline
|
||||
// mode without a valid config file
|
||||
// if (error)
|
||||
// fsHalt("deserializeJson() failed: " + String(error.c_str()));
|
||||
|
||||
// Otherwise, parse away!
|
||||
|
||||
// TODO: Start by detecting which API the config file wants to parse the JSON
|
||||
// into via the "componentAPI" field
|
||||
// TODO: We only need to handle componentTypeAdd messages here
|
||||
|
||||
// TODO: Can we access protobufs here?
|
||||
// create a digitalio protobuf message
|
||||
// wippersnapper_analogio_AnalogIOAdd addMsg =
|
||||
// wippersnapper_analogio_AnalogIOAdd_init_zero;
|
||||
}
|
||||
44
src/provisioning/sdcard/ws_sdcard.h
Normal file
44
src/provisioning/sdcard/ws_sdcard.h
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*!
|
||||
* @file ws_sdcard.h
|
||||
*
|
||||
* Interface for Wippersnapper's SD card filesystem.
|
||||
*
|
||||
* Adafruit invests time and resources providing this open source code,
|
||||
* please support Adafruit and open-source hardware by purchasing
|
||||
* products from Adafruit!
|
||||
*
|
||||
* Copyright (c) Brent Rubell 2024 for Adafruit Industries.
|
||||
*
|
||||
* BSD license, all text here must be included in any redistribution.
|
||||
*
|
||||
*/
|
||||
#ifndef WS_SDCARD_H
|
||||
#define WS_SDCARD_H
|
||||
|
||||
#include "SdFat.h"
|
||||
#include "Wippersnapper_V2.h"
|
||||
#define SD_FAT_TYPE 3
|
||||
|
||||
// forward decl.
|
||||
class Wippersnapper_V2;
|
||||
|
||||
/***************************************************************************/
|
||||
/*!
|
||||
@brief Class that handles Wippersnapper's optional filesystem commands
|
||||
and storage.
|
||||
*/
|
||||
/***************************************************************************/
|
||||
class ws_sdcard {
|
||||
public:
|
||||
ws_sdcard();
|
||||
~ws_sdcard();
|
||||
bool IsSDCardInserted();
|
||||
void parseConfigFile();
|
||||
|
||||
private:
|
||||
bool _is_sd_card_inserted; ///< True if an SD card is inserted, False
|
||||
///< otherwise.
|
||||
SdFat _sd; ///< SD object from Adafruit SDFat library
|
||||
};
|
||||
extern Wippersnapper_V2 WsV2;
|
||||
#endif // WS_SDCARD_H
|
||||
|
|
@ -53,7 +53,6 @@ Adafruit_FlashTransport_RP2040 flashTransport_v2;
|
|||
|
||||
Adafruit_SPIFlash flash_v2(&flashTransport_v2); ///< SPIFlash object
|
||||
FatVolume wipperFatFs_v2; ///< File system object from Adafruit SDFat library
|
||||
SdFat SDCard; ///< SD object from Adafruit SDFat library
|
||||
Adafruit_USBD_MSC usb_msc_v2; /*!< USB mass storage object */
|
||||
|
||||
/**************************************************************************/
|
||||
|
|
@ -586,52 +585,6 @@ void Wippersnapper_FS_V2::parseDisplayConfig(displayConfig &dispCfg) {
|
|||
}
|
||||
#endif // ARDUINO_FUNHOUSE_ESP32S2
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Checks if an SD card is inserted and mounted.
|
||||
@returns True if an SD card is inserted and mounted, False otherwise.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool Wippersnapper_FS_V2::IsSDCardInserted() {
|
||||
// Early-out of the board does not have a defined SD_CS_PIN entry
|
||||
// within boards.h
|
||||
#ifndef SD_CS_PIN
|
||||
return false;
|
||||
#endif
|
||||
if (!SDCard.begin(SD_CS_PIN)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Wippersnapper_FS_V2::parseConfigFile() {
|
||||
File32 file_config;
|
||||
file_config = SDCard.open("config.json", FILE_READ);
|
||||
|
||||
JsonDocument doc;
|
||||
// TODO: Re-enable this
|
||||
// TODO: Change max input length to fit an expected/max json size
|
||||
int max_input_len = 512;
|
||||
|
||||
// Attempt to de-serialize the JSON document
|
||||
//DeserializationError error = deserializeJson(doc, file_config, max_input_len);
|
||||
// If the JSON document failed to deserialize - halt the running device and print the error
|
||||
// because it is not possible to continue running in offline mode without a valid config file
|
||||
//if (error)
|
||||
//fsHalt("deserializeJson() failed: " + String(error.c_str()));
|
||||
|
||||
// Otherwise, parse away!
|
||||
|
||||
// TODO: Start by detecting which API the config file wants to parse the JSON into via the "componentAPI" field
|
||||
// TODO: We only need to handle componentTypeAdd messages here
|
||||
|
||||
// TODO: Can we access protobufs here?
|
||||
// create a digitalio protobuf message
|
||||
// wippersnapper_analogio_AnalogIOAdd addMsg = wippersnapper_analogio_AnalogIOAdd_init_zero;
|
||||
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Callback invoked when received READ10 command. Copies disk's
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
*
|
||||
* BSD license, all text here must be included in any redistribution.
|
||||
*
|
||||
*/
|
||||
*/
|
||||
#ifndef WIPPERSNAPPER_FS_V2_H
|
||||
#define WIPPERSNAPPER_FS_V2_H
|
||||
|
||||
|
|
@ -19,8 +19,8 @@
|
|||
#include "Adafruit_TinyUSB.h"
|
||||
#include "SdFat.h"
|
||||
// using f_mkfs() for formatting
|
||||
#include "fatfs/ff.h" // NOTE: This should be #included before fatfs/diskio.h!!!
|
||||
#include "fatfs/diskio.h"
|
||||
#include "fatfs/ff.h" // NOTE: This should be #included before fatfs/diskio.h!!!
|
||||
|
||||
#include "Wippersnapper_V2.h"
|
||||
#define SD_FAT_TYPE 3
|
||||
|
|
@ -59,9 +59,6 @@ public:
|
|||
void createSecretsFile();
|
||||
bool getSecretsFile();
|
||||
void parseSecrets();
|
||||
// Config.json API
|
||||
bool IsSDCardInserted();
|
||||
void parseConfigFile();
|
||||
|
||||
#ifdef ARDUINO_FUNHOUSE_ESP32S2
|
||||
void parseDisplayConfig(displayConfig &displayFile);
|
||||
|
|
|
|||
Loading…
Reference in a new issue