main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /* array element definition */
  9. /* changeing the values below will require major updates to the form layout */
  10. #define ARRAY_VALUE_MAX 15
  11. #define ARRAY_TIME_MAX 16
  12. struct array_element_struct
  13. {
  14. uint8_t value;
  15. uint8_t time;
  16. };
  17. /* array list */
  18. #define WAVEFORM_ELEMENT_CNT 4
  19. struct array_element_struct waveform_array[WAVEFORM_ELEMENT_CNT];
  20. /* array editable local copy */
  21. volatile uint8_t array_edit_pos = 0; // "volatile" might not be required, but still; array_edit_pos is modified by MUI callbacks and used in the extended MUIF
  22. struct array_element_struct array_edit_element;
  23. uint8_t current_value = 0;
  24. uint8_t current_time = 0;
  25. uint8_t play_waveform = 1;
  26. /*=================================================*/
  27. /* global variables */
  28. u8g2_t u8g2;
  29. mui_t ui;
  30. /*=================================================*/
  31. /* waveform array utility functions */
  32. void waveform_array_init(void)
  33. {
  34. uint8_t i;
  35. for( i = 0; i < WAVEFORM_ELEMENT_CNT; i++ )
  36. {
  37. waveform_array[i].value = i;
  38. waveform_array[i].time = ARRAY_TIME_MAX;
  39. }
  40. }
  41. /* draw wave form based on the array content */
  42. /* x,y is the lower left position of the wave form */
  43. uint8_t waveform_array_draw(u8g2_uint_t x, u8g2_uint_t y, uint8_t array_cnt, uint8_t current_pos, uint8_t current_time)
  44. {
  45. uint8_t i, j;
  46. uint8_t prev_value;
  47. uint8_t curr_value;
  48. uint8_t time;
  49. uint8_t t = 0;
  50. uint8_t v = 255;
  51. prev_value = waveform_array[array_cnt-1].value;
  52. for( i = 0; i < array_cnt; i++ )
  53. {
  54. curr_value = waveform_array[i].value;
  55. time = waveform_array[i].time;
  56. if ( curr_value < prev_value )
  57. u8g2_DrawVLine(&u8g2, x, y-prev_value, prev_value-curr_value+1);
  58. else
  59. u8g2_DrawVLine(&u8g2, x, y-curr_value, curr_value-prev_value+1);
  60. u8g2_DrawHLine(&u8g2, x, y-curr_value, time+1);
  61. for( j = 0; j < ARRAY_VALUE_MAX; j+=2 )
  62. u8g2_DrawPixel(&u8g2, x, y-j);
  63. if ( i == current_pos )
  64. {
  65. u8g2_DrawHLine(&u8g2, x, y+2, time+1);
  66. }
  67. if ( t <= current_time && current_time < t+time )
  68. {
  69. printf("waveform_draw %d %d\n", t, current_time);
  70. u8g2_DrawVLine(&u8g2, x-t+current_time, y-ARRAY_VALUE_MAX, ARRAY_VALUE_MAX+2);
  71. v = curr_value;
  72. }
  73. t += time;
  74. x += time;
  75. prev_value = curr_value;
  76. }
  77. for( j = 0; j < ARRAY_VALUE_MAX; j+=2 )
  78. u8g2_DrawPixel(&u8g2, x, y-j);
  79. return v; // return the current value
  80. }
  81. /*=================================================*/
  82. /* special muif */
  83. /*
  84. Idea: Draw some nice decoration for the waveform editor
  85. */
  86. uint8_t muif_waveform_editor_decoration(mui_t *ui, uint8_t msg)
  87. {
  88. switch(msg)
  89. {
  90. case MUIF_MSG_DRAW:
  91. /* decoration for the array position and the waveform values */
  92. u8g2_DrawHLine(&u8g2, 0, 24, 128);
  93. /* separator between values and waveform graphics */
  94. u8g2_DrawVLine(&u8g2, 50, 0, 24);
  95. /* separator between waveform graphics and lower buttons */
  96. u8g2_DrawHLine(&u8g2, 0, 46, 128);
  97. break;
  98. }
  99. return 0;
  100. }
  101. /*
  102. Idea: Introduce a field, which represents a graphical visualization of the waveform
  103. */
  104. uint8_t muif_waveform_draw(mui_t *ui, uint8_t msg)
  105. {
  106. switch(msg)
  107. {
  108. case MUIF_MSG_DRAW:
  109. waveform_array[array_edit_pos] = array_edit_element; // store the modified local copy back to the array, so that we can draw the current array
  110. waveform_array_draw(mui_get_x(ui), mui_get_y(ui), WAVEFORM_ELEMENT_CNT, array_edit_pos, 255);
  111. break;
  112. }
  113. return 0;
  114. }
  115. /*
  116. Same as muif_waveform_draw but doesn't show the current cursor position
  117. */
  118. uint8_t muif_waveform_show(mui_t *ui, uint8_t msg)
  119. {
  120. switch(msg)
  121. {
  122. case MUIF_MSG_DRAW:
  123. current_value = waveform_array_draw(mui_get_x(ui), mui_get_y(ui), WAVEFORM_ELEMENT_CNT, 255, current_time);
  124. break;
  125. }
  126. return 0;
  127. }
  128. /*
  129. Extends: "mui_u8g2_u8_min_max_wm_mse_pi"
  130. muif_list: Use together with "MUIF_U8G2_U8_MIN_MAX"
  131. Example: MUIF_U8G2_U8_MIN_MAX("AP", &array_edit_pos, 0, WAVEFORM_ELEMENT_CNT, muif_array_edit_pos)
  132. Idea:
  133. Whenever MUI modifies the array position, then also store modified values and load new values
  134. */
  135. uint8_t muif_array_edit_pos(mui_t *ui, uint8_t msg)
  136. {
  137. uint8_t return_value = 0;
  138. switch(msg)
  139. {
  140. case MUIF_MSG_FORM_START:
  141. array_edit_element = waveform_array[array_edit_pos]; // copy local array element to the local editable copy
  142. return_value = mui_u8g2_u8_min_max_wm_mse_pi(ui, msg); // call the original MUIF
  143. break;
  144. case MUIF_MSG_FORM_END:
  145. return_value = mui_u8g2_u8_min_max_wm_mse_pi(ui, msg); // finalise the form
  146. waveform_array[array_edit_pos] = array_edit_element ; // store the current elements in the array before leaving the form
  147. break;
  148. case MUIF_MSG_CURSOR_SELECT:
  149. case MUIF_MSG_EVENT_NEXT:
  150. case MUIF_MSG_EVENT_PREV:
  151. waveform_array[array_edit_pos] = array_edit_element; // store the modified local copy back to the array
  152. return_value = mui_u8g2_u8_min_max_wm_mse_pi(ui, msg); // let MUI modify the current array position
  153. array_edit_element = waveform_array[array_edit_pos] ; // load the new element from the array to the local editable copy
  154. break;
  155. default:
  156. return_value = mui_u8g2_u8_min_max_wm_mse_pi(ui, msg); // for any other messages, just call the original MUIF
  157. }
  158. return return_value;
  159. }
  160. /*=================================================*/
  161. /* muif */
  162. muif_t muif_list[] MUI_PROGMEM = {
  163. MUIF_U8G2_LABEL(),
  164. MUIF_U8G2_FONT_STYLE(0, u8g2_font_helvR08_tr),
  165. MUIF_BUTTON("GO", mui_u8g2_btn_goto_wm_fi),
  166. MUIF_RO("WS", muif_waveform_show),
  167. MUIF_RO("HL", mui_hline),
  168. /* waveform editor */
  169. MUIF_RO("DE", muif_waveform_editor_decoration),
  170. MUIF_RO("AD", muif_waveform_draw),
  171. MUIF_U8G2_U8_MIN_MAX("AP", (uint8_t *)&array_edit_pos, 0, WAVEFORM_ELEMENT_CNT-1, muif_array_edit_pos), // the pointer cast avoids warning because of the volatile keyword
  172. MUIF_U8G2_U8_MIN_MAX("AV", &array_edit_element.value, 0, ARRAY_VALUE_MAX, mui_u8g2_u8_min_max_wm_mse_pi),
  173. MUIF_U8G2_U8_MIN_MAX("AT", &array_edit_element.time, 0, ARRAY_TIME_MAX, mui_u8g2_u8_min_max_wm_mse_pi),
  174. MUIF_BUTTON("AG", mui_u8g2_btn_back_wm_fi)
  175. };
  176. /*=================================================*/
  177. /* fields and forms... */
  178. fds_t fds[] MUI_PROGMEM =
  179. // Main Menu
  180. MUI_FORM(1)
  181. MUI_STYLE(0)
  182. MUI_XYAT("GO", 63, 12, 20, "Waveform Editor")
  183. MUI_XYAT("GO", 63, 28, 2, "LED Test Start")
  184. MUI_XY("HL", 0, 45)
  185. MUI_LABEL(3,60, "Waveform")
  186. MUI_XY("WS", 54, 62)
  187. // Output Waveform to LED
  188. MUI_FORM(2)
  189. MUI_STYLE(0)
  190. MUI_XYAT("GO", 63, 28, 1, "LED Test Stop")
  191. MUI_XY("HL", 0, 45)
  192. MUI_LABEL(3,60, "Waveform")
  193. MUI_XY("WS", 54, 62)
  194. // Waveform Editor
  195. MUI_FORM(20)
  196. MUI_STYLE(0)
  197. MUI_AUX("DE")
  198. MUI_LABEL(3,15, "Pos:")
  199. MUI_XY("AP", 30, 15)
  200. MUI_LABEL(54,10, "Value:")
  201. MUI_XY("AV", 100, 10)
  202. MUI_LABEL(54,20, "Time:")
  203. MUI_XY("AT", 100, 20)
  204. MUI_LABEL(3,39, "Waveform")
  205. MUI_XY("AD", 54, 41)
  206. MUI_XYAT("AG", 63, 59, 1, "Done")
  207. ;
  208. int screenshot_n = 0;
  209. void do_screenshot(void)
  210. {
  211. char s[4096];
  212. u8x8_SaveBitmapTGA(u8g2_GetU8x8(&u8g2), "screenshot.tga");
  213. sprintf( s,
  214. "convert -border 4 -bordercolor 'rgb(255,190,40)'"
  215. " -fill 'rgb(255,170,0)' -opaque white"
  216. " -filter point -resize 200%%"
  217. " screenshot.tga pic%04d.png", screenshot_n);
  218. system(s);
  219. screenshot_n++;
  220. /*
  221. gif animation:
  222. convert -delay 40 -loop 0 pic*.png animation.gif
  223. */
  224. }
  225. int main(void)
  226. {
  227. int x, y;
  228. int k;
  229. waveform_array_init();
  230. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  231. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  232. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  233. u8x8_ConnectBitmapToU8x8(u8g2_GetU8x8(&u8g2)); /* connect to bitmap */
  234. u8g2_SetFont(&u8g2, u8g2_font_6x10_tr);
  235. u8g2_SetFontMode(&u8g2, 1);
  236. mui_Init(&ui, &u8g2, fds, muif_list, sizeof(muif_list)/sizeof(muif_t));
  237. mui_GotoForm(&ui, 1, 0);
  238. x = 4; // use as height for the box
  239. y = 0;
  240. for(;;)
  241. {
  242. u8g2_SetFontRefHeightExtendedText(&u8g2);
  243. u8g2_FirstPage(&u8g2);
  244. do
  245. {
  246. mui_Draw(&ui);
  247. } while( u8g2_NextPage(&u8g2) );
  248. // printf("mui_GetCurrentCursorFocusPosition=%d\n", mui_GetCurrentCursorFocusPosition(&ui));
  249. do
  250. {
  251. k = u8g_sdl_get_key();
  252. if ( mui_GetCurrentFormId(&ui) == 2 )
  253. {
  254. if ( current_value == 255 )
  255. current_time = 0;
  256. else
  257. current_time++;
  258. sleep(1);
  259. break;
  260. }
  261. } while( k < 0 );
  262. if ( k == 273 ) y -= 1;
  263. if ( k == 274 ) y += 1;
  264. if ( k == 276 ) x -= 1;
  265. if ( k == 275 ) x += 1;
  266. /*
  267. if ( k == 'e' ) y -= 1;
  268. if ( k == 'x' ) y += 1;
  269. if ( k == 's' ) x -= 1;
  270. if ( k == 'd' ) x += 1;
  271. */
  272. if ( k == 'q' ) break;
  273. if ( k == 'n' )
  274. {
  275. do_screenshot();
  276. mui_NextField(&ui);
  277. }
  278. if ( k == 'p' )
  279. {
  280. do_screenshot();
  281. mui_PrevField(&ui);
  282. }
  283. if ( k == 's' )
  284. {
  285. do_screenshot();
  286. mui_SendSelect(&ui);
  287. }
  288. if ( k == 't' )
  289. {
  290. puts("screenshot");
  291. do_screenshot();
  292. }
  293. }
  294. return 0;
  295. }