Advertisement
AlexShu

u8glib Menu Example Sketch

Apr 15th, 2015
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.96 KB | None | 0 0
  1. /*
  2.  
  3.   Menu.pde
  4.  
  5.   Simple Menu Selection
  6.  
  7.   >>> Before compiling: Please remove comment from the constructor of the
  8.   >>> connected graphics display (see below).
  9.  
  10.   Universal 8bit Graphics Library, http://code.google.com/p/u8glib/
  11.  
  12.   Copyright (c) 2012, olikraus@gmail.com
  13.   All rights reserved.
  14.  
  15.   Redistribution and use in source and binary forms, with or without modification,
  16.   are permitted provided that the following conditions are met:
  17.  
  18.   * Redistributions of source code must retain the above copyright notice, this list
  19.     of conditions and the following disclaimer.
  20.    
  21.   * Redistributions in binary form must reproduce the above copyright notice, this
  22.     list of conditions and the following disclaimer in the documentation and/or other
  23.     materials provided with the distribution.
  24.  
  25.   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  26.   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  27.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28.   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  30.   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31.   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  32.   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33.   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34.   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35.   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36.   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  37.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
  38.  
  39.  
  40. */
  41.  
  42.  
  43. #include "U8glib.h"
  44.  
  45. // setup u8g object, please remove comment from one of the following constructor calls
  46. // IMPORTANT NOTE: The complete list of supported devices is here: http://code.google.com/p/u8glib/wiki/device
  47.  
  48. // setup u8g object, please remove comment from one of the following constructor calls
  49. // IMPORTANT NOTE: The following list is incomplete. The complete list of supported
  50. // devices with all constructor calls is here: http://code.google.com/p/u8glib/wiki/device
  51. //U8GLIB_NHD27OLED_BW u8g(13, 11, 10, 9);   // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  52. //U8GLIB_NHD27OLED_2X_BW u8g(13, 11, 10, 9);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  53. //U8GLIB_NHD27OLED_GR u8g(13, 11, 10, 9);   // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  54. //U8GLIB_NHD27OLED_2X_GR u8g(13, 11, 10, 9);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  55. //U8GLIB_NHD31OLED_BW u8g(13, 11, 10, 9);   // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  56. //U8GLIB_NHD31OLED_2X_BW u8g(13, 11, 10, 9);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  57. //U8GLIB_NHD31OLED_GR u8g(13, 11, 10, 9);   // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  58. //U8GLIB_NHD31OLED_2X_GR u8g(13, 11, 10, 9);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  59. //U8GLIB_DOGS102 u8g(13, 11, 10, 9, 8);     // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  60. //U8GLIB_DOGM132 u8g(13, 11, 10, 9);        // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  61. //U8GLIB_DOGM128 u8g(13, 11, 10, 9);        // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  62. //U8GLIB_DOGM128_2X u8g(13, 11, 10, 9);     // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  63. //U8GLIB_ST7920_128X64_1X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16
  64. //U8GLIB_ST7920_128X64_4X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16
  65. //U8GLIB_ST7920_128X64_1X u8g(18, 16, 17);  // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
  66. //U8GLIB_ST7920_128X64_4X u8g(18, 16, 17);  // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
  67. //U8GLIB_ST7920_192X32_1X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16
  68. //U8GLIB_ST7920_192X32_4X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16
  69. //U8GLIB_ST7920_192X32_1X u8g(18, 16, 17);  // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
  70. //U8GLIB_ST7920_192X32_4X u8g(18, 16, 17);  // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
  71. //U8GLIB_ST7920_192X32_1X u8g(13, 11, 10);  // SPI Com: SCK = en = 13, MOSI = rw = 11, CS = di = 10
  72. //U8GLIB_ST7920_192X32_4X u8g(10);      // SPI Com: SCK = en = 13, MOSI = rw = 11, CS = di = 10, HW SPI
  73. //U8GLIB_ST7920_202X32_1X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16
  74. //U8GLIB_ST7920_202X32_4X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16
  75. //U8GLIB_ST7920_202X32_1X u8g(18, 16, 17);  // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
  76. //U8GLIB_ST7920_202X32_4X u8g(18, 16, 17);  // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
  77. //U8GLIB_LM6059 u8g(13, 11, 10, 9);     // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  78. //U8GLIB_LM6063 u8g(13, 11, 10, 9);     // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  79. //U8GLIB_DOGXL160_BW u8g(10, 9);        // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  80. //U8GLIB_DOGXL160_GR u8g(13, 11, 10, 9);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  81. //U8GLIB_DOGXL160_2X_BW u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  82. //U8GLIB_DOGXL160_2X_GR u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  83. //U8GLIB_PCD8544 u8g(13, 11, 10, 9, 8);     // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, Reset = 8
  84. //U8GLIB_PCF8812 u8g(13, 11, 10, 9, 8);     // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, Reset = 8
  85. //U8GLIB_KS0108_128 u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 14, 15, 17, 16);      // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs1=14, cs2=15,di=17,rw=16
  86. //U8GLIB_LC7981_160X80 u8g(8, 9, 10, 11, 4, 5, 6, 7,  18, 14, 15, 17, 16);  // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs=14 ,di=15,rw=17, reset = 16
  87. //U8GLIB_LC7981_240X64 u8g(8, 9, 10, 11, 4, 5, 6, 7,  18, 14, 15, 17, 16);  // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs=14 ,di=15,rw=17, reset = 16
  88. //U8GLIB_LC7981_240X128 u8g(8, 9, 10, 11, 4, 5, 6, 7,  18, 14, 15, 17, 16);     // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs=14 ,di=15,rw=17, reset = 16
  89. //U8GLIB_ILI9325D_320x240 u8g(18,17,19,U8G_PIN_NONE,16 );           // 8Bit Com: D0..D7: 0,1,2,3,4,5,6,7 en=wr=18, cs=17, rs=19, rd=U8G_PIN_NONE, reset = 16
  90. //U8GLIB_SBN1661_122X32 u8g(8,9,10,11,4,5,6,7,14,15, 17, U8G_PIN_NONE, 16);     // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 cs1=14, cs2=15,di=17,rw=16,reset = 16
  91. //U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  92. //U8GLIB_SSD1306_128X64 u8g(4, 5, 6, 7);    // SW SPI Com: SCK = 4, MOSI = 5, CS = 6, A0 = 7 (new white HalTec OLED)
  93. //U8GLIB_SSD1306_128X64 u8g(10, 9);     // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
  94. //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);    // I2C / TWI
  95. //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
  96. //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);    // Display which does not send AC
  97. //U8GLIB_SSD1306_ADAFRUIT_128X64 u8g(13, 11, 10, 9);    // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  98. //U8GLIB_SSD1306_ADAFRUIT_128X64 u8g(10, 9);        // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
  99. //U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  100. //U8GLIB_SSD1306_128X32 u8g(10, 9);             // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
  101. //U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI
  102. //U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9);  // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  103. //U8GLIB_SH1106_128X64 u8g(4, 5, 6, 7); // SW SPI Com: SCK = 4, MOSI = 5, CS = 6, A0 = 7 (new blue HalTec OLED)
  104. //U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);   // I2C / TWI
  105. //U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_FAST); // Dev 0, Fast I2C / TWI
  106. //U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send ACK
  107. //U8GLIB_SSD1309_128X64 u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  108. //U8GLIB_SSD1327_96X96_GR u8g(U8G_I2C_OPT_NONE);    // I2C
  109. //U8GLIB_SSD1327_96X96_2X_GR u8g(U8G_I2C_OPT_NONE); // I2C
  110. //U8GLIB_UC1611_DOGM240 u8g(U8G_I2C_OPT_NONE);  // I2C
  111. //U8GLIB_UC1611_DOGM240 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  112. //U8GLIB_UC1611_DOGM240 u8g(10, 9);     // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
  113. //U8GLIB_UC1611_DOGXL240 u8g(U8G_I2C_OPT_NONE); // I2C
  114. //U8GLIB_UC1611_DOGXL240 u8g(13, 11, 10, 9);    // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
  115. //U8GLIB_UC1611_DOGXL240 u8g(10, 9);        // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
  116. //U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);  // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  117. //U8GLIB_NHD_C12832 u8g(13, 11, 10, 9, 8);  // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  118. //U8GLIB_LD7032_60x32 u8g(13, 11, 10, 9, 8);    // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  119. //U8GLIB_LD7032_60x32 u8g(11, 12, 9, 10, 8);    // SPI Com: SCK = 11, MOSI = 12, CS = 9, A0 = 10, RST = 8  (SW SPI Nano Board)
  120. //U8GLIB_UC1608_240X64 u8g(13, 11, 10, 9, 8);   // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  121. //U8GLIB_UC1608_240X64_2X u8g(13, 11, 10, 9, 8);    // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  122. //U8GLIB_UC1608_240X64 u8g(10, 9, 8);   // HW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  123. //U8GLIB_UC1608_240X64_2X u8g(10, 9, 8);    // HW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  124. //U8GLIB_UC1608_240X u8g(13, 11, 10, 9, 8); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  125. //U8GLIB_UC1608_240X64_2X u8g(13, 11, 10, 9, 8);    // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  126. //U8GLIB_UC1608_240X64 u8g(10, 9, 8);   // HW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  127. //U8GLIB_UC1608_240X64_2X u8g(10, 9, 8);    // HW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
  128. //U8GLIB_T6963_240X128 u8g(8, 9, 10, 11, 4, 5, 6, 7, 14, 15, 17, 18, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7, cs=14, a0=15, wr=17, rd=18, reset=16
  129. //U8GLIB_T6963_128X128 u8g(8, 9, 10, 11, 4, 5, 6, 7, 14, 15, 17, 18, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7, cs=14, a0=15, wr=17, rd=18, reset=16
  130. //U8GLIB_T6963_240X64 u8g(8, 9, 10, 11, 4, 5, 6, 7, 14, 15, 17, 18, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7, cs=14, a0=15, wr=17, rd=18, reset=16
  131. //U8GLIB_T6963_128X64 u8g(8, 9, 10, 11, 4, 5, 6, 7, 14, 15, 17, 18, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7, cs=14, a0=15, wr=17, rd=18, reset=16
  132. //U8GLIB_HT1632_24X16 u8g(3, 2, 4);     // WR = 3, DATA = 2, CS = 4
  133. //U8GLIB_SSD1351_128X128_332 u8g(13, 11, 8, 9, 7); // Arduino UNO: SW SPI Com: SCK = 13, MOSI = 11, CS = 8, A0 = 9, RESET = 7 (http://electronics.ilsoft.co.uk/ArduinoShield.aspx)
  134. //U8GLIB_SSD1351_128X128_332 u8g(76, 75, 8, 9, 7); // Arduino DUE: SW SPI Com: SCK = 13, MOSI = 11, CS = 8, A0 = 9, RESET = 7 (http://electronics.ilsoft.co.uk/ArduinoShield.aspx)
  135. //U8GLIB_SSD1351_128X128_332 u8g(8, 9, 7); // Arduino: HW SPI Com: SCK = 13, MOSI = 11, CS = 8, A0 = 9, RESET = 7 (http://electronics.ilsoft.co.uk/ArduinoShield.aspx)
  136. //U8GLIB_SSD1351_128X128_HICOLOR u8g(76, 75, 8, 9, 7); // Arduino DUE, SW SPI Com: SCK = 76, MOSI = 75, CS = 8, A0 = 9, RESET = 7 (http://electronics.ilsoft.co.uk/ArduinoShield.aspx)
  137. //U8GLIB_SSD1351_128X128_HICOLOR u8g(8, 9, 7); // Arduino, HW SPI Com: SCK = 76, MOSI = 75, CS = 8, A0 = 9, RESET = 7 (http://electronics.ilsoft.co.uk/ArduinoShield.aspx)
  138. //U8GLIB_SSD1351_128X128GH_332 u8g(8, 9, 7); // Arduino, HW SPI Com: SCK = 76, MOSI = 75, CS = 8, A0 = 9, RESET = 7 (Freetronics OLED)
  139. //U8GLIB_SSD1351_128X128GH_HICOLOR u8g(8, 9, 7); // Arduino, HW SPI Com: SCK = 76, MOSI = 75, CS = 8, A0 = 9, RESET = 7 (Freetronics OLED)
  140.  
  141.  
  142. #define KEY_NONE 0
  143. #define KEY_PREV 1
  144. #define KEY_NEXT 2
  145. #define KEY_SELECT 3
  146. #define KEY_BACK 4
  147.  
  148. // DOGS102 shield configuration values
  149. //uint8_t uiKeyPrev = 2;
  150. //uint8_t uiKeyNext = 4;
  151. //uint8_t uiKeySelect = 5;
  152. //uint8_t uiKeyBack = 3;
  153.  
  154. // DOGM128-Shield    configuration values
  155. // DOGXL60-Shield configuration values
  156. uint8_t uiKeyPrev = 7;
  157. uint8_t uiKeyNext = 3;
  158. uint8_t uiKeySelect = 2;
  159. uint8_t uiKeyBack = 8;
  160.  
  161. uint8_t uiKeyCodeFirst = KEY_NONE;
  162. uint8_t uiKeyCodeSecond = KEY_NONE;
  163. uint8_t uiKeyCode = KEY_NONE;
  164.  
  165.  
  166. void uiSetup(void) {
  167.   // configure input keys
  168.  
  169.   pinMode(uiKeyPrev, INPUT);           // set pin to input
  170.   digitalWrite(uiKeyPrev, HIGH);       // turn on pullup resistors
  171.   pinMode(uiKeyNext, INPUT);           // set pin to input
  172.   digitalWrite(uiKeyNext, HIGH);       // turn on pullup resistors
  173.   pinMode(uiKeySelect, INPUT);           // set pin to input
  174.   digitalWrite(uiKeySelect, HIGH);       // turn on pullup resistors
  175.   pinMode(uiKeyBack, INPUT);           // set pin to input
  176.   digitalWrite(uiKeyBack, HIGH);       // turn on pullup resistors
  177. }
  178.  
  179. void uiStep(void) {
  180.   uiKeyCodeSecond = uiKeyCodeFirst;
  181.   if ( digitalRead(uiKeyPrev) == LOW )
  182.     uiKeyCodeFirst = KEY_PREV;
  183.   else if ( digitalRead(uiKeyNext) == LOW )
  184.     uiKeyCodeFirst = KEY_NEXT;
  185.   else if ( digitalRead(uiKeySelect) == LOW )
  186.     uiKeyCodeFirst = KEY_SELECT;
  187.   else if ( digitalRead(uiKeyBack) == LOW )
  188.     uiKeyCodeFirst = KEY_BACK;
  189.   else
  190.     uiKeyCodeFirst = KEY_NONE;
  191.  
  192.   if ( uiKeyCodeSecond == uiKeyCodeFirst )
  193.     uiKeyCode = uiKeyCodeFirst;
  194.   else
  195.     uiKeyCode = KEY_NONE;
  196. }
  197.  
  198.  
  199. #define MENU_ITEMS 4
  200. char *menu_strings[MENU_ITEMS] = { "First Line", "Second Item", "3333333", "abcdefg" };
  201.  
  202. uint8_t menu_current = 0;
  203. uint8_t menu_redraw_required = 0;
  204. uint8_t last_key_code = KEY_NONE;
  205.  
  206.  
  207. void drawMenu(void) {
  208.   uint8_t i, h;
  209.   u8g_uint_t w, d;
  210.  
  211.   u8g.setFont(u8g_font_6x13);
  212.   u8g.setFontRefHeightText();
  213.   u8g.setFontPosTop();
  214.  
  215.   h = u8g.getFontAscent()-u8g.getFontDescent();
  216.   w = u8g.getWidth();
  217.   for( i = 0; i < MENU_ITEMS; i++ ) {
  218.     d = (w-u8g.getStrWidth(menu_strings[i]))/2;
  219.     u8g.setDefaultForegroundColor();
  220.     if ( i == menu_current ) {
  221.       u8g.drawBox(0, i*h+1, w, h);
  222.       u8g.setDefaultBackgroundColor();
  223.     }
  224.     u8g.drawStr(d, i*h, menu_strings[i]);
  225.   }
  226. }
  227.  
  228. void updateMenu(void) {
  229.   if ( uiKeyCode != KEY_NONE && last_key_code == uiKeyCode ) {
  230.     return;
  231.   }
  232.   last_key_code = uiKeyCode;
  233.  
  234.   switch ( uiKeyCode ) {
  235.     case KEY_NEXT:
  236.       menu_current++;
  237.       if ( menu_current >= MENU_ITEMS )
  238.         menu_current = 0;
  239.       menu_redraw_required = 1;
  240.       break;
  241.     case KEY_PREV:
  242.       if ( menu_current == 0 )
  243.         menu_current = MENU_ITEMS;
  244.       menu_current--;
  245.       menu_redraw_required = 1;
  246.       break;
  247.   }
  248. }
  249.  
  250.  
  251. void setup() {
  252.   // rotate screen, if required
  253.   // u8g.setRot180();
  254.  
  255.   uiSetup();                                // setup key detection and debounce algorithm
  256.   menu_redraw_required = 1;     // force initial redraw
  257. }
  258.  
  259. void loop() {  
  260.  
  261.   uiStep();                                     // check for key press
  262.    
  263.   if (  menu_redraw_required != 0 ) {
  264.     u8g.firstPage();
  265.     do  {
  266.       drawMenu();
  267.     } while( u8g.nextPage() );
  268.     menu_redraw_required = 0;
  269.   }
  270.  
  271.   updateMenu();                            // update menu bar
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement