main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 blink_light = 4; // brightness
  12. uint8_t blink_time = 1;
  13. uint8_t blink_duty = 1;
  14. uint8_t blink_state = 0;
  15. long blink_last_update = 0;
  16. void blink_update(void)
  17. {
  18. /*
  19. long next_update_time = blink_last_update;
  20. if ( blink_state == 0 )
  21. {
  22. next_update_time += (long)(blink_duty+1)*(blink_time+1)*40L;
  23. }
  24. else
  25. {
  26. next_update_time += (long)(5-blink_duty)*(blink_time+1)*40L;
  27. }
  28. if ( next_update_time < millis() )
  29. {
  30. blink_last_update = millis();
  31. blink_state = 1-blink_state;
  32. if ( blink_state == 0 )
  33. analogWrite(LED_BUILTIN, (blink_light*255)>>2);
  34. else
  35. digitalWrite(LED_BUILTIN, LOW);
  36. }
  37. */
  38. }
  39. uint8_t mui_hrule(mui_t *ui, uint8_t msg)
  40. {
  41. u8g2_t *u8g2 = mui_get_U8g2(ui);
  42. switch(msg)
  43. {
  44. case MUIF_MSG_DRAW:
  45. u8g2_DrawHLine(u8g2, 0, mui_get_y(ui), u8g2_GetDisplayWidth(u8g2));
  46. break;
  47. }
  48. return 0;
  49. }
  50. /*=================================================*/
  51. /* list functions (get cnt/get element pairs) */
  52. muif_t muif_list[] = {
  53. MUIF_U8G2_FONT_STYLE(0, u8g2_font_helvR08_tr),
  54. MUIF_U8G2_FONT_STYLE(1, u8g2_font_helvB08_tr),
  55. //MUIF_U8G2_FONT_STYLE(9, u8g2_font_streamline_interface_essential_loading_t),
  56. MUIF_U8G2_FONT_STYLE(9, u8g2_font_percent_circle_25_hn),
  57. MUIF_RO("HR", mui_hrule),
  58. MUIF_U8G2_LABEL(),
  59. MUIF_RO("GP",mui_u8g2_goto_data),
  60. MUIF_BUTTON("GC", mui_u8g2_goto_form_w1_pi),
  61. MUIF_U8G2_U8_MIN_MAX("NB", &blink_light, 0, 4, mui_u8g2_u8_min_max_wm_mse_pi),
  62. MUIF_U8G2_U8_MIN_MAX("NT", &blink_time, 0, 4, mui_u8g2_u8_min_max_wm_mse_pi),
  63. MUIF_U8G2_U8_MIN_MAX("ND", &blink_duty, 0, 4, mui_u8g2_u8_min_max_wm_mse_pi),
  64. MUIF_U8G2_U8_MIN_MAX_STEP("BB", &blink_light, 0, 4, 1, MUI_MMS_4X_BAR, mui_u8g2_u8_bar_wm_mse_pf),
  65. MUIF_U8G2_U8_MIN_MAX_STEP("BT", &blink_time, 0, 4, 1, MUI_MMS_4X_BAR, mui_u8g2_u8_bar_wm_mse_pf),
  66. MUIF_U8G2_U8_MIN_MAX_STEP("BD", &blink_duty, 0, 4, 1, MUI_MMS_4X_BAR, mui_u8g2_u8_bar_wm_mse_pf),
  67. MUIF_VARIABLE("PB", &blink_light, mui_u8g2_u8_opt_line_wa_mse_pi),
  68. MUIF_VARIABLE("PT", &blink_time, mui_u8g2_u8_opt_line_wa_mse_pi),
  69. MUIF_VARIABLE("PD", &blink_duty, mui_u8g2_u8_opt_line_wa_mse_pi),
  70. MUIF_U8G2_U8_MIN_MAX("nB", &blink_light, 0, 4, mui_u8g2_u8_min_max_wm_mud_pi),
  71. MUIF_U8G2_U8_MIN_MAX("nT", &blink_time, 0, 4, mui_u8g2_u8_min_max_wm_mud_pi),
  72. MUIF_U8G2_U8_MIN_MAX("nD", &blink_duty, 0, 4, mui_u8g2_u8_min_max_wm_mud_pi),
  73. MUIF_U8G2_U8_MIN_MAX_STEP("bB", &blink_light, 0, 4, 1, MUI_MMS_4X_BAR, mui_u8g2_u8_bar_wm_mud_pf),
  74. MUIF_U8G2_U8_MIN_MAX_STEP("bT", &blink_time, 0, 4, 1, MUI_MMS_4X_BAR, mui_u8g2_u8_bar_wm_mud_pf),
  75. MUIF_U8G2_U8_MIN_MAX_STEP("bD", &blink_duty, 0, 4, 1, MUI_MMS_4X_BAR, mui_u8g2_u8_bar_wm_mud_pf),
  76. MUIF_VARIABLE("pB", &blink_light, mui_u8g2_u8_opt_line_wa_mud_pi),
  77. MUIF_VARIABLE("pT", &blink_time, mui_u8g2_u8_opt_line_wa_mud_pi),
  78. MUIF_VARIABLE("pD", &blink_duty, mui_u8g2_u8_opt_line_wa_mud_pi),
  79. /* a button for the menu... */
  80. MUIF_BUTTON("GO", mui_u8g2_btn_goto_wm_fi)
  81. };
  82. fds_t fds[] =
  83. MUI_FORM(1)
  84. MUI_STYLE(1)
  85. MUI_LABEL(5, 8, "MUI Blink")
  86. MUI_STYLE(0)
  87. MUI_XY("HR", 0,11)
  88. MUI_DATA("GP",
  89. MUI_10 "Numeric (MSE)|"
  90. MUI_12 "Progress Bar (MSE)|"
  91. MUI_14 "Progress Pie (MSE)|"
  92. MUI_20 "Numeric (MUD)|"
  93. MUI_22 "Progress Bar (MUD)|"
  94. MUI_24 "Progress Pie (MUD)")
  95. MUI_XYA("GC", 5, 24, 0)
  96. MUI_XYA("GC", 5, 36, 1)
  97. MUI_XYA("GC", 5, 48, 2)
  98. MUI_XYA("GC", 5, 60, 3)
  99. MUI_FORM(10)
  100. MUI_STYLE(1)
  101. MUI_LABEL(5, 8, "Numeric (MSE)")
  102. MUI_STYLE(0)
  103. MUI_XY("HR", 0,11)
  104. MUI_LABEL(5,23, "Light:")
  105. MUI_LABEL(5,35, "Time:")
  106. MUI_LABEL(5,47, "Duty:")
  107. MUI_XY("NB", 50, 23)
  108. MUI_XY("NT", 50, 35)
  109. MUI_XY("ND", 50, 47)
  110. MUI_XYAT("GO", 20, 60, 1, " Exit ")
  111. MUI_FORM(12)
  112. MUI_STYLE(1)
  113. MUI_LABEL(5, 8, "Bar Graph (MSE)")
  114. MUI_STYLE(0)
  115. MUI_XY("HR", 0,11)
  116. MUI_LABEL(5,23, "Light:")
  117. MUI_LABEL(5,35, "Time:")
  118. MUI_LABEL(5,47, "Duty:")
  119. MUI_XY("BB", 50, 23)
  120. MUI_XY("BT", 50, 35)
  121. MUI_XY("BD", 50, 47)
  122. MUI_XYAT("GO", 20, 60, 1, " Exit ")
  123. MUI_FORM(14)
  124. MUI_STYLE(1)
  125. MUI_LABEL(5, 8, "Pie Graph (MSE)")
  126. MUI_STYLE(0)
  127. MUI_XY("HR", 0,11)
  128. MUI_LABEL(5,21, "Light")
  129. MUI_LABEL(37,21, "Time")
  130. MUI_LABEL(69,21, "Duty")
  131. MUI_STYLE(9)
  132. MUI_XYAT("PB", 6, 49, 21, "0|2|4|6|8")
  133. MUI_XYAT("PT", 38, 49, 21, "0|2|4|6|8")
  134. MUI_XYAT("PD", 70, 49, 21, "0|2|4|6|8")
  135. MUI_STYLE(0)
  136. MUI_XYAT("GO", 20, 60, 1, " Exit ")
  137. MUI_FORM(20)
  138. MUI_STYLE(1)
  139. MUI_LABEL(5, 8, "Numeric (MUD)")
  140. MUI_STYLE(0)
  141. MUI_XY("HR", 0,11)
  142. MUI_LABEL(5,23, "Light:")
  143. MUI_LABEL(5,35, "Time:")
  144. MUI_LABEL(5,47, "Duty:")
  145. MUI_XY("nB", 50, 23)
  146. MUI_XY("nT", 50, 35)
  147. MUI_XY("nD", 50, 47)
  148. MUI_XYAT("GO", 20, 60, 1, " Exit ")
  149. MUI_FORM(22)
  150. MUI_STYLE(1)
  151. MUI_LABEL(5, 8, "Bar Graph (MUD)")
  152. MUI_STYLE(0)
  153. MUI_XY("HR", 0,11)
  154. MUI_LABEL(5,23, "Light:")
  155. MUI_LABEL(5,35, "Time:")
  156. MUI_LABEL(5,47, "Duty:")
  157. MUI_XY("bB", 50, 23)
  158. MUI_XY("bT", 50, 35)
  159. MUI_XY("bD", 50, 47)
  160. MUI_XYAT("GO", 20, 60, 1, " Exit ")
  161. MUI_FORM(24)
  162. MUI_STYLE(1)
  163. MUI_LABEL(5, 8, "Pie Graph (MUD)")
  164. MUI_STYLE(0)
  165. MUI_XY("HR", 0,11)
  166. MUI_LABEL(5,21, "Light")
  167. MUI_LABEL(37,21, "Time")
  168. MUI_LABEL(69,21, "Duty")
  169. MUI_STYLE(9)
  170. MUI_XYAT("pB", 6, 49, 21, "0|2|4|6|8")
  171. MUI_XYAT("pT", 38, 49, 21, "0|2|4|6|8")
  172. MUI_XYAT("pD", 70, 49, 21, "0|2|4|6|8")
  173. MUI_STYLE(0)
  174. MUI_XYAT("GO", 20, 60, 1, " Exit ")
  175. ;
  176. int screenshot_n = 0;
  177. void do_screenshot(void)
  178. {
  179. char s[4096];
  180. u8x8_SaveBitmapTGA(u8g2_GetU8x8(&u8g2), "screenshot.tga");
  181. sprintf( s,
  182. "convert -border 4 -bordercolor 'rgb(255,190,40)'"
  183. " -fill 'rgb(255,170,0)' -opaque white"
  184. " -filter point -resize 200%%"
  185. " screenshot.tga pic%04d.png", screenshot_n);
  186. system(s);
  187. screenshot_n++;
  188. /*
  189. gif animation:
  190. convert -delay 40 -loop 0 pic*.png animation.gif
  191. */
  192. }
  193. int main(void)
  194. {
  195. int x, y;
  196. int k;
  197. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  198. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  199. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  200. u8x8_ConnectBitmapToU8x8(u8g2_GetU8x8(&u8g2)); /* connect to bitmap */
  201. u8g2_SetFontMode(&u8g2, 1);
  202. mui_Init(&ui, &u8g2, fds, muif_list, sizeof(muif_list)/sizeof(muif_t));
  203. //mui_GotoForm(&ui, 201, 0);
  204. mui_GotoForm(&ui, 1, 0);
  205. //puts(fds);
  206. x = 4; // use as height for the box
  207. y = 0;
  208. for(;;)
  209. {
  210. u8g2_SetFontRefHeightExtendedText(&u8g2);
  211. u8g2_FirstPage(&u8g2);
  212. do
  213. {
  214. mui_Draw(&ui);
  215. } while( u8g2_NextPage(&u8g2) );
  216. do_screenshot();
  217. // printf("mui_GetCurrentCursorFocusPosition=%d\n", mui_GetCurrentCursorFocusPosition(&ui));
  218. do
  219. {
  220. k = u8g_sdl_get_key();
  221. } while( k < 0 );
  222. if ( k == 273 ) y -= 1;
  223. if ( k == 274 ) y += 1;
  224. if ( k == 276 ) x -= 1;
  225. if ( k == 275 ) x += 1;
  226. /*
  227. if ( k == 'e' ) y -= 1;
  228. if ( k == 'x' ) y += 1;
  229. if ( k == 's' ) x -= 1;
  230. if ( k == 'd' ) x += 1;
  231. */
  232. if ( k == 'q' ) break;
  233. if ( k == 'n' )
  234. {
  235. mui_NextField(&ui);
  236. }
  237. if ( k == 'p' )
  238. {
  239. mui_PrevField(&ui);
  240. }
  241. if ( k == 's' )
  242. {
  243. mui_SendSelect(&ui);
  244. }
  245. if ( k == 't' )
  246. {
  247. puts("screenshot");
  248. do_screenshot();
  249. }
  250. if ( x < 0 )
  251. x = 0;
  252. }
  253. return 0;
  254. }