Advertisement
dustinrobotics

BetterMenu

Mar 24th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include <ShiftRegLCD.h>
  2. #include <Buttons.h>
  3.  
  4. #define HMENU 4
  5. #define WMENU 20
  6.  
  7. //#define DEBUG 1
  8.  
  9. ShiftRegLCD lcd(4, 3, 5, 4);
  10.  
  11. #define UPARROW                 ((uint8_t)3)
  12. #define DOWNARROW               ((uint8_t)4)
  13. #define SELECT                  ((uint8_t)5)
  14. #define HEART                   ((uint8_t)0)
  15. #define SMILE                   ((uint8_t)2)
  16. #define KEY                     ((uint8_t)1)
  17.  
  18. #define XBEECONF 0
  19. #define BTCONF   1
  20. #define CONNCPU  2
  21. #define CONNGAR  3
  22. #define CONNSAB  4
  23.  
  24. #define BACKMAIN 0
  25. #define XBEEPPP   1
  26. #define XBEESM1  2
  27. #define XBEECLOSE  3
  28.  
  29. #define BTPPP   1
  30. #define BTATR0  2
  31. #define BTATH   3
  32. #define BTATF   4
  33. #define BTATD   5
  34. #define BTATA   6
  35. #define BTCLOSE 7
  36. #define MAINSUB 0
  37. #define MAINLEN 4 //actual - 1
  38. const prog_char PROGMEM MenuStr1[] = "XBee Configure";
  39. const prog_char PROGMEM MenuStr2[] = "BT Configure";
  40. const prog_char PROGMEM MenuStr3[] = "Connect to CPU";
  41. const prog_char PROGMEM MenuStr4[] = "Connect to Garden";
  42. const prog_char PROGMEM MenuStr5[] = "Connect to Sable";
  43. #define XBEESUB 5 //starts at 2
  44.  
  45.  
  46. #define XBEELEN 3  //actual - 1
  47. const prog_char PROGMEM Xbee1[] = "Back to Main";
  48. const prog_char PROGMEM Xbee2[] = "+++";
  49. const prog_char PROGMEM Xbee3[] = "SM1,ST3e8,SO1";
  50. const prog_char PROGMEM Xbee4[] = "WN,CN";
  51. #define BTSUB 9
  52.  
  53. #define BTLEN 7 //actual - 1
  54. const prog_char PROGMEM BTser1[] = "Back to Main";
  55. const prog_char PROGMEM BTser2[] = "+++ <-not alneeded";
  56. const prog_char PROGMEM BTser3[] = "ATR0";
  57. const prog_char PROGMEM BTser4[] = "ATH (drops conn)";
  58. const prog_char PROGMEM BTser5[] = "ATF?";
  59. const prog_char PROGMEM BTser6[] = "ATD";
  60. const prog_char PROGMEM BTser7[] = "ATA";
  61. const prog_char PROGMEM BTser8[] = "ATO";
  62. //currentmenu
  63. #define MAIN   MAINSUB
  64. #define XBEE   XBEESUB
  65. #define BT     BTSUB
  66.  
  67. PGM_P PROGMEM MenuTable[] = {
  68.   MenuStr1,
  69.   MenuStr2,
  70.   MenuStr3,
  71.   MenuStr4,
  72.   MenuStr5,
  73.   Xbee1,
  74.   Xbee2,
  75.   Xbee3,
  76.   Xbee4,
  77.   BTser1,
  78.   BTser2,
  79.   BTser3,
  80.   BTser4,
  81.   BTser5,
  82.   BTser6,
  83.   BTser7,
  84.   BTser8,
  85. };
  86. byte Select[8]={  B01000,  B01100,  B00110,  B11111,  B11111,  B00110,  B01100,  B01000};
  87.  
  88. char menu = 0; //0 = main
  89. char menulen = MAINLEN;
  90. char menusel = 0;  //cursor
  91. char index[20];
  92. char currmenu = MAIN;  //current menu
  93.  
  94. void setup()
  95. {
  96.   lcd.createChar(5, Select);
  97.  
  98.   for(int i=0; i<20; i++)
  99.     index[i] = i;  
  100. }
  101.  
  102. //handle things here, not in loop
  103. void select(uint8_t cmenu, uint8_t menus)
  104. {
  105.   switch(cmenu)
  106.   {
  107.     case MAIN:
  108.       if(menus == XBEECONF)
  109.       {
  110.         menu=XBEESUB;
  111.         menulen=XBEELEN;
  112.         currmenu = XBEE;
  113.       }
  114.       else if(menus == BTCONF)
  115.       {
  116.         menu=BTSUB;
  117.         menulen=BTLEN;
  118.         currmenu = BT;
  119.       }
  120.       else
  121.         menu = MAINSUB;
  122.       break;
  123.     case XBEE:
  124.       if(menus == BACKMAIN)
  125.       {
  126.         menu=MAINSUB;
  127.         menulen=MAINLEN;
  128.         currmenu = MAIN;
  129.       }
  130.       else
  131.         menu = XBEESUB;
  132.       break;
  133.     case BT:
  134.       if(menus == BACKMAIN)
  135.       {
  136.         menu=MAINSUB;
  137.         menulen=MAINLEN;
  138.         currmenu = MAIN;
  139.       }
  140.       else
  141.         menu = BTSUB;
  142.       break;
  143.     default:
  144.       break;
  145.   }
  146.   menusel = 0;
  147. }
  148.  
  149. void loop()
  150. {
  151.   uint8_t button;
  152.  
  153.   lcd.clear();
  154.   //simple menu here
  155.   for(int i=0; i<HMENU && index[i+menu]-currmenu <= menulen ; i++)
  156.   {
  157.     lcd.setCursor(1,i);
  158.     lcd.print((__FlashStringHelper *)MenuTable[index[i+menu]]);
  159.    
  160.   }
  161.  
  162.   //draw select
  163.   lcd.setCursor(0,menusel);
  164.   lcd.write(SELECT);
  165. #ifdef DEBUG
  166.   lcd.print((char)(menu + '0'));
  167.   lcd.print(' ');
  168.   lcd.print((char)(menusel + '0'));
  169. #endif
  170.   button = buttonWait(A5,NO,YES);
  171.  
  172.   if(button == BTND)
  173.   {
  174.     if(index[menu] - currmenu + HMENU-1  < menulen)  //space to potentially go down
  175.       menu++;//increment menu
  176.     else if(index[menusel + menu] - currmenu < menulen)
  177.       menusel++;
  178.     else
  179.     {
  180.       //at bottom of menu, go back to top
  181.      menu = currmenu;
  182.      menusel = 0;
  183.     }
  184.   }
  185.   if(button == BTNB)
  186.     select(currmenu, index[menusel+menu]-currmenu);
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement