modframebuf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #if MICROPY_PY_FRAMEBUF
  30. #include "ports/stm32/font_petme128_8x8.h"
  31. typedef struct _mp_obj_framebuf_t {
  32. mp_obj_base_t base;
  33. mp_obj_t buf_obj; // need to store this to prevent GC from reclaiming buf
  34. void *buf;
  35. uint16_t width, height, stride;
  36. uint8_t format;
  37. } mp_obj_framebuf_t;
  38. typedef void (*setpixel_t)(const mp_obj_framebuf_t*, int, int, uint32_t);
  39. typedef uint32_t (*getpixel_t)(const mp_obj_framebuf_t*, int, int);
  40. typedef void (*fill_rect_t)(const mp_obj_framebuf_t *, int, int, int, int, uint32_t);
  41. typedef struct _mp_framebuf_p_t {
  42. setpixel_t setpixel;
  43. getpixel_t getpixel;
  44. fill_rect_t fill_rect;
  45. } mp_framebuf_p_t;
  46. // constants for formats
  47. #define FRAMEBUF_MVLSB (0)
  48. #define FRAMEBUF_RGB565 (1)
  49. #define FRAMEBUF_GS2_HMSB (5)
  50. #define FRAMEBUF_GS4_HMSB (2)
  51. #define FRAMEBUF_GS8 (6)
  52. #define FRAMEBUF_MHLSB (3)
  53. #define FRAMEBUF_MHMSB (4)
  54. // Functions for MHLSB and MHMSB
  55. STATIC void mono_horiz_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  56. size_t index = (x + y * fb->stride) >> 3;
  57. int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07);
  58. ((uint8_t*)fb->buf)[index] = (((uint8_t*)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset);
  59. }
  60. STATIC uint32_t mono_horiz_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  61. size_t index = (x + y * fb->stride) >> 3;
  62. int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07);
  63. return (((uint8_t*)fb->buf)[index] >> (offset)) & 0x01;
  64. }
  65. STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  66. int reverse = fb->format == FRAMEBUF_MHMSB;
  67. int advance = fb->stride >> 3;
  68. while (w--) {
  69. uint8_t *b = &((uint8_t*)fb->buf)[(x >> 3) + y * advance];
  70. int offset = reverse ? x & 7 : 7 - (x & 7);
  71. for (int hh = h; hh; --hh) {
  72. *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset);
  73. b += advance;
  74. }
  75. ++x;
  76. }
  77. }
  78. // Functions for MVLSB format
  79. STATIC void mvlsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  80. size_t index = (y >> 3) * fb->stride + x;
  81. uint8_t offset = y & 0x07;
  82. ((uint8_t*)fb->buf)[index] = (((uint8_t*)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset);
  83. }
  84. STATIC uint32_t mvlsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  85. return (((uint8_t*)fb->buf)[(y >> 3) * fb->stride + x] >> (y & 0x07)) & 0x01;
  86. }
  87. STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  88. while (h--) {
  89. uint8_t *b = &((uint8_t*)fb->buf)[(y >> 3) * fb->stride + x];
  90. uint8_t offset = y & 0x07;
  91. for (int ww = w; ww; --ww) {
  92. *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset);
  93. ++b;
  94. }
  95. ++y;
  96. }
  97. }
  98. // Functions for RGB565 format
  99. STATIC void rgb565_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  100. ((uint16_t*)fb->buf)[x + y * fb->stride] = col;
  101. }
  102. STATIC uint32_t rgb565_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  103. return ((uint16_t*)fb->buf)[x + y * fb->stride];
  104. }
  105. STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  106. uint16_t *b = &((uint16_t*)fb->buf)[x + y * fb->stride];
  107. while (h--) {
  108. for (int ww = w; ww; --ww) {
  109. *b++ = col;
  110. }
  111. b += fb->stride - w;
  112. }
  113. }
  114. // Functions for GS2_HMSB format
  115. STATIC void gs2_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  116. uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 2];
  117. uint8_t shift = (x & 0x3) << 1;
  118. uint8_t mask = 0x3 << shift;
  119. uint8_t color = (col & 0x3) << shift;
  120. *pixel = color | (*pixel & (~mask));
  121. }
  122. STATIC uint32_t gs2_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  123. uint8_t pixel = ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 2];
  124. uint8_t shift = (x & 0x3) << 1;
  125. return (pixel >> shift) & 0x3;
  126. }
  127. STATIC void gs2_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  128. for (int xx=x; xx < x+w; xx++) {
  129. for (int yy=y; yy < y+h; yy++) {
  130. gs2_hmsb_setpixel(fb, xx, yy, col);
  131. }
  132. }
  133. }
  134. // Functions for GS4_HMSB format
  135. STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  136. uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1];
  137. if (x % 2) {
  138. *pixel = ((uint8_t)col & 0x0f) | (*pixel & 0xf0);
  139. } else {
  140. *pixel = ((uint8_t)col << 4) | (*pixel & 0x0f);
  141. }
  142. }
  143. STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  144. if (x % 2) {
  145. return ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1] & 0x0f;
  146. }
  147. return ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1] >> 4;
  148. }
  149. STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  150. col &= 0x0f;
  151. uint8_t *pixel_pair = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1];
  152. uint8_t col_shifted_left = col << 4;
  153. uint8_t col_pixel_pair = col_shifted_left | col;
  154. int pixel_count_till_next_line = (fb->stride - w) >> 1;
  155. bool odd_x = (x % 2 == 1);
  156. while (h--) {
  157. int ww = w;
  158. if (odd_x && ww > 0) {
  159. *pixel_pair = (*pixel_pair & 0xf0) | col;
  160. pixel_pair++;
  161. ww--;
  162. }
  163. memset(pixel_pair, col_pixel_pair, ww >> 1);
  164. pixel_pair += ww >> 1;
  165. if (ww % 2) {
  166. *pixel_pair = col_shifted_left | (*pixel_pair & 0x0f);
  167. if (!odd_x) {
  168. pixel_pair++;
  169. }
  170. }
  171. pixel_pair += pixel_count_till_next_line;
  172. }
  173. }
  174. // Functions for GS8 format
  175. STATIC void gs8_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  176. uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride)];
  177. *pixel = col & 0xff;
  178. }
  179. STATIC uint32_t gs8_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  180. return ((uint8_t*)fb->buf)[(x + y * fb->stride)];
  181. }
  182. STATIC void gs8_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  183. uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride)];
  184. while (h--) {
  185. memset(pixel, col, w);
  186. pixel += fb->stride;
  187. }
  188. }
  189. STATIC mp_framebuf_p_t formats[] = {
  190. [FRAMEBUF_MVLSB] = {mvlsb_setpixel, mvlsb_getpixel, mvlsb_fill_rect},
  191. [FRAMEBUF_RGB565] = {rgb565_setpixel, rgb565_getpixel, rgb565_fill_rect},
  192. [FRAMEBUF_GS2_HMSB] = {gs2_hmsb_setpixel, gs2_hmsb_getpixel, gs2_hmsb_fill_rect},
  193. [FRAMEBUF_GS4_HMSB] = {gs4_hmsb_setpixel, gs4_hmsb_getpixel, gs4_hmsb_fill_rect},
  194. [FRAMEBUF_GS8] = {gs8_setpixel, gs8_getpixel, gs8_fill_rect},
  195. [FRAMEBUF_MHLSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect},
  196. [FRAMEBUF_MHMSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect},
  197. };
  198. static inline void setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  199. formats[fb->format].setpixel(fb, x, y, col);
  200. }
  201. static inline uint32_t getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  202. return formats[fb->format].getpixel(fb, x, y);
  203. }
  204. STATIC void fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  205. if (h < 1 || w < 1 || x + w <= 0 || y + h <= 0 || y >= fb->height || x >= fb->width) {
  206. // No operation needed.
  207. return;
  208. }
  209. // clip to the framebuffer
  210. int xend = MIN(fb->width, x + w);
  211. int yend = MIN(fb->height, y + h);
  212. x = MAX(x, 0);
  213. y = MAX(y, 0);
  214. formats[fb->format].fill_rect(fb, x, y, xend - x, yend - y, col);
  215. }
  216. STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  217. mp_arg_check_num(n_args, n_kw, 4, 5, false);
  218. mp_obj_framebuf_t *o = m_new_obj(mp_obj_framebuf_t);
  219. o->base.type = type;
  220. o->buf_obj = args[0];
  221. mp_buffer_info_t bufinfo;
  222. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE);
  223. o->buf = bufinfo.buf;
  224. o->width = mp_obj_get_int(args[1]);
  225. o->height = mp_obj_get_int(args[2]);
  226. o->format = mp_obj_get_int(args[3]);
  227. if (n_args >= 5) {
  228. o->stride = mp_obj_get_int(args[4]);
  229. } else {
  230. o->stride = o->width;
  231. }
  232. switch (o->format) {
  233. case FRAMEBUF_MVLSB:
  234. case FRAMEBUF_RGB565:
  235. break;
  236. case FRAMEBUF_MHLSB:
  237. case FRAMEBUF_MHMSB:
  238. o->stride = (o->stride + 7) & ~7;
  239. break;
  240. case FRAMEBUF_GS2_HMSB:
  241. o->stride = (o->stride + 3) & ~3;
  242. break;
  243. case FRAMEBUF_GS4_HMSB:
  244. o->stride = (o->stride + 1) & ~1;
  245. break;
  246. case FRAMEBUF_GS8:
  247. break;
  248. default:
  249. mp_raise_ValueError("invalid format");
  250. }
  251. return MP_OBJ_FROM_PTR(o);
  252. }
  253. STATIC mp_int_t framebuf_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
  254. (void)flags;
  255. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
  256. bufinfo->buf = self->buf;
  257. bufinfo->len = self->stride * self->height * (self->format == FRAMEBUF_RGB565 ? 2 : 1);
  258. bufinfo->typecode = 'B'; // view framebuf as bytes
  259. return 0;
  260. }
  261. STATIC mp_obj_t framebuf_fill(mp_obj_t self_in, mp_obj_t col_in) {
  262. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
  263. mp_int_t col = mp_obj_get_int(col_in);
  264. formats[self->format].fill_rect(self, 0, 0, self->width, self->height, col);
  265. return mp_const_none;
  266. }
  267. STATIC MP_DEFINE_CONST_FUN_OBJ_2(framebuf_fill_obj, framebuf_fill);
  268. STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args) {
  269. (void)n_args;
  270. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  271. mp_int_t x = mp_obj_get_int(args[1]);
  272. mp_int_t y = mp_obj_get_int(args[2]);
  273. mp_int_t width = mp_obj_get_int(args[3]);
  274. mp_int_t height = mp_obj_get_int(args[4]);
  275. mp_int_t col = mp_obj_get_int(args[5]);
  276. fill_rect(self, x, y, width, height, col);
  277. return mp_const_none;
  278. }
  279. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_fill_rect_obj, 6, 6, framebuf_fill_rect);
  280. STATIC mp_obj_t framebuf_pixel(size_t n_args, const mp_obj_t *args) {
  281. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  282. mp_int_t x = mp_obj_get_int(args[1]);
  283. mp_int_t y = mp_obj_get_int(args[2]);
  284. if (0 <= x && x < self->width && 0 <= y && y < self->height) {
  285. if (n_args == 3) {
  286. // get
  287. return MP_OBJ_NEW_SMALL_INT(getpixel(self, x, y));
  288. } else {
  289. // set
  290. setpixel(self, x, y, mp_obj_get_int(args[3]));
  291. }
  292. }
  293. return mp_const_none;
  294. }
  295. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_pixel_obj, 3, 4, framebuf_pixel);
  296. STATIC mp_obj_t framebuf_hline(size_t n_args, const mp_obj_t *args) {
  297. (void)n_args;
  298. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  299. mp_int_t x = mp_obj_get_int(args[1]);
  300. mp_int_t y = mp_obj_get_int(args[2]);
  301. mp_int_t w = mp_obj_get_int(args[3]);
  302. mp_int_t col = mp_obj_get_int(args[4]);
  303. fill_rect(self, x, y, w, 1, col);
  304. return mp_const_none;
  305. }
  306. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_hline_obj, 5, 5, framebuf_hline);
  307. STATIC mp_obj_t framebuf_vline(size_t n_args, const mp_obj_t *args) {
  308. (void)n_args;
  309. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  310. mp_int_t x = mp_obj_get_int(args[1]);
  311. mp_int_t y = mp_obj_get_int(args[2]);
  312. mp_int_t h = mp_obj_get_int(args[3]);
  313. mp_int_t col = mp_obj_get_int(args[4]);
  314. fill_rect(self, x, y, 1, h, col);
  315. return mp_const_none;
  316. }
  317. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_vline_obj, 5, 5, framebuf_vline);
  318. STATIC mp_obj_t framebuf_rect(size_t n_args, const mp_obj_t *args) {
  319. (void)n_args;
  320. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  321. mp_int_t x = mp_obj_get_int(args[1]);
  322. mp_int_t y = mp_obj_get_int(args[2]);
  323. mp_int_t w = mp_obj_get_int(args[3]);
  324. mp_int_t h = mp_obj_get_int(args[4]);
  325. mp_int_t col = mp_obj_get_int(args[5]);
  326. fill_rect(self, x, y, w, 1, col);
  327. fill_rect(self, x, y + h- 1, w, 1, col);
  328. fill_rect(self, x, y, 1, h, col);
  329. fill_rect(self, x + w- 1, y, 1, h, col);
  330. return mp_const_none;
  331. }
  332. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_rect_obj, 6, 6, framebuf_rect);
  333. STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
  334. (void)n_args;
  335. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  336. mp_int_t x1 = mp_obj_get_int(args[1]);
  337. mp_int_t y1 = mp_obj_get_int(args[2]);
  338. mp_int_t x2 = mp_obj_get_int(args[3]);
  339. mp_int_t y2 = mp_obj_get_int(args[4]);
  340. mp_int_t col = mp_obj_get_int(args[5]);
  341. mp_int_t dx = x2 - x1;
  342. mp_int_t sx;
  343. if (dx > 0) {
  344. sx = 1;
  345. } else {
  346. dx = -dx;
  347. sx = -1;
  348. }
  349. mp_int_t dy = y2 - y1;
  350. mp_int_t sy;
  351. if (dy > 0) {
  352. sy = 1;
  353. } else {
  354. dy = -dy;
  355. sy = -1;
  356. }
  357. bool steep;
  358. if (dy > dx) {
  359. mp_int_t temp;
  360. temp = x1; x1 = y1; y1 = temp;
  361. temp = dx; dx = dy; dy = temp;
  362. temp = sx; sx = sy; sy = temp;
  363. steep = true;
  364. } else {
  365. steep = false;
  366. }
  367. mp_int_t e = 2 * dy - dx;
  368. for (mp_int_t i = 0; i < dx; ++i) {
  369. if (steep) {
  370. if (0 <= y1 && y1 < self->width && 0 <= x1 && x1 < self->height) {
  371. setpixel(self, y1, x1, col);
  372. }
  373. } else {
  374. if (0 <= x1 && x1 < self->width && 0 <= y1 && y1 < self->height) {
  375. setpixel(self, x1, y1, col);
  376. }
  377. }
  378. while (e >= 0) {
  379. y1 += sy;
  380. e -= 2 * dx;
  381. }
  382. x1 += sx;
  383. e += 2 * dy;
  384. }
  385. if (0 <= x2 && x2 < self->width && 0 <= y2 && y2 < self->height) {
  386. setpixel(self, x2, y2, col);
  387. }
  388. return mp_const_none;
  389. }
  390. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_line);
  391. STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
  392. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  393. mp_obj_framebuf_t *source = MP_OBJ_TO_PTR(args[1]);
  394. mp_int_t x = mp_obj_get_int(args[2]);
  395. mp_int_t y = mp_obj_get_int(args[3]);
  396. mp_int_t key = -1;
  397. if (n_args > 4) {
  398. key = mp_obj_get_int(args[4]);
  399. }
  400. if (
  401. (x >= self->width) ||
  402. (y >= self->height) ||
  403. (-x >= source->width) ||
  404. (-y >= source->height)
  405. ) {
  406. // Out of bounds, no-op.
  407. return mp_const_none;
  408. }
  409. // Clip.
  410. int x0 = MAX(0, x);
  411. int y0 = MAX(0, y);
  412. int x1 = MAX(0, -x);
  413. int y1 = MAX(0, -y);
  414. int x0end = MIN(self->width, x + source->width);
  415. int y0end = MIN(self->height, y + source->height);
  416. for (; y0 < y0end; ++y0) {
  417. int cx1 = x1;
  418. for (int cx0 = x0; cx0 < x0end; ++cx0) {
  419. uint32_t col = getpixel(source, cx1, y1);
  420. if (col != (uint32_t)key) {
  421. setpixel(self, cx0, y0, col);
  422. }
  423. ++cx1;
  424. }
  425. ++y1;
  426. }
  427. return mp_const_none;
  428. }
  429. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_blit_obj, 4, 5, framebuf_blit);
  430. STATIC mp_obj_t framebuf_scroll(mp_obj_t self_in, mp_obj_t xstep_in, mp_obj_t ystep_in) {
  431. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
  432. mp_int_t xstep = mp_obj_get_int(xstep_in);
  433. mp_int_t ystep = mp_obj_get_int(ystep_in);
  434. int sx, y, xend, yend, dx, dy;
  435. if (xstep < 0) {
  436. sx = 0;
  437. xend = self->width + xstep;
  438. dx = 1;
  439. } else {
  440. sx = self->width - 1;
  441. xend = xstep - 1;
  442. dx = -1;
  443. }
  444. if (ystep < 0) {
  445. y = 0;
  446. yend = self->height + ystep;
  447. dy = 1;
  448. } else {
  449. y = self->height - 1;
  450. yend = ystep - 1;
  451. dy = -1;
  452. }
  453. for (; y != yend; y += dy) {
  454. for (int x = sx; x != xend; x += dx) {
  455. setpixel(self, x, y, getpixel(self, x - xstep, y - ystep));
  456. }
  457. }
  458. return mp_const_none;
  459. }
  460. STATIC MP_DEFINE_CONST_FUN_OBJ_3(framebuf_scroll_obj, framebuf_scroll);
  461. STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
  462. // extract arguments
  463. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  464. const char *str = mp_obj_str_get_str(args[1]);
  465. mp_int_t x0 = mp_obj_get_int(args[2]);
  466. mp_int_t y0 = mp_obj_get_int(args[3]);
  467. mp_int_t col = 1;
  468. if (n_args >= 5) {
  469. col = mp_obj_get_int(args[4]);
  470. }
  471. // loop over chars
  472. for (; *str; ++str) {
  473. // get char and make sure its in range of font
  474. int chr = *(uint8_t*)str;
  475. if (chr < 32 || chr > 127) {
  476. chr = 127;
  477. }
  478. // get char data
  479. const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
  480. // loop over char data
  481. for (int j = 0; j < 8; j++, x0++) {
  482. if (0 <= x0 && x0 < self->width) { // clip x
  483. uint vline_data = chr_data[j]; // each byte is a column of 8 pixels, LSB at top
  484. for (int y = y0; vline_data; vline_data >>= 1, y++) { // scan over vertical column
  485. if (vline_data & 1) { // only draw if pixel set
  486. if (0 <= y && y < self->height) { // clip y
  487. setpixel(self, x0, y, col);
  488. }
  489. }
  490. }
  491. }
  492. }
  493. }
  494. return mp_const_none;
  495. }
  496. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 5, framebuf_text);
  497. STATIC const mp_rom_map_elem_t framebuf_locals_dict_table[] = {
  498. { MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&framebuf_fill_obj) },
  499. { MP_ROM_QSTR(MP_QSTR_fill_rect), MP_ROM_PTR(&framebuf_fill_rect_obj) },
  500. { MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&framebuf_pixel_obj) },
  501. { MP_ROM_QSTR(MP_QSTR_hline), MP_ROM_PTR(&framebuf_hline_obj) },
  502. { MP_ROM_QSTR(MP_QSTR_vline), MP_ROM_PTR(&framebuf_vline_obj) },
  503. { MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&framebuf_rect_obj) },
  504. { MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&framebuf_line_obj) },
  505. { MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&framebuf_blit_obj) },
  506. { MP_ROM_QSTR(MP_QSTR_scroll), MP_ROM_PTR(&framebuf_scroll_obj) },
  507. { MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&framebuf_text_obj) },
  508. };
  509. STATIC MP_DEFINE_CONST_DICT(framebuf_locals_dict, framebuf_locals_dict_table);
  510. STATIC const mp_obj_type_t mp_type_framebuf = {
  511. { &mp_type_type },
  512. .name = MP_QSTR_FrameBuffer,
  513. .make_new = framebuf_make_new,
  514. .buffer_p = { .get_buffer = framebuf_get_buffer },
  515. .locals_dict = (mp_obj_dict_t*)&framebuf_locals_dict,
  516. };
  517. // this factory function is provided for backwards compatibility with old FrameBuffer1 class
  518. STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args) {
  519. mp_obj_framebuf_t *o = m_new_obj(mp_obj_framebuf_t);
  520. o->base.type = &mp_type_framebuf;
  521. mp_buffer_info_t bufinfo;
  522. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE);
  523. o->buf = bufinfo.buf;
  524. o->width = mp_obj_get_int(args[1]);
  525. o->height = mp_obj_get_int(args[2]);
  526. o->format = FRAMEBUF_MVLSB;
  527. if (n_args >= 4) {
  528. o->stride = mp_obj_get_int(args[3]);
  529. } else {
  530. o->stride = o->width;
  531. }
  532. return MP_OBJ_FROM_PTR(o);
  533. }
  534. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(legacy_framebuffer1_obj, 3, 4, legacy_framebuffer1);
  535. STATIC const mp_rom_map_elem_t framebuf_module_globals_table[] = {
  536. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_framebuf) },
  537. { MP_ROM_QSTR(MP_QSTR_FrameBuffer), MP_ROM_PTR(&mp_type_framebuf) },
  538. { MP_ROM_QSTR(MP_QSTR_FrameBuffer1), MP_ROM_PTR(&legacy_framebuffer1_obj) },
  539. { MP_ROM_QSTR(MP_QSTR_MVLSB), MP_ROM_INT(FRAMEBUF_MVLSB) },
  540. { MP_ROM_QSTR(MP_QSTR_MONO_VLSB), MP_ROM_INT(FRAMEBUF_MVLSB) },
  541. { MP_ROM_QSTR(MP_QSTR_RGB565), MP_ROM_INT(FRAMEBUF_RGB565) },
  542. { MP_ROM_QSTR(MP_QSTR_GS2_HMSB), MP_ROM_INT(FRAMEBUF_GS2_HMSB) },
  543. { MP_ROM_QSTR(MP_QSTR_GS4_HMSB), MP_ROM_INT(FRAMEBUF_GS4_HMSB) },
  544. { MP_ROM_QSTR(MP_QSTR_GS8), MP_ROM_INT(FRAMEBUF_GS8) },
  545. { MP_ROM_QSTR(MP_QSTR_MONO_HLSB), MP_ROM_INT(FRAMEBUF_MHLSB) },
  546. { MP_ROM_QSTR(MP_QSTR_MONO_HMSB), MP_ROM_INT(FRAMEBUF_MHMSB) },
  547. };
  548. STATIC MP_DEFINE_CONST_DICT(framebuf_module_globals, framebuf_module_globals_table);
  549. const mp_obj_module_t mp_module_framebuf = {
  550. .base = { &mp_type_module },
  551. .globals = (mp_obj_dict_t*)&framebuf_module_globals,
  552. };
  553. #endif // MICROPY_PY_FRAMEBUF