main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "u8g2.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "mui.h"
  6. #include "mui_u8g2.h"
  7. /*=================================================*/
  8. /* global variables */
  9. u8g2_t u8g2;
  10. mui_t ui;
  11. uint16_t menu_get_cnt(void *data) {
  12. return 10; /* number of menu entries */
  13. }
  14. const char *menu_get_str(void *data, uint16_t index) {
  15. static const char *menu[] = {
  16. MUI_1 "Goto Main Menu",
  17. MUI_10 "Enter a number",
  18. MUI_11 "Parent/Child Selection",
  19. MUI_13 "Checkbox",
  20. MUI_14 "Radio Selection",
  21. MUI_15 "Text Input",
  22. MUI_16 "Single Line Selection",
  23. MUI_17 "List Line Selection",
  24. MUI_18 "Parent/Child List",
  25. MUI_20 "Array Edit",
  26. };
  27. return menu[index];
  28. }
  29. uint16_t selection = 0;
  30. uint8_t mui_hrule(mui_t *ui, uint8_t msg)
  31. {
  32. u8g2_t *u8g2 = mui_get_U8g2(ui);
  33. switch(msg)
  34. {
  35. case MUIF_MSG_DRAW:
  36. u8g2_DrawHLine(u8g2, 0, mui_get_y(ui), u8g2_GetDisplayWidth(u8g2));
  37. break;
  38. }
  39. return 0;
  40. }
  41. uint8_t my_u16_list_goto_w1_pi(mui_t *ui, uint8_t msg)
  42. {
  43. uint8_t arg = ui->arg; /* arg provided with MUI_XYA() */
  44. switch(msg)
  45. {
  46. case MUIF_MSG_EVENT_NEXT:
  47. if ( arg+1 == ui->form_scroll_visible )
  48. {
  49. if ( ui->form_scroll_visible + ui->form_scroll_top < ui->form_scroll_total )
  50. {
  51. ui->form_scroll_top++;
  52. return 1;
  53. }
  54. else
  55. {
  56. /* no wrap around to the top */
  57. return 1;
  58. /*
  59. ui->form_scroll_top = 0;
  60. */
  61. }
  62. }
  63. break;
  64. case MUIF_MSG_EVENT_PREV:
  65. if ( arg == 0 ) /* only if we are at the topmost field */
  66. {
  67. if ( ui->form_scroll_top > 0 )
  68. {
  69. ui->form_scroll_top--;
  70. return 1;
  71. }
  72. else
  73. {
  74. /* no wrap around to the button */
  75. return 1;
  76. /*
  77. if ( ui->form_scroll_total > ui->form_scroll_visible )
  78. {
  79. ui->form_scroll_top = ui->form_scroll_total - ui->form_scroll_visible;
  80. }
  81. else
  82. {
  83. ui->form_scroll_top = 0;
  84. }
  85. */
  86. }
  87. }
  88. break;
  89. default:
  90. return mui_u8g2_u16_list_goto_w1_pi(ui, msg);
  91. }
  92. return 0;
  93. }
  94. muif_t muif_list[] MUI_PROGMEM = {
  95. MUIF_U8G2_FONT_STYLE(0, u8g2_font_helvR08_tr),
  96. MUIF_LABEL(mui_u8g2_draw_text),
  97. /* horizontal line (hrule) */
  98. MUIF_RO("HR", mui_hrule),
  99. MUIF_U8G2_U16_LIST("ID", &selection, NULL, menu_get_str, menu_get_cnt, my_u16_list_goto_w1_pi)
  100. };
  101. fds_t fds[] MUI_PROGMEM =
  102. MUI_FORM(1)
  103. MUI_STYLE(0)
  104. MUI_LABEL(5,10, "Issue 2597")
  105. MUI_XY("HR", 0, 13)
  106. MUI_XYA("ID", 5, 25, 0)
  107. MUI_XYA("ID", 5, 37, 1)
  108. MUI_XYA("ID", 5, 49, 2)
  109. MUI_XYA("ID", 5, 61, 3)
  110. ;
  111. int screenshot_n = 0;
  112. void do_screenshot(void)
  113. {
  114. char s[4096];
  115. u8x8_SaveBitmapTGA(u8g2_GetU8x8(&u8g2), "screenshot.tga");
  116. sprintf( s,
  117. "convert -border 4 -bordercolor 'rgb(255,190,40)'"
  118. " -fill 'rgb(255,170,0)' -opaque white"
  119. " -filter point -resize 200%%"
  120. " screenshot.tga pic%04d.png", screenshot_n);
  121. system(s);
  122. screenshot_n++;
  123. /*
  124. gif animation:
  125. convert -delay 40 -loop 0 pic*.png animation.gif
  126. */
  127. }
  128. int main(void)
  129. {
  130. int x, y;
  131. int k;
  132. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  133. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  134. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  135. u8x8_ConnectBitmapToU8x8(u8g2_GetU8x8(&u8g2)); /* connect to bitmap */
  136. u8g2_SetFont(&u8g2, u8g2_font_6x10_tr);
  137. u8g2_SetFontMode(&u8g2, 1);
  138. mui_Init(&ui, &u8g2, fds, muif_list, sizeof(muif_list)/sizeof(muif_t));
  139. mui_GotoForm(&ui, 1, 0);
  140. x = 4; // use as height for the box
  141. y = 0;
  142. for(;;)
  143. {
  144. u8g2_SetFontRefHeightExtendedText(&u8g2);
  145. u8g2_FirstPage(&u8g2);
  146. do
  147. {
  148. mui_Draw(&ui);
  149. } while( u8g2_NextPage(&u8g2) );
  150. do_screenshot();
  151. // printf("mui_GetCurrentCursorFocusPosition=%d\n", mui_GetCurrentCursorFocusPosition(&ui));
  152. do
  153. {
  154. k = u8g_sdl_get_key();
  155. } while( k < 0 );
  156. if ( k == 273 ) y -= 1;
  157. if ( k == 274 ) y += 1;
  158. if ( k == 276 ) x -= 1;
  159. if ( k == 275 ) x += 1;
  160. /*
  161. if ( k == 'e' ) y -= 1;
  162. if ( k == 'x' ) y += 1;
  163. if ( k == 's' ) x -= 1;
  164. if ( k == 'd' ) x += 1;
  165. */
  166. if ( k == 'q' ) break;
  167. if ( k == 'n' )
  168. {
  169. mui_NextField(&ui);
  170. }
  171. if ( k == 'p' )
  172. {
  173. mui_PrevField(&ui);
  174. }
  175. if ( k == 's' )
  176. {
  177. mui_SendSelect(&ui);
  178. }
  179. if ( k == 't' )
  180. {
  181. puts("screenshot");
  182. do_screenshot();
  183. }
  184. if ( x < 0 )
  185. x = 0;
  186. }
  187. return 0;
  188. }