u8g_pb8h1.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. u8g_pb8h1.c
  3. 8bit height monochrom (1 bit) page buffer
  4. byte has horizontal orientation
  5. Universal 8bit Graphics Library
  6. Copyright (c) 2011, 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. total buffer size is limited to 256 bytes because of the calculation inside the set pixel procedure
  29. 23. Sep 2012: Bug with down procedure, see FPS 1st page --> fixed (bug located in u8g_clip.c)
  30. */
  31. #include "u8g.h"
  32. #include <string.h>
  33. #ifdef __unix__
  34. #include <assert.h>
  35. #endif
  36. /* NEW_CODE disabled, because the performance increase was too slow and not worth compared */
  37. /* to the increase of code size */
  38. /* #define NEW_CODE */
  39. #ifdef __unix__
  40. void *u8g_buf_lower_limit;
  41. void *u8g_buf_upper_limit;
  42. #endif
  43. void u8g_pb8h1_Init(u8g_pb_t *b, void *buf, u8g_uint_t width) U8G_NOINLINE;
  44. void u8g_pb8h1_set_pixel(u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y, uint8_t color_index) U8G_NOINLINE;
  45. void u8g_pb8h1_SetPixel(u8g_pb_t *b, const u8g_dev_arg_pixel_t * const arg_pixel) U8G_NOINLINE ;
  46. void u8g_pb8h1_Set8PixelStd(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel) U8G_NOINLINE;
  47. uint8_t u8g_dev_pb8h1_base_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg);
  48. #ifdef NEW_CODE
  49. struct u8g_pb_h1_struct
  50. {
  51. u8g_uint_t x;
  52. u8g_uint_t y;
  53. uint8_t *ptr;
  54. uint8_t mask;
  55. uint8_t line_byte_len;
  56. uint8_t cnt;
  57. };
  58. static uint8_t u8g_pb8h1_bitmask[8] = { 0x080, 0x040, 0x020, 0x010, 0x008, 0x004, 0x002, 0x001 };
  59. static void u8g_pb8h1_state_right(struct u8g_pb_h1_struct *s) U8G_NOINLINE;
  60. static void u8g_pb8h1_state_right(struct u8g_pb_h1_struct *s)
  61. {
  62. register u8g_uint_t x;
  63. x = s->x;
  64. x++;
  65. s->x = x;
  66. x &= 7;
  67. s->mask = u8g_pb8h1_bitmask[x];
  68. if ( x == 0 )
  69. s->ptr++;
  70. }
  71. static void u8g_pb8h1_state_left(struct u8g_pb_h1_struct *s)
  72. {
  73. register u8g_uint_t x;
  74. x = s->x;
  75. x--;
  76. s->x = x;
  77. x &= 7;
  78. s->mask = u8g_pb8h1_bitmask[x];
  79. if ( x == 7 )
  80. s->ptr--;
  81. }
  82. static void u8g_pb8h1_state_down(struct u8g_pb_h1_struct *s)
  83. {
  84. s->y++;
  85. s->ptr += s->line_byte_len;
  86. }
  87. static void u8g_pb8h1_state_up(struct u8g_pb_h1_struct *s)
  88. {
  89. s->y--;
  90. s->ptr -= s->line_byte_len;
  91. }
  92. static void u8g_pb8h1_state_init(struct u8g_pb_h1_struct *s, u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y) U8G_NOINLINE;
  93. static void u8g_pb8h1_state_init(struct u8g_pb_h1_struct *s, u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y)
  94. {
  95. u8g_uint_t tmp;
  96. uint8_t *ptr = b->buf;
  97. s->x = x;
  98. s->y = y;
  99. y -= b->p.page_y0;
  100. tmp = b->width;
  101. tmp >>= 3;
  102. s->line_byte_len = tmp;
  103. /* assume negative y values, can be down to -7, subtract this from the pointer and add correction of 8 to y */
  104. ptr -= tmp*8;
  105. y+=8;
  106. /* it is important that the result of tmp*y can be 16 bit value also for 8 bit mode */
  107. ptr += tmp*y;
  108. s->mask = u8g_pb8h1_bitmask[x & 7];
  109. /* assume negative x values (to -7), subtract 8 pixel from the pointer and add 8 to x */
  110. ptr--;
  111. x += 8;
  112. x >>= 3;
  113. ptr += x;
  114. s->ptr = ptr;
  115. }
  116. static void u8g_pb8h1_state_set_pixel(struct u8g_pb_h1_struct *s, uint8_t color_index) U8G_NOINLINE;
  117. static void u8g_pb8h1_state_set_pixel(struct u8g_pb_h1_struct *s, uint8_t color_index)
  118. {
  119. #ifdef __unix__
  120. assert( s->ptr >= u8g_buf_lower_limit );
  121. assert( s->ptr < u8g_buf_upper_limit );
  122. #endif
  123. if ( color_index )
  124. {
  125. *s->ptr |= s->mask;
  126. }
  127. else
  128. {
  129. uint8_t mask = s->mask;
  130. mask ^=0xff;
  131. *s->ptr &= mask;
  132. }
  133. }
  134. #endif
  135. void u8g_pb8h1_Init(u8g_pb_t *b, void *buf, u8g_uint_t width)
  136. {
  137. b->buf = buf;
  138. b->width = width;
  139. u8g_pb_Clear(b);
  140. }
  141. /* limitation: total buffer must not exceed 256 bytes */
  142. void u8g_pb8h1_set_pixel(u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y, uint8_t color_index)
  143. {
  144. #ifdef NEW_CODE
  145. struct u8g_pb_h1_struct s;
  146. u8g_pb8h1_state_init(&s, b, x, y);
  147. u8g_pb8h1_state_set_pixel(&s, color_index);
  148. // u8g_pb8h1_state_up(&s);
  149. // if ( s.y > b->p.page_y1 )
  150. // return;
  151. // if ( s.x > b->width )
  152. // return;
  153. // u8g_pb8h1_state_set_pixel(&s, color_index);
  154. #else
  155. register uint8_t mask;
  156. u8g_uint_t tmp;
  157. uint8_t *ptr = b->buf;
  158. y -= b->p.page_y0;
  159. tmp = b->width;
  160. tmp >>= 3;
  161. tmp *= (uint8_t)y;
  162. ptr += tmp;
  163. mask = 0x080;
  164. mask >>= x & 7;
  165. x >>= 3;
  166. ptr += x;
  167. if ( color_index )
  168. {
  169. *ptr |= mask;
  170. }
  171. else
  172. {
  173. mask ^=0xff;
  174. *ptr &= mask;
  175. }
  176. #endif
  177. }
  178. void u8g_pb8h1_SetPixel(u8g_pb_t *b, const u8g_dev_arg_pixel_t * const arg_pixel)
  179. {
  180. if ( arg_pixel->y < b->p.page_y0 )
  181. return;
  182. if ( arg_pixel->y > b->p.page_y1 )
  183. return;
  184. if ( arg_pixel->x >= b->width )
  185. return;
  186. u8g_pb8h1_set_pixel(b, arg_pixel->x, arg_pixel->y, arg_pixel->color);
  187. }
  188. void u8g_pb8h1_Set8PixelStd(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel)
  189. {
  190. register uint8_t pixel = arg_pixel->pixel;
  191. do
  192. {
  193. if ( pixel & 128 )
  194. {
  195. u8g_pb8h1_SetPixel(b, arg_pixel);
  196. }
  197. switch( arg_pixel->dir )
  198. {
  199. case 0: arg_pixel->x++; break;
  200. case 1: arg_pixel->y++; break;
  201. case 2: arg_pixel->x--; break;
  202. case 3: arg_pixel->y--; break;
  203. }
  204. pixel <<= 1;
  205. } while( pixel != 0 );
  206. }
  207. void u8g_pb8h1_Set8PixelOpt2(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel)
  208. {
  209. register uint8_t pixel = arg_pixel->pixel;
  210. u8g_uint_t dx = 0;
  211. u8g_uint_t dy = 0;
  212. switch( arg_pixel->dir )
  213. {
  214. case 0: dx++; break;
  215. case 1: dy++; break;
  216. case 2: dx--; break;
  217. case 3: dy--; break;
  218. }
  219. do
  220. {
  221. if ( pixel & 128 )
  222. u8g_pb8h1_SetPixel(b, arg_pixel);
  223. arg_pixel->x += dx;
  224. arg_pixel->y += dy;
  225. pixel <<= 1;
  226. } while( pixel != 0 );
  227. }
  228. #ifdef NEW_CODE
  229. static void u8g_pb8h1_Set8PixelState(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel)
  230. {
  231. register uint8_t pixel = arg_pixel->pixel;
  232. struct u8g_pb_h1_struct s;
  233. uint8_t cnt;
  234. u8g_pb8h1_state_init(&s, b, arg_pixel->x, arg_pixel->y);
  235. cnt = 8;
  236. switch( arg_pixel->dir )
  237. {
  238. case 0:
  239. do
  240. {
  241. if ( s.x < b->width )
  242. if ( pixel & 128 )
  243. u8g_pb8h1_state_set_pixel(&s, arg_pixel->color);
  244. u8g_pb8h1_state_right(&s);
  245. pixel <<= 1;
  246. cnt--;
  247. } while( cnt > 0 && pixel != 0 );
  248. break;
  249. case 1:
  250. do
  251. {
  252. if ( s.y >= b->p.page_y0 )
  253. if ( s.y <= b->p.page_y1 )
  254. if ( pixel & 128 )
  255. u8g_pb8h1_state_set_pixel(&s, arg_pixel->color);
  256. u8g_pb8h1_state_down(&s);
  257. pixel <<= 1;
  258. cnt--;
  259. } while( cnt > 0 && pixel != 0 );
  260. break;
  261. case 2:
  262. do
  263. {
  264. if ( s.x < b->width )
  265. if ( pixel & 128 )
  266. u8g_pb8h1_state_set_pixel(&s, arg_pixel->color);
  267. u8g_pb8h1_state_left(&s);
  268. pixel <<= 1;
  269. cnt--;
  270. } while( cnt > 0 && pixel != 0 );
  271. break;
  272. case 3:
  273. do
  274. {
  275. if ( s.y >= b->p.page_y0 )
  276. if ( s.y <= b->p.page_y1 )
  277. if ( pixel & 128 )
  278. u8g_pb8h1_state_set_pixel(&s, arg_pixel->color);
  279. u8g_pb8h1_state_up(&s);
  280. pixel <<= 1;
  281. cnt--;
  282. } while( cnt > 0 && pixel != 0 );
  283. break;
  284. }
  285. }
  286. #endif
  287. uint8_t u8g_dev_pb8h1_base_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  288. {
  289. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  290. switch(msg)
  291. {
  292. case U8G_DEV_MSG_SET_8PIXEL:
  293. #ifdef NEW_CODE
  294. if ( u8g_pb_Is8PixelVisible(pb, (u8g_dev_arg_pixel_t *)arg) )
  295. u8g_pb8h1_Set8PixelState(pb, (u8g_dev_arg_pixel_t *)arg);
  296. #else
  297. if ( u8g_pb_Is8PixelVisible(pb, (u8g_dev_arg_pixel_t *)arg) )
  298. u8g_pb8h1_Set8PixelOpt2(pb, (u8g_dev_arg_pixel_t *)arg);
  299. #endif
  300. break;
  301. case U8G_DEV_MSG_SET_PIXEL:
  302. u8g_pb8h1_SetPixel(pb, (u8g_dev_arg_pixel_t *)arg);
  303. break;
  304. case U8G_DEV_MSG_INIT:
  305. break;
  306. case U8G_DEV_MSG_STOP:
  307. break;
  308. case U8G_DEV_MSG_PAGE_FIRST:
  309. u8g_pb_Clear(pb);
  310. u8g_page_First(&(pb->p));
  311. break;
  312. case U8G_DEV_MSG_PAGE_NEXT:
  313. if ( u8g_page_Next(&(pb->p)) == 0 )
  314. return 0;
  315. u8g_pb_Clear(pb);
  316. break;
  317. #ifdef U8G_DEV_MSG_IS_BBX_INTERSECTION
  318. case U8G_DEV_MSG_IS_BBX_INTERSECTION:
  319. return u8g_pb_IsIntersection(pb, (u8g_dev_arg_bbx_t *)arg);
  320. #endif
  321. case U8G_DEV_MSG_GET_PAGE_BOX:
  322. u8g_pb_GetPageBox(pb, (u8g_box_t *)arg);
  323. break;
  324. case U8G_DEV_MSG_GET_WIDTH:
  325. *((u8g_uint_t *)arg) = pb->width;
  326. break;
  327. case U8G_DEV_MSG_GET_HEIGHT:
  328. *((u8g_uint_t *)arg) = pb->p.total_height;
  329. break;
  330. case U8G_DEV_MSG_SET_COLOR_ENTRY:
  331. break;
  332. case U8G_DEV_MSG_SET_XY_CB:
  333. break;
  334. case U8G_DEV_MSG_GET_MODE:
  335. return U8G_MODE_BW;
  336. }
  337. return 1;
  338. }