ui.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (C) 2023 Daniele Lacamera <root@danielinux.net>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "stm32f7_ltdc.h"
  22. #include "ui.h"
  23. #include "systick.h"
  24. #include "button.h"
  25. static uint8_t menu_on = 0;
  26. uint8_t ui_menu_is_on(void)
  27. {
  28. return menu_on;
  29. }
  30. void ui_menu(uint8_t onoff)
  31. {
  32. menu_on = onoff;
  33. }
  34. #define BUTTON_RIGHT 1
  35. #define BUTTON_LEFT 2
  36. #define BUTTON_UP 0
  37. #define BUTTON_DOWN 3
  38. #define BUTTON_IN 7
  39. #define BUTTON_OUT 6
  40. #define BUTTON_FIRE 5
  41. #define NO_CHANNEL (-1)
  42. enum control_mode_e {
  43. CTL_RUN,
  44. CTL_STOP,
  45. CTL_PAUSE,
  46. CTL_TRIGGER,
  47. CTL_MAX
  48. };
  49. int res[] = {
  50. 1000000 , 500000 , 200000 , 100000 , 50000 , 20000 , 10000 , 5000 , 2000 , 1000 , 500 , 200 , 100 , 50 , 20 , 0
  51. };
  52. #define FONT_HEIGHT 8
  53. #define FONT_WIDTH 8
  54. extern const unsigned char fb_font[256][FONT_WIDTH];
  55. uint8_t *ui_get_screen(int layer)
  56. {
  57. if (layer == 0)
  58. return (uint8_t *)Screen_address;
  59. else
  60. return (uint8_t *)Overlay_address;
  61. }
  62. void ui_draw_h_segment(int layer, uint8_t color, uint32_t row, uint32_t start, uint32_t len)
  63. {
  64. uint32_t i;
  65. uint32_t pos = row * xres + start;
  66. uint8_t *screen = ui_get_screen(layer);
  67. for (i = 0; i < len; i++) {
  68. screen[pos + i] = color;
  69. }
  70. }
  71. void ui_draw_v_segment(int layer, uint8_t color, uint32_t col, uint32_t start, uint32_t len)
  72. {
  73. uint32_t i;
  74. uint32_t pos = start * xres + col;
  75. uint8_t *screen = ui_get_screen(layer);
  76. for (i = 0; i < len; i++) {
  77. screen[pos + i * xres] = color;
  78. }
  79. }
  80. void ui_fill_area(int layer, uint8_t color, uint32_t x, uint32_t y, uint32_t x1, uint32_t y1)
  81. {
  82. int i;
  83. for (i = y; i < y1; i++) {
  84. ui_draw_h_segment(layer, color, i, x, x1 - x);
  85. }
  86. }
  87. void ui_text_at(int layer, uint8_t color, uint32_t x, uint32_t y, char *text)
  88. {
  89. uint32_t i, j;
  90. char fc;
  91. const uint8_t *render;
  92. uint32_t pos = y * xres + x;
  93. uint8_t *screen = ui_get_screen(layer);
  94. while(text[0]) {
  95. uint32_t cur = pos;
  96. fc = text[0];
  97. render = fb_font[(int)fc];
  98. for (i = 0; i < FONT_HEIGHT; i++) {
  99. for (j = 0; j < FONT_WIDTH; j++) {
  100. int right_shift = (FONT_HEIGHT - 1) - j;
  101. cur = pos + i * xres + j;
  102. if (render[i] & (1 << (7 - j))) {
  103. screen[cur] = color;
  104. }
  105. }
  106. }
  107. pos += FONT_WIDTH;
  108. text++;
  109. }
  110. }
  111. void ui_selected_text_at(int layer, uint8_t color, uint8_t highlight, uint32_t x, uint32_t y, char *text)
  112. {
  113. ui_fill_area(layer, highlight, x, y, x + (FONT_WIDTH * strlen(text)), y + FONT_HEIGHT);
  114. ui_text_at(layer, color, x, y, text);
  115. }
  116. void image_at(int layer, const uint8_t *img, uint32_t x, uint32_t y, uint32_t size_x, uint32_t size_y)
  117. {
  118. uint32_t i, j;
  119. uint32_t pos = y * xres + x;
  120. uint8_t *screen = ui_get_screen(layer);
  121. for (i = 0; i < size_y; i++) {
  122. for (j = 0; j < size_x; j++) {
  123. screen[pos + i * xres + j] = img[i * size_x + j];
  124. }
  125. }
  126. }
  127. /**** ****/
  128. static char _ui_printn_res[20];
  129. char *ui_printn(uint32_t n)
  130. {
  131. int i = 0;
  132. uint32_t div = 1000000000;
  133. int started = 0;
  134. while (div >= 10) {
  135. if ((n >= div) || started) {
  136. _ui_printn_res[i++] = '0' + n / div;
  137. n = n % div;
  138. started++;
  139. }
  140. div /= 10;
  141. }
  142. _ui_printn_res[i++] = '0' + n;
  143. _ui_printn_res[i] = '\0';
  144. return (char *)(&_ui_printn_res[0]);
  145. }
  146. char *ui_printn2(uint32_t n)
  147. {
  148. _ui_printn_res[0] = '`';
  149. _ui_printn_res[1] = '0' + (n % 10);
  150. if (n > 9)
  151. _ui_printn_res[0] = '0' + (n / 10);
  152. _ui_printn_res[2] = 0;
  153. return _ui_printn_res;
  154. }
  155. char *ui_printhex(uint8_t n)
  156. {
  157. uint8_t a = (n & 0xF0) >> 4;
  158. uint8_t b = (n & 0x0F);
  159. if (a < 10)
  160. _ui_printn_res[0] = a + '0';
  161. else
  162. _ui_printn_res[0] = (a - 0xa) + 'A';
  163. if (b < 10)
  164. _ui_printn_res[1] = b + '0';
  165. else
  166. _ui_printn_res[1] = (b - 0xa) + 'A';
  167. _ui_printn_res[2] = '\0';
  168. return _ui_printn_res;
  169. }
  170. char *ui_printhex1(uint8_t n)
  171. {
  172. uint8_t a = (n & 0x0F);
  173. if (a < 10)
  174. _ui_printn_res[0] = a + '0';
  175. else
  176. _ui_printn_res[0] = (a - 0xa) + 'A';
  177. _ui_printn_res[1] = '\0';
  178. return _ui_printn_res;
  179. }
  180. static void ui_switch_screen(int newscreen);
  181. static void force_redraw(void)
  182. {
  183. struct screen *s = screen_get_focus();
  184. if (s && s->draw)
  185. s->draw();
  186. }
  187. int ui_process_button_pressed(void)
  188. {
  189. int i;
  190. for (i = 0; i < N_BUTTONS; i++) {
  191. if (Input_status.b[i].pressed == 1) {
  192. Input_status.b[i].pressed = 0;
  193. return i;
  194. }
  195. }
  196. return -1;
  197. }
  198. void ui_draw_blocks(void)
  199. {
  200. ui_fill_area(0, BLACK, 420, 90, 440, 120);
  201. ui_fill_area(0, DARKRED, 420, 100, 440, 110);
  202. }
  203. struct point {
  204. int x, y;
  205. };
  206. const struct point pot[] = {
  207. {-9, 17}, {-10, 17}, {-11, 16}, {-12, 16}, {-13, 15}, {-14, 14}, {-15, 13}, {-16, 12}, {-17, 10}, {-18, 8}, {-19, 6}, {-20, 0},
  208. {-19, -6}, {-18, -8}, {-16, -12}, {-15, -13}, {-14, -14}, {-13, -15}, {-12, -16}, {-11, -16}, {-10, -17}, {-9, -17}, {-8, -18}, {-7, -18}, {-6, -19}, {-5, -19}, {-4, -19}, {-3, -19}, {-2, -19}, {-1, -19}, {0, -20},
  209. {1, -19}, {2, -19}, {3, -19}, {4, -19}, {5, -19}, {6, -19}, {7, -18}, {8, -18}, {9, -17}, {10, -17}, {11, -16}, {12, -16}, {13, -15}, {14, -14}, {15, -13}, {16, -12}, {17, -10}, {18, -8}, {19, -6}, {20, 0},
  210. {19, 6}, {18, 8}, {17, 10}, {16, 12}, {15, 13}, {14, 14}, {13, 15}, {12, 16}, {11, 16}, {10, 17}, {9, 17},
  211. };
  212. static void ui_run(uint16_t event)
  213. {
  214. if (event & EV_BUTTON) {
  215. int button = ui_process_button_pressed();
  216. switch(button) {
  217. case 7:
  218. if (menu_on) {
  219. menu_on = 0;
  220. force_redraw();
  221. } else {
  222. menu_on = 1;
  223. ui_redraw(EV_NONE);
  224. }
  225. break;
  226. }
  227. clear_event(EV_BUTTON);
  228. }
  229. }
  230. static struct screen *ui_get_screen_by_id(int id)
  231. {
  232. struct screen *s = screens();
  233. while(s) {
  234. if (id == s->id) {
  235. return s;
  236. }
  237. s = s->next;
  238. }
  239. return NULL;
  240. }
  241. void ui_switch_screen(int newscreen)
  242. {
  243. struct screen *s = ui_get_screen_by_id(newscreen);
  244. if (s)
  245. screen_set_focus(s);
  246. }
  247. void ui_menu_redraw(uint16_t event)
  248. {
  249. static int selected;
  250. int button;
  251. int i;
  252. int n;
  253. n = screen_n_screens();
  254. if (event & EV_BUTTON) {
  255. button = ui_process_button_pressed();
  256. switch(button) {
  257. case 1:
  258. selected--;
  259. if (selected < 0)
  260. selected = n - 1;
  261. ui_redraw(EV_NONE);
  262. break;
  263. case 3:
  264. selected++;
  265. if (selected > n - 1)
  266. selected = 0;
  267. ui_redraw(EV_NONE);
  268. break;
  269. case 7:
  270. menu_on = 0;
  271. force_redraw();
  272. break;
  273. default:
  274. menu_on = 0;
  275. ui_switch_screen(selected);
  276. }
  277. return;
  278. }
  279. ui_fill_area(0, BLACK, 400, 200, 480, 240);
  280. for (i = 0; i < n; i++) {
  281. if (i == selected) {
  282. ui_fill_area(0, WHITE, 400, 200 + i * 10, 480, 210 + i * 10);
  283. ui_text_at(0, PURPLE, 400, 200 + i*10, ui_get_screen_by_id(i)->name);
  284. } else {
  285. ui_text_at(0, WHITE, 400, 200 + i*10, ui_get_screen_by_id(i)->name);
  286. }
  287. }
  288. }
  289. void ui_redraw(uint16_t event)
  290. {
  291. struct screen *sc;
  292. if (menu_on) {
  293. ui_menu_redraw(event);
  294. } else {
  295. sc = screen_get_focus();
  296. if (sc && sc->draw)
  297. sc->draw();
  298. }
  299. }
  300. void ui_init(void)
  301. {
  302. int i;
  303. force_redraw();
  304. }
  305. struct task task_ui = {
  306. .init = ui_init,
  307. .run = ui_run,
  308. .events = EV_BUTTON,
  309. .name = "ui"
  310. };
  311. void ui_setup(void)
  312. {
  313. register_task(&task_ui);
  314. }