Advertisement
Guest User

Untitled

a guest
Dec 28th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.57 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, OK, 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.  
  70.   if (MENU != LAST_BTN_STATE) {lastDebTime = millis();}
  71.   if ((millis() - lastDebTime) > debTime) {if (MENU != BTN_STATE) {BTN_STATE = MENU;
  72.      if (BTN_STATE == HIGH) {MENU_STATE = !MENU_STATE;}}}
  73.  
  74.   if(MENU_STATE == LOW){drawMenu(); digitalWrite(LED, HIGH);}
  75.   if(MENU_STATE == HIGH){noMenu();digitalWrite(LED, LOW);}
  76.  
  77.   LAST_BTN_STATE = MENU;
  78.  
  79. }
  80.  
  81. ENUM_BUTTON getButton() {
  82.   if(!digitalRead(BTN_BACK)) return BACK;
  83.   if(!digitalRead(BTN_NEXT)) return NEXT;
  84.   if(!digitalRead(BTN_PREV)) return PREV;
  85.   if(!digitalRead(BTN_OK)) return OK;
  86.  
  87.   return NONE;
  88. }
  89.  
  90. void noMenu() {
  91.   static unsigned long lastRead = 0;
  92.    int autoSwitchTime = 500;
  93.    if(millis() - lastRead < autoSwitchTime) return;
  94.    lcd.clear();
  95.    lcd.print("Brak menu");
  96.    lastRead = millis();
  97. }
  98.  
  99. void drawMenu() {
  100.   static unsigned long lastRead = 0;
  101.   static ENUM_BUTTON lastPressedButton = OK;
  102.   static unsigned int isPressedSince = 0;
  103.   int autoSwitchTime = 500;
  104.  
  105.    ENUM_BUTTON pressedButton = getButton();
  106.  
  107.   if(pressedButton == NONE && lastRead != 0) {
  108.     isPressedSince = 0;
  109.     return;
  110.   }
  111.   if(pressedButton != lastPressedButton) {
  112.     isPressedSince = 0;
  113.   }
  114.  
  115.   if(isPressedSince > 3) autoSwitchTime = 70;
  116.   if(lastRead != 0 && millis() - lastRead < autoSwitchTime && pressedButton == lastPressedButton) return;
  117.  
  118.   isPressedSince++;
  119.   lastRead = millis();
  120.   lastPressedButton = pressedButton;
  121.  
  122.   switch(pressedButton) {
  123.     case NEXT: handleNext(); break;
  124.     case PREV: handlePrev(); break;
  125.     case BACK: handleBack(); break;
  126.     case OK: handleOk(); break;
  127.  }
  128.  
  129.   lcd.home();
  130.   lcd.clear();
  131.   if(isInLowerLevel) {
  132.     lcd.print(menu[currentMenuPos].label);
  133.     lcd.setCursor(0, 1);
  134.     lcd.print(F("> "));
  135.  
  136.     if(menu[currentMenuPos].handler != NULL) {
  137.       (*(menu[currentMenuPos].handler))();
  138.     } else {
  139.       lcd.print(tempVal);
  140.     }
  141.   } else {
  142.     lcd.print(F("Menu glowne"));
  143.     lcd.setCursor(0, 1);
  144.     lcd.print(F("> "));
  145.  
  146.     lcd.print(menu[currentMenuPos].label);
  147.   }
  148. }
  149.  
  150.  
  151.  
  152. void handleNext() {
  153.   if(isInLowerLevel) {
  154.     tempVal++;
  155.     if(tempVal > menu[currentMenuPos].maxVal) tempVal = menu[currentMenuPos].maxVal;
  156.   } else {
  157.     currentMenuPos = (currentMenuPos + 1) % menuSize;
  158.   }
  159. }
  160.  
  161. void handlePrev() {
  162.   if(isInLowerLevel) {
  163.     tempVal--;
  164.     if(tempVal < menu[currentMenuPos].minVal) tempVal = menu[currentMenuPos].minVal;
  165.   } else {
  166.     currentMenuPos--;
  167.     if(currentMenuPos < 0) currentMenuPos = menuSize - 1;
  168.   }
  169. }
  170.  
  171. void handleBack() {
  172.   if(isInLowerLevel) {
  173.     isInLowerLevel = false;
  174.   }
  175. }
  176.  
  177. void handleOk() {
  178.   if(menu[currentMenuPos].handler != NULL && menu[currentMenuPos].maxVal <= menu[currentMenuPos].minVal) {
  179.     (*(menu[currentMenuPos].handler))();
  180.     return;
  181.   }
  182.   if(isInLowerLevel) {
  183.     menu[currentMenuPos].currentVal = tempVal;
  184.     isInLowerLevel = false;
  185.   } else {
  186.     tempVal = menu[currentMenuPos].currentVal;
  187.     isInLowerLevel = true;
  188.   }
  189. }
  190.  
  191. /* Funkcje-uchwyty użytkownika */
  192. void actionPortSzeregowy() {
  193.   Serial.println("Wywolano akcje: Port szeregowy");
  194. }
  195.  
  196. void formatNapisy() {
  197.   String dictonary[] = {"Napis 1", "Napis 2", "Napis 3 :)"};
  198.   lcd.print(dictonary[tempVal]);
  199. }
  200.  
  201. void formatUlamki() {
  202.   lcd.print(tempVal / 10.0);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement