ui.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * (c) danielinux 2019
  3. *
  4. * GPLv.2
  5. *
  6. * See LICENSE for details
  7. */
  8. #include <stdint.h>
  9. #include "system.h"
  10. #include "display.h"
  11. #include "systick.h"
  12. #include "button.h"
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include "ui.h"
  17. #include "timer.h"
  18. #include "led.h"
  19. /* Uncomment for device initialization */
  20. //#define DEVICE_INITIALIZATION
  21. static const char welcome_message0[]= " Waveblender ";
  22. static const char welcome_message1[]= "(c) danielinux ";
  23. static const char welcome_message2[]= " GPL v.2 ";
  24. static const char welcome_message3[]= "src: wb.vado.li";
  25. static uint32_t last_action = 0;
  26. static uint32_t offset = 0;
  27. #define ST_LOCKED 0
  28. #define ST_PIN_ENTRY 1
  29. #define ST_UNLOCKED 2
  30. #define MAX_PASSWORD 32
  31. static int fsm_state = ST_LOCKED;
  32. static int sleeping = 0;
  33. static char password[MAX_PASSWORD] = {};
  34. static int pin_idx = 0;
  35. #define SLEEP_TIME (5000)
  36. static void ui_sleep(void)
  37. {
  38. display_scroll(NULL, 0);
  39. display_clear(NULL);
  40. sleeping = 1;
  41. }
  42. static int ui_wakeup(void)
  43. {
  44. last_action = jiffies;
  45. if (sleeping) {
  46. sleeping = 0;
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. void ui_msg(const char *txt)
  52. {
  53. ui_wakeup();
  54. display_scroll(NULL, 0);
  55. display_clear(NULL);
  56. display_text(6, txt);
  57. }
  58. static uint8_t display_cur_v = 0;
  59. static uint8_t menu_selection = 0;
  60. void ui_menu_autoscroll(void)
  61. {
  62. int lines = CurrentMenu->entry_n;
  63. if ((lines < 5) || (menu_selection < 2))
  64. display_cur_v = 0;
  65. else if (menu_selection > lines - 2)
  66. display_cur_v = 0x40 - ((lines - 4) * 8);
  67. else
  68. display_cur_v = 0x40 - ((menu_selection - 2) * 8);
  69. display_scroll(NULL, display_cur_v);
  70. }
  71. extern struct display_menu MainMenu;
  72. static void ui_display_menu_refresh(void)
  73. {
  74. const struct display_menu *menu = CurrentMenu;
  75. uint8_t vline;
  76. int i;
  77. char txt[16];
  78. display_scroll(NULL, display_cur_v);
  79. display_clear(NULL);
  80. if (menu->render)
  81. menu->render(menu);
  82. for (i = 0; i < menu->entry_n; i++) {
  83. vline = i + 4;
  84. if (vline > 7)
  85. vline -= 8;
  86. switch (menu->entry[i].type) {
  87. case ENTRY_TYPE_TEXT:
  88. strncpy(txt, menu->entry[i].label, 15);
  89. break;
  90. case ENTRY_TYPE_HEX:
  91. {
  92. int x = *menu->entry[i].var ;
  93. int j;
  94. strncpy(txt, menu->entry[i].label, 11);
  95. for (j = strlen(menu->entry[i].label); j < 12; j++)
  96. txt[j] = ' ';
  97. if (x > 0xFF) {
  98. if (((x >> 8) & 0x0F) > 9)
  99. txt[12] = (((x >> 8) & 0x0F) - 10) + 'A';
  100. else
  101. txt[12] = ((x >> 8) & 0x0F) + '0';
  102. } else
  103. txt[12] = ' ';
  104. if (x > 0x0F) {
  105. if (((x >> 4) & 0x0F) > 9)
  106. txt[13] = (((x >> 4) & 0x0F) - 10) + 'A';
  107. else
  108. txt[13] = ((x >> 4) & 0x0F) + '0';
  109. } else
  110. txt[13] = ' ';
  111. if ((x & 0x0F) > 9)
  112. txt[14] = ((x & 0x0F) - 10) + 'A';
  113. else
  114. txt[14] = (x & 0x0F) + '0';
  115. }
  116. break;
  117. case ENTRY_TYPE_BOOL:
  118. {
  119. int x = *menu->entry[i].var ;
  120. int j;
  121. strncpy(txt, menu->entry[i].label, 10);
  122. for (j = strlen(menu->entry[i].label); j < 12; j++)
  123. txt[j] = ' ';
  124. if (x)
  125. strcpy(txt + 10, "[ ON]");
  126. else
  127. strcpy(txt + 10, "[OFF]");
  128. }
  129. break;
  130. case ENTRY_TYPE_DEC:
  131. {
  132. int x = *menu->entry[i].var ;
  133. int j;
  134. strncpy(txt, menu->entry[i].label, 11);
  135. for (j = strlen(menu->entry[i].label); j < 12; j++)
  136. txt[j] = ' ';
  137. if (x > 99) {
  138. txt[12] = (x / 100) + '0';
  139. } else
  140. txt[12] = ' ';
  141. if (x > 9) {
  142. txt[13] = ((x % 100)/10) + '0';
  143. } else
  144. txt[13] = ' ';
  145. txt[14] = (x % 10) + '0';
  146. }
  147. break;
  148. }
  149. if (i == menu_selection)
  150. display_text_inverse(vline, txt);
  151. else
  152. display_text(vline, txt);
  153. }
  154. WFI();
  155. }
  156. int ui_get_menu_selection(void)
  157. {
  158. return menu_selection;
  159. }
  160. void ui_display_menu(const struct display_menu *menu)
  161. {
  162. uint8_t i;
  163. display_clear(NULL);
  164. menu_selection = 0;
  165. display_cur_v = 0;
  166. CurrentMenu = (struct display_menu *)menu;
  167. ui_display_menu_refresh();
  168. }
  169. void ui_button_press(uint8_t b, int hold)
  170. {
  171. int n = menu_selection;
  172. struct display_menu_entry e;
  173. switch (b) {
  174. case 'U':
  175. if (menu_selection > 0) {
  176. menu_selection--;
  177. ui_menu_autoscroll();
  178. ui_display_menu_refresh();
  179. }
  180. break;
  181. case 'D':
  182. if (menu_selection < CurrentMenu->entry_n - 1) {
  183. menu_selection++;
  184. ui_menu_autoscroll();
  185. ui_display_menu_refresh();
  186. }
  187. break;
  188. default:
  189. e = CurrentMenu->entry[n];
  190. int changed = 0;
  191. /* Default actions for '-', '+', '*' */
  192. if (e.var && (e.type == ENTRY_TYPE_BOOL)) {
  193. if ((*e.var) && ((b == '-') || (b == '*'))) {
  194. *e.var = 0;
  195. changed = 1;
  196. } else if ((!(*e.var)) && ((b == '+') || (b == '*'))) {
  197. *e.var = 1;
  198. changed = 1;
  199. }
  200. } else if ((e.min != e.max) && e.var && ((e.type == ENTRY_TYPE_DEC) || (e.type == ENTRY_TYPE_DEC))) {
  201. if ((*e.var > e.min) && (b == '-')) {
  202. *e.var -= e.step;
  203. changed = 1;
  204. } else if ((*e.var < e.max) && (b == '+')) {
  205. *e.var += e.step;
  206. changed = 1;
  207. }
  208. }
  209. /* Custom actions for '-', '+', '*' (after changes are applied) */
  210. if (b != 0 && e.action) {
  211. e.action(&e, b);
  212. changed = 1;
  213. }
  214. if (changed) {
  215. ui_display_menu_refresh();
  216. }
  217. }
  218. }
  219. void ui_init(void)
  220. {
  221. uint32_t now;
  222. int i;
  223. display_scroll(NULL, 0x3f);
  224. display_text(0, welcome_message0);
  225. display_text(1, welcome_message1);
  226. display_text(2, welcome_message2);
  227. display_text(3, welcome_message3);
  228. now = jiffies;
  229. for (i = 0x3f; i >= 0x20; i--) {
  230. display_scroll(NULL, i);
  231. while ((jiffies - now) < 20)
  232. WFI();
  233. now = jiffies;
  234. }
  235. for (i = display_getcontrast(NULL); i >= 0; i--) {
  236. display_setcontrast(NULL, i);
  237. while ((jiffies - now) < 16)
  238. WFI();
  239. now = jiffies;
  240. }
  241. display_scroll(NULL, display_cur_v);
  242. display_clear(NULL);
  243. display_setcontrast(NULL, 0xcf);
  244. ui_display_menu(CurrentMenu);
  245. now = jiffies;
  246. for (i = 1; i < 9; i++) {
  247. led_beat(i);
  248. while((jiffies - now) < 100)
  249. WFI();
  250. now = jiffies;
  251. }
  252. for (i = 8; i > 0; i--) {
  253. led_beat(i);
  254. while((jiffies - now) < 100)
  255. WFI();
  256. now = jiffies;
  257. }
  258. led_beat(0);
  259. }
  260. void ui_keepalive(uint32_t timeslice)
  261. {
  262. }