/*
MUIInputVersatileRotaryEncoder.ino
MUI: https://github.com/olikraus/u8g2/wiki/muimanual
U8g2 Menu with Rotary Encoder (Versatile_RotaryEncoder library).
Short Button Press: Switch between field movement and data increment/decrement
Long Button Press: Return to main menu
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2022, [email protected]
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Arduino.h>
#include <U8g2lib.h>
#include <MUIU8g2.h>
#include <Versatile_RotaryEncoder.h>
// Set here your encoder reading pins
#define clk 2
#define dt 3
#define sw 4
// Create encoder object
Versatile_RotaryEncoder versatile_encoder(clk, dt, sw);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
// End of constructor list
MUIU8G2 mui;
/*
global variables which form the communication gateway between the user interface and the rest of the code
*/
uint8_t num_value = 0;
uint8_t bar_value = 0;
uint16_t animal_idx = 0;
/*
list of animal names
*/
const char *animals[] = { "Bird", "Bison", "Cat", "Crow", "Dog", "Elephant", "Fish", "Gnu", "Horse", "Koala", "Lion", "Mouse", "Owl", "Rabbit", "Spider", "Turtle", "Zebra" };
uint16_t animal_name_list_get_cnt(void *data) {
return sizeof(animals)/sizeof(*animals); /* number of animals */
}
const char *animal_name_list_get_str(void *data, uint16_t index) {
return animals[index];
}
uint8_t mui_hrule(mui_t *ui, uint8_t msg) {
if ( msg == MUIF_MSG_DRAW ) {
u8g2.drawHLine(0, mui_get_y(ui), u8g2.getDisplayWidth());
}
return 0;
}
uint8_t show_my_data(mui_t *ui, uint8_t msg) {
if ( msg == MUIF_MSG_DRAW ) {
u8g2_uint_t x = mui_get_x(ui);
u8g2_uint_t y = mui_get_y(ui);
u8g2.setCursor(x+5, y);
u8g2.print("Num:");
u8g2.setCursor(x+50, y);
u8g2.print(num_value);
u8g2.setCursor(x+5, y+12);
u8g2.print("Bar:");
u8g2.setCursor(x+50, y+12);
u8g2.print(bar_value);
u8g2.setCursor(x+5, y+24);
u8g2.print("Animal:");
u8g2.setCursor(x+50, y+24);
u8g2.print(animal_idx);
u8g2.print("=");
u8g2.print(animals[animal_idx]);
}
return 0;
}
muif_t muif_list[] = {
MUIF_U8G2_FONT_STYLE(0, u8g2_font_helvR08_tr), /* regular font */
MUIF_U8G2_FONT_STYLE(1, u8g2_font_helvB08_tr), /* bold font */
MUIF_RO("HR", mui_hrule),
MUIF_U8G2_LABEL(),
MUIF_RO("GP",mui_u8g2_goto_data),
MUIF_BUTTON("GC", mui_u8g2_goto_form_w1_pi),
MUIF_U8G2_U8_MIN_MAX("NV", &num_value, 0, 99, mui_u8g2_u8_min_max_wm_mud_pi),
MUIF_U8G2_U8_MIN_MAX_STEP("NB", &bar_value, 0, 16, 1, MUI_MMS_2X_BAR, mui_u8g2_u8_bar_wm_mud_pf),
MUIF_U8G2_U16_LIST("NA", &animal_idx, NULL, animal_name_list_get_str, animal_name_list_get_cnt, mui_u8g2_u16_list_line_wa_mud_pi),
/* register custom function to show the data */
MUIF_RO("SH", show_my_data),
/* a button for the menu... */
//MUIF_BUTTON("GO", mui_u8g2_btn_goto_wm_fi)
MUIF_EXECUTE_ON_SELECT_BUTTON("GO", mui_u8g2_btn_goto_wm_fi)
};
fds_t fds_data[] =
MUI_FORM(1)
MUI_STYLE(1)
MUI_LABEL(5, 8, "VersatileRotaryEncoder")
MUI_STYLE(0)
MUI_XY("HR", 0,11)
MUI_DATA("GP",
MUI_10 "Enter Data|"
MUI_12 "Show Data")
MUI_XYA("GC", 5, 24, 0)
MUI_XYA("GC", 5, 36, 1)
MUI_FORM(10)
MUI_STYLE(1)
MUI_LABEL(5, 8, "Enter Data")
MUI_XY("HR", 0,11)
MUI_STYLE(0)
MUI_LABEL(5,23, "Num:")
MUI_LABEL(5,36, "Bar:")
MUI_LABEL(5,49, "Animal:")
MUI_XY("NV", 50, 23)
MUI_XY("NB", 50, 36)
MUI_XYA("NA", 50, 49, 44)
MUI_XYAT("GO", 114, 60, 1, " Ok ")
MUI_FORM(12)
MUI_STYLE(1)
MUI_LABEL(5, 8, "Show Data")
MUI_XY("HR", 0,11)
MUI_STYLE(0)
MUI_XY("SH", 0, 23)
MUI_XYAT("GO", 114, 60, 1, " Ok ")
;
// global variables for menu redraw and input event handling
uint8_t is_redraw = 1;
uint8_t rotate_event = 0; // 0 = not turning, 1 = CW, 2 = CCW
uint8_t press_event = 0; // 0 = not pushed, 1 = pushed
uint8_t long_press_event = 0; // 0 = not pushed, 1 = pushed
// Functions prototyping to be handled on each Encoder Event
void handleRotate(int8_t rotation) {
if ( rotation > 0 )
rotate_event = 2; // CW
else
rotate_event = 1; // CCW
}
void handlePressRelease() {
press_event = 1;
}
void handleLongPressRelease() {
long_press_event = 1;
}
void setup(void) {
// Load to the encoder all nedded handle functions here (up to 9 functions)
versatile_encoder.setHandleRotate(handleRotate);
versatile_encoder.setHandlePressRelease(handlePressRelease);
versatile_encoder.setHandleLongPressRelease(handleLongPressRelease);
u8g2.begin();
mui.begin(u8g2, fds_data, muif_list, sizeof(muif_list)/sizeof(muif_t));
mui.gotoForm(/* form_id= */ 1, /* initial_cursor_position= */ 0);
}
void handle_events(void) {
// 0 = not pushed, 1 = pushed
if ( press_event == 1 ) {
mui.sendSelect();
is_redraw = 1;
press_event = 0;
}
// 0 = not pushed, 1 = pushed
if ( long_press_event == 1 ) {
mui.sendSelectWithExecuteOnSelectFieldSearch();
is_redraw = 1;
long_press_event = 0;
}
// 0 = not turning, 1 = CW, 2 = CCW
if ( rotate_event == 1 ) {
mui.nextField();
is_redraw = 1;
rotate_event = 0;
}
if ( rotate_event == 2 ) {
mui.prevField();
is_redraw = 1;
rotate_event = 0;
}
}
void loop(void) {
/* check whether the menu is active */
if ( mui.isFormActive() ) {
/* update the display content, if the redraw flag is set */
if ( is_redraw ) {
u8g2.firstPage();
do {
versatile_encoder.ReadEncoder(); // Do the encoder reading and processing
mui.draw();
versatile_encoder.ReadEncoder(); // Do the encoder reading and processing
} while( u8g2.nextPage() );
is_redraw = 0; /* clear the redraw flag */
}
versatile_encoder.ReadEncoder(); // Do the encoder reading and processing
handle_events();
} else {
/* the menu should never become inactive, but if so, then restart the menu system */
mui.gotoForm(/* form_id= */ 1, /* initial_cursor_position= */ 0);
}
}