u8x8_d_utf8.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "u8g2.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define CHGR_WIDTH 96
  6. //#define CHGR_WIDTH 102
  7. //#define CHGR_HEIGHT 32
  8. #define CHGR_HEIGHT 68
  9. unsigned char chgr_bitmap[CHGR_HEIGHT/2][CHGR_WIDTH/2];
  10. unsigned char *chgr_bitmap_pos(unsigned x, unsigned y)
  11. {
  12. if ( x >= CHGR_WIDTH )
  13. return NULL;
  14. if ( y >= CHGR_HEIGHT )
  15. return NULL;
  16. return &(chgr_bitmap[y/2][x/2]);
  17. }
  18. void chgr_set_pixel(unsigned x, unsigned y)
  19. {
  20. unsigned char *p;
  21. p = chgr_bitmap_pos(x, y);
  22. if ( p == NULL )
  23. return;
  24. *p |= 1<<(((y&1)<<1) + (1-(x&1)));
  25. //printf("x=%d y=%d *p=%d\n", x, y, *p);
  26. }
  27. void chgr_clr_pixel(unsigned x, unsigned y)
  28. {
  29. unsigned char *p;
  30. p = chgr_bitmap_pos(x, y);
  31. if ( p == NULL )
  32. return;
  33. *p &= ~ (1<<(((y&1)<<1) + (1-(x&1))));
  34. //printf("x=%d y=%d *p=%d\n", x, y, *p);
  35. }
  36. void chgr_clear(void)
  37. {
  38. unsigned x, y;
  39. for( y = 0; y < CHGR_HEIGHT/2; y++)
  40. {
  41. for( x = 0; x < CHGR_WIDTH/2; x++)
  42. {
  43. chgr_bitmap[y][x] = 0;
  44. }
  45. }
  46. }
  47. const char *chgr_to_str(unsigned v)
  48. {
  49. switch(v)
  50. {
  51. case 0: return " "; //
  52. //case 0: return "\xE2\x96\x91"; // light shade
  53. case 1: return "\xE2\x96\x9d"; // QUADRANT UPPER RIGHT
  54. case 2: return "\xE2\x96\x98"; // QUADRANT UPPER LEFT
  55. case 3: return "\xE2\x96\x80"; // UPPER HALF
  56. case 4: return "\xE2\x96\x97"; // QUADRANT LOWER RIGHT
  57. case 5: return "\xE2\x96\x90"; // RIGHT BAR
  58. case 6: return "\xE2\x96\x9A"; //
  59. case 7: return "\xE2\x96\x9C"; //
  60. case 8: return "\xE2\x96\x96"; // QUADRANT LOWER LEFT
  61. case 9: return "\xE2\x96\x9E"; //
  62. case 10: return "\xE2\x96\x8B"; //
  63. case 11: return "\xE2\x96\x9B"; //
  64. case 12: return "\xE2\x96\x84"; // LOWER HALF
  65. case 13: return "\xE2\x96\x9F"; //
  66. case 14: return "\xE2\x96\x99"; //
  67. case 15: return "\xE2\x96\x88"; //
  68. }
  69. return "\xE2\x96\x88 "; // full block
  70. }
  71. void chgr_show(void)
  72. {
  73. unsigned x, y;
  74. for( y = 0; y < CHGR_HEIGHT/2; y++)
  75. {
  76. printf("%02d|", y*2);
  77. for( x = 0; x < CHGR_WIDTH/2; x++)
  78. {
  79. //printf("%x", chgr_bitmap[y][x]);
  80. printf("%s", chgr_to_str(chgr_bitmap[y][x]));
  81. }
  82. printf("|\n");
  83. }
  84. }
  85. void chgr_set_8pixel(unsigned x, unsigned y, uint8_t pixel, uint16_t f)
  86. {
  87. int cnt = 8;
  88. while( cnt > 0 )
  89. {
  90. if ( (pixel & 1) != 0 )
  91. {
  92. chgr_set_pixel(x,y);
  93. }
  94. else
  95. {
  96. chgr_clr_pixel(x,y);
  97. }
  98. pixel >>= 1;
  99. y+=f;
  100. cnt--;
  101. }
  102. }
  103. void chgr_set_multiple_8pixel(int x, int y, int cnt, uint8_t *pixel, uint16_t f)
  104. {
  105. uint8_t b;
  106. while( cnt > 0 )
  107. {
  108. b = *pixel;
  109. chgr_set_8pixel(x, y, b, f);
  110. x+=f;
  111. pixel++;
  112. cnt--;
  113. }
  114. }
  115. /*==========================================*/
  116. /* tga description procedures */
  117. static const u8x8_display_info_t u8x8_utf8_info =
  118. {
  119. /* chip_enable_level = */ 0,
  120. /* chip_disable_level = */ 1,
  121. /* post_chip_enable_wait_ns = */ 0,
  122. /* pre_chip_disable_wait_ns = */ 0,
  123. /* reset_pulse_width_ms = */ 0,
  124. /* post_reset_wait_ms = */ 0,
  125. /* sda_setup_time_ns = */ 0,
  126. /* sck_pulse_width_ns = */ 0,
  127. /* sck_clock_hz = */ 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  128. /* spi_mode = */ 1,
  129. /* i2c_bus_clock_100kHz = */ 0,
  130. /* data_setup_time_ns = */ 0,
  131. /* write_pulse_width_ns = */ 0,
  132. /* tile_width = */ (CHGR_WIDTH+7)/8,
  133. /* tile_hight = */ (CHGR_HEIGHT+7)/8,
  134. /* default_x_offset = */ 0,
  135. /* flipmode_x_offset = */ 0,
  136. /* pixel_width = */ CHGR_WIDTH,
  137. /* pixel_height = */ CHGR_HEIGHT
  138. };
  139. uint8_t u8x8_d_utf8(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  140. {
  141. u8g2_uint_t x, y, c;
  142. uint8_t *ptr;
  143. switch(msg)
  144. {
  145. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  146. u8x8_d_helper_display_setup_memory(u8g2, &u8x8_utf8_info);
  147. break;
  148. case U8X8_MSG_DISPLAY_INIT:
  149. u8x8_d_helper_display_init(u8g2);
  150. chgr_clear();
  151. break;
  152. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  153. break;
  154. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  155. break;
  156. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  157. break;
  158. case U8X8_MSG_DISPLAY_DRAW_TILE:
  159. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  160. //printf("U8X8_MSG_DISPLAY_DRAW_TILE x=%d, ", x);
  161. x *= 8;
  162. x += u8g2->x_offset;
  163. y = ((u8x8_tile_t *)arg_ptr)->y_pos;
  164. //printf("y=%d, c=%d\n", y, ((u8x8_tile_t *)arg_ptr)->cnt);
  165. y *= 8;
  166. do
  167. {
  168. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  169. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  170. chgr_set_multiple_8pixel(x, y, c*8, ptr, 1);
  171. arg_int--;
  172. x += c*8;
  173. } while( arg_int > 0 );
  174. break;
  175. default:
  176. return 0;
  177. }
  178. return 1;
  179. }
  180. void u8x8_Setup_Utf8(u8x8_t *u8x8)
  181. {
  182. /* setup defaults */
  183. u8x8_SetupDefaults(u8x8);
  184. /* setup specific callbacks */
  185. u8x8->display_cb = u8x8_d_utf8;
  186. /* setup display info */
  187. u8x8_SetupMemory(u8x8);
  188. }
  189. void u8g2_SetupBuffer_Utf8(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb)
  190. {
  191. static uint8_t buf[CHGR_WIDTH*8]; // i guess this is to big. it should be (CHGR_WIDTH+7)/8
  192. u8x8_Setup_Utf8(u8g2_GetU8x8(u8g2));
  193. // u8g2_ll_hvline_vertical_top_lsb
  194. // u8g2_ll_hvline_horizontal_right_lsb
  195. u8g2_SetupBuffer(u8g2, buf, 1, u8g2_ll_hvline_vertical_top_lsb, u8g2_cb);
  196. }
  197. void utf8_show(void)
  198. {
  199. chgr_show();
  200. }