Advertisement
Plast0000

lcdmenutemplate.ino

Jun 8th, 2021 (edited)
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.03 KB | None | 0 0
  1. #include <Arduino_FreeRTOS.h>
  2.  
  3. #include <LiquidCrystal.h>
  4. #include "LcdKeypad.h"
  5. #include "MenuData.h"
  6. #include "colorsensor.h"
  7.  
  8. #define interrupt_pin 19
  9.  
  10. enum AppModeValues
  11. {
  12.   APP_NORMAL_MODE,
  13.   APP_MENU_MODE,
  14.   APP_PROCESS_MENU_CMD
  15. };
  16.  
  17. byte appMode = APP_NORMAL_MODE;
  18.  
  19. TaskHandle_t menuTaskHandle;
  20. TaskHandle_t sensorTaskHandle;
  21.  
  22. MenuManager Menu1(sampleMenu_Root, menuCount(sampleMenu_Root));
  23.  
  24. char strbuf[LCD_COLS + 1]; // one line of lcd display
  25. byte btn;
  26.  
  27. // initialize the library with the numbers of the interface pins
  28. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  29.  
  30. void refreshMenuDisplay (byte refreshMode);
  31. byte getNavAction();
  32. int nots = 0;
  33.  
  34.  
  35. void setup()
  36. {
  37.   Serial.begin(115200);
  38.   pinMode(interrupt_pin, INPUT_PULLUP);
  39.   attachInterrupt(digitalPinToInterrupt(interrupt_pin), interruptHandler, CHANGE   );
  40.  
  41.   backLightOn();
  42.   // set up the LCD's number of columns and rows:
  43.   lcd.begin(LCD_COLS, LCD_ROWS);
  44.  
  45.   // fall in to menu mode by default.
  46.   appMode = APP_MENU_MODE;
  47.   refreshMenuDisplay(REFRESH_DESCEND);
  48.  
  49.   // Use soft PWM for backlight, as hardware PWM must be avoided for some LCD shields.
  50.   // piggy back on to timer0, which is already set to approx 1khz.
  51.   // OCR0A = 0xAF;
  52.   // TIMSK0 |= _BV(OCIE0A);
  53.  
  54.   setBacklightBrightness(1);
  55.  
  56. ////////////// TASKS ///////////////////////////////////////////////////////
  57.   // LCD Menu Task
  58.   xTaskCreate(TaskMenu, // Task function
  59.             "Menu", // Task name
  60.             128, // Stack size
  61.             NULL,
  62.             3, // Priority
  63.             &menuTaskHandle );
  64.  
  65.   // Color Sensor Task.
  66.   xTaskCreate(
  67.     TaskColorSensor
  68.     ,  "3200"
  69.     ,  128
  70.     ,  NULL
  71.     ,  2
  72.     ,  &sensorTaskHandle );
  73.  
  74.   vTaskSuspend(sensorTaskHandle);
  75.  
  76. } //setup
  77.  
  78. // SIGNAL(TIMER0_COMPA_vect)
  79. // {
  80. //   lcdBacklightISR();
  81. // }
  82.  
  83. void loop()
  84. {
  85.  
  86. }
  87.  
  88. void TaskMenu(void *pvParameters)
  89. {
  90.   (void) pvParameters;
  91.  
  92.   for(;;)
  93.   {
  94.     // if (ulTaskNotifyTake(pdTRUE, portMAX_DELAY))
  95.     // {
  96.       //nots++;
  97.       //Serial.println(nots);
  98.  
  99.       int analogReading = analogRead (A0);
  100.       Serial.println(analogReading);
  101.       btn = getButton();
  102.       switch (appMode)
  103.       {
  104.         case APP_NORMAL_MODE :
  105.           if (btn == BUTTON_UP_LONG_PRESSED)
  106.           {
  107.             appMode = APP_MENU_MODE;
  108.             refreshMenuDisplay(REFRESH_DESCEND);
  109.           }
  110.           break;
  111.         case APP_MENU_MODE :
  112.         {
  113.           byte menuMode = Menu1.handleNavigation(getNavAction, refreshMenuDisplay);
  114.  
  115.           if (menuMode == MENU_EXIT)
  116.           {
  117.             lcd.clear();
  118.             lcd.print("Hold UP for menu");
  119.             appMode = APP_NORMAL_MODE;
  120.           }
  121.           else if (menuMode == MENU_INVOKE_ITEM)
  122.           {
  123.             appMode = APP_PROCESS_MENU_CMD;
  124.  
  125.             // Indicate selected item.
  126.             if (Menu1.getCurrentItemCmdId())
  127.             {
  128.               lcd.setCursor(0, 1);
  129.               strbuf[0] = 0b01111110; // forward arrow representing input prompt.
  130.               strbuf[1] = 0;
  131.               lcd.print(strbuf);
  132.             }
  133.           }
  134.           break;
  135.         }
  136.         case APP_PROCESS_MENU_CMD :
  137.         {
  138.           byte processingComplete = processMenuCommand(Menu1.getCurrentItemCmdId());
  139.  
  140.           if (processingComplete)
  141.           {
  142.             appMode = APP_MENU_MODE;
  143.             // clear forward arrow
  144.             lcd.setCursor(0, 1);
  145.             strbuf[0] = ' '; // clear forward arrow
  146.             strbuf[1] = 0;
  147.             lcd.print(strbuf);
  148.           }
  149.           break;
  150.         }
  151.       }
  152.     //}
  153.       vTaskDelay(5);  // one tick delay (15ms) in between reads for stability
  154.   }
  155. }
  156.  
  157. // void interruptHandler() {
  158. //   BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  159. //   vTaskNotifyGiveFromISR(menuTaskHandle, &xHigherPriorityTaskWoken);
  160. //   if (xHigherPriorityTaskWoken) {
  161. //     taskYIELD();
  162. //   }
  163. // }
  164.  
  165. //----------------------------------------------------------------------
  166. // Addition or removal of menu items in MenuData.h will require this method
  167. // to be modified accordingly.
  168. byte processMenuCommand(byte cmdId)
  169. {
  170.   byte complete = false;  // set to true when menu command processing complete.
  171.  
  172.   if (btn == BUTTON_SELECT_PRESSED)
  173.   {
  174.     complete = true;
  175.   }
  176.  
  177.   switch (cmdId)
  178.   {
  179.     // TODO Process menu commands here:
  180.       case mnuCmdColorSensor:
  181.         Serial.println("Resuming color sensor task");
  182.         vTaskResume(sensorTaskHandle);
  183.         break;
  184.       default:
  185.         Serial.println("aaaaaa");
  186.     break;
  187.   }
  188.  
  189.   return complete;
  190. }
  191.  
  192.  
  193. //----------------------------------------------------------------------
  194. // Callback to convert button press to navigation action.
  195. byte getNavAction()
  196. {
  197.   byte navAction = 0;
  198.   byte currentItemHasChildren = Menu1.currentItemHasChildren();
  199.  
  200.   if (btn == BUTTON_UP_PRESSED || btn == BUTTON_UP_LONG_PRESSED) navAction = MENU_ITEM_PREV;
  201.   else if (btn == BUTTON_DOWN_PRESSED || btn == BUTTON_DOWN_LONG_PRESSED) navAction = MENU_ITEM_NEXT;
  202.   else if (btn == BUTTON_SELECT_PRESSED || (btn == BUTTON_RIGHT_PRESSED && currentItemHasChildren)) navAction = MENU_ITEM_SELECT;
  203.   //else if (btn == BUTTON_LEFT_PRESSED) navAction = MENU_BACK;
  204.   return navAction;
  205. }
  206.  
  207.  
  208. //----------------------------------------------------------------------
  209. const char EmptyStr[] = "";
  210.  
  211. // Callback to refresh display during menu navigation, using parameter of type enum DisplayRefreshMode.
  212. void refreshMenuDisplay (byte refreshMode)
  213. {
  214.   char nameBuf[LCD_COLS+1];
  215.  
  216. /*
  217.   if (refreshMode == REFRESH_DESCEND || refreshMode == REFRESH_ASCEND)
  218.   {
  219.     byte menuCount = Menu1.getMenuItemCount();
  220.    
  221.     // uncomment below code to output menus to serial monitor
  222.     if (Menu1.currentMenuHasParent())
  223.     {
  224.       Serial.print("Parent menu: ");
  225.       Serial.println(Menu1.getParentItemName(nameBuf));
  226.     }
  227.     else
  228.     {
  229.       Serial.println("Main menu:");
  230.     }
  231.    
  232.     for (int i=0; i<menuCount; i++)
  233.     {
  234.       Serial.print(Menu1.getItemName(nameBuf, i));
  235.  
  236.       if (Menu1.itemHasChildren(i))
  237.       {
  238.         Serial.println("->");
  239.       }
  240.       else
  241.       {
  242.         Serial.println();
  243.       }
  244.     }
  245.   }
  246. */
  247.  
  248.   lcd.setCursor(0, 0);
  249.   if (Menu1.currentItemHasChildren())
  250.   {
  251.     rpad(strbuf, Menu1.getCurrentItemName(nameBuf));
  252.     strbuf[LCD_COLS-1] = 0b01111110;            // Display forward arrow if this menu item has children.
  253.     lcd.print(strbuf);
  254.     lcd.setCursor(0, 1);
  255.     lcd.print(rpad(strbuf, EmptyStr));          // Clear config value in display
  256.   }
  257.   else
  258.   {
  259.     byte cmdId;
  260.     rpad(strbuf, Menu1.getCurrentItemName(nameBuf));
  261.    
  262.     if ((cmdId = Menu1.getCurrentItemCmdId()) == 0)
  263.     {
  264.       strbuf[LCD_COLS-1] = 0b01111111;          // Display back arrow if this menu item ascends to parent.
  265.       lcd.print(strbuf);
  266.       lcd.setCursor(0, 1);
  267.       lcd.print(rpad(strbuf, EmptyStr));        // Clear config value in display.
  268.     }
  269.     else
  270.     {
  271.       lcd.print(strbuf);
  272.       lcd.setCursor(0, 1);
  273.       lcd.print(" ");
  274.      
  275.       // TODO Display config value.
  276.     }
  277.   }
  278. }
  279.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement