main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "u8g2.h"
  2. #include <stdio.h>
  3. u8g2_t u8g2;
  4. u8g2_t u8g2_bitmap;
  5. /* width and height must be multipe of 8 */
  6. #define BITMAP_WIDTH 16
  7. #define BITMAP_HEIGHT 16
  8. static unsigned char bitmap_data[BITMAP_WIDTH/8*BITMAP_HEIGHT];
  9. static void u8g2_draw_pixel_horizontal_xbm(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y)
  10. {
  11. uint16_t offset;
  12. uint8_t *ptr;
  13. uint8_t bit_pos, mask;
  14. bit_pos = x; /* overflow truncate is ok here... */
  15. bit_pos &= 7; /* ... because only the lowest 3 bits are needed */
  16. mask = 1;
  17. mask <<= bit_pos;
  18. x >>= 3;
  19. offset = y; /* y might be 8 or 16 bit, but we need 16 bit, so use a 16 bit variable */
  20. offset *= u8g2->pixel_buf_width/8;
  21. offset += x;
  22. ptr = u8g2->tile_buf_ptr;
  23. ptr += offset;
  24. if ( u8g2->draw_color <= 1 )
  25. *ptr |= mask;
  26. if ( u8g2->draw_color != 1 )
  27. *ptr ^= mask;
  28. }
  29. /*
  30. x,y Upper left position of the line within the local buffer (not the display!)
  31. len length of the line in pixel, len must not be 0
  32. dir 0: horizontal line (left to right)
  33. 1: vertical line (top to bottom)
  34. asumption:
  35. all clipping done
  36. */
  37. void u8g2_ll_hvline_xbm(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir)
  38. {
  39. if ( dir == 0 )
  40. {
  41. do
  42. {
  43. u8g2_draw_pixel_horizontal_xbm(u8g2, x, y);
  44. x++;
  45. len--;
  46. } while( len != 0 );
  47. }
  48. else
  49. {
  50. do
  51. {
  52. u8g2_draw_pixel_horizontal_xbm(u8g2, x, y);
  53. y++;
  54. len--;
  55. } while( len != 0 );
  56. }
  57. }
  58. void u8g2_update_dimension_xbm(u8g2_t *u8g2)
  59. {
  60. /* do nothing */
  61. }
  62. const u8g2_cb_t u8g2_cb_xbm = { u8g2_update_dimension_xbm, u8g2_draw_l90_r0 };
  63. int main(void)
  64. {
  65. int x, y;
  66. int k;
  67. int i;
  68. /* setup bitmap */
  69. u8x8_SetupDefaults(u8g2_GetU8x8(&u8g2_bitmap));
  70. u8g2_SetupBuffer(&u8g2_bitmap, bitmap_data, BITMAP_HEIGHT/8, u8g2_ll_hvline_xbm, &u8g2_cb_xbm);
  71. u8g2_bitmap.pixel_buf_height = BITMAP_HEIGHT;
  72. u8g2_bitmap.pixel_buf_width= ((BITMAP_WIDTH+7)/8)*8;
  73. u8g2_bitmap.pixel_curr_row=0;
  74. u8g2_bitmap.buf_y0 = 0;
  75. u8g2_bitmap.buf_y1 = u8g2_bitmap.pixel_buf_height;
  76. u8g2_bitmap.width = u8g2_bitmap.pixel_buf_width;
  77. u8g2_bitmap.height = u8g2_bitmap.pixel_buf_height;
  78. u8g2_bitmap.user_x0 = 0;
  79. u8g2_bitmap.user_x1 = u8g2_bitmap.pixel_buf_width;
  80. u8g2_bitmap.user_y0 = u8g2_bitmap.buf_y0;
  81. u8g2_bitmap.user_y1 = u8g2_bitmap.buf_y1;
  82. /* draw something into the bitmap */
  83. /* send buffer is not required, in fact it MUST NOT BE CALLED */
  84. u8g2_DrawLine(&u8g2_bitmap, 0, 0, 15, 15 );
  85. u8g2_SetFont(&u8g2_bitmap, u8g2_font_6x10_tr);
  86. u8g2_DrawStr(&u8g2_bitmap, 0, 11, "ABC");
  87. /* setup target display */
  88. u8g2_SetupBuffer_SDL_128x64_4(&u8g2, &u8g2_cb_r0);
  89. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  90. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  91. u8g2_SetFont(&u8g2, u8g2_font_6x10_tf);
  92. x = 30;
  93. y = 31-7;
  94. for(;;)
  95. {
  96. #ifdef U8G2_WITH_HVLINE_COUNT
  97. u8g2.hv_cnt = 0UL;
  98. #endif /* U8G2_WITH_HVLINE_COUNT */
  99. /*
  100. u8g2_ClearBuffer(&u8g2);
  101. u8g2_SetFontDirection(&u8g2, 0);
  102. u8g2_DrawStr(&u8g2, x, y, "ABC");
  103. u8g2_SetFontDirection(&u8g2, 1);
  104. u8g2_DrawStr(&u8g2, x, y, "abc");
  105. u8g2_SetFontDirection(&u8g2, 2);
  106. u8g2_DrawStr(&u8g2, x, y, "abc");
  107. u8g2_SetFontDirection(&u8g2, 3);
  108. u8g2_DrawStr(&u8g2, x, y, "abc");
  109. u8g2_SendBuffer(&u8g2);
  110. */
  111. u8g2_FirstPage(&u8g2);
  112. i = 0;
  113. do
  114. {
  115. u8g2_DrawXBMP(&u8g2, x, y, BITMAP_WIDTH, BITMAP_HEIGHT, u8g2_bitmap.tile_buf_ptr);
  116. if ( i == 1 )
  117. {
  118. u8g2_DrawHVLine(&u8g2, u8g2.user_x0, u8g2.user_y0, 1, 0);
  119. u8g2_DrawHVLine(&u8g2, u8g2.user_x0, u8g2.user_y1-1, 1, 0);
  120. u8g2_DrawHVLine(&u8g2, u8g2.user_x1-1, u8g2.user_y1-1, 1, 0);
  121. u8g2_DrawHVLine(&u8g2, u8g2.user_x1-1, u8g2.user_y0, 1, 0);
  122. }
  123. i++;
  124. } while( u8g2_NextPage(&u8g2) );
  125. #ifdef U8G2_WITH_HVLINE_COUNT
  126. printf("hv cnt: %ld\n", u8g2.hv_cnt);
  127. #endif /* U8G2_WITH_HVLINE_COUNT */
  128. do
  129. {
  130. k = u8g_sdl_get_key();
  131. } while( k < 0 );
  132. if ( k == 273 ) y -= 1;
  133. if ( k == 274 ) y += 1;
  134. if ( k == 276 ) x -= 1;
  135. if ( k == 275 ) x += 1;
  136. if ( k == 'e' ) y -= 1;
  137. if ( k == 'x' ) y += 1;
  138. if ( k == 's' ) x -= 1;
  139. if ( k == 'd' ) x += 1;
  140. if ( k == 'q' ) break;
  141. }
  142. //u8x8_Set8x8Font(u8g2_GetU8x8(&u8g2), bdf_font);
  143. //u8x8_Draw8x8String(u8g2_GetU8x8(&u8g2), 0, 0, "Hello World!");
  144. return 0;
  145. }