TrackControl.ino 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. TrackControl.ino
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2018, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <Arduino.h>
  28. #include <U8g2lib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #ifdef U8X8_HAVE_HW_SPI
  32. #include <SPI.h>
  33. #endif
  34. #ifdef U8X8_HAVE_HW_I2C
  35. #include <Wire.h>
  36. #endif
  37. #define MENU_FONT u8g2_font_helvB18_tr
  38. #define SUBMENU_FONT u8g2_font_helvB12_tr
  39. /*
  40. 158*128
  41. bold: 65..70
  42. normal: 71...76
  43. */
  44. /*
  45. const uint8_t track_font[1646] U8G2_FONT_SECTION("track_font") =
  46. "\14\0\7\4\7\7\10\6\11\232|\2\2~\0~\0\0\0\0\0\6UA\273z\276\240\350\271\365\0"
  47. "?\200\260=\300\17\200d\17\360\3\260\327\3\374\265\36\340\17 V\17\360\7 \251\7\70\36\304{\200"
  48. "\220nk\326^n\355\255t\250\225z\207\225\200\204\225\204\203\205\342G~\315\315\374\310\257\271\231\237\271"
  49. "\231\237\271E\67sC\267\346\206n\350\205n\321K\275\320\355\377\377\377\377W\257\325\15\335V\257\352\206"
  50. "n\325K\335\320M\335\320\255\272\241\233\272\241\37\272\241\337\346\210<\30H\134\30'|\30\307\233\32h"
  51. "\233\36(\233\42\310\232(I\252,\213\311\62\13\311\70\357\367>\17\320B\17\360@\17\360O\364\0\177"
  52. "\200Vz\200?\200B=\300\37\340\264\36\340\7X\17\0Bc<%\243\352Y\60p\4Wlv\66"
  53. "DS\62M\226fy\63D\303\277\332}\65T\273\331N\355\330N\355\330N\355\330N\355\330N\15\253"
  54. "\35\333\251\241\332\261\235\32VK\264\34M\315\226\214\305Z\314\275\36@\260\36\300\201\36\340\201\36\240\211"
  55. "\36`\221\36 \231\36\340\244\36\0\235\36\300\254\36@\270\234L\1Cu\256\275\373\350\31\320B\17\20"
  56. "<\17@\66\17p\60\17\200.\17\240BO\324H\253\244L\351\204\357tP\27\312\244\14\312\244\14\352"
  57. "\240L\312\240L\252\244\14\312\244\14\312J\31\224\275T]\312\240\354\377\377\377\377\177'\203\262N\6e"
  58. "\235\316\311\240\314\311\240\314\311\240\314\311\234\316\311\234\216\351\62\241\22\36)\33)\321\2\61P\353<\316"
  59. "\3\210\216\3\61Q*\31\21\0D\236~\362\243\354\271\364\0\37\240\375\36\340\7\70\267\7\370\1\332\354"
  60. "\1\376\0\342\365\0\177\200\302z\270\206j?Fj\303%ZH\365a\313\324Y\316\264QR\224IU"
  61. "tIWTIY\64A\335\23A\336\23\71\340\363\70\342\323\70\344\263@e\263\70\346\223\70h\203\70"
  62. "js\70jc\70lS\60mC\70\355\63\60o\63\60o#\60\360\23\60q\23(r\63\20\363\203"
  63. "\267\377\377\377\377\341K\276\340\355\341\13\336\302\27\274\205/x\353n\360\346n\360\346~\354\347n\356\307"
  64. "~w?w\223'\16\0E|\63s\262\352\331\42b\304eZg\225o\205X\370X\247uX\210u"
  65. "Z\247eZW\313\264N\313\264N\313\270L\313\270L\313\264N\313j\235\226\345*.\323\262\134\225\313"
  66. "\264\354\377Z\305U\327\62\255\342\252Z\246U\134\245U\134\245eZ\245eZ\245eZ\245eX\246e"
  67. "X\26\353\254\356VH\205RH\205R\351\264L\313\304H\16\364\0B\363\0\247\363\0\345\363\0B\364"
  68. "H\13\5\0F\254\332.%\354y\344\216\17\220~\17\360\0\1\367\0\17`h\17\360\0\353\365\0!"
  69. "\361\0e\325(\17\20N\214\303JJ\264D\311\244>I\245\70\251\225\64\10\226\60H\226,\250\206("
  70. "\350\206$(\207\42g\207\36\247\207\32\347\207\26'x\26fx\22\207x\20\246x\16\346x\14%i"
  71. "\12&y\10ei\10ey\6\244i\6\244i\10\342i\250V\325P\255jU\65T{\325\372\243Z"
  72. "\324R\255\250\26\265P-j\241\32j\241ZS\63=Skz\244gj\246Gz\213\342\240\351\251\234"
  73. "\253\271\230M\351\220\17\20\224\216\311\234)jF\0G\240\367<\341\350\271\364\0?\0z=\300\17\300"
  74. "Z\17\360\3\70\325\3\31\340B\25\37j\37\67r\237\267\5\67\276\322*\301\262*C\301*\361JN"
  75. "\67\37s\42'\363\61'r\62'r\62\37\364\61'\363A\37s\62\37sj>\350\363\377\377\377\377"
  76. "\177\365A\237G'\364\211N\346\204>\350\204>\350dN\350dN\350\324\234\320if\301 d\302 "
  77. "\341\342 \337\2!\134#!\332R)Vs\61S\243\61\316\323A\310\4R\301\65z\0\363z\200A"
  78. "z\200?\0:=\300\37\240\264\36\340\7 \23\0H[:d\343\352\71 p\343\66l\364\2C\206"
  79. "dgv\67\63\64\213Vj\365l\64[\251\325l\245V\331\210\255\330([\261\21[\261\21[e#"
  80. "\67b+\66r#\266z\65\253vjV\355\320\320L\311\26\314\265\36\340\230\36@\215\36\0\225\36\300"
  81. "\240\36 \264\36 \270\236\213\311\20I_+\374;\351\31\340:\17 \64\17PF\212\204J\250tN"
  82. "\346T;\21\24\35j\240(\324@\21\324@\21\324@\21\324@\21\324\204\42\250\11EN\4\65\377\377"
  83. "\377\377\377\377w\242N\343D\235\306\211\234\306\211\62\221\23\61\221\23\61Q\246b\242\257T\221\254\221\21"
  84. "\241\20\372\250\316\2%Q\11\1J\227{\61\344\354\71\364\0?\200\320=\300\17\200h\17\360\3\260\230"
  85. "\322\241\26\12\236\24*\36\323A\236\321a\236\317\201\232\216\241\226\215\261\226L\311\226K\331\22K\351\22"
  86. "J\371\22I\11\23H\31\23G)\223\6\61\23\6A\23\5Q\217DQ\23\4a\23\3i\23\3q"
  87. "\23\2y\23\302\210\217\1\211\23\301\220\223\0\231\217\300\240\37:\11\4\373\300\317\377\377\377\377\357N\335"
  88. "\7~\272\223\373\300\217;\271\17\374\270\17\374\270\223\373\270\23;\271\217;\261\223\373\270\23;\355>\32"
  89. "\0Ki\61r\362\352\331\42\134\244UXV\253~-\342\42\255\252E\134\244UZ\304EZ\304E\134"
  90. "\303E\134\303E\134\303E\134\303E\134\303EZ\304\65\134\244E\134\223\213\270\346\377\377\271\204k\326\32"
  91. "\256\251\65\134Sk\270Fk\270Fk\264Hkj\221\326\324\42,z\253JUT&eR\347\204L"
  92. "\211\264<\17\20@n\244N\0L\240Y.%\354\271\264\220\17p\200\17\340r\212\321f\310\242^\247"
  93. "\203XFtP\346dJeUD\305e>%V:\204V\66\304V\62\4W.dG*\244G&"
  94. "\344G\42$H cH\34\204H\30\244X\24\344H\24#I\20$I\16dI\14dY\10\244I"
  95. "\10\304\71\10\343I\6\42:\254\244J\254\223\225T\211uf%Ub\235\377\253\16\353\254J\252\303:"
  96. "U\207uTIuXGuTIuT\11\225TG\225Pi\324\62\255\223\32i\221\232\250\201\242'"
  97. "j*\246r\242h\251\6\23\0\0\0\0";
  98. */
  99. /*
  100. Fontname: track
  101. Copyright: unknown
  102. Glyphs: 12/12
  103. BBX Build Mode: 0
  104. */
  105. const uint8_t track_font[1650] U8G2_FONT_SECTION("track_font") =
  106. "\14\0\7\4\7\7\10\6\11\232|\2\2~\0~\0\0\0\0\0\6UA\273z\276\240\350\271\365\0"
  107. "\77\200\260=\300\17\200d\17\360\3\260\327\3\374\265\36\340\17 V\17\360\7 \251\7\70\36\304{\200"
  108. "\220nk\326^n\355\255t\250\225z\207\225\200\204\225\204\203\205\342G~\315\315\374\310\257\271\231\237\271"
  109. "\231\237\271E\67sC\267\346\206n\350\205n\321K\275\320\355\377\377\377\377W\257\325\15\335V\257\352\206"
  110. "n\325K\335\320M\335\320\255\272\241\233\272\241\37\272\241\337\346\210<\30H\134\30'|\30\307\233\32h"
  111. "\233\36(\233\42\310\232(I\252,\213\311\62\13\311\70\357\367>\17\320B\17\360@\17\360O\364\0\177"
  112. "\200Vz\200\77\200B=\300\37\340\264\36\340\7X\17\0Bc<%\243\352Y\60p\4Wlv\66"
  113. "DS\62M\226fy\63D\303\277\332}\65T\273\331N\355\330N\355\330N\355\330N\355\330N\15\253"
  114. "\35\333\251\241\332\261\235\32VK\264\34M\315\226\214\305Z\314\275\36@\260\36\300\201\36\340\201\36\240\211"
  115. "\36`\221\36 \231\36\340\244\36\0\235\36\300\254\36@\270\234L\1Cu\256\275\373\350\31\320B\17\20"
  116. "<\17@\66\17p\60\17\200.\17\240BO\324H\253\244L\351\204\357tP\27\312\244\14\312\244\14\352"
  117. "\240L\312\240L\252\244\14\312\244\14\312J\31\224\275T]\312\240\354\377\377\377\377\177'\203\262N\6e"
  118. "\235\316\311\240\314\311\240\314\311\240\314\311\234\316\311\234\216\351\62\241\22\36)\33)\321\2\61P\353<\316"
  119. "\3\210\216\3\61Q*\31\21\0D\236~\362\243\354\271\364\0\37\240\375\36\340\7\70\267\7\370\1\332\354"
  120. "\1\376\0\342\365\0\177\200\302z\270\206j\77Fj\303%ZH\365a\313\324Y\316\264QR\224IU"
  121. "tIWTIY\64A\335\23A\336\23\71\340\363\70\342\323\70\344\263@e\263\70\346\223\70h\203\70"
  122. "js\70jc\70lS\60mC\70\355\63\60o\63\60o#\60\360\23\60q\23(r\63\20\363\203"
  123. "\267\377\377\377\377\341K\276\340\355\341\13\336\302\27\274\205/x\353n\360\346n\360\346~\354\347n\356\307"
  124. "~w\77w\223'\16\0E|\63s\262\352\331\42b\304eZg\225o\205X\370X\247uX\210u"
  125. "Z\247eZW\313\264N\313\264N\313\270L\313\270L\313\264N\313j\235\226\345*.\323\262\134\225\313"
  126. "\264\354\377Z\305U\327\62\255\342\252Z\246U\134\245U\134\245eZ\245eZ\245eZ\245eX\246e"
  127. "X\26\353\254\356VH\205RH\205R\351\264L\313\304H\16\364\0B\363\0\247\363\0\345\363\0B\364"
  128. "H\13\5\0F\254\332.%\354y\344\216\17\220~\17\360\0\1\367\0\17`h\17\360\0\353\365\0!"
  129. "\361\0e\325(\17\20N\214\303JJ\264D\311\244>I\245\70\251\225\64\10\226\60H\226,\250\206("
  130. "\350\206$(\207\42g\207\36\247\207\32\347\207\26'x\26fx\22\207x\20\246x\16\346x\14%i"
  131. "\12&y\10ei\10ey\6\244i\6\244i\10\342i\250V\325P\255jU\65T{\325\372\243Z"
  132. "\324R\255\250\26\265P-j\241\32j\241ZS\63=Skz\244gj\246Gz\213\342\240\351\251\234"
  133. "\253\271\230M\351\220\17\20\224\216\311\234)jF\0G\240\367<\341\350\271\364\0\77\0z=\300\17\300"
  134. "Z\17\360\3\70\325\3\31\340B\25\37j\37\67r\237\267\5\67\276\322*\301\262*C\301*\361JN"
  135. "\67\37s\42'\363\61'r\62'r\62\37\364\61'\363A\37s\62\37sj>\350\363\377\377\377\377"
  136. "\177\365A\237G'\364\211N\346\204>\350\204>\350dN\350dN\350\324\234\320if\301 d\302 "
  137. "\341\342 \337\2!\134#!\332R)Vs\61S\243\61\316\323A\310\4R\301\65z\0\363z\200A"
  138. "z\200\77\0:=\300\37\240\264\36\340\7 \23\0H[:d\343\352\71 p\343\66l\364\2C\206"
  139. "dgv\67\63\64\213Vj\365l\64[\251\325l\245V\331\210\255\330([\261\21[\261\21[e#"
  140. "\67b+\66r#\266z\65\253vjV\355\320\320L\311\26\314\265\36\340\230\36@\215\36\0\225\36\300"
  141. "\240\36 \264\36 \270\236\213\311\20I_+\374;\351\31\340:\17 \64\17PF\212\204J\250tN"
  142. "\346T;\21\24\35j\240(\324@\21\324@\21\324@\21\324@\21\324\204\42\250\11EN\4\65\377\377"
  143. "\377\377\377\377w\242N\343D\235\306\211\234\306\211\62\221\23\61\221\23\61Q\246b\242\257T\221\254\221\21"
  144. "\241\20\372\250\316\2%Q\11\1J\227{\61\344\354\71\364\0\77\200\320=\300\17\200h\17\360\3\260\230"
  145. "\322\241\26\12\236\24*\36\323A\236\321a\236\317\201\232\216\241\226\215\261\226L\311\226K\331\22K\351\22"
  146. "J\371\22I\11\23H\31\23G)\223\6\61\23\6A\23\5Q\217DQ\23\4a\23\3i\23\3q"
  147. "\23\2y\23\302\210\217\1\211\23\301\220\223\0\231\217\300\240\37:\11\4\373\300\317\377\377\377\377\357N\335"
  148. "\7~\272\223\373\300\217;\271\17\374\270\17\374\270\223\373\270\23;\271\217;\261\223\373\270\23;\355>\32"
  149. "\0Ki\61r\362\352\331\42\134\244UXV\253~-\342\42\255\252E\134\244UZ\304EZ\304E\134"
  150. "\303E\134\303E\134\303E\134\303E\134\303EZ\304\65\134\244E\134\223\213\270\346\377\377\271\204k\326\32"
  151. "\256\251\65\134Sk\270Fk\270Fk\264Hkj\221\326\324\42,z\253JUT&eR\347\204L"
  152. "\211\264<\17\20@n\244N\0L\240Y.%\354\271\264\220\17p\200\17\340r\212\321f\310\242^\247"
  153. "\203XFtP\346dJeUD\305e>%V:\204V\66\304V\62\4W.dG*\244G&"
  154. "\344G\42$H cH\34\204H\30\244X\24\344H\24#I\20$I\16dI\14dY\10\244I"
  155. "\10\304\71\10\343I\6\42:\254\244J\254\223\225T\211uf%Ub\235\377\253\16\353\254J\252\303:"
  156. "U\207uTIuXGuTIuT\11\225TG\225Pi\324\62\255\223\32i\221\232\250\201\242'"
  157. "j*\246r\242h\251\6\23\0\0\0\0\4\377\377\0";
  158. /*================================================================================*/
  159. U8G2_ST75256_JLX256128_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 7, /* dc=*/ 6, /* reset=*/ 5); // MKR Zero, Enable U8g2 16 bit mode for this display
  160. /*================================================================================*/
  161. uint8_t test_i2c_address(uint8_t adr)
  162. {
  163. Wire.beginTransmission(adr);
  164. return Wire.endTransmission(false);
  165. }
  166. void system_test(void)
  167. {
  168. u8x8_SetFont(u8g2.getU8x8(), u8x8_font_amstrad_cpc_extended_r);
  169. u8x8_Draw1x2String(u8g2.getU8x8(), 0, 0, "Oli's Spur N Controller (2018)");
  170. u8x8_DrawString(u8g2.getU8x8(), 0, 3, "Teste I2C Weichen...");
  171. if ( test_i2c_address(17) == 0 )
  172. u8x8_DrawString(u8g2.getU8x8(), 26, 3, "Ok");
  173. else
  174. u8x8_DrawString(u8g2.getU8x8(), 26, 3, "Fehler");
  175. u8x8_DrawString(u8g2.getU8x8(), 0, 4, "Teste I2C Motortreiber...");
  176. if ( test_i2c_address(40) == 0 )
  177. u8x8_DrawString(u8g2.getU8x8(), 26, 4, "Ok");
  178. else
  179. u8x8_DrawString(u8g2.getU8x8(), 26, 4, "Fehler");
  180. /* wait for keypress */
  181. while( u8g2.getMenuEvent() == 0 )
  182. ;
  183. }
  184. void setup(void) {
  185. // MKR Zero Test Board
  186. u8g2.begin(/*Select=*/ 0, /*Right/Next=*/ 1, /*Left/Prev=*/ 2, /*Up=*/ 4, /*Down=*/ 3, /*Home/Cancel=*/ A6);
  187. u8g2.setFontMode(1);
  188. Wire.begin();
  189. system_test();
  190. }
  191. /*================================================================================*/
  192. #define LOG_BUF_LINES 7
  193. #define LOG_BUF_LINE_LEN 32
  194. #define LOG_FONT u8g2_font_lucasfont_alternate_tr
  195. #define LOG_FONT_LH 9
  196. char log_buf[LOG_BUF_LINES][LOG_BUF_LINE_LEN];
  197. uint8_t log_end = 0;
  198. //u8g2_font_u8glib_4_tr
  199. void draw_log(void)
  200. {
  201. uint8_t i;
  202. u8g2.setFont(LOG_FONT);
  203. for( i = 0; i < LOG_BUF_LINES; i++ )
  204. {
  205. u8g2.drawStr(160, 64+i*LOG_FONT_LH, log_buf[i]);
  206. }
  207. }
  208. void scroll_log(void)
  209. {
  210. uint8_t i;
  211. for( i = 1; i < LOG_BUF_LINES; i++ )
  212. {
  213. strcpy(log_buf[i-1], log_buf[i]);
  214. }
  215. log_buf[LOG_BUF_LINES-1][0] = '\0';
  216. }
  217. void log_line(const char *fmt, ...)
  218. {
  219. va_list va;
  220. va_start(va, fmt);
  221. vsnprintf(log_buf[log_end], LOG_BUF_LINE_LEN, fmt, va);
  222. log_buf[log_end][LOG_BUF_LINE_LEN-1] = '\0';
  223. va_end(va);
  224. if ( log_end < LOG_BUF_LINES-1 )
  225. {
  226. log_end++;
  227. }
  228. else
  229. {
  230. scroll_log();
  231. }
  232. }
  233. /*================================================================================*/
  234. struct menu_entry_type
  235. {
  236. const uint8_t *font;
  237. uint16_t icon;
  238. const char *name;
  239. };
  240. struct menu_state
  241. {
  242. int16_t menu_start; /* in pixel */
  243. int16_t frame_position; /* in pixel */
  244. uint8_t position; /* position, array index */
  245. };
  246. /*
  247. Icon configuration
  248. Width and height must match the icon font size
  249. GAP: Space between the icons
  250. BGAP: Gap between the display border and the cursor.
  251. */
  252. #define ICON_WIDTH 64
  253. #define ICON_HEIGHT 64
  254. #define ICON_GAP 8
  255. #define ICON_BGAP 16
  256. #define ICON_Y 64+ ICON_GAP
  257. struct menu_entry_type menu_entry_list[] =
  258. {
  259. { u8g2_font_open_iconic_other_8x_t, 66, "Weichen-Test"},
  260. { u8g2_font_open_iconic_embedded_8x_t, 67, "Motor Test"},
  261. { u8g2_font_open_iconic_embedded_8x_t, 66, "Gear Game"},
  262. { u8g2_font_open_iconic_embedded_8x_t, 68, "Home"},
  263. { u8g2_font_open_iconic_embedded_8x_t, 72, "Configuration"},
  264. { NULL, 0, NULL }
  265. };
  266. int8_t button_event = 0; // set this to 0, once the event has been processed
  267. void check_button_event(void)
  268. {
  269. if ( button_event == 0 )
  270. button_event = u8g2.getMenuEvent();
  271. }
  272. void main_menu_draw(struct menu_state *state)
  273. {
  274. int16_t x;
  275. uint8_t i;
  276. x = state->menu_start;
  277. i = 0;
  278. while( menu_entry_list[i].icon > 0 )
  279. {
  280. if ( x >= -ICON_WIDTH && x < u8g2.getDisplayWidth() )
  281. {
  282. u8g2.setFont(menu_entry_list[i].font);
  283. u8g2.drawGlyph(x, ICON_Y, menu_entry_list[i].icon );
  284. }
  285. i++;
  286. x += ICON_WIDTH + ICON_GAP;
  287. check_button_event();
  288. }
  289. //u8g2.drawFrame(state->frame_position-1, ICON_Y-ICON_HEIGHT-1, ICON_WIDTH+2, ICON_WIDTH+2);
  290. u8g2.drawFrame(state->frame_position-2, ICON_Y-ICON_HEIGHT-2, ICON_WIDTH+4, ICON_WIDTH+4);
  291. u8g2.drawFrame(state->frame_position-3, ICON_Y-ICON_HEIGHT-3, ICON_WIDTH+6, ICON_WIDTH+6);
  292. u8g2.drawFrame(state->frame_position-4, ICON_Y-ICON_HEIGHT-4, ICON_WIDTH+8, ICON_WIDTH+8);
  293. u8g2.drawFrame(state->frame_position-5, ICON_Y-ICON_HEIGHT-5, ICON_WIDTH+10, ICON_WIDTH+10);
  294. u8g2.drawFrame(state->frame_position-6, ICON_Y-ICON_HEIGHT-6, ICON_WIDTH+12, ICON_WIDTH+12);
  295. check_button_event();
  296. }
  297. void main_menu_to_right(struct menu_state *state)
  298. {
  299. if ( menu_entry_list[state->position+1].font != NULL )
  300. {
  301. if ( (int16_t)state->frame_position+ 2*(int16_t)ICON_WIDTH + (int16_t)ICON_BGAP < (int16_t)u8g2.getDisplayWidth() )
  302. {
  303. state->position++;
  304. state->frame_position += ICON_WIDTH + (int16_t)ICON_GAP;
  305. }
  306. else
  307. {
  308. state->position++;
  309. state->frame_position = (int16_t)u8g2.getDisplayWidth() - (int16_t)ICON_WIDTH - (int16_t)ICON_BGAP;
  310. state->menu_start = state->frame_position - state->position*((int16_t)ICON_WIDTH + (int16_t)ICON_GAP);
  311. }
  312. }
  313. }
  314. void main_menu_to_left(struct menu_state *state)
  315. {
  316. if ( state->position > 0 )
  317. {
  318. if ( (int16_t)state->frame_position >= (int16_t)ICON_BGAP+(int16_t)ICON_WIDTH+ (int16_t)ICON_GAP )
  319. {
  320. state->position--;
  321. state->frame_position -= ICON_WIDTH + (int16_t)ICON_GAP;
  322. }
  323. else
  324. {
  325. state->position--;
  326. state->frame_position = ICON_BGAP;
  327. state->menu_start = state->frame_position - state->position*((int16_t)ICON_WIDTH + (int16_t)ICON_GAP);
  328. }
  329. }
  330. }
  331. uint8_t towards_int16(int16_t *current, int16_t dest)
  332. {
  333. if ( *current < dest )
  334. {
  335. (*current)++;
  336. return 1;
  337. }
  338. else if ( *current > dest )
  339. {
  340. (*current)--;
  341. return 1;
  342. }
  343. return 0;
  344. }
  345. uint8_t towards(struct menu_state *current, struct menu_state *destination)
  346. {
  347. uint8_t r = 0;
  348. uint8_t i;
  349. for( i = 0; i < 10; i++ )
  350. {
  351. r |= towards_int16( &(current->frame_position), destination->frame_position);
  352. r |= towards_int16( &(current->menu_start), destination->menu_start);
  353. }
  354. return r;
  355. }
  356. struct menu_state current_state = { ICON_BGAP, ICON_BGAP, 0 };
  357. struct menu_state destination_state = { ICON_BGAP, ICON_BGAP, 0 };
  358. uint16_t main_menu(void)
  359. {
  360. button_event = 0;
  361. for(;;)
  362. {
  363. do
  364. {
  365. u8g2.firstPage();
  366. do
  367. {
  368. main_menu_draw(&current_state);
  369. u8g2.setFont(MENU_FONT);
  370. u8g2.setCursor((u8g2.getDisplayWidth()-u8g2.getStrWidth(menu_entry_list[destination_state.position].name))/2,u8g2.getDisplayHeight()-7);
  371. u8g2.print(menu_entry_list[destination_state.position].name);
  372. check_button_event();
  373. delay(20);
  374. } while( u8g2.nextPage() );
  375. if ( button_event == U8X8_MSG_GPIO_MENU_NEXT )
  376. main_menu_to_right(&destination_state);
  377. if ( button_event == U8X8_MSG_GPIO_MENU_PREV )
  378. main_menu_to_left(&destination_state);
  379. if ( button_event == U8X8_MSG_GPIO_MENU_SELECT )
  380. {
  381. button_event = 0;
  382. return destination_state.position;
  383. }
  384. if ( button_event > 0 ) // all known events are processed, clear event
  385. button_event = 0;
  386. } while ( towards(&current_state, &destination_state) );
  387. }
  388. }
  389. /*================================================================================*/
  390. int8_t track_bold1_idx = -1;
  391. int8_t track_bold2_idx = -1;
  392. int8_t track_switch[4] = { 0, 0, 0, 0 };
  393. int16_t track_switch_x[4][2] = { { 85, 85 }, { 9, 19 }, { 58, 58 }, { 105, 105 } };
  394. int16_t track_switch_y[4][2] = { { 0, 16 }, { 55, 55 }, { 104, 114 }, { 114, 123 } };
  395. /* sw=0..3, pos=0..1 */
  396. void set_swtich(uint8_t sw, uint8_t pos)
  397. {
  398. uint8_t error;
  399. sw &= 3;
  400. pos &= 1;
  401. Wire.beginTransmission(17); // switch controller
  402. error = Wire.endTransmission(false);
  403. if ( error == 0 )
  404. {
  405. Wire.beginTransmission(17); // switch controller
  406. Wire.write(0);
  407. Wire.write(sw*2+pos);
  408. Wire.endTransmission();
  409. log_line("I2C ok %d, sw=%d/%d", error, sw, pos);
  410. }
  411. else
  412. {
  413. log_line("I2C err %d, sw=%d/%d", error, sw, pos);
  414. }
  415. }
  416. void show_tracks(void)
  417. {
  418. int i;
  419. int16_t x, y;
  420. u8g2.firstPage();
  421. do
  422. {
  423. u8g2.setFont(track_font);
  424. for( i = 0; i < 6; i++ )
  425. {
  426. if ( i == track_bold1_idx || i == track_bold2_idx )
  427. {
  428. u8g2.drawGlyph(0,128, 'A'+i);
  429. }
  430. else
  431. {
  432. u8g2.drawGlyph(0,128, 'A'+6+i);
  433. }
  434. }
  435. check_button_event();
  436. for( i = 0; i < 4; i++ )
  437. {
  438. x = track_switch_x[i][track_switch[i]];
  439. y = track_switch_y[i][track_switch[i]];
  440. u8g2.setDrawColor(0);
  441. u8g2.drawBox(x, y, 6, 6);
  442. u8g2.setDrawColor(1);
  443. u8g2.drawBox(x+1, y+1, 4, 4);
  444. }
  445. /*
  446. for( i = 0; i < 4; i++ )
  447. {
  448. x = track_switch_x[i][1];
  449. y = track_switch_y[i][1];
  450. u8g2.setDrawColor(0);
  451. u8g2.drawBox(x, y, 6, 6);
  452. u8g2.setDrawColor(1);
  453. u8g2.drawBox(x+1, y+1, 4, 4);
  454. }
  455. */
  456. check_button_event();
  457. draw_log();
  458. } while( u8g2.nextPage() );
  459. }
  460. void track_switch_test(void)
  461. {
  462. track_bold1_idx = -1;
  463. track_bold2_idx = -1;
  464. button_event = 0;
  465. for(;;)
  466. {
  467. show_tracks();
  468. if ( button_event == U8X8_MSG_GPIO_MENU_NEXT )
  469. {
  470. track_switch[3] ^=1;
  471. set_swtich(3, track_switch[3]);
  472. }
  473. if ( button_event == U8X8_MSG_GPIO_MENU_PREV )
  474. {
  475. track_switch[1] ^=1;
  476. set_swtich(1, track_switch[1]);
  477. }
  478. if ( button_event == U8X8_MSG_GPIO_MENU_UP )
  479. {
  480. track_switch[0] ^=1;
  481. set_swtich(0, track_switch[0]);
  482. }
  483. if ( button_event == U8X8_MSG_GPIO_MENU_DOWN )
  484. {
  485. track_switch[2] ^=1;
  486. set_swtich(2, track_switch[2]);
  487. }
  488. if ( button_event == U8X8_MSG_GPIO_MENU_HOME )
  489. return;
  490. button_event = 0;
  491. }
  492. }
  493. /*================================================================================*/
  494. /* sw=0..3, pos=0..1 */
  495. void set_motor_speed(uint8_t motor, uint8_t speed)
  496. {
  497. uint8_t error;
  498. Wire.beginTransmission(40+motor); // motor controller
  499. error = Wire.endTransmission(false);
  500. if ( error == 0 )
  501. {
  502. Wire.beginTransmission(40+motor); // switch controller
  503. Wire.write(0);
  504. Wire.write(speed);
  505. Wire.endTransmission();
  506. log_line("I2C ok %d, m%d/%d", error, motor, speed);
  507. }
  508. else
  509. {
  510. log_line("I2C err %d, m%d/%d", error, motor, speed);
  511. }
  512. }
  513. uint16_t motor_noise_raw;
  514. uint16_t motor_noise_filt;
  515. uint16_t motor_speed_raw; // lower is faster
  516. uint16_t motor_speed_filt; // lower is faster
  517. void read_motor_data(uint8_t motor)
  518. {
  519. uint8_t error;
  520. Wire.beginTransmission(40+motor); // motor controller
  521. error = Wire.endTransmission(false);
  522. if ( error == 0 )
  523. {
  524. Wire.beginTransmission(40+motor); // motor controller
  525. Wire.write(1); // reset to index 0
  526. Wire.endTransmission();
  527. Wire.requestFrom(40+motor, 9);
  528. Wire.read(); // dummy read
  529. motor_noise_raw = Wire.read();
  530. motor_noise_raw |= (((uint16_t)Wire.read())<<8);
  531. motor_noise_filt = Wire.read();
  532. motor_noise_filt |= (((uint16_t)Wire.read())<<8);
  533. motor_speed_raw = Wire.read();
  534. motor_speed_raw |= (((uint16_t)Wire.read())<<8);
  535. motor_speed_filt = Wire.read();
  536. motor_speed_filt |= (((uint16_t)Wire.read())<<8);
  537. }
  538. }
  539. void show_motor_set_speed(uint8_t motor, uint8_t speed)
  540. {
  541. u8g2_uint_t y;
  542. u8g2_uint_t o = 18;
  543. button_event = 0;
  544. read_motor_data(motor);
  545. u8g2.firstPage();
  546. do
  547. {
  548. check_button_event();
  549. u8g2.setFont(MENU_FONT);
  550. u8g2.drawBox(5, 0, 2, 128);
  551. u8g2.drawHLine(0, 127-speed/2, 12);
  552. check_button_event();
  553. y = (uint8_t)(((uint16_t)(128-o)*(uint16_t)(127-speed/2))>>7);
  554. u8g2.setCursor(14, y+o);
  555. u8g2.print(speed);
  556. check_button_event();
  557. u8g2.setCursor(60, 20);
  558. u8g2.print("Motor: ");
  559. u8g2.print(motor);
  560. u8g2.setFont(SUBMENU_FONT);
  561. u8g2.setCursor(60, 50);
  562. u8g2.print("Noise: ");
  563. u8g2.setCursor(120, 50);
  564. u8g2.print(motor_noise_filt);
  565. u8g2.setCursor(60, 70);
  566. u8g2.print("Speed: ");
  567. u8g2.setCursor(120, 70);
  568. u8g2.print(motor_speed_filt);
  569. draw_log();
  570. } while( u8g2.nextPage() );
  571. }
  572. void motor_speed_test(void)
  573. {
  574. uint8_t speed[6] = { 128, 128, 128, 128, 128, 128 };
  575. uint8_t step = 10;
  576. uint8_t motor = 0;
  577. for( motor = 0; motor < 6; motor++)
  578. set_motor_speed(motor, speed[motor]);
  579. motor = 0;
  580. for(;;)
  581. {
  582. show_motor_set_speed(motor, speed[motor]);
  583. if ( button_event == U8X8_MSG_GPIO_MENU_UP )
  584. {
  585. if ( speed[motor] < 128 && speed[motor]+step > 128 )
  586. speed[motor] = 128;
  587. else if ( speed[motor] < 255-step )
  588. speed[motor] += step;
  589. else
  590. speed[motor] = 255;
  591. set_motor_speed(motor, speed[motor]);
  592. }
  593. if ( button_event == U8X8_MSG_GPIO_MENU_DOWN )
  594. {
  595. if ( speed[motor] > 128 && speed[motor]-step < 128 )
  596. speed[motor] = 128;
  597. else if ( speed[motor] >= step )
  598. speed[motor] -=step;
  599. else
  600. speed[motor] = 0;
  601. set_motor_speed(motor, speed[motor]);
  602. }
  603. if ( button_event == U8X8_MSG_GPIO_MENU_SELECT )
  604. {
  605. motor++;
  606. if ( motor >= 6 )
  607. motor = 0;
  608. set_motor_speed(motor, speed[motor]);
  609. }
  610. if ( button_event == U8X8_MSG_GPIO_MENU_HOME )
  611. {
  612. button_event = 0;
  613. return;
  614. }
  615. button_event = 0;
  616. }
  617. }
  618. /*================================================================================*/
  619. void loop(void)
  620. {
  621. uint16_t pos;
  622. pos = main_menu();
  623. if ( pos == 0 )
  624. track_switch_test();
  625. else if ( pos == 1 )
  626. motor_speed_test();
  627. }