u8g_pb16v2.c 4.4 KB

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