Skip to content

Commit

Permalink
- Add hardware RNG.
Browse files Browse the repository at this point in the history
- Add wifi error handler
  • Loading branch information
JasXSL committed Aug 25, 2018
1 parent e36e01d commit 832399f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 30 deletions.
9 changes: 9 additions & 0 deletions Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

};


Expand Down
5 changes: 2 additions & 3 deletions RandObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef RandObject_h
#define RandObject_h
#include <ArduinoJson.h>
#include "Configuration.h"
// Motor class extending PWM class
class RandObject{

Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions StatusLED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions StatusLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion UserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 13 additions & 9 deletions VhWifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}

Expand Down Expand Up @@ -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){
Expand Down
4 changes: 2 additions & 2 deletions VhWifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions VibHub-ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ void setup() {
//Serial.setDebugOutput(true);
delay(1000);
Serial.println("\nStarting...");

// Configure RNG
randomSeed(analogRead(A0));

// Set LED state
statusLED.initialize();
Expand Down

0 comments on commit 832399f

Please sign in to comment.