u8g_rot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. u8g_rot.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2011, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "u8g.h"
  28. uint8_t u8g_dev_rot90_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg);
  29. uint8_t u8g_dev_rot180_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg);
  30. uint8_t u8g_dev_rot270_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg);
  31. uint8_t u8g_dev_rot_dummy_fn(u8g_t *u8g, u8g_dev_t*dev, uint8_t msg, void *arg)
  32. {
  33. return 0;
  34. }
  35. u8g_dev_t u8g_dev_rot = { u8g_dev_rot_dummy_fn, NULL, NULL };
  36. void u8g_UndoRotation(u8g_t *u8g)
  37. {
  38. if ( u8g->dev != &u8g_dev_rot )
  39. return;
  40. u8g->dev = u8g_dev_rot.dev_mem;
  41. u8g_UpdateDimension(u8g);
  42. }
  43. void u8g_SetRot90(u8g_t *u8g)
  44. {
  45. if ( u8g->dev != &u8g_dev_rot )
  46. {
  47. u8g_dev_rot.dev_mem = u8g->dev;
  48. u8g->dev = &u8g_dev_rot;
  49. }
  50. u8g_dev_rot.dev_fn = u8g_dev_rot90_fn;
  51. u8g_UpdateDimension(u8g);
  52. }
  53. void u8g_SetRot180(u8g_t *u8g)
  54. {
  55. if ( u8g->dev != &u8g_dev_rot )
  56. {
  57. u8g_dev_rot.dev_mem = u8g->dev;
  58. u8g->dev = &u8g_dev_rot;
  59. }
  60. u8g_dev_rot.dev_fn = u8g_dev_rot180_fn;
  61. u8g_UpdateDimension(u8g);
  62. }
  63. void u8g_SetRot270(u8g_t *u8g)
  64. {
  65. if ( u8g->dev != &u8g_dev_rot )
  66. {
  67. u8g_dev_rot.dev_mem = u8g->dev;
  68. u8g->dev = &u8g_dev_rot;
  69. }
  70. u8g_dev_rot.dev_fn = u8g_dev_rot270_fn;
  71. u8g_UpdateDimension(u8g);
  72. }
  73. uint8_t u8g_dev_rot90_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  74. {
  75. u8g_dev_t *rotation_chain = (u8g_dev_t *)(dev->dev_mem);
  76. switch(msg)
  77. {
  78. default:
  79. /*
  80. case U8G_DEV_MSG_INIT:
  81. case U8G_DEV_MSG_STOP:
  82. case U8G_DEV_MSG_PAGE_FIRST:
  83. case U8G_DEV_MSG_PAGE_NEXT:
  84. case U8G_DEV_MSG_SET_COLOR_ENTRY:
  85. case U8G_DEV_MSG_SET_XY_CB:
  86. */
  87. return u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  88. #ifdef U8G_DEV_MSG_IS_BBX_INTERSECTION
  89. case U8G_DEV_MSG_IS_BBX_INTERSECTION:
  90. {
  91. u8g_dev_arg_bbx_t *bbx = (u8g_dev_arg_bbx_t *)arg;
  92. u8g_uint_t x, y, tmp;
  93. /* transform the reference point */
  94. y = bbx->x;
  95. x = u8g->height;
  96. /* x = u8g_GetWidthLL(u8g, rotation_chain); */
  97. x -= bbx->y;
  98. x--;
  99. /* adjust point to be the uppler left corner again */
  100. x -= bbx->h;
  101. x++;
  102. /* swap box dimensions */
  103. tmp = bbx->w;
  104. bbx->w = bbx->h;
  105. bbx->h = tmp;
  106. /* store x,y */
  107. bbx->x = x;
  108. bbx->y = y;
  109. }
  110. return u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  111. #endif /* U8G_DEV_MSG_IS_BBX_INTERSECTION */
  112. case U8G_DEV_MSG_GET_PAGE_BOX:
  113. /* get page size from next device in the chain */
  114. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  115. //printf("pre x: %3d..%3d y: %3d..%3d ", ((u8g_box_t *)arg)->x0, ((u8g_box_t *)arg)->x1, ((u8g_box_t *)arg)->y0, ((u8g_box_t *)arg)->y1);
  116. {
  117. u8g_box_t new_box;
  118. //new_box.x0 = u8g_GetHeightLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->y1 - 1;
  119. //new_box.x1 = u8g_GetHeightLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->y0 - 1;
  120. new_box.x0 = ((u8g_box_t *)arg)->y0;
  121. new_box.x1 = ((u8g_box_t *)arg)->y1;
  122. new_box.y0 = ((u8g_box_t *)arg)->x0;
  123. new_box.y1 = ((u8g_box_t *)arg)->x1;
  124. *((u8g_box_t *)arg) = new_box;
  125. //printf("post x: %3d..%3d y: %3d..%3d\n", ((u8g_box_t *)arg)->x0, ((u8g_box_t *)arg)->x1, ((u8g_box_t *)arg)->y0, ((u8g_box_t *)arg)->y1);
  126. }
  127. break;
  128. case U8G_DEV_MSG_GET_WIDTH:
  129. *((u8g_uint_t *)arg) = u8g_GetHeightLL(u8g,rotation_chain);
  130. break;
  131. case U8G_DEV_MSG_GET_HEIGHT:
  132. *((u8g_uint_t *)arg) = u8g_GetWidthLL(u8g, rotation_chain);
  133. break;
  134. case U8G_DEV_MSG_SET_PIXEL:
  135. case U8G_DEV_MSG_SET_TPIXEL:
  136. {
  137. u8g_uint_t x, y;
  138. y = ((u8g_dev_arg_pixel_t *)arg)->x;
  139. x = u8g_GetWidthLL(u8g, rotation_chain);
  140. x -= ((u8g_dev_arg_pixel_t *)arg)->y;
  141. x--;
  142. ((u8g_dev_arg_pixel_t *)arg)->x = x;
  143. ((u8g_dev_arg_pixel_t *)arg)->y = y;
  144. }
  145. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  146. break;
  147. case U8G_DEV_MSG_SET_8PIXEL:
  148. case U8G_DEV_MSG_SET_4TPIXEL:
  149. {
  150. u8g_uint_t x, y;
  151. //uint16_t x,y;
  152. y = ((u8g_dev_arg_pixel_t *)arg)->x;
  153. x = u8g_GetWidthLL(u8g, rotation_chain);
  154. x -= ((u8g_dev_arg_pixel_t *)arg)->y;
  155. x--;
  156. ((u8g_dev_arg_pixel_t *)arg)->x = x;
  157. ((u8g_dev_arg_pixel_t *)arg)->y = y;
  158. ((u8g_dev_arg_pixel_t *)arg)->dir+=1;
  159. ((u8g_dev_arg_pixel_t *)arg)->dir &= 3;
  160. }
  161. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  162. break;
  163. }
  164. return 1;
  165. }
  166. uint8_t u8g_dev_rot180_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  167. {
  168. u8g_dev_t *rotation_chain = (u8g_dev_t *)(dev->dev_mem);
  169. switch(msg)
  170. {
  171. default:
  172. /*
  173. case U8G_DEV_MSG_INIT:
  174. case U8G_DEV_MSG_STOP:
  175. case U8G_DEV_MSG_PAGE_FIRST:
  176. case U8G_DEV_MSG_PAGE_NEXT:
  177. case U8G_DEV_MSG_SET_COLOR_ENTRY:
  178. case U8G_DEV_MSG_SET_XY_CB:
  179. */
  180. return u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  181. #ifdef U8G_DEV_MSG_IS_BBX_INTERSECTION
  182. case U8G_DEV_MSG_IS_BBX_INTERSECTION:
  183. {
  184. u8g_dev_arg_bbx_t *bbx = (u8g_dev_arg_bbx_t *)arg;
  185. u8g_uint_t x, y;
  186. /* transform the reference point */
  187. //y = u8g_GetHeightLL(u8g, rotation_chain);
  188. y = u8g->height;
  189. y -= bbx->y;
  190. y--;
  191. //x = u8g_GetWidthLL(u8g, rotation_chain);
  192. x = u8g->width;
  193. x -= bbx->x;
  194. x--;
  195. /* adjust point to be the uppler left corner again */
  196. y -= bbx->h;
  197. y++;
  198. x -= bbx->w;
  199. x++;
  200. /* store x,y */
  201. bbx->x = x;
  202. bbx->y = y;
  203. }
  204. return u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  205. #endif /* U8G_DEV_MSG_IS_BBX_INTERSECTION */
  206. case U8G_DEV_MSG_GET_PAGE_BOX:
  207. /* get page size from next device in the chain */
  208. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  209. //printf("pre x: %3d..%3d y: %3d..%3d ", ((u8g_box_t *)arg)->x0, ((u8g_box_t *)arg)->x1, ((u8g_box_t *)arg)->y0, ((u8g_box_t *)arg)->y1);
  210. {
  211. u8g_box_t new_box;
  212. new_box.x0 = u8g_GetWidthLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->x1 - 1;
  213. new_box.x1 = u8g_GetWidthLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->x0 - 1;
  214. new_box.y0 = u8g_GetHeightLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->y1 - 1;
  215. new_box.y1 = u8g_GetHeightLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->y0 - 1;
  216. *((u8g_box_t *)arg) = new_box;
  217. //printf("post x: %3d..%3d y: %3d..%3d\n", ((u8g_box_t *)arg)->x0, ((u8g_box_t *)arg)->x1, ((u8g_box_t *)arg)->y0, ((u8g_box_t *)arg)->y1);
  218. }
  219. break;
  220. case U8G_DEV_MSG_GET_WIDTH:
  221. *((u8g_uint_t *)arg) = u8g_GetWidthLL(u8g,rotation_chain);
  222. break;
  223. case U8G_DEV_MSG_GET_HEIGHT:
  224. *((u8g_uint_t *)arg) = u8g_GetHeightLL(u8g, rotation_chain);
  225. break;
  226. case U8G_DEV_MSG_SET_PIXEL:
  227. case U8G_DEV_MSG_SET_TPIXEL:
  228. {
  229. u8g_uint_t x, y;
  230. y = u8g_GetHeightLL(u8g, rotation_chain);
  231. y -= ((u8g_dev_arg_pixel_t *)arg)->y;
  232. y--;
  233. x = u8g_GetWidthLL(u8g, rotation_chain);
  234. x -= ((u8g_dev_arg_pixel_t *)arg)->x;
  235. x--;
  236. ((u8g_dev_arg_pixel_t *)arg)->x = x;
  237. ((u8g_dev_arg_pixel_t *)arg)->y = y;
  238. }
  239. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  240. break;
  241. case U8G_DEV_MSG_SET_8PIXEL:
  242. case U8G_DEV_MSG_SET_4TPIXEL:
  243. {
  244. u8g_uint_t x, y;
  245. y = u8g_GetHeightLL(u8g, rotation_chain);
  246. y -= ((u8g_dev_arg_pixel_t *)arg)->y;
  247. y--;
  248. x = u8g_GetWidthLL(u8g, rotation_chain);
  249. x -= ((u8g_dev_arg_pixel_t *)arg)->x;
  250. x--;
  251. ((u8g_dev_arg_pixel_t *)arg)->x = x;
  252. ((u8g_dev_arg_pixel_t *)arg)->y = y;
  253. ((u8g_dev_arg_pixel_t *)arg)->dir+=2;
  254. ((u8g_dev_arg_pixel_t *)arg)->dir &= 3;
  255. }
  256. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  257. break;
  258. }
  259. return 1;
  260. }
  261. uint8_t u8g_dev_rot270_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  262. {
  263. u8g_dev_t *rotation_chain = (u8g_dev_t *)(dev->dev_mem);
  264. switch(msg)
  265. {
  266. default:
  267. /*
  268. case U8G_DEV_MSG_INIT:
  269. case U8G_DEV_MSG_STOP:
  270. case U8G_DEV_MSG_PAGE_FIRST:
  271. case U8G_DEV_MSG_PAGE_NEXT:
  272. case U8G_DEV_MSG_SET_COLOR_ENTRY:
  273. case U8G_DEV_MSG_SET_XY_CB:
  274. */
  275. return u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  276. #ifdef U8G_DEV_MSG_IS_BBX_INTERSECTION
  277. case U8G_DEV_MSG_IS_BBX_INTERSECTION:
  278. {
  279. u8g_dev_arg_bbx_t *bbx = (u8g_dev_arg_bbx_t *)arg;
  280. u8g_uint_t x, y, tmp;
  281. /* transform the reference point */
  282. x = bbx->y;
  283. y = u8g->width;
  284. /* y = u8g_GetHeightLL(u8g, rotation_chain); */
  285. y -= bbx->x;
  286. y--;
  287. /* adjust point to be the uppler left corner again */
  288. y -= bbx->w;
  289. y++;
  290. /* swap box dimensions */
  291. tmp = bbx->w;
  292. bbx->w = bbx->h;
  293. bbx->h = tmp;
  294. /* store x,y */
  295. bbx->x = x;
  296. bbx->y = y;
  297. }
  298. return u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  299. #endif /* U8G_DEV_MSG_IS_BBX_INTERSECTION */
  300. case U8G_DEV_MSG_GET_PAGE_BOX:
  301. /* get page size from next device in the chain */
  302. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  303. //printf("pre x: %3d..%3d y: %3d..%3d ", ((u8g_box_t *)arg)->x0, ((u8g_box_t *)arg)->x1, ((u8g_box_t *)arg)->y0, ((u8g_box_t *)arg)->y1);
  304. {
  305. u8g_box_t new_box;
  306. new_box.x0 = u8g_GetHeightLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->y1 - 1;
  307. new_box.x1 = u8g_GetHeightLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->y0 - 1;
  308. new_box.y0 = u8g_GetWidthLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->x1 - 1;
  309. new_box.y1 = u8g_GetWidthLL(u8g,rotation_chain) - ((u8g_box_t *)arg)->x0 - 1;
  310. *((u8g_box_t *)arg) = new_box;
  311. //printf("post x: %3d..%3d y: %3d..%3d\n", ((u8g_box_t *)arg)->x0, ((u8g_box_t *)arg)->x1, ((u8g_box_t *)arg)->y0, ((u8g_box_t *)arg)->y1);
  312. }
  313. break;
  314. case U8G_DEV_MSG_GET_WIDTH:
  315. *((u8g_uint_t *)arg) = u8g_GetHeightLL(u8g,rotation_chain);
  316. break;
  317. case U8G_DEV_MSG_GET_HEIGHT:
  318. *((u8g_uint_t *)arg) = u8g_GetWidthLL(u8g, rotation_chain);
  319. break;
  320. case U8G_DEV_MSG_SET_PIXEL:
  321. case U8G_DEV_MSG_SET_TPIXEL:
  322. {
  323. u8g_uint_t x, y;
  324. x = ((u8g_dev_arg_pixel_t *)arg)->y;
  325. y = u8g_GetHeightLL(u8g, rotation_chain);
  326. y -= ((u8g_dev_arg_pixel_t *)arg)->x;
  327. y--;
  328. /*
  329. x = u8g_GetWidthLL(u8g, rotation_chain);
  330. x -= ((u8g_dev_arg_pixel_t *)arg)->y;
  331. x--;
  332. */
  333. ((u8g_dev_arg_pixel_t *)arg)->x = x;
  334. ((u8g_dev_arg_pixel_t *)arg)->y = y;
  335. }
  336. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  337. break;
  338. case U8G_DEV_MSG_SET_8PIXEL:
  339. case U8G_DEV_MSG_SET_4TPIXEL:
  340. {
  341. u8g_uint_t x, y;
  342. x = ((u8g_dev_arg_pixel_t *)arg)->y;
  343. y = u8g_GetHeightLL(u8g, rotation_chain);
  344. y -= ((u8g_dev_arg_pixel_t *)arg)->x;
  345. y--;
  346. /*
  347. x = u8g_GetWidthLL(u8g, rotation_chain);
  348. x -= ((u8g_dev_arg_pixel_t *)arg)->y;
  349. x--;
  350. */
  351. ((u8g_dev_arg_pixel_t *)arg)->x = x;
  352. ((u8g_dev_arg_pixel_t *)arg)->y = y;
  353. ((u8g_dev_arg_pixel_t *)arg)->dir+=3;
  354. ((u8g_dev_arg_pixel_t *)arg)->dir &= 3;
  355. }
  356. u8g_call_dev_fn(u8g, rotation_chain, msg, arg);
  357. break;
  358. }
  359. return 1;
  360. }