main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. uint8_t var1 = '0';
  12. uint8_t var2 = '0';
  13. uint8_t var3 = '0';
  14. uint8_t var4 = '0';
  15. /* hb test: github.com/olikraus/u8g2/issues/2656 */
  16. static const unsigned char hb_null[] U8X8_PROGMEM = { 0x00, 0x00 };
  17. static const unsigned char hb_abc[] U8X8_PROGMEM = {
  18. 0x00, 'a', 0x00, 0x00,
  19. 0x00, 'b', 0x08, 0x00,
  20. 0x00, 'c', 0x08, 0x00,
  21. 0x00, 0x00 // end of binary
  22. };
  23. static const unsigned char teststring[] U8X8_PROGMEM = {
  24. 0x09, 0x28, 0x00, 0x00, // u8g2_DrawGlyph(&u8g2, 0, 0, 2344);
  25. 0x09, 0x2e, 0x10, 0x00, // u8g2_DrawGlyph(&u8g2, 16, 0, 2350);
  26. 0x09, 0x38, 0x10, 0x00, // u8g2_DrawGlyph(&u8g2, 32, 0, 2360);
  27. 0x09, 0x4d, 0x00, 0x00, // u8g2_DrawGlyph(&u8g2, 32, 0, 2381);
  28. 0x09, 0x24, 0x10, 0x00, // u8g2_DrawGlyph(&u8g2, 48, 0, 2340);
  29. 0x09, 0x47, 0x00, 0x00, // u8g2_DrawGlyph(&u8g2, 48, 0, 2375);
  30. 0x00, 0x00 // end of binary
  31. };
  32. uint8_t mui_u8g2_draw_hb(mui_t *ui, uint8_t msg)
  33. {
  34. if ( msg == MUIF_MSG_DRAW )
  35. {
  36. const unsigned char *data = hb_null;
  37. const uint8_t *old_font = mui_get_U8g2(ui)->font; // backup the current font
  38. switch(mui_get_arg(ui))
  39. {
  40. case 123:
  41. u8g2_SetFont(mui_get_U8g2(ui), u8g2_font_unifont_tr); // assign the font,which is used for the hb data
  42. data = hb_abc;
  43. break; // MUI_XYA(x, y, 123)
  44. case 235:
  45. u8g2_SetFont(mui_get_U8g2(ui), u8g2_font_unifont_t_devanagari); // assign the font,which is used for the hb data
  46. data = teststring;
  47. break; // MUI_XYA(x, y, 235)
  48. }
  49. u8g2_DrawHB(mui_get_U8g2(ui), mui_get_x(ui), mui_get_y(ui), data); // draw the HB data
  50. u8g2_SetFont(mui_get_U8g2(ui), old_font); // restore the previous font
  51. }
  52. return 0;
  53. }
  54. muif_t muif_list[] MUI_PROGMEM = {
  55. MUIF_U8G2_LABEL(),
  56. MUIF_U8G2_FONT_STYLE(0, u8g2_font_5x8_tr),
  57. MUIF_VARIABLE("B1",&var1,mui_u8g2_u8_char_wm_mud_pi),
  58. MUIF_VARIABLE("B2",&var2,mui_u8g2_u8_char_wm_mud_pi),
  59. MUIF_VARIABLE("B3",&var3,mui_u8g2_u8_char_wm_mud_pi),
  60. MUIF_VARIABLE("B4",&var4,mui_u8g2_u8_char_wm_mud_pi),
  61. MUIF_BUTTON("G0", mui_u8g2_btn_goto_wm_fi),
  62. MUIF_RO("HB",mui_u8g2_draw_hb) // Use MUI_XYA for this
  63. };
  64. fds_t fds[] MUI_PROGMEM =
  65. MUI_FORM(1)
  66. MUI_STYLE(0)
  67. MUI_LABEL(5,10, "Password")
  68. MUI_XY("B1", 5, 20)
  69. MUI_XY("B2", 15, 20)
  70. MUI_XY("B3", 25, 20)
  71. MUI_XY("B4", 35, 20)
  72. MUI_XYAT("G0", 64, 40, 10, "Goto Static Pages")
  73. MUI_FORM(10)
  74. MUI_STYLE(0)
  75. MUI_LABEL(5,10, "Page 1")
  76. MUI_XYA("HB", 5, 30, 123)
  77. MUI_XYA("HB", 5, 50, 235)
  78. MUI_FORM(11)
  79. MUI_STYLE(0)
  80. MUI_LABEL(5,10, "Page 2")
  81. MUI_FORM(12)
  82. MUI_STYLE(0)
  83. MUI_LABEL(5,10, "Page 3")
  84. ;
  85. int screenshot_n = 0;
  86. void do_screenshot(void)
  87. {
  88. char s[4096];
  89. u8x8_SaveBitmapTGA(u8g2_GetU8x8(&u8g2), "screenshot.tga");
  90. sprintf( s,
  91. "convert -border 4 -bordercolor 'rgb(255,190,40)'"
  92. " -fill 'rgb(255,170,0)' -opaque white"
  93. " -filter point -resize 200%%"
  94. " screenshot.tga pic%04d.png", screenshot_n);
  95. system(s);
  96. screenshot_n++;
  97. /*
  98. gif animation:
  99. convert -delay 40 -loop 0 pic*.png animation.gif
  100. */
  101. }
  102. int main(void)
  103. {
  104. int x, y;
  105. int k;
  106. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  107. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  108. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  109. u8x8_ConnectBitmapToU8x8(u8g2_GetU8x8(&u8g2)); /* connect to bitmap */
  110. u8g2_SetFont(&u8g2, u8g2_font_6x10_tr);
  111. u8g2_SetFontMode(&u8g2, 1);
  112. mui_Init(&ui, &u8g2, fds, muif_list, sizeof(muif_list)/sizeof(muif_t));
  113. mui_GotoForm(&ui, 10, 0);
  114. x = 4; // use as height for the box
  115. y = 0;
  116. for(;;)
  117. {
  118. u8g2_SetFontRefHeightExtendedText(&u8g2);
  119. u8g2_FirstPage(&u8g2);
  120. do
  121. {
  122. mui_Draw(&ui);
  123. } while( u8g2_NextPage(&u8g2) );
  124. do_screenshot();
  125. // printf("mui_GetCurrentCursorFocusPosition=%d\n", mui_GetCurrentCursorFocusPosition(&ui));
  126. do
  127. {
  128. k = u8g_sdl_get_key();
  129. } while( k < 0 );
  130. if ( k == 273 ) y -= 1;
  131. if ( k == 274 ) y += 1;
  132. if ( k == 276 ) x -= 1;
  133. if ( k == 275 ) x += 1;
  134. /*
  135. if ( k == 'e' ) y -= 1;
  136. if ( k == 'x' ) y += 1;
  137. if ( k == 's' ) x -= 1;
  138. if ( k == 'd' ) x += 1;
  139. */
  140. if ( k == 'q' ) break;
  141. if ( mui_GetCurrentFormId(&ui) >= 10 ) // static pages
  142. {
  143. if ( k == 'n' ) /* next static form */
  144. {
  145. if ( mui_GetCurrentFormId(&ui) >= 12 )
  146. mui_GotoForm(&ui, 10, 0);
  147. else
  148. mui_GotoForm(&ui, mui_GetCurrentFormId(&ui)+1, 0);
  149. }
  150. if ( k == 'p' ) /* previous static form */
  151. {
  152. if ( mui_GetCurrentFormId(&ui) <= 10 )
  153. mui_GotoForm(&ui, 12, 0);
  154. else
  155. mui_GotoForm(&ui, mui_GetCurrentFormId(&ui)-1, 0);
  156. }
  157. if ( k == 's' ) /* goto password form */
  158. {
  159. mui_GotoForm(&ui, 1, 0);
  160. }
  161. }
  162. else /* password input, do the normal event handling */
  163. {
  164. if ( k == 'n' )
  165. mui_NextField(&ui);
  166. if ( k == 'p' )
  167. mui_PrevField(&ui);
  168. if ( k == 's' )
  169. mui_SendSelect(&ui);
  170. }
  171. if ( k == 't' )
  172. {
  173. puts("screenshot");
  174. do_screenshot();
  175. }
  176. if ( x < 0 )
  177. x = 0;
  178. }
  179. return 0;
  180. }