u8g_rect.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. u8g_rect.c
  3. U8G high level interface for horizontal and vertical things
  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_draw_hline(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w)
  30. {
  31. uint8_t pixel = 0x0ff;
  32. while( w >= 8 )
  33. {
  34. u8g_Draw8Pixel(u8g, x, y, 0, pixel);
  35. w-=8;
  36. x+=8;
  37. }
  38. if ( w != 0 )
  39. {
  40. w ^=7;
  41. w++;
  42. pixel <<= w&7;
  43. u8g_Draw8Pixel(u8g, x, y, 0, pixel);
  44. }
  45. }
  46. void u8g_draw_vline(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t h)
  47. {
  48. uint8_t pixel = 0x0ff;
  49. while( h >= 8 )
  50. {
  51. u8g_Draw8Pixel(u8g, x, y, 1, pixel);
  52. h-=8;
  53. y+=8;
  54. }
  55. if ( h != 0 )
  56. {
  57. h ^=7;
  58. h++;
  59. pixel <<= h&7;
  60. u8g_Draw8Pixel(u8g, x, y, 1, pixel);
  61. }
  62. }
  63. void u8g_DrawHLine(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w)
  64. {
  65. if ( u8g_IsBBXIntersection(u8g, x, y, w, 1) == 0 )
  66. return;
  67. u8g_draw_hline(u8g, x, y, w);
  68. }
  69. void u8g_DrawVLine(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w)
  70. {
  71. if ( u8g_IsBBXIntersection(u8g, x, y, 1, w) == 0 )
  72. return;
  73. u8g_draw_vline(u8g, x, y, w);
  74. }
  75. /* restrictions: w > 0 && h > 0 */
  76. void u8g_DrawFrame(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h)
  77. {
  78. u8g_uint_t xtmp = x;
  79. if ( u8g_IsBBXIntersection(u8g, x, y, w, h) == 0 )
  80. return;
  81. u8g_draw_hline(u8g, x, y, w);
  82. u8g_draw_vline(u8g, x, y, h);
  83. x+=w;
  84. x--;
  85. u8g_draw_vline(u8g, x, y, h);
  86. y+=h;
  87. y--;
  88. u8g_draw_hline(u8g, xtmp, y, w);
  89. }
  90. void u8g_draw_box(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h)
  91. {
  92. do
  93. {
  94. u8g_draw_hline(u8g, x, y, w);
  95. y++;
  96. h--;
  97. } while( h != 0 );
  98. }
  99. /* restrictions: h > 0 */
  100. void u8g_DrawBox(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h)
  101. {
  102. if ( u8g_IsBBXIntersection(u8g, x, y, w, h) == 0 )
  103. return;
  104. u8g_draw_box(u8g, x, y, w, h);
  105. }
  106. void u8g_DrawRFrame(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, u8g_uint_t r)
  107. {
  108. u8g_uint_t xl, yu;
  109. if ( u8g_IsBBXIntersection(u8g, x, y, w, h) == 0 )
  110. return;
  111. xl = x;
  112. xl += r;
  113. yu = y;
  114. yu += r;
  115. {
  116. u8g_uint_t yl, xr;
  117. xr = x;
  118. xr += w;
  119. xr -= r;
  120. xr -= 1;
  121. yl = y;
  122. yl += h;
  123. yl -= r;
  124. yl -= 1;
  125. u8g_draw_circle(u8g, xl, yu, r, U8G_DRAW_UPPER_LEFT);
  126. u8g_draw_circle(u8g, xr, yu, r, U8G_DRAW_UPPER_RIGHT);
  127. u8g_draw_circle(u8g, xl, yl, r, U8G_DRAW_LOWER_LEFT);
  128. u8g_draw_circle(u8g, xr, yl, r, U8G_DRAW_LOWER_RIGHT);
  129. }
  130. {
  131. u8g_uint_t ww, hh;
  132. ww = w;
  133. ww -= r;
  134. ww -= r;
  135. ww -= 2;
  136. hh = h;
  137. hh -= r;
  138. hh -= r;
  139. hh -= 2;
  140. xl++;
  141. yu++;
  142. h--;
  143. w--;
  144. u8g_draw_hline(u8g, xl, y, ww);
  145. u8g_draw_hline(u8g, xl, y+h, ww);
  146. u8g_draw_vline(u8g, x, yu, hh);
  147. u8g_draw_vline(u8g, x+w, yu, hh);
  148. }
  149. }
  150. void u8g_DrawRBox(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, u8g_uint_t r)
  151. {
  152. u8g_uint_t xl, yu;
  153. u8g_uint_t yl, xr;
  154. if ( u8g_IsBBXIntersection(u8g, x, y, w, h) == 0 )
  155. return;
  156. xl = x;
  157. xl += r;
  158. yu = y;
  159. yu += r;
  160. xr = x;
  161. xr += w;
  162. xr -= r;
  163. xr -= 1;
  164. yl = y;
  165. yl += h;
  166. yl -= r;
  167. yl -= 1;
  168. u8g_draw_disc(u8g, xl, yu, r, U8G_DRAW_UPPER_LEFT);
  169. u8g_draw_disc(u8g, xr, yu, r, U8G_DRAW_UPPER_RIGHT);
  170. u8g_draw_disc(u8g, xl, yl, r, U8G_DRAW_LOWER_LEFT);
  171. u8g_draw_disc(u8g, xr, yl, r, U8G_DRAW_LOWER_RIGHT);
  172. {
  173. u8g_uint_t ww, hh;
  174. ww = w;
  175. ww -= r;
  176. ww -= r;
  177. ww -= 2;
  178. hh = h;
  179. hh -= r;
  180. hh -= r;
  181. hh -= 2;
  182. xl++;
  183. yu++;
  184. h--;
  185. u8g_draw_box(u8g, xl, y, ww, r+1);
  186. u8g_draw_box(u8g, xl, yl, ww, r+1);
  187. //u8g_draw_hline(u8g, xl, y+h, ww);
  188. u8g_draw_box(u8g, x, yu, w, hh);
  189. //u8g_draw_vline(u8g, x+w, yu, hh);
  190. }
  191. }