diff --git a/aREST_UI.h b/aREST_UI.h index 6a23d48..5f658f4 100644 --- a/aREST_UI.h +++ b/aREST_UI.h @@ -28,6 +28,21 @@ aREST_UI() { } +typedef struct { + String function_name; + String description; +} function_button_t; + +typedef enum { + TYPE_BUTTON=0, + TYPE_FUNCTION_BUTTON, + TYPE_FUNCTION_WITH_INPUT_BUTTON, + TYPE_SLIDER, + TYPE_VARIABLE_LABEL, + TYPE_LABEL +} ui_type_t; + + // Get title void title(String the_title) { ui_title = the_title; @@ -43,6 +58,41 @@ void button(int pin){ buttons[buttons_index] = pin; buttons_index++; + // Set in the ui_order_list + ui_order_list[ui_order_index] = TYPE_BUTTON; + ui_order_index++; +} + +// Create function button +void function_button(char *function_name, char *description, int (*f)(String)){ + + // Setup function + aREST::function(function_name, f); + + // Set in function button array + function_buttons[function_buttons_index].function_name = String(function_name); + function_buttons[function_buttons_index].description = String(description); + function_buttons_index++; + + // Set in the ui_order_list + ui_order_list[ui_order_index] = TYPE_FUNCTION_BUTTON; + ui_order_index++; +} + +// Create function button +void function_with_input_button(char *function_name, char *description, int (*f)(String)){ + + // Setup function + aREST::function(function_name, f); + + // Set in function with input button array + func_with_input_buttons[func_with_input_buttons_index].function_name = String(function_name); + func_with_input_buttons[func_with_input_buttons_index].description = String(description); + func_with_input_buttons_index++; + + // Set in the ui_order_list + ui_order_list[ui_order_index] = TYPE_FUNCTION_WITH_INPUT_BUTTON; + ui_order_index++; } void slider(int pin) { @@ -54,14 +104,148 @@ void slider(int pin) { sliders[sliders_index] = pin; sliders_index++; + // Set in the ui_order_list + ui_order_list[ui_order_index] = TYPE_SLIDER; + ui_order_index++; +} + +void variable_label(char *variable_name, char *label_text, int *var){ + + // Initialize var + aREST::variable(variable_name, var); + variable_label_add(variable_name, label_text); +} + +// Float variables (Mega & ESP only, or without CC3000) +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H) +void variable_label(char *variable_name, char *label_text, float *var){ + + // Initialize variable + aREST::variable(variable_name, var); + variable_label_add(variable_name, label_text); +} +#endif + +// String variables (Mega & ESP only, or without CC3000) +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H) +void variable_label(char *variable_name, char *label_text, String *var){ + + // Initialize var + aREST::variable(variable_name, var); + variable_label_add(variable_name, label_text); +} +#endif + +// Create variable label +void variable_label_add(char *variable_name, char *label_text){ + + var_labels_names[var_labels_index] = variable_name; + var_labels_text[var_labels_index] = label_text; + var_labels_index++; + + // Set in the ui_order_list + ui_order_list[ui_order_index] = TYPE_VARIABLE_LABEL; + ui_order_index++; } // Create label -void label(char * label_name){ +void label(char *label_text, bool small_text=false){ + + labels_text[labels_index] = label_text; + labels_text_should_be_small[labels_index] = small_text; + labels_index++; + + // Set in the ui_order_list + ui_order_list[ui_order_index] = TYPE_LABEL; + ui_order_index++; +} + +void add_next_button() { + addToBuffer("
"); + addToBuffer("
"); + addToBuffer("
"); + addToBuffer("
"); + current_buttons_index++; +} - int_labels_names[int_labels_index] = label_name; - int_labels_index++; +void add_next_function_button() { + addToBuffer("
"); + addToBuffer("
"); + addToBuffer("
"); + current_function_buttons_index++; +} + +void add_next_function_with_input_button() { + addToBuffer("
"); + addToBuffer("
"); + addToBuffer("
"); + addToBuffer("
"); + current_func_with_input_buttons_index++; +} +void add_next_slider() { + addToBuffer("
"); +#if defined(ESP8266) + addToBuffer("
"); + addToBuffer("
"); + current_sliders_index++; +} + +void add_next_variable_label() { + addToBuffer("
"); + addToBuffer("
"); + addToBuffer(var_labels_text[current_var_labels_index]); + addToBuffer(":
"); + addToBuffer("
"); + addToBuffer("
"); + current_var_labels_index++; +} + +void add_next_label() { + if (labels_text_should_be_small[current_labels_index]) + { + addToBuffer("
"); + addToBuffer("
"); + } + else + { + addToBuffer("
"); + addToBuffer("
"); + } + addToBuffer(labels_text[current_labels_index]); + addToBuffer("
"); + addToBuffer("
"); + current_labels_index++; +} + +void reset_current_indices() { + current_buttons_index = 0; + current_function_buttons_index = 0; + current_func_with_input_buttons_index = 0; + current_sliders_index = 0; + current_var_labels_index = 0; + current_labels_index = 0; } // Handle connection @@ -75,11 +259,12 @@ virtual void root_answer() { addToBuffer("src=\"http://code.jquery.com/jquery-2.1.3.min.js\">"); addToBuffer(""); addToBuffer(""); - addToBuffer(""); + addToBuffer(""); addToBuffer(""); addToBuffer(""); addToBuffer("
"); + // Title if (ui_title.length() != 0) { addToBuffer("

"); @@ -90,41 +275,29 @@ virtual void root_answer() { addToBuffer("

Interface

"); } - // Buttons UI - for (int i = 0; i < buttons_index; i++) { - addToBuffer("
"); - addToBuffer("
"); - addToBuffer("
"); - addToBuffer("
"); - } - - // // Sliders UI - for (int i = 0; i < sliders_index; i++) { - addToBuffer("
"); - #if defined(ESP8266) - addToBuffer("
"); - addToBuffer("
"); - } - - // Labels UI - for (int j = 0; j < int_labels_index; j++) { - addToBuffer("
"); - addToBuffer("
"); - addToBuffer(int_labels_names[j]); - addToBuffer(":
"); - addToBuffer("
"); - addToBuffer("
"); + reset_current_indices(); + // Add all UI elements in order they were added + for (int i = 0; i < ui_order_index; i++) { + switch (ui_order_list[i]) { + case TYPE_BUTTON: + add_next_button(); + break; + case TYPE_FUNCTION_BUTTON: + add_next_function_button(); + break; + case TYPE_FUNCTION_WITH_INPUT_BUTTON: + add_next_function_with_input_button(); + break; + case TYPE_SLIDER: + add_next_slider(); + break; + case TYPE_VARIABLE_LABEL: + add_next_variable_label(); + break; + case TYPE_LABEL: + add_next_label(); + break; + } } addToBuffer("
"); @@ -145,6 +318,26 @@ virtual void root_answer() { addToBuffer("/0');});"); } + // Function Buttons JavaScript + for (int i = 0; i < function_buttons_index; i++) { + addToBuffer("$('#func_btn"); + addToBuffer(i); + addToBuffer("').click(function() {$.getq('queue','/"); + addToBuffer(function_buttons[i].function_name); + addToBuffer("');});"); + } + + // Function With Input Buttons JavaScript + for (int i = 0; i < func_with_input_buttons_index; i++) { + addToBuffer("$('#func_with_input_btn"); + addToBuffer(i); + addToBuffer("').click(function() {var text = document.getElementById(\"func_with_input_text"); + addToBuffer(i); + addToBuffer("\").value; $.getq('queue','/"); + addToBuffer(func_with_input_buttons[i].function_name); + addToBuffer("?params=' + text); });"); + } + // Sliders JavaScript for (int i = 0; i < sliders_index; i++) { addToBuffer("$('#slider"); @@ -156,14 +349,14 @@ virtual void root_answer() { addToBuffer("/' + val); });"); } - // Labels JavaScript - for (int j = 0; j < int_labels_index; j++) { + // Variable Labels JavaScript + for (int j = 0; j < var_labels_index; j++) { addToBuffer("$.getq('queue','/"); - addToBuffer(int_labels_names[j]); + addToBuffer(var_labels_names[j]); addToBuffer("', function(data) { $('#"); - addToBuffer(int_labels_names[j]); + addToBuffer(var_labels_names[j]); addToBuffer("').html(data."); - addToBuffer(int_labels_names[j]); + addToBuffer(var_labels_names[j]); addToBuffer("); });"); } @@ -181,16 +374,37 @@ virtual void root_answer() { // Buttons array int buttons[10]; int buttons_index; + int current_buttons_index; + + // Function Buttons array + function_button_t function_buttons[10]; + int function_buttons_index; + int current_function_buttons_index; + + // Function With Input Buttons array + function_button_t func_with_input_buttons[10]; + int func_with_input_buttons_index; + int current_func_with_input_buttons_index; // Buttons array int sliders[10]; int sliders_index; + int current_sliders_index; - // Indicators array - uint8_t int_labels_index; - int * int_labels_variables[10]; - char * int_labels_names[10]; - + // Variable Labels array + uint8_t var_labels_index; + uint8_t current_var_labels_index; + char * var_labels_names[10]; + char * var_labels_text[10]; + + // Labels array + uint8_t labels_index; + uint8_t current_labels_index; + char * labels_text[10]; + bool labels_text_should_be_small[10]; + + ui_type_t ui_order_list[30]; + int ui_order_index; }; -#endif \ No newline at end of file +#endif