#include <Arduino.h>
#include <U8g2lib.h>
#include <MUIU8g2.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
MUIU8G2 mui;
/*
global variables which form the communication gateway between the user interface and the rest of the code
*/
uint8_t hours = 2; // variable where the user can input a number between 0 and 9
uint8_t minutes = 5;
uint8_t exit_code = 0; // return value from the menu system
char Svetlo_state[] = "On";
/*
MUIF table: Each entry defines a field, which can be used in FDS to describe a form.
*/
muif_t muif_list[] = {
/* normal text style */
MUIF_U8G2_FONT_STYLE(0, u8g2_font_helvR08_tr),
/* Leave the menu system */
MUIF_VARIABLE("LV",&exit_code,mui_u8g2_btn_exit_wm_fi),
MUIF_VARIABLE("ID",&Svetlo_state,mui_u8g2_u8_opt_line_wa_mse_pi),
/* input for a number between 0 to 9 */
MUIF_U8G2_U8_MIN_MAX("IN", &hours, 0, 24, mui_u8g2_u8_min_max_wm_mse_pf),
MUIF_U8G2_U8_MIN_MAX("MN", &minutes, 0, 59, mui_u8g2_u8_min_max_wm_mse_pf),
/* MUI_LABEL is used to place fixed text on the screeen */
MUIF_LABEL(mui_u8g2_draw_text)
};
/*
The form definition string (fds) defines all forms and the fields on those forms.
A new form always starts with MUI_FORM(u). The form ends with the next MUI_FORM() or the end of the fds.
Inside the form use fields or the style command (MUI_STYLE)
The fields are placed on the form with
MUI_XY(id, x, y) Field 'id' without extra argument or text placed at the specified xy position
MUI_XYT(id, x, y, text) Field 'id' with the specified test at position xy
MUI_XYA(id, x, y, a) Field 'id' with argument 'a' at position xy
MUI_XYAT(id, x, y, a, text) Field 'id' with argument and text placed at position xy
MUI_LABEL(x, y, text) Place "text" on the form. Can be used only if "MUIF_LABEL(mui_u8g2_draw_text)" is available in MUIF table.
*/
fds_t fds_data[] MUI_PROGMEM =
MUI_FORM(1)
MUI_STYLE(0)
MUI_LABEL(5, 10, "Svetlo:")
MUI_XYAT("ID", 60, 10, 10, "Banana|Apple|Melon|Cranberry")
MUI_STYLE(0)
MUI_LABEL(5,30, "Cas:")
MUI_XY("IN",60, 30)
MUI_XY("MN",80, 30)
MUI_XYT("LV",64, 59, " OK ")
;
uint8_t menu_select_pin = 4;
uint8_t menu_next_pin = 5;
uint8_t menu_prev_pin = 3;
uint8_t menu_up_pin = 2;
uint8_t menu_down_pin = 6;
void setup(void) {
u8g2.begin(menu_select_pin, menu_next_pin, menu_prev_pin, menu_up_pin, menu_down_pin, U8X8_PIN_NONE); // initialise display
mui.begin(u8g2, fds_data, muif_list, sizeof(muif_list)/sizeof(muif_t));
mui.gotoForm(/* form_id= */ 1, /* initial_cursor_position= */ 0);
Serial.begin(9600);
}
uint8_t is_redraw = 1;
long milliseconds = 0;
void loop(void) {
/* check whether the menu is active */
if ( mui.isFormActive() ) {
/* if so, then draw the menu */
if ( is_redraw ) {
u8g2.firstPage();
do {
mui.draw();
} while( u8g2.nextPage() );
is_redraw = 0;
}
/* handle events */
switch(u8g2.getMenuEvent()) {
case U8X8_MSG_GPIO_MENU_SELECT:
mui.sendSelect();
is_redraw = 1;
break;
case U8X8_MSG_GPIO_MENU_NEXT:
mui.nextField();
is_redraw = 1;
break;
case U8X8_MSG_GPIO_MENU_PREV:
mui.prevField();
is_redraw = 1;
break;
}
/* did the user deactivate the menu? If so, start the countdown */
if ( mui.isFormActive() )
milliseconds = ((long)hours) * 1000;
} else {
/* menu not active: show countdown */
if ( milliseconds <= 0 ) {
/* countdown reached 0: activate menu again */
/* The exit button has stored its location: restoreForm will */
/* activate MUI and will put focus on the ok button */
mui.restoreForm();
} else {
/* execute countdown */
u8g2.setFont(u8g2_font_helvR08_tr);
u8g2.firstPage();
do {
u8g2.setCursor(0,20);
u8g2.print(milliseconds);
} while( u8g2.nextPage() );
milliseconds -= 100;
delay(100);
}
}
} /* loop */