u8g_pb.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. u8g_pb.c
  3. common procedures for the page buffer
  4. Universal 8bit Graphics Library
  5. Copyright (c) 2011, 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. */
  28. #include "u8g.h"
  29. void u8g_pb_Clear(u8g_pb_t *b)
  30. {
  31. uint8_t *ptr = (uint8_t *)b->buf;
  32. uint8_t *end_ptr = ptr;
  33. end_ptr += b->width;
  34. do
  35. {
  36. *ptr++ = 0;
  37. } while( ptr != end_ptr );
  38. }
  39. /* the following procedure does not work. why? Can be checked with descpic */
  40. /*
  41. void u8g_pb_Clear(u8g_pb_t *b)
  42. {
  43. uint8_t *ptr = (uint8_t *)b->buf;
  44. uint8_t cnt = b->width;
  45. do
  46. {
  47. *ptr++ = 0;
  48. cnt--;
  49. } while( cnt != 0 );
  50. }
  51. */
  52. /*
  53. intersection assumptions:
  54. a1 <= a2 is always true
  55. */
  56. /*
  57. minimized version
  58. ---1----0 1 b1 <= a2 && b1 > b2
  59. -----1--0 1 b2 >= a1 && b1 > b2
  60. ---1-1--- 1 b1 <= a2 && b2 >= a1
  61. */
  62. /*
  63. uint8_t u8g_pb8v1_IsYIntersection___Old(u8g_pb_t *b, u8g_uint_t v0, u8g_uint_t v1)
  64. {
  65. uint8_t c0, c1, c;
  66. c0 = v0 <= b->p.page_y1;
  67. c1 = v1 >= b->p.page_y0;
  68. c = v0 > v1;
  69. if ( c0 && c1 ) return 1;
  70. if ( c0 && c ) return 1;
  71. if ( c1 && c ) return 1;
  72. return 0;
  73. }
  74. */
  75. uint8_t u8g_pb_IsYIntersection(u8g_pb_t *pb, u8g_uint_t v0, u8g_uint_t v1)
  76. {
  77. uint8_t c1, c2, c3, tmp;
  78. c1 = v0 <= pb->p.page_y1;
  79. c2 = v1 >= pb->p.page_y0;
  80. c3 = v0 > v1;
  81. /*
  82. if ( c1 && c2 )
  83. return 1;
  84. if ( c1 && c3 )
  85. return 1;
  86. if ( c2 && c3 )
  87. return 1;
  88. return 0;
  89. */
  90. tmp = c1;
  91. c1 &= c2;
  92. c2 &= c3;
  93. c3 &= tmp;
  94. c1 |= c2;
  95. c1 |= c3;
  96. return c1 & 1;
  97. }
  98. uint8_t u8g_pb_IsXIntersection(u8g_pb_t *b, u8g_uint_t v0, u8g_uint_t v1)
  99. {
  100. uint8_t /*c0, c1, */ c2, c3;
  101. /*
  102. conditions: b->p.page_y0 < b->p.page_y1
  103. there are no restriction on v0 and v1. If v0 > v1, then warp around unsigned is assumed
  104. */
  105. /*
  106. c0 = v0 < 0;
  107. c1 = v1 < 0;
  108. */
  109. c2 = v0 > b->width;
  110. c3 = v1 > b->width;
  111. /*if ( c0 && c1 ) return 0;*/
  112. if ( c2 && c3 ) return 0;
  113. /*if ( c1 && c2 ) return 0;*/
  114. return 1;
  115. }
  116. uint8_t u8g_pb_IsIntersection(u8g_pb_t *pb, u8g_dev_arg_bbx_t *bbx)
  117. {
  118. u8g_uint_t tmp;
  119. tmp = bbx->y;
  120. tmp += bbx->h;
  121. tmp--;
  122. if ( u8g_pb_IsYIntersection(pb, bbx->y, tmp) == 0 )
  123. return 0;
  124. /* maybe this one can be skiped... probability is very high to have an intersection, so it would be ok to always return 1 */
  125. tmp = bbx->x;
  126. tmp += bbx->w;
  127. tmp--;
  128. return u8g_pb_IsXIntersection(pb, bbx->x, tmp);
  129. }
  130. void u8g_pb_GetPageBox(u8g_pb_t *pb, u8g_box_t *box)
  131. {
  132. box->x0 = 0;
  133. box->y0 = pb->p.page_y0;
  134. box->x1 = pb->width;
  135. box->x1--;
  136. box->y1 = pb->p.page_y1;
  137. }
  138. uint8_t u8g_pb_Is8PixelVisible(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel)
  139. {
  140. u8g_uint_t v0, v1;
  141. v0 = arg_pixel->y;
  142. v1 = v0;
  143. switch( arg_pixel->dir )
  144. {
  145. case 0:
  146. break;
  147. case 1:
  148. v1 += 8; /* this is independent from the page height */
  149. break;
  150. case 2:
  151. break;
  152. case 3:
  153. v0 -= 8;
  154. break;
  155. }
  156. return u8g_pb_IsYIntersection(b, v0, v1);
  157. }
  158. uint8_t u8g_pb_WriteBuffer(u8g_pb_t *b, u8g_t *u8g, u8g_dev_t *dev)
  159. {
  160. return u8g_WriteSequence(u8g, dev, b->width, b->buf);
  161. }