2017-10-19 15:43:39 +00:00
|
|
|
#ifndef ESPUI_h
|
|
|
|
#define ESPUI_h
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2018-07-03 15:43:58 +00:00
|
|
|
#define DEBUG_ESPUI true
|
2018-12-27 10:34:28 +00:00
|
|
|
#define WS_AUTHENTICATION false
|
2017-11-13 15:10:56 +00:00
|
|
|
|
2017-05-18 22:05:32 +00:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "ArduinoJson.h"
|
2017-10-16 22:10:48 +00:00
|
|
|
#include "FS.h"
|
2017-11-14 11:09:52 +00:00
|
|
|
#include "stdlib_noniso.h"
|
2017-10-16 13:00:53 +00:00
|
|
|
|
2017-12-01 16:11:16 +00:00
|
|
|
#if defined(ESP32)
|
|
|
|
|
2017-10-16 13:00:53 +00:00
|
|
|
#include <AsyncTCP.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
2018-11-26 17:25:10 +00:00
|
|
|
#include "SPIFFS.h"
|
|
|
|
#include "WiFi.h"
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2017-12-01 16:11:16 +00:00
|
|
|
#else
|
|
|
|
|
2018-11-26 17:25:10 +00:00
|
|
|
#include <ArduinoOTA.h>
|
2017-12-01 16:11:16 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESP8266mDNS.h>
|
|
|
|
#include <ESPAsyncTCP.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
2018-11-26 17:25:10 +00:00
|
|
|
#include <FS.h>
|
|
|
|
#include <Hash.h>
|
2017-12-01 16:11:16 +00:00
|
|
|
#include <SPIFFSEditor.h>
|
|
|
|
|
|
|
|
#define FILE_WRITE "w"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-05-13 18:19:29 +00:00
|
|
|
// Message Types (and control types)
|
2018-05-27 09:47:53 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
enum ControlType : uint8_t {
|
2019-03-03 22:22:01 +00:00
|
|
|
// fixed controls
|
2019-03-03 20:13:45 +00:00
|
|
|
Title = 0,
|
|
|
|
Button,
|
|
|
|
Pad,
|
2019-03-03 22:22:01 +00:00
|
|
|
PadWithCenter,
|
|
|
|
|
|
|
|
// updatable controls
|
2019-03-03 20:13:45 +00:00
|
|
|
Label,
|
|
|
|
Switcher,
|
|
|
|
Slider,
|
|
|
|
Number,
|
|
|
|
Text,
|
|
|
|
Graph,
|
|
|
|
GraphPoint,
|
2019-03-03 22:22:01 +00:00
|
|
|
Tab,
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
UpdateOffset = 100,
|
|
|
|
UpdateLabel = 104,
|
|
|
|
UpdateSwitcher,
|
|
|
|
UpdateSlider,
|
|
|
|
UpdateNumber,
|
|
|
|
UpdateText,
|
|
|
|
ClearGraph,
|
2019-03-03 22:22:01 +00:00
|
|
|
UpdateTab,
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
InitialGui = 200
|
|
|
|
};
|
2018-05-27 09:47:53 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
#define UI_INITIAL_GUI ControlType::InitialGui
|
|
|
|
|
|
|
|
#define UI_TITLE ControlType::Title
|
|
|
|
#define UI_LABEL ControlType::Label
|
|
|
|
#define UI_BUTTON ControlType::Button
|
|
|
|
#define UI_SWITCHER ControlType::Switcher
|
|
|
|
#define UI_PAD ControlType::Pad
|
|
|
|
#define UI_CPAD ControlType::Cpad
|
|
|
|
#define UI_SLIDER ControlType::Slider
|
|
|
|
#define UI_NUMBER ControlType::Number
|
|
|
|
#define UI_TEXT_INPUT ControlType::Text
|
|
|
|
#define UI_GRAPH ControlType::Graph
|
|
|
|
#define UI_ADD_GRAPH_POINT ControlType::GraphPoint
|
|
|
|
|
|
|
|
#define UPDATE_LABEL ControlType::UpdateLabel
|
|
|
|
#define UPDATE_SWITCHER ControlType::UpdateSwitcher
|
|
|
|
#define UPDATE_SLIDER ControlType::UpdateSlider
|
|
|
|
#define UPDATE_NUMBER ControlType::UpdateNumber
|
|
|
|
#define UPDATE_TEXT_INPUT ControlType::UpdateText
|
|
|
|
#define CLEAR_GRAPH ControlType::ClearGraph
|
2018-05-27 09:47:53 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
// Colors
|
|
|
|
enum ControlColor : uint8_t {
|
|
|
|
Turquoise,
|
|
|
|
Emerald,
|
|
|
|
Peterriver,
|
|
|
|
Wetasphalt,
|
|
|
|
Sunflower,
|
|
|
|
Carrot,
|
|
|
|
Alizarin,
|
|
|
|
Dark,
|
|
|
|
None = 0xFF
|
|
|
|
};
|
|
|
|
#define COLOR_TURQUOISE ControlColor::Turquoise
|
|
|
|
#define COLOR_EMERALD ControlColor::Emerald
|
|
|
|
#define COLOR_PETERRIVER ControlColor::Peterriver
|
|
|
|
#define COLOR_WETASPHALT ControlColor::Wetasphalt
|
|
|
|
#define COLOR_SUNFLOWER ControlColor::Sunflower
|
|
|
|
#define COLOR_CARROT ControlColor::Carrot
|
|
|
|
#define COLOR_ALIZARIN ControlColor::Alizarin
|
|
|
|
#define COLOR_DARK ControlColor::Dark
|
|
|
|
#define COLOR_NONE ControlColor::None
|
|
|
|
|
|
|
|
class Control {
|
|
|
|
public:
|
|
|
|
ControlType type;
|
2019-03-03 20:53:59 +00:00
|
|
|
uint16_t id; // just mirroring the id here for practical reasons
|
2019-03-03 20:13:45 +00:00
|
|
|
const char* label;
|
|
|
|
void ( *callback )( Control, int );
|
|
|
|
String value;
|
|
|
|
ControlColor color;
|
2019-03-03 22:22:01 +00:00
|
|
|
uint16_t parentControl;
|
2019-03-03 20:53:59 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
Control* next;
|
|
|
|
|
|
|
|
Control(
|
|
|
|
ControlType type, const char* label,
|
|
|
|
void ( *callback )( Control, int ),
|
2019-03-03 22:22:01 +00:00
|
|
|
String value, ControlColor color, uint16_t parentControl = 0xffff )
|
|
|
|
: type( type ), label( label ), callback( callback ), value( value ), color( color ), parentControl(parentControl), next( nullptr ) {
|
2019-03-03 20:13:45 +00:00
|
|
|
id = idCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Control( const Control& control )
|
|
|
|
: type( control.type ), id( control.id ), label( control.label ),
|
|
|
|
callback( control.callback ), value( control.value ),
|
2019-03-03 22:22:01 +00:00
|
|
|
color( control.color ), parentControl(control.parentControl), next( control.next ) {}
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static uint16_t idCounter;
|
|
|
|
};
|
2018-05-27 09:47:53 +00:00
|
|
|
|
2018-05-13 18:19:29 +00:00
|
|
|
|
2017-10-19 11:46:47 +00:00
|
|
|
// Values
|
|
|
|
#define B_DOWN -1
|
2017-11-14 11:09:52 +00:00
|
|
|
#define B_UP 1
|
2017-10-19 11:46:47 +00:00
|
|
|
|
|
|
|
#define P_LEFT_DOWN -2
|
|
|
|
#define P_LEFT_UP 2
|
|
|
|
#define P_RIGHT_DOWN -3
|
|
|
|
#define P_RIGHT_UP 3
|
|
|
|
#define P_FOR_DOWN -4
|
|
|
|
#define P_FOR_UP 4
|
|
|
|
#define P_BACK_DOWN -5
|
|
|
|
#define P_BACK_UP 5
|
|
|
|
#define P_CENTER_DOWN -6
|
|
|
|
#define P_CENTER_UP 6
|
2017-11-29 10:32:07 +00:00
|
|
|
|
2017-10-19 15:30:32 +00:00
|
|
|
#define S_ACTIVE -7
|
|
|
|
#define S_INACTIVE 7
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2017-11-29 10:32:07 +00:00
|
|
|
#define SL_VALUE 8
|
2018-05-13 18:19:29 +00:00
|
|
|
#define N_VALUE 9
|
2018-11-26 17:25:10 +00:00
|
|
|
#define T_VALUE 10
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2019-03-03 20:13:45 +00:00
|
|
|
enum Verbosity : uint8_t {
|
2019-03-03 20:50:06 +00:00
|
|
|
Quiet = 0,
|
|
|
|
Verbose,
|
|
|
|
VerboseJSON
|
2019-03-03 20:13:45 +00:00
|
|
|
};
|
2017-11-14 11:09:52 +00:00
|
|
|
|
2017-11-13 15:10:56 +00:00
|
|
|
class ESPUIClass {
|
2019-03-03 20:13:45 +00:00
|
|
|
public:
|
|
|
|
ESPUIClass( Verbosity verbosity = Verbosity::Quiet )
|
|
|
|
: verbosity( verbosity ) {}
|
|
|
|
|
|
|
|
// void begin( const char* _title, bool enableDebug=false ); // Setup servers and page in Memorymode
|
|
|
|
void begin( const char* _title, const char* username = nullptr, const char* password = nullptr );
|
|
|
|
|
|
|
|
// void beginSPIFFS( const char* _title, bool enableDebug=false ); // Setup servers and page in SPIFFSmode
|
|
|
|
void beginSPIFFS( const char* _title, const char* username = nullptr, const char* password = nullptr );
|
|
|
|
|
|
|
|
void prepareFileSystem(); // Initially preps the filesystem and loads a lot
|
|
|
|
// of stuff into SPIFFS
|
|
|
|
void list();
|
|
|
|
// Creating Elements
|
|
|
|
|
2019-03-03 22:22:01 +00:00
|
|
|
uint16_t addControl( ControlType type, const char* label,
|
2019-03-03 20:50:06 +00:00
|
|
|
String value = String( "" ), ControlColor color = ControlColor::Turquoise,
|
2019-03-03 22:22:01 +00:00
|
|
|
void ( *callback )( Control, int ) = nullptr, uint16_t parentControl = 0xffff );
|
2019-03-03 20:13:45 +00:00
|
|
|
|
|
|
|
int button( const char* label,
|
|
|
|
void ( *callback )( Control, int ), ControlColor color,
|
|
|
|
String value = "" ); // Create Event Button
|
|
|
|
int switcher( const char* label, bool startState,
|
|
|
|
void ( *callback )( Control, int ),
|
|
|
|
ControlColor color ); // Create Toggle Button
|
|
|
|
int pad( const char* label, bool centerButton,
|
|
|
|
void ( *callback )( Control, int ),
|
|
|
|
ControlColor color ); // Create Pad Control
|
|
|
|
int slider( const char* label,
|
|
|
|
void ( *callback )( Control, int ),
|
|
|
|
ControlColor color, String value ); // Create Slider Control
|
|
|
|
int number( const char* label,
|
|
|
|
void ( *callback )( Control, int ),
|
|
|
|
ControlColor color, int number, int min, int max ); // Create a Number Input Control
|
|
|
|
int text( const char* label,
|
|
|
|
void ( *callback )( Control, int ),
|
|
|
|
ControlColor color, String value = "" ); // Create a Text Input Control
|
|
|
|
|
|
|
|
// Output only
|
|
|
|
int label( const char* label, ControlColor color, String value = "" ); // Create Label
|
|
|
|
int graph( const char* label, ControlColor color ); // Create Graph display
|
|
|
|
|
|
|
|
// Update Elements
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
Control* getControl( uint16_t id );
|
2019-03-03 20:13:45 +00:00
|
|
|
Control* getControl( String label );
|
|
|
|
|
|
|
|
// Update Elements
|
2019-03-03 20:53:59 +00:00
|
|
|
void updateControl( uint16_t id, String value, int clientId = -1 );
|
2019-03-03 20:13:45 +00:00
|
|
|
void updateControl( String label, String value, int clientId = -1 );
|
|
|
|
void updateControl( Control* control, String value, int clientId = -1 );
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void print( uint16_t id, String value );
|
2019-03-03 20:13:45 +00:00
|
|
|
void print( String label, String value );
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void updateLabel( uint16_t id, String value );
|
2019-03-03 20:46:38 +00:00
|
|
|
void updateLabel( String label, String value );
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void updateSwitcher( uint16_t id, bool nValue, int clientId = -1 );
|
2019-03-03 20:13:45 +00:00
|
|
|
void updateSwitcher( String label, bool nValue, int clientId = -1 );
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void updateSlider( uint16_t id, int nValue, int clientId = -1 );
|
2019-03-03 20:13:45 +00:00
|
|
|
void updateSlider( String label, int nValue, int clientId = -1 );
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void updateNumber( uint16_t id, int nValue, int clientId = -1 );
|
2019-03-03 20:13:45 +00:00
|
|
|
void updateNumber( String label, int nValue, int clientId = -1 );
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void updateText( uint16_t id, String nValue, int clientId = -1 );
|
2019-03-03 20:13:45 +00:00
|
|
|
void updateText( String label, String nValue, int clientId = -1 );
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void clearGraph( uint16_t id, int clientId = -1 );
|
2019-03-03 20:13:45 +00:00
|
|
|
void clearGraph( String label, int clientId = -1 );
|
|
|
|
|
2019-03-03 20:53:59 +00:00
|
|
|
void addGraphPoint( uint16_t id, int nValue, int clientId = -1 );
|
2019-03-03 20:13:45 +00:00
|
|
|
void addGraphPoint( String label, int nValue, int clientId = -1 );
|
|
|
|
|
|
|
|
// void textThem( String text, int clientId = -1 );
|
|
|
|
|
|
|
|
// Variables ---
|
|
|
|
const char* ui_title = "ESPUI"; // Store UI Title and Header Name
|
|
|
|
Control* controls = nullptr;
|
|
|
|
void jsonDom( AsyncWebSocketClient* client );
|
|
|
|
|
|
|
|
Verbosity verbosity;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* basicAuthUsername = nullptr;
|
|
|
|
const char* basicAuthPassword = nullptr;
|
|
|
|
bool basicAuth = true;
|
|
|
|
AsyncWebServer* server;
|
|
|
|
AsyncWebSocket* ws;
|
2017-05-18 22:05:32 +00:00
|
|
|
};
|
|
|
|
|
2017-10-19 15:43:39 +00:00
|
|
|
extern ESPUIClass ESPUI;
|
2017-05-18 22:05:32 +00:00
|
|
|
#endif
|
2019-03-03 22:22:01 +00:00
|
|
|
|