1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-10-31 22:10:54 +00:00

Changed signature of the callback

Call the callback with a pointer instead of a copy of the Control.
Changed all the examples
This commit is contained in:
Christian Riggenbach 2019-03-04 20:49:18 +01:00
parent 2b31e81f50
commit 3c42b43fe6
5 changed files with 197 additions and 153 deletions

View File

@ -18,19 +18,23 @@ const char* ssid = "ESPUI";
const char* password = "espui"; const char* password = "espui";
const char* hostname = "EspuiTest"; const char* hostname = "EspuiTest";
void numberCall( Control sender, int type ) { void numberCall( Control* sender, int type ) {
Serial.println( sender.value ); Serial.println( sender->value );
} }
void textCall( Control sender, int type ) { void textCall( Control* sender, int type ) {
Serial.println( sender.value ); Serial.print("Text: ID: ");
} Serial.print(sender->id);
Serial.print(", Value: ");
Serial.println( sender->value );}
void slider( Control sender, int type ) { void slider( Control* sender, int type ) {
Serial.println( sender.value ); Serial.print("Slider: ID: ");
} Serial.print(sender->id);
Serial.print(", Value: ");
Serial.println( sender->value );}
void buttonCallback( Control sender, int type ) { void buttonCallback( Control* sender, int type ) {
switch ( type ) { switch ( type ) {
case B_DOWN: case B_DOWN:
Serial.println( "Button DOWN" ); Serial.println( "Button DOWN" );
@ -42,20 +46,20 @@ void buttonCallback( Control sender, int type ) {
} }
} }
void buttonExample( Control sender, int type ) { void buttonExample( Control* sender, int type ) {
switch ( type ) { switch ( type ) {
case B_DOWN: case B_DOWN:
Serial.println( "Status: Start" ); Serial.println( "Status: Start" );
ESPUI.updateControl( "Status", "Start" ); ESPUI.updateControl( "Status:", "Start" );
break; break;
case B_UP: case B_UP:
Serial.println( "Status: Stop" ); Serial.println( "Status: Stop" );
ESPUI.updateControl( "Status", "Stop" ); ESPUI.updateControl( "Status:", "Stop" );
break; break;
} }
} }
void padExample( Control sender, int value ) { void padExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case P_LEFT_DOWN: case P_LEFT_DOWN:
Serial.print( "left down" ); Serial.print( "left down" );
@ -99,10 +103,10 @@ void padExample( Control sender, int value ) {
} }
Serial.print( " " ); Serial.print( " " );
Serial.println( sender.id ); Serial.println( sender->id );
} }
void switchExample( Control sender, int value ) { void switchExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case S_ACTIVE: case S_ACTIVE:
Serial.print( "Active:" ); Serial.print( "Active:" );
@ -117,7 +121,7 @@ void switchExample( Control sender, int value ) {
Serial.println( sender.id ); Serial.println( sender.id );
} }
void otherSwitchExample( Control sender, int value ) { void otherSwitchExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case S_ACTIVE: case S_ACTIVE:
Serial.print( "Active:" ); Serial.print( "Active:" );
@ -129,7 +133,7 @@ void otherSwitchExample( Control sender, int value ) {
} }
Serial.print( " " ); Serial.print( " " );
Serial.println( sender.id ); Serial.println( sender->id );
} }
void setup( void ) { void setup( void ) {
@ -181,18 +185,18 @@ void setup( void ) {
Serial.print( "IP address: " ); Serial.print( "IP address: " );
Serial.println( WiFi.getMode() == WIFI_AP ? WiFi.softAPIP() : WiFi.localIP() ); Serial.println( WiFi.getMode() == WIFI_AP ? WiFi.softAPIP() : WiFi.localIP() );
ESPUI.addControl( ControlType::Label, "Status:", "Stop" ); ESPUI.addControl( ControlType::Label, "Status:", "Stop", ControlColor::Turquoise );
ESPUI.addControl( ControlType::Label, "Millis:", "0", ControlColor::Emerald ); ESPUI.addControl( ControlType::Text, "Text Test:", "a Text Field", ControlColor::Alizarin, Control::noParent, &textCall );
ESPUI.addControl( ControlType::Button, "Push Button", "Press", ControlColor::Peterriver, &buttonCallback ); ESPUI.addControl( ControlType::Label, "Millis:", "0", ControlColor::Emerald, Control::noParent );
ESPUI.addControl( ControlType::Button, "Other Button", "Press", ControlColor::Wetasphalt, &buttonExample ); ESPUI.addControl( ControlType::Button, "Push Button", "Press", ControlColor::Peterriver, Control::noParent, &buttonCallback );
ESPUI.addControl( ControlType::Cpad, "Pad with center", "", ControlColor::Sunflower, &padExample ); ESPUI.addControl( ControlType::Button, "Other Button", "Press", ControlColor::Wetasphalt, Control::noParent, &buttonExample );
ESPUI.addControl( ControlType::Pad, "Pad without center", "", ControlColor::Carrot, &padExample ); ESPUI.addControl( ControlType::PadWithCenter, "Pad with center", "", ControlColor::Sunflower, Control::noParent, &padExample );
ESPUI.addControl( ControlType::Switcher, "Switch one", "", ControlColor::Alizarin, &switchExample ); ESPUI.addControl( ControlType::Pad, "Pad without center", "", ControlColor::Carrot, Control::noParent, &padExample );
ESPUI.addControl( ControlType::Switcher, "Switch two", "", ControlColor::None, &otherSwitchExample ); ESPUI.addControl( ControlType::Switcher, "Switch one", "", ControlColor::Alizarin, Control::noParent, &switchExample );
ESPUI.addControl( ControlType::Slider, "Slider one", "30", ControlColor::Alizarin, &slider ); ESPUI.addControl( ControlType::Switcher, "Switch two", "", ControlColor::None, Control::noParent, &otherSwitchExample );
ESPUI.addControl( ControlType::Slider, "Slider two", "100", ControlColor::Alizarin, &slider ); ESPUI.addControl( ControlType::Slider, "Slider one", "30", ControlColor::Alizarin, Control::noParent, &slider );
ESPUI.addControl( ControlType::Text, "Text Test:", "a Text Field", ControlColor::Alizarin, &textCall ); ESPUI.addControl( ControlType::Slider, "Slider two", "100", ControlColor::Alizarin, Control::noParent, &slider );
ESPUI.addControl( ControlType::Number, "Number:", "50", ControlColor::Alizarin, &numberCall ); ESPUI.addControl( ControlType::Number, "Number:", "50", ControlColor::Alizarin, Control::noParent, &numberCall );
/* /*
* .begin loads and serves all files from PROGMEM directly. * .begin loads and serves all files from PROGMEM directly.
@ -223,3 +227,5 @@ void loop( void ) {
oldTime = millis(); oldTime = millis();
} }
} }

View File

@ -21,19 +21,26 @@ const char* hostname = "EspuiTest";
long oldTime = 0; long oldTime = 0;
bool switchi = false; bool switchi = false;
void numberCall( Control sender, int type ) {
Serial.println( sender.value ); void numberCall( Control* sender, int type ) {
Serial.println( sender->value );
} }
void textCall( Control sender, int type ) { void textCall( Control* sender, int type ) {
Serial.println( sender.value ); Serial.print( "Text: ID: " );
Serial.print( sender->id );
Serial.print( ", Value: " );
Serial.println( sender->value );
} }
void slider( Control sender, int type ) { void slider( Control* sender, int type ) {
Serial.println( sender.value ); Serial.print( "Slider: ID: " );
Serial.print( sender->id );
Serial.print( ", Value: " );
Serial.println( sender->value );
} }
void buttonCallback( Control sender, int type ) { void buttonCallback( Control* sender, int type ) {
switch ( type ) { switch ( type ) {
case B_DOWN: case B_DOWN:
Serial.println( "Button DOWN" ); Serial.println( "Button DOWN" );
@ -45,20 +52,20 @@ void buttonCallback( Control sender, int type ) {
} }
} }
void buttonExample( Control sender, int type ) { void buttonExample( Control* sender, int type ) {
switch ( type ) { switch ( type ) {
case B_DOWN: case B_DOWN:
Serial.println( "Status: Start" ); Serial.println( "Status: Start" );
ESPUI.print( 0, "Status: Start" ); ESPUI.updateControl( "Status:", "Start" );
break; break;
case B_UP: case B_UP:
Serial.println( "Status: Stop" ); Serial.println( "Status: Stop" );
ESPUI.print( 0, "Status: Stop" ); ESPUI.updateControl( "Status:", "Stop" );
break; break;
} }
} }
void padExample( Control sender, int value ) { void padExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case P_LEFT_DOWN: case P_LEFT_DOWN:
Serial.print( "left down" ); Serial.print( "left down" );
@ -102,10 +109,10 @@ void padExample( Control sender, int value ) {
} }
Serial.print( " " ); Serial.print( " " );
Serial.println( sender.id ); Serial.println( sender->id );
} }
void switchExample( Control sender, int value ) { void switchExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case S_ACTIVE: case S_ACTIVE:
Serial.print( "Active:" ); Serial.print( "Active:" );
@ -117,10 +124,10 @@ void switchExample( Control sender, int value ) {
} }
Serial.print( " " ); Serial.print( " " );
Serial.println( sender.id ); Serial.println( sender->id );
} }
void otherSwitchExample( Control sender, int value ) { void otherSwitchExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case S_ACTIVE: case S_ACTIVE:
Serial.print( "Active:" ); Serial.print( "Active:" );
@ -132,7 +139,7 @@ void otherSwitchExample( Control sender, int value ) {
} }
Serial.print( " " ); Serial.print( " " );
Serial.println( sender.id ); Serial.println( sender->id );
} }
void setup( void ) { void setup( void ) {
@ -186,7 +193,7 @@ void setup( void ) {
ESPUI.label( "Status:", COLOR_TURQUOISE, "Stop" ); ESPUI.label( "Status:", COLOR_TURQUOISE, "Stop" );
ESPUI.label( "Millis:", COLOR_EMERALD, "0" ); ESPUI.label( "Millis:", COLOR_EMERALD, "0" );
ESPUI.button( "Push Button", &buttonCallback, COLOR_PETERRIVER ); ESPUI.button( "Push Button", &buttonCallback, COLOR_PETERRIVER, "Press" );
ESPUI.button( "Other Button", &buttonExample, COLOR_WETASPHALT, "Press" ); ESPUI.button( "Other Button", &buttonExample, COLOR_WETASPHALT, "Press" );
ESPUI.pad( "Pad with center", true, &padExample, COLOR_SUNFLOWER ); ESPUI.pad( "Pad with center", true, &padExample, COLOR_SUNFLOWER );
ESPUI.pad( "Pad without center", false, &padExample, COLOR_CARROT ); ESPUI.pad( "Pad without center", false, &padExample, COLOR_CARROT );

View File

@ -18,19 +18,23 @@ const char* ssid = "ESPUI";
const char* password = "espui"; const char* password = "espui";
const char* hostname = "EspuiTest"; const char* hostname = "EspuiTest";
void numberCall( Control sender, int type ) { void numberCall( Control* sender, int type ) {
Serial.println( sender.value ); Serial.println( sender->value );
} }
void textCall( Control sender, int type ) { void textCall( Control* sender, int type ) {
Serial.println( sender.value ); Serial.print("Text: ID: ");
} Serial.print(sender->id);
Serial.print(", Value: ");
Serial.println( sender->value );}
void slider( Control sender, int type ) { void slider( Control* sender, int type ) {
Serial.println( sender.value ); Serial.print("Slider: ID: ");
} Serial.print(sender->id);
Serial.print(", Value: ");
Serial.println( sender->value );}
void buttonCallback( Control sender, int type ) { void buttonCallback( Control* sender, int type ) {
switch ( type ) { switch ( type ) {
case B_DOWN: case B_DOWN:
Serial.println( "Button DOWN" ); Serial.println( "Button DOWN" );
@ -42,7 +46,7 @@ void buttonCallback( Control sender, int type ) {
} }
} }
void buttonExample( Control sender, int type ) { void buttonExample( Control* sender, int type ) {
switch ( type ) { switch ( type ) {
case B_DOWN: case B_DOWN:
Serial.println( "Status: Start" ); Serial.println( "Status: Start" );
@ -55,7 +59,7 @@ void buttonExample( Control sender, int type ) {
break; break;
} }
} }
void padExample( Control sender, int value ) { void padExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case P_LEFT_DOWN: case P_LEFT_DOWN:
Serial.print( "left down" ); Serial.print( "left down" );
@ -99,10 +103,10 @@ void padExample( Control sender, int value ) {
} }
Serial.print( " " ); Serial.print( " " );
Serial.println( sender.id ); Serial.println( sender->id );
} }
void switchExample( Control sender, int value ) { void switchExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case S_ACTIVE: case S_ACTIVE:
Serial.print( "Active:" ); Serial.print( "Active:" );
@ -117,7 +121,7 @@ void switchExample( Control sender, int value ) {
Serial.println( sender.id ); Serial.println( sender.id );
} }
void otherSwitchExample( Control sender, int value ) { void otherSwitchExample( Control* sender, int value ) {
switch ( value ) { switch ( value ) {
case S_ACTIVE: case S_ACTIVE:
Serial.print( "Active:" ); Serial.print( "Active:" );
@ -129,7 +133,7 @@ void otherSwitchExample( Control sender, int value ) {
} }
Serial.print( " " ); Serial.print( " " );
Serial.println( sender.id ); Serial.println( sender->id );
} }
void setup( void ) { void setup( void ) {
@ -180,23 +184,27 @@ void setup( void ) {
Serial.println( WiFi.getMode() == WIFI_AP ? "Station" : "Client" ); Serial.println( WiFi.getMode() == WIFI_AP ? "Station" : "Client" );
Serial.print( "IP address: " ); Serial.print( "IP address: " );
Serial.println( WiFi.getMode() == WIFI_AP ? WiFi.softAPIP() : WiFi.localIP() ); Serial.println( WiFi.getMode() == WIFI_AP ? WiFi.softAPIP() : WiFi.localIP() );
uint16_t tab1 = ESPUI.addControl( ControlType::Tab, "Settings 1", "Settings 1");
uint16_t tab2 = ESPUI.addControl( ControlType::Tab, "Settings 2", "Settings 2");
uint16_t tab3 = ESPUI.addControl( ControlType::Tab, "Settings 3", "Settings 3");
ESPUI.addControl( ControlType::Label, "Status:", "Stop",ControlColor::Turquoise, nullptr, tab1 ); uint16_t tab1 = ESPUI.addControl( ControlType::Tab, "Settings 1", "Settings 1" );
ESPUI.addControl( ControlType::Label, "Millis:", "0", ControlColor::Emerald, nullptr, tab1 ); uint16_t tab2 = ESPUI.addControl( ControlType::Tab, "Settings 2", "Settings 2" );
ESPUI.addControl( ControlType::Button, "Push Button", "Press", ControlColor::Peterriver, &buttonCallback, tab1 ); uint16_t tab3 = ESPUI.addControl( ControlType::Tab, "Settings 3", "Settings 3" );
ESPUI.addControl( ControlType::Button, "Other Button", "Press", ControlColor::Wetasphalt, &buttonExample, tab1 );
ESPUI.addControl( ControlType::PadWithCenter, "Pad with center", "", ControlColor::Sunflower, &padExample, tab2 ); // shown above all tabs
ESPUI.addControl( ControlType::Pad, "Pad without center", "", ControlColor::Carrot, &padExample, tab3 ); ESPUI.addControl( ControlType::Label, "Status:", "Stop", ControlColor::Turquoise );
ESPUI.addControl( ControlType::Switcher, "Switch one", "", ControlColor::Alizarin, &switchExample, tab3 );
ESPUI.addControl( ControlType::Switcher, "Switch two", "", ControlColor::None, &otherSwitchExample, tab3 ); ESPUI.addControl( ControlType::Text, "Text Test:", "a Text Field", ControlColor::Alizarin, tab1, &textCall );
ESPUI.addControl( ControlType::Slider, "Slider one", "30", ControlColor::Alizarin, &slider, tab1 );
ESPUI.addControl( ControlType::Slider, "Slider two", "100", ControlColor::Alizarin, &slider, tab3 ); // tabbed controls
ESPUI.addControl( ControlType::Text, "Text Test:", "a Text Field", ControlColor::Alizarin, &textCall, tab3 ); ESPUI.addControl( ControlType::Label, "Millis:", "0", ControlColor::Emerald, tab1 );
ESPUI.addControl( ControlType::Number, "Number:", "50", ControlColor::Alizarin, &numberCall, tab3 ); ESPUI.addControl( ControlType::Button, "Push Button", "Press", ControlColor::Peterriver, tab1, &buttonCallback );
ESPUI.addControl( ControlType::Button, "Other Button", "Press", ControlColor::Wetasphalt, tab1, &buttonExample );
ESPUI.addControl( ControlType::PadWithCenter, "Pad with center", "", ControlColor::Sunflower, tab2, &padExample );
ESPUI.addControl( ControlType::Pad, "Pad without center", "", ControlColor::Carrot, tab3, &padExample );
ESPUI.addControl( ControlType::Switcher, "Switch one", "", ControlColor::Alizarin, tab3, &switchExample );
ESPUI.addControl( ControlType::Switcher, "Switch two", "", ControlColor::None, tab3, &otherSwitchExample );
ESPUI.addControl( ControlType::Slider, "Slider one", "30", ControlColor::Alizarin, tab1, &slider );
ESPUI.addControl( ControlType::Slider, "Slider two", "100", ControlColor::Alizarin, tab3, &slider );
ESPUI.addControl( ControlType::Number, "Number:", "50", ControlColor::Alizarin, tab3, &numberCall );
/* /*
* .begin loads and serves all files from PROGMEM directly. * .begin loads and serves all files from PROGMEM directly.

View File

@ -299,60 +299,82 @@ void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
for ( size_t i = 0; i < len; i++ ) { for ( size_t i = 0; i < len; i++ ) {
msg += ( char )data[i]; msg += ( char )data[i];
} }
uint16_t id = msg.substring( msg.lastIndexOf( ':' ) + 1 ).toInt();
Control* c = ESPUI.getControl( msg.substring( msg.lastIndexOf( ':' ) + 1 ).toInt() ); if ( ESPUI.verbosity >= Verbosity::VerboseJSON ) {
Serial.print("WS rec: ");
Serial.println( msg );
Serial.print( "WS recognised ID: " );
Serial.println( id );
}
if ( c != nullptr && c->callback != nullptr ) { Control* c = ESPUI.getControl( id );
if ( msg.startsWith( "bdown:" ) ) {
c->callback( *c, B_DOWN ); if ( c == nullptr ) {
} else if ( msg.startsWith( "bup:" ) ) {
c->callback( *c, B_UP );
} else if ( msg.startsWith( "pfdown:" ) ) {
c->callback( *c, P_FOR_DOWN );
} else if ( msg.startsWith( "pfup:" ) ) {
c->callback( *c, P_FOR_UP );
} else if ( msg.startsWith( "pldown:" ) ) {
c->callback( *c, P_LEFT_DOWN );
} else if ( msg.startsWith( "plup:" ) ) {
c->callback( *c, P_LEFT_UP );
} else if ( msg.startsWith( "prdown:" ) ) {
c->callback( *c, P_RIGHT_DOWN );
} else if ( msg.startsWith( "prup:" ) ) {
c->callback( *c, P_RIGHT_UP );
} else if ( msg.startsWith( "pbdown:" ) ) {
c->callback( *c, P_BACK_DOWN );
} else if ( msg.startsWith( "pbup:" ) ) {
c->callback( *c, P_BACK_UP );
} else if ( msg.startsWith( "pcdown:" ) ) {
c->callback( *c, P_CENTER_DOWN );
} else if ( msg.startsWith( "pcup:" ) ) {
c->callback( *c, P_CENTER_UP );
} else if ( msg.startsWith( "sactive:" ) ) {
ESPUI.updateSwitcher( c->id, true );
c->callback( *c, S_ACTIVE );
} else if ( msg.startsWith( "sinactive:" ) ) {
ESPUI.updateSwitcher( c->id, false );
c->callback( *c, S_INACTIVE );
} else if ( msg.startsWith( "slvalue:" ) ) {
int value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) ).toInt();
ESPUI.updateSlider( c->id, value, client->id() );
c->callback( *c, SL_VALUE );
} else if ( msg.startsWith( "nvalue:" ) ) {
int value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) ).toInt();
ESPUI.updateNumber( c->id, value, client->id() );
c->callback( *c, N_VALUE );
} else if ( msg.startsWith( "tvalue:" ) ) {
String value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) );
ESPUI.updateText( c->id, value, client->id() );
c->callback( *c, T_VALUE );
}
} else {
if ( ESPUI.verbosity ) { if ( ESPUI.verbosity ) {
Serial.println( "Maleformated id in websocket message" ); Serial.print( "No control found for ID " );
Serial.println( id );
} }
return; return;
} }
if ( c->callback == nullptr ) {
if ( ESPUI.verbosity ) {
Serial.print( "No callback found for ID " );
Serial.println( id );
}
return;
}
if ( msg.startsWith( "bdown:" ) ) {
c->callback( c, B_DOWN );
} else if ( msg.startsWith( "bup:" ) ) {
c->callback( c, B_UP );
} else if ( msg.startsWith( "pfdown:" ) ) {
c->callback( c, P_FOR_DOWN );
} else if ( msg.startsWith( "pfup:" ) ) {
c->callback( c, P_FOR_UP );
} else if ( msg.startsWith( "pldown:" ) ) {
c->callback( c, P_LEFT_DOWN );
} else if ( msg.startsWith( "plup:" ) ) {
c->callback( c, P_LEFT_UP );
} else if ( msg.startsWith( "prdown:" ) ) {
c->callback( c, P_RIGHT_DOWN );
} else if ( msg.startsWith( "prup:" ) ) {
c->callback( c, P_RIGHT_UP );
} else if ( msg.startsWith( "pbdown:" ) ) {
c->callback( c, P_BACK_DOWN );
} else if ( msg.startsWith( "pbup:" ) ) {
c->callback( c, P_BACK_UP );
} else if ( msg.startsWith( "pcdown:" ) ) {
c->callback( c, P_CENTER_DOWN );
} else if ( msg.startsWith( "pcup:" ) ) {
c->callback( c, P_CENTER_UP );
} else if ( msg.startsWith( "sactive:" ) ) {
ESPUI.updateSwitcher( c->id, true );
c->callback( c, S_ACTIVE );
} else if ( msg.startsWith( "sinactive:" ) ) {
ESPUI.updateSwitcher( c->id, false );
c->callback( c, S_INACTIVE );
} else if ( msg.startsWith( "slvalue:" ) ) {
int value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) ).toInt();
ESPUI.updateSlider( c->id, value, client->id() );
c->callback( c, SL_VALUE );
} else if ( msg.startsWith( "nvalue:" ) ) {
int value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) ).toInt();
ESPUI.updateNumber( c->id, value, client->id() );
c->callback( c, N_VALUE );
} else if ( msg.startsWith( "tvalue:" ) ) {
String value = msg.substring( msg.indexOf( ':' ) + 1, msg.lastIndexOf( ':' ) );
ESPUI.updateText( c->id, value, client->id() );
c->callback( c, T_VALUE );
} else {
if ( ESPUI.verbosity ) {
Serial.println( "Malformated message from the websocket" );
}
}
} }
break; break;
@ -363,8 +385,8 @@ void onWsEvent( AsyncWebSocket* server, AsyncWebSocketClient* client,
uint16_t ESPUIClass::addControl( ControlType type, const char* label, uint16_t ESPUIClass::addControl( ControlType type, const char* label,
String value, ControlColor color, String value, ControlColor color,
void ( *callback )( Control, int ), uint16_t parentControl,
uint16_t parentControl void ( *callback )( Control*, int )
) { ) {
if ( this->getControl( label ) != nullptr ) { if ( this->getControl( label ) != nullptr ) {
if ( this->verbosity ) { if ( this->verbosity ) {
@ -401,39 +423,39 @@ int ESPUIClass::graph( const char* label, ControlColor color ) {
} }
// TODO: this still needs a range setting // TODO: this still needs a range setting
int ESPUIClass::slider( const char* label, void ( *callback )( Control, int ), int ESPUIClass::slider( const char* label, void ( *callback )( Control*, int ),
ControlColor color, String value ) { ControlColor color, String value ) {
return addControl( ControlType::Button, label, "", color, callback ); return addControl( ControlType::Button, label, "", color, Control::noParent, callback );
} }
int ESPUIClass::button( const char* label, void ( *callback )( Control, int ), int ESPUIClass::button( const char* label, void ( *callback )( Control*, int ),
ControlColor color, String value ) { ControlColor color, String value ) {
return addControl( ControlType::Button, label, value, color, callback ); return addControl( ControlType::Button, label, value, color, Control::noParent, callback );
} }
int ESPUIClass::switcher( const char* label, bool startState, int ESPUIClass::switcher( const char* label, bool startState,
void ( *callback )( Control, int ), ControlColor color ) { void ( *callback )( Control*, int ), ControlColor color ) {
return addControl( ControlType::Switcher, label, "", color, callback ); return addControl( ControlType::Switcher, label, "", color, Control::noParent, callback );
} }
int ESPUIClass::pad( const char* label, bool center, int ESPUIClass::pad( const char* label, bool center,
void ( *callback )( Control, int ), ControlColor color ) { void ( *callback )( Control*, int ), ControlColor color ) {
if ( center ) { if ( center ) {
return addControl( ControlType::PadWithCenter, label, "", color, callback ); return addControl( ControlType::PadWithCenter, label, "", color, Control::noParent, callback );
} else { } else {
return addControl( ControlType::Pad, label, "", color, callback ); return addControl( ControlType::Pad, label, "", color, Control::noParent, callback );
} }
} }
// TODO: min and max need to be saved, they also need to be sent to the frontend // TODO: min and max need to be saved, they also need to be sent to the frontend
int ESPUIClass::number( const char* label, void ( *callback )( Control, int ), int ESPUIClass::number( const char* label, void ( *callback )( Control*, int ),
ControlColor color, int number, int min, int max ) { ControlColor color, int number, int min, int max ) {
return addControl( ControlType::Number, label, String( number ), color, callback ); return addControl( ControlType::Number, label, String( number ), color, Control::noParent, callback );
} }
int ESPUIClass::text( const char* label, void ( *callback )( Control, int ), int ESPUIClass::text( const char* label, void ( *callback )( Control*, int ),
ControlColor color, String value ) { ControlColor color, String value ) {
return addControl( ControlType::Text, label, value, color, callback ); return addControl( ControlType::Text, label, value, color, Control::noParent, callback );
} }
@ -556,11 +578,11 @@ void ESPUIClass::updateSlider( String label, int nValue, int clientId ) {
} }
void ESPUIClass::updateSwitcher( uint16_t id, bool nValue, int clientId ) { void ESPUIClass::updateSwitcher( uint16_t id, bool nValue, int clientId ) {
updateControl( id, String( int( nValue ? 1 : 0 ) ), clientId ); updateControl( id, String( nValue ? "1" : "0" ), clientId );
} }
void ESPUIClass::updateSwitcher( String label, bool nValue, int clientId ) { void ESPUIClass::updateSwitcher( String label, bool nValue, int clientId ) {
updateControl( label, String( int( nValue ? 1 : 0 ) ), clientId ); updateControl( label, String( nValue ? "1" : "0" ), clientId );
} }
void ESPUIClass::updateNumber( uint16_t id, int number, int clientId ) { void ESPUIClass::updateNumber( uint16_t id, int number, int clientId ) {
@ -610,7 +632,7 @@ void ESPUIClass::jsonDom( AsyncWebSocketClient* client ) {
item["value"] = String( control->value ); item["value"] = String( control->value );
item["color"] = ( int )control->color; item["color"] = ( int )control->color;
if ( control->parentControl != 0xffff ) { if ( control->parentControl != Control::noParent ) {
item["parentControl"] = String( control->parentControl ); item["parentControl"] = String( control->parentControl );
} }

View File

@ -110,17 +110,18 @@ class Control {
ControlType type; ControlType type;
uint16_t id; // just mirroring the id here for practical reasons uint16_t id; // just mirroring the id here for practical reasons
const char* label; const char* label;
void ( *callback )( Control, int ); void ( *callback )( Control*, int );
String value; String value;
ControlColor color; ControlColor color;
uint16_t parentControl; uint16_t parentControl;
Control* next; Control* next;
static constexpr uint16_t noParent = 0xffff;
Control( Control(
ControlType type, const char* label, ControlType type, const char* label,
void ( *callback )( Control, int ), void ( *callback )( Control*, int ),
String value, ControlColor color, uint16_t parentControl = 0xffff ) String value, ControlColor color, uint16_t parentControl = Control::noParent )
: type( type ), label( label ), callback( callback ), value( value ), color( color ), parentControl(parentControl), next( nullptr ) { : type( type ), label( label ), callback( callback ), value( value ), color( color ), parentControl(parentControl), next( nullptr ) {
id = idCounter++; id = idCounter++;
} }
@ -181,25 +182,25 @@ class ESPUIClass {
uint16_t addControl( ControlType type, const char* label, uint16_t addControl( ControlType type, const char* label,
String value = String( "" ), ControlColor color = ControlColor::Turquoise, String value = String( "" ), ControlColor color = ControlColor::Turquoise,
void ( *callback )( Control, int ) = nullptr, uint16_t parentControl = 0xffff ); uint16_t parentControl = Control::noParent, void ( *callback )( Control*, int ) = nullptr );
int button( const char* label, int button( const char* label,
void ( *callback )( Control, int ), ControlColor color, void ( *callback )( Control*, int ), ControlColor color,
String value = "" ); // Create Event Button String value = "" ); // Create Event Button
int switcher( const char* label, bool startState, int switcher( const char* label, bool startState,
void ( *callback )( Control, int ), void ( *callback )( Control*, int ),
ControlColor color ); // Create Toggle Button ControlColor color ); // Create Toggle Button
int pad( const char* label, bool centerButton, int pad( const char* label, bool centerButton,
void ( *callback )( Control, int ), void ( *callback )( Control*, int ),
ControlColor color ); // Create Pad Control ControlColor color ); // Create Pad Control
int slider( const char* label, int slider( const char* label,
void ( *callback )( Control, int ), void ( *callback )( Control*, int ),
ControlColor color, String value ); // Create Slider Control ControlColor color, String value ); // Create Slider Control
int number( const char* label, int number( const char* label,
void ( *callback )( Control, int ), void ( *callback )( Control*, int ),
ControlColor color, int number, int min, int max ); // Create a Number Input Control ControlColor color, int number, int min, int max ); // Create a Number Input Control
int text( const char* label, int text( const char* label,
void ( *callback )( Control, int ), void ( *callback )( Control*, int ),
ControlColor color, String value = "" ); // Create a Text Input Control ControlColor color, String value = "" ); // Create a Text Input Control
// Output only // Output only