u8g_pbxh16.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. u8g_pbxh16.c
  3. x lines per page, horizontal, 16 bits per pixel (hi color modes)
  4. Universal 8bit Graphics Library
  5. Copyright (c) 2013, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. struct _u8g_pb_t
  28. {
  29. u8g_page_t p;
  30. u8g_uint_t width;
  31. void *buf;
  32. };
  33. typedef struct _u8g_pb_t u8g_pb_t;
  34. uint8_t u8g_index_color_xh16_buf[2*WIDTH*PAGE_HEIGHT] U8G_NOCOMMON ;
  35. u8g_pb_t u8g_index_color_xh16_pb = { {PAGE_HEIGHT, HEIGHT, 0, 0, 0}, WIDTH, u8g_index_color_xh16_buf};
  36. u8g_dev_t name = { dev_fn, &u8g_index_color_xh16_pb , com_fn }
  37. */
  38. #include "u8g.h"
  39. /*
  40. #define WIDTH_BITS 7
  41. #define WIDTH (1<<WIDTH_BITS)
  42. #define PAGE_HEIGHT_BITS 3
  43. #define PAGE_HEIGHT (1<<PAGE_HEIGHT_BITS)
  44. */
  45. void u8g_pbxh16_Clear(u8g_pb_t *b)
  46. {
  47. uint8_t *ptr = (uint8_t *)b->buf;
  48. uint8_t *end_ptr = ptr;
  49. uint8_t cnt = b->p.page_height;
  50. do
  51. {
  52. end_ptr += b->width*2;
  53. cnt--;
  54. } while( cnt > 0 );
  55. do
  56. {
  57. *ptr++ = 0;
  58. } while( ptr != end_ptr );
  59. }
  60. void u8g_pbxh16_Init(u8g_pb_t *b, void *buf, u8g_uint_t width)
  61. {
  62. b->buf = buf;
  63. b->width = width;
  64. u8g_pbxh16_Clear(b);
  65. }
  66. static void u8g_pbxh16_set_pixel(u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y, uint8_t low, uint8_t high)
  67. {
  68. uint16_t tmp;
  69. uint8_t *ptr = b->buf;
  70. y -= b->p.page_y0;
  71. tmp = y;
  72. tmp *= b->width;
  73. tmp += x;
  74. tmp <<= 1;
  75. ptr += tmp;
  76. *ptr = low;
  77. ptr++;
  78. *ptr = high;
  79. }
  80. void u8g_pbxh16_SetPixel(u8g_pb_t *b, const u8g_dev_arg_pixel_t * const arg_pixel)
  81. {
  82. if ( arg_pixel->y < b->p.page_y0 )
  83. return;
  84. if ( arg_pixel->y > b->p.page_y1 )
  85. return;
  86. if ( arg_pixel->x >= b->width )
  87. return;
  88. u8g_pbxh16_set_pixel(b, arg_pixel->x, arg_pixel->y, arg_pixel->color, arg_pixel->hi_color);
  89. }
  90. void u8g_pbxh16_Set8Pixel(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel)
  91. {
  92. register uint8_t pixel = arg_pixel->pixel;
  93. u8g_uint_t dx = 0;
  94. u8g_uint_t dy = 0;
  95. switch( arg_pixel->dir )
  96. {
  97. case 0: dx++; break;
  98. case 1: dy++; break;
  99. case 2: dx--; break;
  100. case 3: dy--; break;
  101. }
  102. do
  103. {
  104. if ( pixel & 128 )
  105. u8g_pbxh16_SetPixel(b, arg_pixel);
  106. arg_pixel->x += dx;
  107. arg_pixel->y += dy;
  108. pixel <<= 1;
  109. } while( pixel != 0 );
  110. }
  111. uint8_t u8g_dev_pbxh16_base_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  112. {
  113. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  114. switch(msg)
  115. {
  116. case U8G_DEV_MSG_SET_8PIXEL:
  117. if ( u8g_pb_Is8PixelVisible(pb, (u8g_dev_arg_pixel_t *)arg) )
  118. u8g_pbxh16_Set8Pixel(pb, (u8g_dev_arg_pixel_t *)arg);
  119. break;
  120. case U8G_DEV_MSG_SET_PIXEL:
  121. u8g_pbxh16_SetPixel(pb, (u8g_dev_arg_pixel_t *)arg);
  122. break;
  123. case U8G_DEV_MSG_INIT:
  124. break;
  125. case U8G_DEV_MSG_STOP:
  126. break;
  127. case U8G_DEV_MSG_PAGE_FIRST:
  128. u8g_pbxh16_Clear(pb);
  129. u8g_page_First(&(pb->p));
  130. break;
  131. case U8G_DEV_MSG_PAGE_NEXT:
  132. if ( u8g_page_Next(&(pb->p)) == 0 )
  133. return 0;
  134. u8g_pbxh16_Clear(pb);
  135. break;
  136. #ifdef U8G_DEV_MSG_IS_BBX_INTERSECTION
  137. case U8G_DEV_MSG_IS_BBX_INTERSECTION:
  138. return u8g_pb_IsIntersection(pb, (u8g_dev_arg_bbx_t *)arg);
  139. #endif
  140. case U8G_DEV_MSG_GET_PAGE_BOX:
  141. u8g_pb_GetPageBox(pb, (u8g_box_t *)arg);
  142. break;
  143. case U8G_DEV_MSG_GET_WIDTH:
  144. *((u8g_uint_t *)arg) = pb->width;
  145. break;
  146. case U8G_DEV_MSG_GET_HEIGHT:
  147. *((u8g_uint_t *)arg) = pb->p.total_height;
  148. break;
  149. case U8G_DEV_MSG_SET_COLOR_ENTRY:
  150. break;
  151. case U8G_DEV_MSG_SET_XY_CB:
  152. break;
  153. case U8G_DEV_MSG_GET_MODE:
  154. return U8G_MODE_HICOLOR;
  155. }
  156. return 1;
  157. }