Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal.h>
- #define BTN_BACK 45
- #define BTN_NEXT 47
- #define BTN_PREV 46
- #define BTN_OK 44
- #define BTN_MENU 43
- #define LED 31
- typedef struct {
- String label;
- int minVal;
- int maxVal;
- int currentVal;
- void (*handler)();
- } STRUCT_MENUPOS;
- typedef enum {
- BACK, NEXT, PREV, SET, NONE
- } ENUM_BUTTON;
- LiquidCrystal lcd(48,49,50,51,52,53);
- STRUCT_MENUPOS menu[5];
- int currentMenuPos = 0;
- int menuSize;
- bool isInLowerLevel = false;
- int tempVal;
- int BTN_STATE;
- int LAST_BTN_STATE = LOW;
- int MENU_STATE = LOW;
- long lastDebTime = 0;
- long debTime = 50;
- void setup() {
- Serial.begin(9600);
- lcd.begin(16, 2);
- pinMode(BTN_NEXT, INPUT_PULLUP);
- pinMode(BTN_PREV, INPUT_PULLUP);
- pinMode(BTN_BACK, INPUT_PULLUP);
- pinMode(BTN_OK, INPUT_PULLUP);
- pinMode(BTN_MENU, INPUT_PULLUP);
- pinMode(LED, OUTPUT);
- digitalWrite(LED, MENU_STATE);
- menu[0] = {"Cyfry", 0, 9, 5, NULL};
- menu[1] = {"Liczby", 10, 1000, 15, NULL};
- menu[2] = {"Napisy", 0, 2, 0, formatNapisy};
- menu[3] = {"Ulamki", 0, 30, 15, formatUlamki};
- menu[4] = {"Port szer.", 0, 0, 0, actionPortSzeregowy};
- menuSize = sizeof(menu)/sizeof(STRUCT_MENUPOS);
- }
- void loop() {
- lcdDisplay();
- }
- void lcdDisplay() {
- int MENU = digitalRead(BTN_MENU);
- boolean forceRefresh = false;
- if (MENU != LAST_BTN_STATE) {lastDebTime = millis();}
- if ((millis() - lastDebTime) > debTime) {
- if (MENU != BTN_STATE) {
- BTN_STATE = MENU;
- if (BTN_STATE == HIGH) {
- MENU_STATE = !MENU_STATE;
- forceRefresh = true;
- }
- }
- }
- if(MENU_STATE == LOW){drawMenu(forceRefresh); digitalWrite(LED, HIGH);}
- if(MENU_STATE == HIGH){noMenu();digitalWrite(LED, LOW);}
- LAST_BTN_STATE = MENU;
- }
- ENUM_BUTTON getButton() {
- if(!digitalRead(BTN_BACK)) return BACK;
- if(!digitalRead(BTN_NEXT)) return NEXT;
- if(!digitalRead(BTN_PREV)) return PREV;
- if(!digitalRead(BTN_OK)) return SET;
- return NONE;
- }
- void noMenu() {
- static unsigned long lastRead = 0;
- int autoSwitchTime = 500;
- if(millis() - lastRead < autoSwitchTime) return;
- lcd.clear();
- lcd.print("Brak menu");
- lastRead = millis();
- }
- void drawMenu() {
- drawMenu(false);
- }
- void drawMenu(boolean forceRefresh) {
- static unsigned long lastRead = 0;
- static ENUM_BUTTON lastPressedButton = SET;
- static unsigned int isPressedSince = 0;
- int autoSwitchTime = 500;
- ENUM_BUTTON pressedButton = getButton();
- if(pressedButton == NONE && lastRead != 0) {
- isPressedSince = 0;
- if(!forceRefresh) {
- return;
- }
- }
- if(pressedButton != lastPressedButton) {
- isPressedSince = 0;
- }
- if(isPressedSince > 3) autoSwitchTime = 70;
- if(lastRead != 0 && millis() - lastRead < autoSwitchTime && pressedButton == lastPressedButton) {
- if(!forceRefresh) {
- return;
- }
- }
- isPressedSince++;
- lastRead = millis();
- lastPressedButton = pressedButton;
- switch(pressedButton) {
- case NEXT: handleNext(); break;
- case PREV: handlePrev(); break;
- case BACK: handleBack(); break;
- case SET: handleOk(); break;
- }
- lcd.home();
- lcd.clear();
- if(isInLowerLevel) {
- lcd.print(menu[currentMenuPos].label);
- lcd.setCursor(0, 1);
- lcd.print(F("> "));
- if(menu[currentMenuPos].handler != NULL) {
- (*(menu[currentMenuPos].handler))();
- } else {
- lcd.print(tempVal);
- }
- } else {
- lcd.print(F("Menu glowne"));
- lcd.setCursor(0, 1);
- lcd.print(F("> "));
- lcd.print(menu[currentMenuPos].label);
- }
- }
- void handleNext() {
- if(isInLowerLevel) {
- tempVal++;
- if(tempVal > menu[currentMenuPos].maxVal) tempVal = menu[currentMenuPos].maxVal;
- } else {
- currentMenuPos = (currentMenuPos + 1) % menuSize;
- }
- }
- void handlePrev() {
- if(isInLowerLevel) {
- tempVal--;
- if(tempVal < menu[currentMenuPos].minVal) tempVal = menu[currentMenuPos].minVal;
- } else {
- currentMenuPos--;
- if(currentMenuPos < 0) currentMenuPos = menuSize - 1;
- }
- }
- void handleBack() {
- if(isInLowerLevel) {
- isInLowerLevel = false;
- }
- }
- void handleOk() {
- if(menu[currentMenuPos].handler != NULL && menu[currentMenuPos].maxVal <= menu[currentMenuPos].minVal) {
- (*(menu[currentMenuPos].handler))();
- return;
- }
- if(isInLowerLevel) {
- menu[currentMenuPos].currentVal = tempVal;
- isInLowerLevel = false;
- } else {
- tempVal = menu[currentMenuPos].currentVal;
- isInLowerLevel = true;
- }
- }
- /* Funkcje-uchwyty użytkownika */
- void actionPortSzeregowy() {
- Serial.println("Wywolano akcje: Port szeregowy");
- }
- void formatNapisy() {
- String dictonary[] = {"Napis 1", "Napis 2", "Napis 3 :)"};
- lcd.print(dictonary[tempVal]);
- }
- void formatUlamki() {
- lcd.print(tempVal / 10.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement