main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #include "u8g2.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. void ep_skip_space(const char **s)
  5. {
  6. for(;;)
  7. {
  8. if ( **s == '\0' )
  9. break;
  10. if ( **s == '\n' )
  11. break;
  12. if ( **s == '\r' )
  13. break;
  14. if ( **s > ' ' )
  15. return;
  16. (*s)++;
  17. }
  18. }
  19. void ep_skip_new_line(const char **s)
  20. {
  21. for(;;)
  22. {
  23. if ( **s == '\0' )
  24. return;
  25. if ( **s == '\n' )
  26. break;
  27. if ( **s == '\r' )
  28. break;
  29. (*s)++;
  30. }
  31. for(;;)
  32. {
  33. if ( **s == '\0' )
  34. return;
  35. if ( **s > ' ' )
  36. return;
  37. (*s)++;
  38. }
  39. }
  40. unsigned ep_get_integer(const char **s)
  41. {
  42. unsigned v = 0;
  43. for(;;)
  44. {
  45. if ( **s >= '0' && **s <= '9' )
  46. {
  47. v *= 10;
  48. v+= (u8g2_uint_t)(**s-'0');
  49. }
  50. else
  51. {
  52. break;
  53. }
  54. (*s)++;
  55. }
  56. ep_skip_space(s);
  57. return v;
  58. }
  59. const char *ep_get_identifier(const char **s)
  60. {
  61. static char b[34] = "";
  62. uint8_t i = 0;
  63. b[0] = '\0';
  64. for(;;)
  65. {
  66. if ( **s == '\0' )
  67. break;
  68. if ( (**s >= '0' && **s <= '9')
  69. || (**s >= 'a' && **s <= 'z')
  70. || (**s >= 'A' && **s <= 'Z')
  71. || ( **s == '_' ) )
  72. {
  73. if ( i < 32 )
  74. {
  75. b[i] = **s;
  76. i++;
  77. b[i] = '\0';
  78. }
  79. }
  80. else
  81. {
  82. break;
  83. }
  84. (*s)++;
  85. }
  86. ep_skip_space(s);
  87. return b;
  88. }
  89. const char *ep_get_string(const char **s)
  90. {
  91. static char b[66] = "";
  92. uint8_t i = 0;
  93. b[0] = '\0';
  94. if ( **s != '\"' )
  95. return b;
  96. (*s)++;
  97. for(;;)
  98. {
  99. if ( **s == '\"' )
  100. {
  101. (*s)++;
  102. break;
  103. }
  104. if ( **s == '\0' )
  105. break;
  106. if ( i < 64 )
  107. {
  108. b[i] = **s;
  109. i++;
  110. b[i] = '\0';
  111. }
  112. (*s)++;
  113. }
  114. ep_skip_space(s);
  115. return b;
  116. }
  117. void ep_cmd_text(u8g2_t *u8g2, const char **s)
  118. {
  119. u8g2_uint_t x;
  120. u8g2_uint_t y;
  121. const char *text;
  122. x = ep_get_integer(s);
  123. y = ep_get_integer(s);
  124. if ( **s == '\"' )
  125. text = ep_get_string(s);
  126. else
  127. text = ep_get_identifier(s);
  128. u8g2_DrawStr(u8g2, x, y, text);
  129. }
  130. void ep_cmd_char(u8g2_t *u8g2, const char **s)
  131. {
  132. u8g2_uint_t x;
  133. u8g2_uint_t y;
  134. uint16_t g;
  135. x = ep_get_integer(s);
  136. y = ep_get_integer(s);
  137. g = ep_get_integer(s);
  138. u8g2_DrawGlyph(u8g2, x, y, g);
  139. }
  140. void ep_cmd_font(u8g2_t *u8g2, const char **s)
  141. {
  142. const char *font = ep_get_identifier(s);
  143. if ( strcmp(font, "cu12") == 0 )
  144. u8g2_SetFont(u8g2, u8g2_font_cu12_tf);
  145. else if ( strcmp(font, "helvB08") == 0 )
  146. u8g2_SetFont(u8g2, u8g2_font_helvB08_tf);
  147. else if ( strcmp(font, "helvR08") == 0 )
  148. u8g2_SetFont(u8g2, u8g2_font_helvR08_tf);
  149. else if ( strcmp(font, "helvB10") == 0 )
  150. u8g2_SetFont(u8g2, u8g2_font_helvB10_tf);
  151. else if ( strcmp(font, "helvR10") == 0 )
  152. u8g2_SetFont(u8g2, u8g2_font_helvR10_tf);
  153. else if ( strcmp(font, "helvB12") == 0 )
  154. u8g2_SetFont(u8g2, u8g2_font_helvB12_tf);
  155. else if ( strcmp(font, "helvR12") == 0 )
  156. u8g2_SetFont(u8g2, u8g2_font_helvR12_tf);
  157. else if ( strcmp(font, "helvB14") == 0 )
  158. u8g2_SetFont(u8g2, u8g2_font_helvB14_tf);
  159. else if ( strcmp(font, "helvR14") == 0 )
  160. u8g2_SetFont(u8g2, u8g2_font_helvR14_tf);
  161. else if ( strcmp(font, "helvB18") == 0 )
  162. u8g2_SetFont(u8g2, u8g2_font_helvB18_tf);
  163. else if ( strcmp(font, "helvR18") == 0 )
  164. u8g2_SetFont(u8g2, u8g2_font_helvR18_tf);
  165. else if ( strcmp(font, "helvB24") == 0 )
  166. u8g2_SetFont(u8g2, u8g2_font_helvB24_tf);
  167. else if ( strcmp(font, "helvR24") == 0 )
  168. u8g2_SetFont(u8g2, u8g2_font_helvR24_tf);
  169. else if ( strcmp(font, "4x6") == 0 )
  170. u8g2_SetFont(u8g2, u8g2_font_4x6_tf);
  171. else if ( strcmp(font, "5x7") == 0 )
  172. u8g2_SetFont(u8g2, u8g2_font_5x7_tf);
  173. else if ( strcmp(font, "5x8") == 0 )
  174. u8g2_SetFont(u8g2, u8g2_font_5x8_tf);
  175. else if ( strcmp(font, "6x10") == 0 )
  176. u8g2_SetFont(u8g2, u8g2_font_6x10_tf);
  177. else if ( strcmp(font, "6x13") == 0 )
  178. u8g2_SetFont(u8g2, u8g2_font_6x13_tf);
  179. else if ( strcmp(font, "6x13B") == 0 )
  180. u8g2_SetFont(u8g2, u8g2_font_6x13B_tf);
  181. else if ( strcmp(font, "7x13") == 0 )
  182. u8g2_SetFont(u8g2, u8g2_font_7x13_tf);
  183. else if ( strcmp(font, "7x13B") == 0 )
  184. u8g2_SetFont(u8g2, u8g2_font_7x13B_tf);
  185. else if ( strcmp(font, "7x14") == 0 )
  186. u8g2_SetFont(u8g2, u8g2_font_7x14_tf);
  187. else if ( strcmp(font, "7x14B") == 0 )
  188. u8g2_SetFont(u8g2, u8g2_font_7x14B_tf);
  189. else if ( strcmp(font, "8x13") == 0 )
  190. u8g2_SetFont(u8g2, u8g2_font_7x13_tf);
  191. else if ( strcmp(font, "8x13B") == 0 )
  192. u8g2_SetFont(u8g2, u8g2_font_8x13B_tf);
  193. else if ( strcmp(font, "open_inconic_1x") == 0 )
  194. u8g2_SetFont(u8g2, u8g2_font_open_iconic_all_1x_t);
  195. else if ( strcmp(font, "open_inconic_2x") == 0 )
  196. u8g2_SetFont(u8g2, u8g2_font_open_iconic_all_2x_t);
  197. else if ( strcmp(font, "open_inconic_4x") == 0 )
  198. u8g2_SetFont(u8g2, u8g2_font_open_iconic_all_4x_t);
  199. else if ( strcmp(font, "open_inconic_6x") == 0 )
  200. u8g2_SetFont(u8g2, u8g2_font_open_iconic_all_6x_t);
  201. else if ( strcmp(font, "open_inconic_8x") == 0 )
  202. u8g2_SetFont(u8g2, u8g2_font_open_iconic_all_8x_t);
  203. else
  204. u8g2_SetFont(u8g2, u8g2_font_6x10_tf);
  205. }
  206. void ep_cmd_color(u8g2_t *u8g2, const char **s)
  207. {
  208. uint8_t c = ep_get_integer(s);
  209. u8g2_SetDrawColor(u8g2, c);
  210. }
  211. void ep_cmd_transparent(u8g2_t *u8g2, const char **s)
  212. {
  213. uint8_t v = ep_get_integer(s);
  214. u8g2_SetFontMode(u8g2, v);
  215. }
  216. void ep_cmd_box(u8g2_t *u8g2, const char **s)
  217. {
  218. u8g2_uint_t x;
  219. u8g2_uint_t y;
  220. u8g2_uint_t w;
  221. u8g2_uint_t h;
  222. x = ep_get_integer(s);
  223. y = ep_get_integer(s);
  224. w = ep_get_integer(s);
  225. h = ep_get_integer(s);
  226. u8g2_DrawBox(u8g2, x, y, w, h);
  227. }
  228. void ep_cmd_rbox(u8g2_t *u8g2, const char **s)
  229. {
  230. u8g2_uint_t x;
  231. u8g2_uint_t y;
  232. u8g2_uint_t w;
  233. u8g2_uint_t h;
  234. u8g2_uint_t r;
  235. x = ep_get_integer(s);
  236. y = ep_get_integer(s);
  237. w = ep_get_integer(s);
  238. h = ep_get_integer(s);
  239. r = ep_get_integer(s);
  240. u8g2_DrawRBox(u8g2, x, y, w, h, r);
  241. }
  242. void ep_cmd_frame(u8g2_t *u8g2, const char **s)
  243. {
  244. u8g2_uint_t x;
  245. u8g2_uint_t y;
  246. u8g2_uint_t w;
  247. u8g2_uint_t h;
  248. x = ep_get_integer(s);
  249. y = ep_get_integer(s);
  250. w = ep_get_integer(s);
  251. h = ep_get_integer(s);
  252. u8g2_DrawFrame(u8g2, x, y, w, h);
  253. }
  254. void ep_cmd_rframe(u8g2_t *u8g2, const char **s)
  255. {
  256. u8g2_uint_t x;
  257. u8g2_uint_t y;
  258. u8g2_uint_t w;
  259. u8g2_uint_t h;
  260. u8g2_uint_t r;
  261. x = ep_get_integer(s);
  262. y = ep_get_integer(s);
  263. w = ep_get_integer(s);
  264. h = ep_get_integer(s);
  265. r = ep_get_integer(s);
  266. u8g2_DrawRFrame(u8g2, x, y, w, h, r);
  267. }
  268. void ep_cmd_clear(u8g2_t *u8g2, const char **s)
  269. {
  270. u8g2_ClearBuffer(u8g2);
  271. }
  272. void ep_cmd(u8g2_t *u8g2, const char **s)
  273. {
  274. const char *cmd;
  275. ep_skip_space(s);
  276. for(;;)
  277. {
  278. if ( **s == '\0' )
  279. break;
  280. cmd = ep_get_identifier(s);
  281. //printf("cmd: %s %s\n", cmd, *s);
  282. if ( strcmp(cmd, "text") == 0 )
  283. ep_cmd_text(u8g2, s);
  284. else if ( strcmp(cmd, "char") == 0 )
  285. ep_cmd_char(u8g2, s);
  286. else if ( strcmp(cmd, "font") == 0 )
  287. ep_cmd_font(u8g2, s);
  288. else if ( strcmp(cmd, "color") == 0 )
  289. ep_cmd_color(u8g2, s);
  290. else if ( strcmp(cmd, "transparent") == 0 )
  291. ep_cmd_transparent(u8g2, s);
  292. else if ( strcmp(cmd, "box") == 0 )
  293. ep_cmd_box(u8g2, s);
  294. else if ( strcmp(cmd, "rbox") == 0 )
  295. ep_cmd_rbox(u8g2, s);
  296. else if ( strcmp(cmd, "frame") == 0 )
  297. ep_cmd_frame(u8g2, s);
  298. else if ( strcmp(cmd, "rframe") == 0 )
  299. ep_cmd_rframe(u8g2, s);
  300. else if ( strcmp(cmd, "clear") == 0 )
  301. ep_cmd_clear(u8g2, s);
  302. ep_skip_new_line(s);
  303. }
  304. }
  305. u8g2_t u8g2;
  306. int main(void)
  307. {
  308. int x, y;
  309. int k;
  310. int i;
  311. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  312. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  313. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  314. //u8g2_SetFont(&u8g2, u8g2_font_cu12_t_arabic);
  315. //u8g2_SetFont(&u8g2, u8g2_font_10x20_t_arabic);
  316. u8g2_SetFont(&u8g2, u8g2_font_cu12_tf);
  317. x = 50;
  318. y = 30;
  319. for(;;)
  320. {
  321. #ifdef U8G2_WITH_HVLINE_COUNT
  322. u8g2.hv_cnt = 0UL;
  323. #endif /* U8G2_WITH_HVLINE_COUNT */
  324. u8g2_FirstPage(&u8g2);
  325. i = 0;
  326. do
  327. {
  328. const char *s1 =
  329. "transparent 1\n"
  330. "font 4x5\n"
  331. "text 0 14 Hello\n"
  332. "font helvB12\n"
  333. "text 10 28 World\n"
  334. "box 0 0 10 2\n"
  335. "rbox 40 0 10 6 2\n"
  336. "frame 20 0 10 4\n"
  337. "rframe 60 0 10 8 3\n"
  338. "font open_inconic_8x\n"
  339. "char 64 64 282\n"
  340. ;
  341. ep_cmd(&u8g2, &s1);
  342. if ( i == 1 )
  343. {
  344. u8g2_DrawHVLine(&u8g2, u8g2.user_x0, u8g2.user_y0, 1, 0);
  345. u8g2_DrawHVLine(&u8g2, u8g2.user_x0, u8g2.user_y1-1, 1, 0);
  346. u8g2_DrawHVLine(&u8g2, u8g2.user_x1-1, u8g2.user_y1-1, 1, 0);
  347. u8g2_DrawHVLine(&u8g2, u8g2.user_x1-1, u8g2.user_y0, 1, 0);
  348. }
  349. i++;
  350. } while( u8g2_NextPage(&u8g2) );
  351. #ifdef U8G2_WITH_HVLINE_COUNT
  352. printf("hv cnt: %ld\n", u8g2.hv_cnt);
  353. #endif /* U8G2_WITH_HVLINE_COUNT */
  354. do
  355. {
  356. k = u8g_sdl_get_key();
  357. } while( k < 0 );
  358. if ( k == 273 ) y -= 7;
  359. if ( k == 274 ) y += 7;
  360. if ( k == 276 ) x -= 7;
  361. if ( k == 275 ) x += 7;
  362. if ( k == 'e' ) y -= 1;
  363. if ( k == 'x' ) y += 1;
  364. if ( k == 's' ) x -= 1;
  365. if ( k == 'd' ) x += 1;
  366. if ( k == 'q' ) break;
  367. }
  368. //u8x8_Set8x8Font(u8g2_GetU8x8(&u8g2), bdf_font);
  369. //u8x8_Draw8x8String(u8g2_GetU8x8(&u8g2), 0, 0, "Hello World!");
  370. return 0;
  371. }