Advertisement
Guest User

Untitled

a guest
Dec 28th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal.h>
  3.  
  4. #define BTN_BACK  45
  5. #define BTN_NEXT  47
  6. #define BTN_PREV  46
  7. #define BTN_OK    44
  8. #define BTN_MENU  43
  9. #define LED       31
  10.  
  11. typedef struct {
  12.   String label;
  13.   int minVal;
  14.   int maxVal;
  15.   int currentVal;
  16.   void (*handler)();
  17. } STRUCT_MENUPOS;
  18.  
  19. typedef enum {
  20.   BACK, NEXT, PREV, SET, NONE
  21. } ENUM_BUTTON;
  22.  
  23. LiquidCrystal lcd(48,49,50,51,52,53);      
  24. STRUCT_MENUPOS menu[5];
  25.  
  26. int currentMenuPos = 0;
  27. int menuSize;
  28. bool isInLowerLevel = false;
  29. int tempVal;
  30.  
  31.  
  32. int BTN_STATE;
  33. int LAST_BTN_STATE = LOW;
  34. int MENU_STATE = LOW;          
  35. long lastDebTime = 0;
  36. long debTime = 50;  
  37.  
  38.  
  39. void setup() {
  40.   Serial.begin(9600);
  41.   lcd.begin(16, 2);
  42.  
  43.   pinMode(BTN_NEXT, INPUT_PULLUP);
  44.   pinMode(BTN_PREV, INPUT_PULLUP);
  45.   pinMode(BTN_BACK, INPUT_PULLUP);
  46.   pinMode(BTN_OK, INPUT_PULLUP);
  47.   pinMode(BTN_MENU, INPUT_PULLUP);
  48.    pinMode(LED, OUTPUT);
  49.  
  50.   digitalWrite(LED, MENU_STATE);
  51.  
  52.   menu[0] = {"Cyfry", 0, 9, 5, NULL};
  53.   menu[1] = {"Liczby", 10, 1000, 15, NULL};
  54.   menu[2] = {"Napisy", 0, 2, 0, formatNapisy};
  55.   menu[3] = {"Ulamki", 0, 30, 15, formatUlamki};
  56.   menu[4] = {"Port szer.", 0, 0, 0, actionPortSzeregowy};
  57.  
  58.   menuSize = sizeof(menu)/sizeof(STRUCT_MENUPOS);
  59.  
  60. }
  61.  
  62. void loop() {
  63.    lcdDisplay();
  64. }
  65.  
  66. void lcdDisplay() {
  67.  
  68.   int MENU = digitalRead(BTN_MENU);
  69.   boolean forceRefresh = false;
  70.  
  71.   if (MENU != LAST_BTN_STATE) {lastDebTime = millis();}
  72.   if ((millis() - lastDebTime) > debTime) {
  73.     if (MENU != BTN_STATE) {
  74.       BTN_STATE = MENU;
  75.       if (BTN_STATE == HIGH) {
  76.         MENU_STATE = !MENU_STATE;
  77.         forceRefresh = true;
  78.       }
  79.     }
  80.   }
  81.  
  82.   if(MENU_STATE == LOW){drawMenu(forceRefresh); digitalWrite(LED, HIGH);}
  83.   if(MENU_STATE == HIGH){noMenu();digitalWrite(LED, LOW);}
  84.  
  85.   LAST_BTN_STATE = MENU;
  86.  
  87. }
  88.  
  89. ENUM_BUTTON getButton() {
  90.   if(!digitalRead(BTN_BACK)) return BACK;
  91.   if(!digitalRead(BTN_NEXT)) return NEXT;
  92.   if(!digitalRead(BTN_PREV)) return PREV;
  93.   if(!digitalRead(BTN_OK)) return SET;
  94.  
  95.   return NONE;
  96. }
  97.  
  98. void noMenu() {
  99.   static unsigned long lastRead = 0;
  100.    int autoSwitchTime = 500;
  101.    if(millis() - lastRead < autoSwitchTime) return;
  102.    lcd.clear();
  103.    lcd.print("Brak menu");
  104.    lastRead = millis();
  105. }
  106.  
  107. void drawMenu() {
  108.   drawMenu(false);
  109. }
  110.  
  111. void drawMenu(boolean forceRefresh) {
  112.   static unsigned long lastRead = 0;
  113.   static ENUM_BUTTON lastPressedButton = SET;
  114.   static unsigned int isPressedSince = 0;
  115.   int autoSwitchTime = 500;
  116.  
  117.    ENUM_BUTTON pressedButton = getButton();
  118.  
  119.   if(pressedButton == NONE && lastRead != 0) {
  120.     isPressedSince = 0;
  121.     if(!forceRefresh) {
  122.       return;
  123.     }
  124.   }
  125.   if(pressedButton != lastPressedButton) {
  126.     isPressedSince = 0;
  127.   }
  128.  
  129.   if(isPressedSince > 3) autoSwitchTime = 70;
  130.   if(lastRead != 0 && millis() - lastRead < autoSwitchTime && pressedButton == lastPressedButton) {
  131.     if(!forceRefresh) {
  132.       return;
  133.     }
  134.   }
  135.  
  136.   isPressedSince++;
  137.   lastRead = millis();
  138.   lastPressedButton = pressedButton;
  139.  
  140.   switch(pressedButton) {
  141.     case NEXT: handleNext(); break;
  142.     case PREV: handlePrev(); break;
  143.     case BACK: handleBack(); break;
  144.     case SET: handleOk(); break;
  145.  }
  146.  
  147.   lcd.home();
  148.   lcd.clear();
  149.   if(isInLowerLevel) {
  150.     lcd.print(menu[currentMenuPos].label);
  151.     lcd.setCursor(0, 1);
  152.     lcd.print(F("> "));
  153.  
  154.     if(menu[currentMenuPos].handler != NULL) {
  155.       (*(menu[currentMenuPos].handler))();
  156.     } else {
  157.       lcd.print(tempVal);
  158.     }
  159.   } else {
  160.     lcd.print(F("Menu glowne"));
  161.     lcd.setCursor(0, 1);
  162.     lcd.print(F("> "));
  163.  
  164.     lcd.print(menu[currentMenuPos].label);
  165.   }
  166. }
  167.  
  168.  
  169.  
  170. void handleNext() {
  171.   if(isInLowerLevel) {
  172.     tempVal++;
  173.     if(tempVal > menu[currentMenuPos].maxVal) tempVal = menu[currentMenuPos].maxVal;
  174.   } else {
  175.     currentMenuPos = (currentMenuPos + 1) % menuSize;
  176.   }
  177. }
  178.  
  179. void handlePrev() {
  180.   if(isInLowerLevel) {
  181.     tempVal--;
  182.     if(tempVal < menu[currentMenuPos].minVal) tempVal = menu[currentMenuPos].minVal;
  183.   } else {
  184.     currentMenuPos--;
  185.     if(currentMenuPos < 0) currentMenuPos = menuSize - 1;
  186.   }
  187. }
  188.  
  189. void handleBack() {
  190.   if(isInLowerLevel) {
  191.     isInLowerLevel = false;
  192.   }
  193. }
  194.  
  195. void handleOk() {
  196.   if(menu[currentMenuPos].handler != NULL && menu[currentMenuPos].maxVal <= menu[currentMenuPos].minVal) {
  197.     (*(menu[currentMenuPos].handler))();
  198.     return;
  199.   }
  200.   if(isInLowerLevel) {
  201.     menu[currentMenuPos].currentVal = tempVal;
  202.     isInLowerLevel = false;
  203.   } else {
  204.     tempVal = menu[currentMenuPos].currentVal;
  205.     isInLowerLevel = true;
  206.   }
  207. }
  208.  
  209. /* Funkcje-uchwyty użytkownika */
  210. void actionPortSzeregowy() {
  211.   Serial.println("Wywolano akcje: Port szeregowy");
  212. }
  213.  
  214. void formatNapisy() {
  215.   String dictonary[] = {"Napis 1", "Napis 2", "Napis 3 :)"};
  216.   lcd.print(dictonary[tempVal]);
  217. }
  218.  
  219. void formatUlamki() {
  220.   lcd.print(tempVal / 10.0);
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement