u8x8_d_sdl_128x64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. u8x8_d_sdl_128x64.c
  3. */
  4. #include "u8g2.h"
  5. #ifndef NO_SDL
  6. #include "SDL.h"
  7. #endif
  8. #include <assert.h>
  9. #include <stdio.h>
  10. //#define HEIGHT (64)
  11. //#define WIDTH 128
  12. #define W(x,w) (((x)*(w))/100)
  13. #ifndef NO_SDL
  14. SDL_Window *u8g_sdl_window;
  15. SDL_Surface *u8g_sdl_screen;
  16. #endif
  17. int u8g_sdl_multiple = 3;
  18. uint32_t u8g_sdl_color[256];
  19. int u8g_sdl_height, u8g_sdl_width;
  20. static void u8g_sdl_set_pixel(int x, int y, int idx)
  21. {
  22. uint32_t *ptr;
  23. uint32_t offset;
  24. int i, j;
  25. if ( y >= u8g_sdl_height )
  26. return;
  27. if ( y < 0 )
  28. return;
  29. if ( x >= u8g_sdl_width )
  30. return;
  31. if ( x < 0 )
  32. return;
  33. for( i = 0; i < u8g_sdl_multiple; i++ )
  34. for( j = 0; j < u8g_sdl_multiple; j++ )
  35. {
  36. #ifndef NO_SDL
  37. offset = (
  38. ((y * u8g_sdl_multiple) + i) * (u8g_sdl_width * u8g_sdl_multiple) +
  39. ((x * u8g_sdl_multiple) + j)) * u8g_sdl_screen->format->BytesPerPixel;
  40. assert( offset < (Uint32)(u8g_sdl_width * u8g_sdl_multiple * u8g_sdl_height * u8g_sdl_multiple * u8g_sdl_screen->format->BytesPerPixel) );
  41. ptr = (uint32_t *)(((uint8_t *)(u8g_sdl_screen->pixels)) + offset);
  42. *ptr = u8g_sdl_color[idx];
  43. #endif
  44. }
  45. }
  46. static void u8g_sdl_set_8pixel(int x, int y, uint8_t pixel)
  47. {
  48. int cnt = 8;
  49. int bg = 0;
  50. if ( (x/8 + y/8) & 1 )
  51. bg = 4;
  52. while( cnt > 0 )
  53. {
  54. if ( (pixel & 1) == 0 )
  55. {
  56. u8g_sdl_set_pixel(x,y,bg);
  57. }
  58. else
  59. {
  60. u8g_sdl_set_pixel(x,y,3);
  61. }
  62. pixel >>= 1;
  63. y++;
  64. cnt--;
  65. }
  66. }
  67. static void u8g_sdl_set_multiple_8pixel(int x, int y, int cnt, uint8_t *pixel)
  68. {
  69. uint8_t b;
  70. while( cnt > 0 )
  71. {
  72. b = *pixel;
  73. u8g_sdl_set_8pixel(x, y, b);
  74. x++;
  75. pixel++;
  76. cnt--;
  77. }
  78. }
  79. static void u8g_sdl_init(int width, int height)
  80. {
  81. u8g_sdl_height = height;
  82. u8g_sdl_width = width;
  83. #ifndef NO_SDL
  84. if (SDL_Init(SDL_INIT_VIDEO) != 0)
  85. {
  86. printf("Unable to initialize SDL: %s\n", SDL_GetError());
  87. exit(1);
  88. }
  89. u8g_sdl_window = SDL_CreateWindow("U8g2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, u8g_sdl_width * u8g_sdl_multiple, u8g_sdl_height * u8g_sdl_multiple, 0);
  90. if ( u8g_sdl_window == NULL )
  91. {
  92. printf("Couldn't create window: %s\n", SDL_GetError());
  93. exit(1);
  94. }
  95. u8g_sdl_screen = SDL_GetWindowSurface(u8g_sdl_window);
  96. if ( u8g_sdl_screen == NULL )
  97. {
  98. printf("Couldn't create screen: %s\n", SDL_GetError());
  99. exit(1);
  100. }
  101. printf("%d bits-per-pixel mode\n", u8g_sdl_screen->format->BitsPerPixel);
  102. printf("%d bytes-per-pixel mode\n", u8g_sdl_screen->format->BytesPerPixel);
  103. u8g_sdl_color[0] = SDL_MapRGB( u8g_sdl_screen->format, 0, 0, 0 );
  104. u8g_sdl_color[1] = SDL_MapRGB( u8g_sdl_screen->format, W(100, 50), W(255,50), 0 );
  105. u8g_sdl_color[2] = SDL_MapRGB( u8g_sdl_screen->format, W(100, 80), W(255,80), 0 );
  106. u8g_sdl_color[3] = SDL_MapRGB( u8g_sdl_screen->format, 100, 255, 0 );
  107. u8g_sdl_color[4] = SDL_MapRGB( u8g_sdl_screen->format, 30, 30, 30 );
  108. /*
  109. u8g_sdl_set_pixel(0,0);
  110. u8g_sdl_set_pixel(1,1);
  111. u8g_sdl_set_pixel(2,2);
  112. */
  113. /* update all */
  114. SDL_UpdateWindowSurface(u8g_sdl_window);
  115. atexit(SDL_Quit);
  116. #endif
  117. return;
  118. }
  119. /*
  120. void main(void)
  121. {
  122. u8g_sdl_init();
  123. u8g_sdl_set_pixel(0,0,3);
  124. u8g_sdl_set_pixel(0,1,3);
  125. u8g_sdl_set_pixel(0,2,3);
  126. u8g_sdl_set_pixel(1,1,3);
  127. u8g_sdl_set_pixel(2,2,3);
  128. while( u8g_sdl_get_key() < 0 )
  129. ;
  130. }
  131. */
  132. static uint8_t u8x8_d_sdl_gpio(u8x8_t *u8x8, uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
  133. {
  134. static int debounce_cnt = 0;
  135. static int curr_msg = 0;
  136. static int db_cnt = 10;
  137. int event;
  138. if ( curr_msg > 0 )
  139. {
  140. if ( msg == curr_msg )
  141. {
  142. u8x8_SetGPIOResult(u8x8, 0);
  143. if ( debounce_cnt == 0 )
  144. curr_msg = 0;
  145. else
  146. debounce_cnt--;
  147. return 1;
  148. }
  149. }
  150. else
  151. {
  152. event = u8g_sdl_get_key();
  153. switch(event)
  154. {
  155. case 273:
  156. curr_msg = U8X8_MSG_GPIO_MENU_UP;
  157. debounce_cnt = db_cnt;
  158. break;
  159. case 274:
  160. curr_msg = U8X8_MSG_GPIO_MENU_DOWN;
  161. debounce_cnt = db_cnt;
  162. break;
  163. case 275:
  164. curr_msg = U8X8_MSG_GPIO_MENU_NEXT;
  165. debounce_cnt = db_cnt;
  166. break;
  167. case 276:
  168. curr_msg = U8X8_MSG_GPIO_MENU_PREV;
  169. debounce_cnt = db_cnt;
  170. break;
  171. case 's':
  172. curr_msg = U8X8_MSG_GPIO_MENU_SELECT;
  173. debounce_cnt = db_cnt;
  174. break;
  175. case 'q':
  176. curr_msg = U8X8_MSG_GPIO_MENU_HOME;
  177. debounce_cnt = db_cnt;
  178. break;
  179. }
  180. }
  181. u8x8_SetGPIOResult(u8x8, 1);
  182. return 1;
  183. }
  184. /*========================================*/
  185. /* 128x64 */
  186. static const u8x8_display_info_t u8x8_sdl_128x64_info =
  187. {
  188. /* chip_enable_level = */ 0,
  189. /* chip_disable_level = */ 1,
  190. /* post_chip_enable_wait_ns = */ 0,
  191. /* pre_chip_disable_wait_ns = */ 0,
  192. /* reset_pulse_width_ms = */ 0,
  193. /* post_reset_wait_ms = */ 0,
  194. /* sda_setup_time_ns = */ 0,
  195. /* sck_pulse_width_ns = */ 0,
  196. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  197. /* spi_mode = */ 1,
  198. /* i2c_bus_clock_100kHz = */ 0,
  199. /* data_setup_time_ns = */ 0,
  200. /* write_pulse_width_ns = */ 0,
  201. /* tile_width = */ 16,
  202. /* tile_hight = */ 8,
  203. /* default_x_offset = */ 0,
  204. /* flipmode_x_offset = */ 0,
  205. /* pixel_width = */ 128,
  206. /* pixel_height = */ 64
  207. };
  208. uint8_t u8x8_d_sdl_128x64(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  209. {
  210. uint8_t x, y, c;
  211. uint8_t *ptr;
  212. switch(msg)
  213. {
  214. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  215. u8x8_d_helper_display_setup_memory(u8g2, &u8x8_sdl_128x64_info);
  216. u8g_sdl_init(128, 64);
  217. break;
  218. case U8X8_MSG_DISPLAY_INIT:
  219. u8x8_d_helper_display_init(u8g2);
  220. break;
  221. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  222. break;
  223. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  224. break;
  225. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  226. break;
  227. case U8X8_MSG_DISPLAY_DRAW_TILE:
  228. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  229. x *= 8;
  230. x += u8g2->x_offset;
  231. y = ((u8x8_tile_t *)arg_ptr)->y_pos;
  232. y *= 8;
  233. do
  234. {
  235. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  236. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  237. u8g_sdl_set_multiple_8pixel(x, y, c*8, ptr);
  238. arg_int--;
  239. x+=c*8;
  240. } while( arg_int > 0 );
  241. /* update all */
  242. #ifndef NO_SDL
  243. SDL_UpdateWindowSurface(u8g_sdl_window);
  244. #endif
  245. break;
  246. default:
  247. return 0;
  248. }
  249. return 1;
  250. }
  251. void u8x8_Setup_SDL_128x64(u8x8_t *u8x8)
  252. {
  253. /* setup defaults */
  254. u8x8_SetupDefaults(u8x8);
  255. /* setup specific callbacks */
  256. u8x8->display_cb = u8x8_d_sdl_128x64;
  257. u8x8->gpio_and_delay_cb = u8x8_d_sdl_gpio;
  258. /* setup display info */
  259. u8x8_SetupMemory(u8x8);
  260. }
  261. void u8g2_SetupBuffer_SDL_128x64(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb)
  262. {
  263. static uint8_t buf[128*8];
  264. u8x8_Setup_SDL_128x64(u8g2_GetU8x8(u8g2));
  265. u8g2_SetupBuffer(u8g2, buf, 8, u8g2_ll_hvline_vertical_top_lsb, u8g2_cb);
  266. }
  267. void u8g2_SetupBuffer_SDL_128x64_4(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb)
  268. {
  269. static uint8_t buf[128*3];
  270. u8x8_Setup_SDL_128x64(u8g2_GetU8x8(u8g2));
  271. u8g2_SetupBuffer(u8g2, buf, 3, u8g2_ll_hvline_vertical_top_lsb, u8g2_cb);
  272. }
  273. void u8g2_SetupBuffer_SDL_128x64_1(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb)
  274. {
  275. static uint8_t buf[128];
  276. u8x8_Setup_SDL_128x64(u8g2_GetU8x8(u8g2));
  277. u8g2_SetupBuffer(u8g2, buf, 1, u8g2_ll_hvline_vertical_top_lsb, u8g2_cb);
  278. }
  279. /*========================================*/
  280. /* 240x160 */
  281. static const u8x8_display_info_t u8x8_sdl_240x160_info =
  282. {
  283. /* chip_enable_level = */ 0,
  284. /* chip_disable_level = */ 1,
  285. /* post_chip_enable_wait_ns = */ 0,
  286. /* pre_chip_disable_wait_ns = */ 0,
  287. /* reset_pulse_width_ms = */ 0,
  288. /* post_reset_wait_ms = */ 0,
  289. /* sda_setup_time_ns = */ 0,
  290. /* sck_pulse_width_ns = */ 0,
  291. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  292. /* spi_mode = */ 1,
  293. /* i2c_bus_clock_100kHz = */ 0,
  294. /* data_setup_time_ns = */ 0,
  295. /* write_pulse_width_ns = */ 0,
  296. /* tile_width = */ 30, /* width of 30*8=240 pixel */
  297. /* tile_hight = */ 20, /* height: 160 pixel */
  298. /* default_x_offset = */ 0,
  299. /* flipmode_x_offset = */ 0,
  300. /* pixel_width = */ 240,
  301. /* pixel_height = */ 160
  302. };
  303. uint8_t u8x8_d_sdl_240x160(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  304. {
  305. uint8_t x, y, c;
  306. uint8_t *ptr;
  307. switch(msg)
  308. {
  309. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  310. u8x8_d_helper_display_setup_memory(u8g2, &u8x8_sdl_240x160_info);
  311. u8g_sdl_init(240, 160);
  312. break;
  313. case U8X8_MSG_DISPLAY_INIT:
  314. u8x8_d_helper_display_init(u8g2);
  315. break;
  316. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  317. break;
  318. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  319. break;
  320. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  321. break;
  322. case U8X8_MSG_DISPLAY_DRAW_TILE:
  323. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  324. x *= 8;
  325. x += u8g2->x_offset;
  326. y = ((u8x8_tile_t *)arg_ptr)->y_pos;
  327. y *= 8;
  328. do
  329. {
  330. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  331. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  332. u8g_sdl_set_multiple_8pixel(x, y, c*8, ptr);
  333. arg_int--;
  334. } while( arg_int > 0 );
  335. #ifndef NO_SDL
  336. /* update all */
  337. SDL_UpdateWindowSurface(u8g_sdl_window);
  338. #endif
  339. break;
  340. default:
  341. return 0;
  342. }
  343. return 1;
  344. }
  345. void u8x8_Setup_SDL_240x160(u8x8_t *u8x8)
  346. {
  347. /* setup defaults */
  348. u8x8_SetupDefaults(u8x8);
  349. /* setup specific callbacks */
  350. u8x8->display_cb = u8x8_d_sdl_240x160;
  351. u8x8->gpio_and_delay_cb = u8x8_d_sdl_gpio;
  352. /* setup display info */
  353. u8x8_SetupMemory(u8x8);
  354. }
  355. void u8g2_SetupBuffer_SDL_240x160(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb)
  356. {
  357. static uint8_t buf[240*20];
  358. u8x8_Setup_SDL_240x160(u8g2_GetU8x8(u8g2));
  359. u8g2_SetupBuffer(u8g2, buf, 20, u8g2_ll_hvline_vertical_top_lsb, u8g2_cb);
  360. }
  361. /*========================================*/
  362. /* 256x128 */
  363. static const u8x8_display_info_t u8x8_sdl_256x128_info =
  364. {
  365. /* chip_enable_level = */ 0,
  366. /* chip_disable_level = */ 1,
  367. /* post_chip_enable_wait_ns = */ 0,
  368. /* pre_chip_disable_wait_ns = */ 0,
  369. /* reset_pulse_width_ms = */ 0,
  370. /* post_reset_wait_ms = */ 0,
  371. /* sda_setup_time_ns = */ 0,
  372. /* sck_pulse_width_ns = */ 0,
  373. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  374. /* spi_mode = */ 1,
  375. /* i2c_bus_clock_100kHz = */ 0,
  376. /* data_setup_time_ns = */ 0,
  377. /* write_pulse_width_ns = */ 0,
  378. /* tile_width = */ 32, /* width of 32*8=256 pixel */
  379. /* tile_hight = */ 16, /* height: 128 pixel */
  380. /* default_x_offset = */ 0,
  381. /* flipmode_x_offset = */ 0,
  382. /* pixel_width = */ 256,
  383. /* pixel_height = */ 128
  384. };
  385. uint8_t u8x8_d_sdl_256x128(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  386. {
  387. uint8_t x, y, c;
  388. uint8_t *ptr;
  389. switch(msg)
  390. {
  391. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  392. u8x8_d_helper_display_setup_memory(u8g2, &u8x8_sdl_256x128_info);
  393. u8g_sdl_init(256, 128);
  394. break;
  395. case U8X8_MSG_DISPLAY_INIT:
  396. u8x8_d_helper_display_init(u8g2);
  397. break;
  398. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  399. break;
  400. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  401. break;
  402. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  403. break;
  404. case U8X8_MSG_DISPLAY_DRAW_TILE:
  405. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  406. x *= 8;
  407. x += u8g2->x_offset;
  408. y = ((u8x8_tile_t *)arg_ptr)->y_pos;
  409. y *= 8;
  410. do
  411. {
  412. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  413. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  414. u8g_sdl_set_multiple_8pixel(x, y, c*8, ptr);
  415. arg_int--;
  416. } while( arg_int > 0 );
  417. #ifndef NO_SDL
  418. /* update all */
  419. SDL_UpdateWindowSurface(u8g_sdl_window);
  420. #endif
  421. break;
  422. default:
  423. return 0;
  424. }
  425. return 1;
  426. }
  427. void u8x8_Setup_SDL_256x128(u8x8_t *u8x8)
  428. {
  429. /* setup defaults */
  430. u8x8_SetupDefaults(u8x8);
  431. /* setup specific callbacks */
  432. u8x8->display_cb = u8x8_d_sdl_256x128;
  433. u8x8->gpio_and_delay_cb = u8x8_d_sdl_gpio;
  434. /* setup display info */
  435. u8x8_SetupMemory(u8x8);
  436. }
  437. void u8g2_SetupBuffer_SDL_256x128(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb)
  438. {
  439. static uint8_t buf[256*16];
  440. u8x8_Setup_SDL_256x128(u8g2_GetU8x8(u8g2));
  441. u8g2_SetupBuffer(u8g2, buf, 16, u8g2_ll_hvline_vertical_top_lsb, u8g2_cb);
  442. }