diff --git a/Configuration.h b/Configuration.h index d7b1204..5e001b3 100644 --- a/Configuration.h +++ b/Configuration.h @@ -46,6 +46,15 @@ namespace Configuration{ const int BUTTON_HOLD_TIME = 3000; // Time before it enters config mode const int BUTTON_DEBOUNCE = 100; // Debounce time + + // Global randomizer function + // Returns a random value which can be min through and including max + // min0 max3 would generate 0, 1, 2, or 3 + inline int espRandBetween(int minimum, int maximum){ + float r = esp_random()/UINT32_MAX; + return (minimum+floor((maximum+1-minimum)*r)); + } + }; diff --git a/RandObject.h b/RandObject.h index 885a397..b7e0d8e 100644 --- a/RandObject.h +++ b/RandObject.h @@ -6,6 +6,7 @@ #ifndef RandObject_h #define RandObject_h #include +#include "Configuration.h" // Motor class extending PWM class class RandObject{ @@ -40,10 +41,8 @@ class RandObject{ int getValue(int valueOnFalse = 0){ if(useDefault) return valueOnFalse; - - int out = random(min, max+1)*multi+offset; //Serial.printf("Returning a random value between min %i, max %i, multi %i, offset %i: %i\n", min, max, multi, offset, out); - return out; + return Configuration::espRandBetween(min, max)*multi+offset; } private: diff --git a/StatusLED.cpp b/StatusLED.cpp index 1ed0d43..7b58fab 100644 --- a/StatusLED.cpp +++ b/StatusLED.cpp @@ -5,12 +5,12 @@ Ticker ledTicker; // Ticker for LED -const char StatusLED::STATE_BOOT = 0; -const char StatusLED::STATE_INIT = 1; -const char StatusLED::STATE_PORTAL = 2; -const char StatusLED::STATE_WIFI_ERR = 3; -const char StatusLED::STATE_SOCKET_ERR = 4; -const char StatusLED::STATE_RUNNING = 5; +const uint8_t StatusLED::STATE_BOOT = 0; +const uint8_t StatusLED::STATE_INIT = 1; +const uint8_t StatusLED::STATE_PORTAL = 2; +const uint8_t StatusLED::STATE_WIFI_ERR = 3; +const uint8_t StatusLED::STATE_SOCKET_ERR = 4; +const uint8_t StatusLED::STATE_RUNNING = 5; #define RED 0x1 #define GREEN 0x2 diff --git a/StatusLED.h b/StatusLED.h index b782fdc..bb0d5cf 100644 --- a/StatusLED.h +++ b/StatusLED.h @@ -13,12 +13,12 @@ class StatusLED{ public: StatusLED(void); void initialize(); // Reserves PWM channels - static const char STATE_BOOT; - static const char STATE_INIT; - static const char STATE_PORTAL; - static const char STATE_WIFI_ERR; - static const char STATE_SOCKET_ERR; - static const char STATE_RUNNING; + static const uint8_t STATE_BOOT; + static const uint8_t STATE_INIT; + static const uint8_t STATE_PORTAL; + static const uint8_t STATE_WIFI_ERR; + static const uint8_t STATE_SOCKET_ERR; + static const uint8_t STATE_RUNNING; bool ledTickerHigh; void setState( int state = STATE_BOOT ); diff --git a/UserSettings.cpp b/UserSettings.cpp index a109667..6460708 100644 --- a/UserSettings.cpp +++ b/UserSettings.cpp @@ -112,7 +112,7 @@ void UserSettings::gen_random( char *s, const int len ){ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; for( int i = 0; i < len; ++i ) - s[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; + s[i] = alphanum[esp_random() % (sizeof(alphanum) - 1)]; s[len] = 0; diff --git a/VhWifi.cpp b/VhWifi.cpp index a2a4a0e..e0c5fee 100644 --- a/VhWifi.cpp +++ b/VhWifi.cpp @@ -61,24 +61,19 @@ void VhWifi::connect( bool force, bool reset ){ if( !wifiManager.startConfigPortal(ssid.c_str()) ){ - statusLED.setState( StatusLED::STATE_WIFI_ERR ); Serial.println("VhWifi: Failed to connect and hit timeout"); - //reset and try again, or maybe put it to deep sleep - ESP.restart(); - delay(1000); + handleFatalError(); } } + // Try to connect to AP, if that doesn't work, enter config mode else if( !wifiManager.autoConnect(ssid.c_str()) ){ - statusLED.setState( StatusLED::STATE_WIFI_ERR ); + // Config mode failed to enter Serial.println("VhWifi: Failed to connect and hit timeout"); - //reset and try again, or maybe put it to deep sleep - // Todo: Decide what to do here - ESP.restart(); - delay(1000); + handleFatalError(); } @@ -109,6 +104,15 @@ void VhWifi::connect( bool force, bool reset ){ Serial.println("VhWifi: connected"); } +// Unrecoverable connection error +void VhWifi::handleFatalError(){ + + statusLED.setState( StatusLED::STATE_WIFI_ERR ); + delay(5000); + esp_deep_sleep_start(); + +} + void VhWifi::clearSettings(){ Serial.println("VhWifi::clearSettings("); if (_wifiManager){ diff --git a/VhWifi.h b/VhWifi.h index 0dc3740..f8248df 100644 --- a/VhWifi.h +++ b/VhWifi.h @@ -333,8 +333,8 @@ class VhWifi{ private: WiFiManager* _wifiManager; - bool shouldSaveConfig; // Checks if config should be saved, default false - + bool shouldSaveConfig; // Checks if config should be saved, default false + void handleFatalError(); // If something happened that can't be recovered from // Any non-constant data needed to be loaded should go in here String getCustomJSPre(); // Non constant data that should go above the constant data String getCustomJSPost(); // == || == below the constant data diff --git a/VibHub-ESP32.ino b/VibHub-ESP32.ino index 371a241..d7e4a02 100644 --- a/VibHub-ESP32.ino +++ b/VibHub-ESP32.ino @@ -25,9 +25,6 @@ void setup() { //Serial.setDebugOutput(true); delay(1000); Serial.println("\nStarting..."); - - // Configure RNG - randomSeed(analogRead(A0)); // Set LED state statusLED.initialize();