/*
Basic Menu for 0.96 I2C OLED displays on Arduino.
*/
#include <Button.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSans9pt7b.h>
//#include <Fonts/FreeSans12pt7b.h>
// make sure the you choose the correct address for your display
#define OLED_ADDR 0x3C
#define OLED_RESET 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Button upBtn(12); // Pin 12
Button selectBtn(3); // Pin 3
Button downBtn(11); // Pin 11
//new manu
const int NUM_ITEMS = 8; // number of items in the list and also the number of screenshots and screenshots with QR codes (other screens)
const int MAX_ITEM_LENGTH = 20; // maximum characters for the item name
char menu_items [NUM_ITEMS] [MAX_ITEM_LENGTH] = { // array with item names
{ "3D Cube" },
{ "Battery" },
{ "Dashboard" },
{ "Fireworks" },
{ "GPS Speed" },
{ "Big Knob" },
{ "Park Sensor" },
{ "Turbo Gauge" }
};
byte menuSelected = 0;
#define MENU_HEIGHT 3 // number of lines in menu
byte shiftMenu=0;
void setup() {
// setup buttons
upBtn.begin();
selectBtn.begin();
downBtn.begin();
// setup display
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.display();
display.clearDisplay();
}
void loop() {
// updates display based on input from the buttons
getButtonInput();
showMenu();
//menuInteract();
display.clearDisplay();
}
void showMenu() {
// change text display here
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 0);
// menu title
display.println("Menu Test");
//---------------------------------
display.setTextSize(1);
// display menu items
for (int i=0; i<MENU_HEIGHT; i++)
{
display.setCursor(10, (2+i)*10);
if (i == menuSelected) {
display.print(">"); // Display ">" before the selected menu item
display.print(menu_items[i+shiftMenu]);
display.println("<"); // Display "<" at the end of the line
} else {
display.print(" ");
display.print(menu_items[i+shiftMenu]);
display.println(" ");
}
}
display.display();
}
void getButtonInput() {
// handles menu scrolling
if (downBtn.pressed()) {
if (menuSelected==0){
menuSelected++;
} else {
if (menuSelected>0 and shiftMenu<5){
shiftMenu++;
} else {
if (menuSelected<2){
menuSelected++;
}
}
}
}
if (upBtn.pressed()) {
if (menuSelected==2){
menuSelected--;
} else {
if (menuSelected<2 and shiftMenu>0){
shiftMenu--;
} else {
if (menuSelected>=1){
menuSelected--;
}
}
}
}
if (selectBtn.pressed()) {
Serial.println("select");
Serial.println(menu_items[menuSelected+shiftMenu] + String(menuSelected+shiftMenu));
if (menuSelected == 1)
// menu item 1 clicked
function_one();
else if (menuSelected == 2)
// menu item 2 clicked
function_two();
else if (menuSelected == 3)
// menu item 2 clicked
function_three();
else if (menuSelected == 4)
// menu item 3 clicked
function_four();
}
}
void function_one() {
// insert fuction code form menu item 1 here
Serial.println('Function 1 Ffffiring;');
}
void function_two() {
// insert fuction code form menu item 2 here
Serial.println('Function 2 Firing;');
}
void function_three() {
// insert fuction code form menu item 3 here
Serial.println('Function 3 Firing;');
}
void function_four() {
// insert fuction code form menu item 4 here
Serial.println('Function 4 Firing;');
}