2022-09-21 19:45:48 +00:00
# pragma once
2017-05-18 22:05:32 +00:00
2022-09-21 19:45:48 +00:00
// comment out to turn off debug output
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
2022-09-21 19:45:48 +00:00
# include <Arduino.h>
# include <ArduinoJson.h>
# include <stdlib_noniso.h>
2022-10-13 20:09:05 +00:00
# ifdef ESP32
# if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4
# include <LittleFS.h>
# else
# include <LITTLEFS.h>
# endif
2022-10-13 19:52:19 +00:00
# else
2022-10-13 20:09:05 +00:00
# include <LittleFS.h>
2022-10-13 19:52:19 +00:00
# endif
2022-09-21 19:45:48 +00:00
# include <map>
2022-09-22 13:34:53 +00:00
# include <ESPAsyncWebServer.h>
2022-09-21 19:45:48 +00:00
# include "ESPUIcontrol.h"
# include "ESPUIclient.h"
2017-10-16 13:00:53 +00:00
2017-12-01 16:11:16 +00:00
# if defined(ESP32)
2019-03-24 17:18:08 +00:00
# include <AsyncTCP.h>
2020-09-28 08:40:31 +00:00
# include "WiFi.h"
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>
2018-11-26 17:25:10 +00:00
# include <Hash.h>
2017-12-01 16:11:16 +00:00
# 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
2022-09-21 19:45:48 +00:00
enum MessageTypes : uint8_t
2020-08-26 20:13:21 +00:00
{
2020-09-28 08:40:31 +00:00
InitialGui = 200 ,
2022-01-01 22:04:32 +00:00
Reload = 201 ,
2022-09-21 19:45:48 +00:00
ExtendGUI = 210 ,
UpdateGui = 220 ,
ExtendedUpdateGui = 230 ,
2019-03-03 20:13:45 +00:00
} ;
2018-05-27 09:47:53 +00:00
2022-09-21 19:45:48 +00:00
# define UI_INITIAL_GUI MessageTypes::InitialGui
# define UI_EXTEND_GUI MessageTypes::ExtendGUI
# define UI_RELOAD MessageTypes::Reload
2018-05-27 09:47:53 +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
2019-03-04 20:07:39 +00:00
# define S_VALUE 11
2022-01-20 21:50:06 +00:00
# define TM_VALUE 12
2017-10-19 11:46:47 +00:00
2020-08-26 20:13:21 +00:00
enum Verbosity : uint8_t
{
2020-09-28 08:40:31 +00:00
Quiet = 0 ,
Verbose ,
VerboseJSON
2020-08-26 20:13:21 +00:00
} ;
2017-11-14 11:09:52 +00:00
2020-08-26 20:13:21 +00:00
class ESPUIClass
{
2019-03-24 17:18:08 +00:00
public :
2020-09-28 08:40:31 +00:00
ESPUIClass ( )
{
verbosity = Verbosity : : Quiet ;
jsonUpdateDocumentSize = 2000 ;
jsonInitialDocumentSize = 8000 ;
sliderContinuous = false ;
2022-09-21 19:45:48 +00:00
# ifdef ESP32
ControlsSemaphore = xSemaphoreCreateMutex ( ) ;
xSemaphoreGive ( ControlsSemaphore ) ;
# endif // def ESP32
2020-09-28 08:40:31 +00:00
}
unsigned int jsonUpdateDocumentSize ;
unsigned int jsonInitialDocumentSize ;
bool sliderContinuous ;
2022-09-21 19:45:48 +00:00
void onWsEvent ( AsyncWebSocket * server , AsyncWebSocketClient * client , AwsEventType type , void * arg , uint8_t * data , size_t len ) ;
2022-09-20 14:29:34 +00:00
bool captivePortal = true ;
2020-09-28 08:40:31 +00:00
void setVerbosity ( Verbosity verbosity ) ;
2021-10-29 17:20:20 +00:00
void begin ( const char * _title , const char * username = nullptr , const char * password = nullptr ,
uint16_t port = 80 ) ; // Setup server and page in Memorymode
void beginSPIFFS ( const char * _title , const char * username = nullptr , const char * password = nullptr ,
2022-01-04 10:35:43 +00:00
uint16_t port = 80 ) ; // Setup server and page in LITTLEFS mode (DEPRECATED, use beginLITTLEFS)
void beginLITTLEFS ( const char * _title , const char * username = nullptr , const char * password = nullptr ,
uint16_t port = 80 ) ; // Setup server and page in LITTLEFS mode
2020-09-28 08:40:31 +00:00
2022-10-13 18:53:55 +00:00
void prepareFileSystem ( bool format = true ) ; // Initially preps the filesystem and loads a lot of
2022-01-04 10:35:43 +00:00
// stuff into LITTLEFS
void list ( ) ; // Lists LITTLEFS directory
2020-09-28 08:40:31 +00:00
2022-06-11 04:42:25 +00:00
uint16_t addControl ( ControlType type , const char * label ) ;
uint16_t addControl ( ControlType type , const char * label , const String & value ) ;
uint16_t addControl ( ControlType type , const char * label , const String & value , ControlColor color ) ;
uint16_t addControl ( ControlType type , const char * label , const String & value , ControlColor color , uint16_t parentControl ) ;
uint16_t addControl ( ControlType type , const char * label , const String & value , ControlColor color , uint16_t parentControl , void ( * callback ) ( Control * , int ) ) ;
uint16_t addControl ( ControlType type , const char * label , const String & value , ControlColor color , uint16_t parentControl , void ( * callback ) ( Control * , int , void * ) , void * UserData ) ;
2022-09-21 19:45:48 +00:00
bool removeControl ( uint16_t id , bool force_rebuild_ui = false ) ;
2020-09-28 08:40:31 +00:00
// create Elements
2022-06-11 05:20:25 +00:00
// Create Event Button
uint16_t button ( const char * label , void ( * callback ) ( Control * , int ) , ControlColor color , const String & value = " " ) ;
uint16_t button ( const char * label , void ( * callback ) ( Control * , int , void * ) , ControlColor color , const String & value , void * UserData ) ;
uint16_t switcher ( const char * label , void ( * callback ) ( Control * , int ) , ControlColor color , bool startState = false ) ; // Create Toggle Button
uint16_t switcher ( const char * label , void ( * callback ) ( Control * , int , void * ) , ControlColor color , bool startState , void * UserData ) ; // Create Toggle Button
uint16_t pad ( const char * label , void ( * callback ) ( Control * , int ) , ControlColor color ) ; // Create Pad Control
uint16_t pad ( const char * label , void ( * callback ) ( Control * , int , void * ) , ControlColor color , void * UserData ) ; // Create Pad Control
uint16_t padWithCenter ( const char * label , void ( * callback ) ( Control * , int ) , ControlColor color ) ; // Create Pad Control with Centerbutton
uint16_t padWithCenter ( const char * label , void ( * callback ) ( Control * , int , void * ) , ControlColor color , void * UserData ) ; // Create Pad Control with Centerbutton
uint16_t slider ( const char * label , void ( * callback ) ( Control * , int ) , ControlColor color , int value , int min = 0 , int max = 100 ) ; // Create Slider Control
uint16_t slider ( const char * label , void ( * callback ) ( Control * , int , void * ) , ControlColor color , int value , int min , int max , void * UserData ) ; // Create Slider Control
uint16_t number ( const char * label , void ( * callback ) ( Control * , int ) , ControlColor color , int value , int min = 0 , int max = 100 ) ; // Create a Number Input Control
uint16_t number ( const char * label , void ( * callback ) ( Control * , int , void * ) , ControlColor color , int value , int min , int max , void * UserData ) ; // Create a Number Input Control
uint16_t text ( const char * label , void ( * callback ) ( Control * , int ) , ControlColor color , const String & value = " " ) ; // Create a Text Input Control
uint16_t text ( const char * label , void ( * callback ) ( Control * , int , void * ) , ControlColor color , const String & value , void * UserData ) ; // Create a Text Input Control
2020-09-28 08:40:31 +00:00
// Output only
uint16_t label ( const char * label , ControlColor color ,
const String & value = " " ) ; // Create Label
uint16_t graph ( const char * label , ControlColor color ) ; // Create Graph display
uint16_t gauge ( const char * label , ControlColor color , int value , int min = 0 ,
int max = 100 ) ; // Create Gauge display
2022-01-08 20:58:22 +00:00
uint16_t separator ( const char * label ) ; //Create separator
2020-09-28 08:40:31 +00:00
// Input only
uint16_t accelerometer ( const char * label , void ( * callback ) ( Control * , int ) , ControlColor color ) ;
2022-06-11 05:20:25 +00:00
uint16_t accelerometer ( const char * label , void ( * callback ) ( Control * , int , void * ) , ControlColor color , void * UserData ) ;
2020-09-28 08:40:31 +00:00
// Update Elements
Control * getControl ( uint16_t id ) ;
2022-09-21 19:45:48 +00:00
Control * getControlNoLock ( uint16_t id ) ;
2020-09-28 08:40:31 +00:00
// Update Elements
void updateControlValue ( uint16_t id , const String & value , int clientId = - 1 ) ;
void updateControlValue ( Control * control , const String & value , int clientId = - 1 ) ;
2022-09-21 19:45:48 +00:00
void updateControlLabel ( uint16_t control , const char * value , int clientId = - 1 ) ;
void updateControlLabel ( Control * control , const char * value , int clientId = - 1 ) ;
2020-09-28 08:40:31 +00:00
void updateControl ( uint16_t id , int clientId = - 1 ) ;
void updateControl ( Control * control , int clientId = - 1 ) ;
void print ( uint16_t id , const String & value ) ;
void updateLabel ( uint16_t id , const String & value ) ;
2022-01-16 14:47:41 +00:00
void updateButton ( uint16_t id , const String & value ) ;
2020-09-28 08:40:31 +00:00
void updateSwitcher ( uint16_t id , bool nValue , int clientId = - 1 ) ;
void updateSlider ( uint16_t id , int nValue , int clientId = - 1 ) ;
void updateNumber ( uint16_t id , int nValue , int clientId = - 1 ) ;
void updateText ( uint16_t id , const String & nValue , int clientId = - 1 ) ;
void updateSelect ( uint16_t id , const String & nValue , int clientId = - 1 ) ;
void updateGauge ( uint16_t id , int number , int clientId ) ;
2022-01-20 21:50:06 +00:00
void updateTime ( uint16_t id , int clientId = - 1 ) ;
2020-09-28 08:40:31 +00:00
void clearGraph ( uint16_t id , int clientId = - 1 ) ;
void addGraphPoint ( uint16_t id , int nValue , int clientId = - 1 ) ;
2022-01-02 21:56:32 +00:00
void setPanelStyle ( uint16_t id , String style , int clientId = - 1 ) ;
void setElementStyle ( uint16_t id , String style , int clientId = - 1 ) ;
2022-05-22 22:31:48 +00:00
void setInputType ( uint16_t id , String type , int clientId = - 1 ) ;
2022-01-02 21:56:32 +00:00
2022-01-08 21:25:10 +00:00
void setPanelWide ( uint16_t id , bool wide ) ;
2022-01-21 23:30:08 +00:00
void setVertical ( uint16_t id , bool vert = true ) ;
2022-01-31 21:43:09 +00:00
void setEnabled ( uint16_t id , bool enabled = true , int clientId = - 1 ) ;
2022-01-28 20:31:25 +00:00
void updateVisibility ( uint16_t id , bool visibility , int clientId = - 1 ) ;
2022-01-08 21:25:10 +00:00
2020-09-28 08:40:31 +00:00
// Variables
const char * ui_title = " ESPUI " ; // Store UI Title and Header Name
Control * controls = nullptr ;
void jsonReload ( ) ;
2022-09-21 19:45:48 +00:00
void jsonDom ( uint16_t startidx , AsyncWebSocketClient * client = nullptr , bool Updating = false ) ;
2020-09-28 08:40:31 +00:00
Verbosity verbosity ;
2022-09-21 19:45:48 +00:00
protected :
friend class ESPUIclient ;
friend class ESPUIcontrol ;
# ifdef ESP32
SemaphoreHandle_t ControlsSemaphore = NULL ;
# endif // def ESP32
void RemoveToBeDeletedControls ( ) ;
2020-09-28 08:40:31 +00:00
AsyncWebServer * server ;
AsyncWebSocket * ws ;
2019-12-27 14:36:24 +00:00
2020-09-28 08:40:31 +00:00
const char * basicAuthUsername = nullptr ;
const char * basicAuthPassword = nullptr ;
bool basicAuth = true ;
2022-01-12 20:12:20 +00:00
uint16_t controlCount = 0 ;
2022-09-21 19:45:48 +00:00
# define ClientUpdateType_t ESPUIclient::ClientUpdateType_t
void NotifyClients ( ClientUpdateType_t newState ) ;
void NotifyClient ( uint32_t WsClientId , ClientUpdateType_t newState ) ;
void ClearControlUpdateFlags ( ) ;
bool SendJsonDocToWebSocket ( ArduinoJson : : DynamicJsonDocument & document , uint16_t clientId ) ;
std : : map < uint32_t , ESPUIclient * > MapOfClients ;
2017-05-18 22:05:32 +00:00
} ;
2017-10-19 15:43:39 +00:00
extern ESPUIClass ESPUI ;