/*
MUIInput2BtnBounce2.ino
MUI: https://github.com/olikraus/u8g2/wiki/muimanual
U8g2 Menu (MUI) with Bounce2 Library (https://github.com/thomasfredericks/Bounce2).
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
Copyright (c) 2022, olikraus@gmail.com
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 <Bounce2.h>
// To which pins are the buttons connected?
#define SELECT_PIN 2
#define JOYSTICK_X_PIN A0
#define JOYSTICK_Y_PIN A1
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
// End of constructor list
// U8g2 User Interface
MUIU8G2 mui;
// Define button objects
Bounce2::Button selectBtn = Bounce2::Button();
/*
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),
/* this example will use three buttons, so use "mse" functions here */
MUIF_U8G2_U8_MIN_MAX("NV", &num_value, 0, 99, mui_u8g2_u8_min_max_wm_mse_pi),
MUIF_U8G2_U8_MIN_MAX_STEP("NB", &bar_value, 0, 16, 1, MUI_MMS_2X_BAR, mui_u8g2_u8_bar_wm_mse_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_mse_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, "H00PS 1964 T1 ECU")
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;
void setup(void) {
selectBtn.attach(SELECT_PIN, INPUT_PULLUP);
selectBtn.interval(5); // debounce interval in milliseconds
selectBtn.setPressedState(LOW); // Indicate which state corresponds to a button press
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 check_events(void) {
selectBtn.update();
}
void handle_events(void) {
// 0 = not pushed, 1 = pushed
if (selectBtn.pressed()) {
mui.sendSelect();
is_redraw = 1;
}
else {
int xValue = analogRead(JOYSTICK_X_PIN);
int yValue = analogRead(JOYSTICK_Y_PIN);
if (xValue < 300) { // Left
mui.prevField();
is_redraw = 1;
}
else if (xValue > 700) { // Right
mui.nextField();
is_redraw = 1;
}
if (yValue < 300) { // Up
mui.prevField();
is_redraw = 1;
}
else if (yValue > 700) { // Down
mui.nextField();
is_redraw = 1;
}
}
}
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 {
check_events(); // check for button press with bounce2 library
mui.draw();
check_events(); // check for button press with bounce2 library
} while (u8g2.nextPage());
is_redraw = 0; /* clear the redraw flag */
}
check_events(); // check for button press with bounce2 library
handle_events(); // process events from bounce2 library
}
else {
/* the menu should never become inactive, but if so, then restart the menu system */
mui.gotoForm(/* form_id= */ 1, /* initial_cursor_position= */ 0);
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
led1:A
led1:C
r1:1
r1:2
joystick1:VCC
joystick1:VERT
joystick1:HORZ
joystick1:SEL
joystick1:GND