modframebuf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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_GS4_HMSB (2)
  50. #define FRAMEBUF_MHLSB (3)
  51. #define FRAMEBUF_MHMSB (4)
  52. // Functions for MHLSB and MHMSB
  53. STATIC void mono_horiz_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  54. size_t index = (x + y * fb->stride) >> 3;
  55. int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07);
  56. ((uint8_t*)fb->buf)[index] = (((uint8_t*)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset);
  57. }
  58. STATIC uint32_t mono_horiz_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  59. size_t index = (x + y * fb->stride) >> 3;
  60. int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07);
  61. return (((uint8_t*)fb->buf)[index] >> (offset)) & 0x01;
  62. }
  63. STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  64. int reverse = fb->format == FRAMEBUF_MHMSB;
  65. int advance = fb->stride >> 3;
  66. while (w--) {
  67. uint8_t *b = &((uint8_t*)fb->buf)[(x >> 3) + y * advance];
  68. int offset = reverse ? x & 7 : 7 - (x & 7);
  69. for (int hh = h; hh; --hh) {
  70. *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset);
  71. b += advance;
  72. }
  73. ++x;
  74. }
  75. }
  76. // Functions for MVLSB format
  77. STATIC void mvlsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  78. size_t index = (y >> 3) * fb->stride + x;
  79. uint8_t offset = y & 0x07;
  80. ((uint8_t*)fb->buf)[index] = (((uint8_t*)fb->buf)[index] & ~(0x01 << offset)) | ((col != 0) << offset);
  81. }
  82. STATIC uint32_t mvlsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  83. return (((uint8_t*)fb->buf)[(y >> 3) * fb->stride + x] >> (y & 0x07)) & 0x01;
  84. }
  85. STATIC void mvlsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  86. while (h--) {
  87. uint8_t *b = &((uint8_t*)fb->buf)[(y >> 3) * fb->stride + x];
  88. uint8_t offset = y & 0x07;
  89. for (int ww = w; ww; --ww) {
  90. *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset);
  91. ++b;
  92. }
  93. ++y;
  94. }
  95. }
  96. // Functions for RGB565 format
  97. STATIC void rgb565_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  98. ((uint16_t*)fb->buf)[x + y * fb->stride] = col;
  99. }
  100. STATIC uint32_t rgb565_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  101. return ((uint16_t*)fb->buf)[x + y * fb->stride];
  102. }
  103. STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  104. uint16_t *b = &((uint16_t*)fb->buf)[x + y * fb->stride];
  105. while (h--) {
  106. for (int ww = w; ww; --ww) {
  107. *b++ = col;
  108. }
  109. b += fb->stride - w;
  110. }
  111. }
  112. // Functions for GS4_HMSB format
  113. STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  114. uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1];
  115. if (x % 2) {
  116. *pixel = ((uint8_t)col & 0x0f) | (*pixel & 0xf0);
  117. } else {
  118. *pixel = ((uint8_t)col << 4) | (*pixel & 0x0f);
  119. }
  120. }
  121. STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  122. if (x % 2) {
  123. return ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1] & 0x0f;
  124. }
  125. return ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1] >> 4;
  126. }
  127. STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  128. col &= 0x0f;
  129. uint8_t *pixel_pair = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1];
  130. uint8_t col_shifted_left = col << 4;
  131. uint8_t col_pixel_pair = col_shifted_left | col;
  132. int pixel_count_till_next_line = (fb->stride - w) >> 1;
  133. bool odd_x = (x % 2 == 1);
  134. while (h--) {
  135. int ww = w;
  136. if (odd_x && ww > 0) {
  137. *pixel_pair = (*pixel_pair & 0xf0) | col;
  138. pixel_pair++;
  139. ww--;
  140. }
  141. memset(pixel_pair, col_pixel_pair, ww >> 1);
  142. pixel_pair += ww >> 1;
  143. if (ww % 2) {
  144. *pixel_pair = col_shifted_left | (*pixel_pair & 0x0f);
  145. if (!odd_x) {
  146. pixel_pair++;
  147. }
  148. }
  149. pixel_pair += pixel_count_till_next_line;
  150. }
  151. }
  152. STATIC mp_framebuf_p_t formats[] = {
  153. [FRAMEBUF_MVLSB] = {mvlsb_setpixel, mvlsb_getpixel, mvlsb_fill_rect},
  154. [FRAMEBUF_RGB565] = {rgb565_setpixel, rgb565_getpixel, rgb565_fill_rect},
  155. [FRAMEBUF_GS4_HMSB] = {gs4_hmsb_setpixel, gs4_hmsb_getpixel, gs4_hmsb_fill_rect},
  156. [FRAMEBUF_MHLSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect},
  157. [FRAMEBUF_MHMSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect},
  158. };
  159. static inline void setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) {
  160. formats[fb->format].setpixel(fb, x, y, col);
  161. }
  162. static inline uint32_t getpixel(const mp_obj_framebuf_t *fb, int x, int y) {
  163. return formats[fb->format].getpixel(fb, x, y);
  164. }
  165. STATIC void fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) {
  166. if (h < 1 || w < 1 || x + w <= 0 || y + h <= 0 || y >= fb->height || x >= fb->width) {
  167. // No operation needed.
  168. return;
  169. }
  170. // clip to the framebuffer
  171. int xend = MIN(fb->width, x + w);
  172. int yend = MIN(fb->height, y + h);
  173. x = MAX(x, 0);
  174. y = MAX(y, 0);
  175. formats[fb->format].fill_rect(fb, x, y, xend - x, yend - y, col);
  176. }
  177. 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) {
  178. mp_arg_check_num(n_args, n_kw, 4, 5, false);
  179. mp_obj_framebuf_t *o = m_new_obj(mp_obj_framebuf_t);
  180. o->base.type = type;
  181. o->buf_obj = args[0];
  182. mp_buffer_info_t bufinfo;
  183. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE);
  184. o->buf = bufinfo.buf;
  185. o->width = mp_obj_get_int(args[1]);
  186. o->height = mp_obj_get_int(args[2]);
  187. o->format = mp_obj_get_int(args[3]);
  188. if (n_args >= 5) {
  189. o->stride = mp_obj_get_int(args[4]);
  190. } else {
  191. o->stride = o->width;
  192. }
  193. switch (o->format) {
  194. case FRAMEBUF_MVLSB:
  195. case FRAMEBUF_RGB565:
  196. break;
  197. case FRAMEBUF_MHLSB:
  198. case FRAMEBUF_MHMSB:
  199. o->stride = (o->stride + 7) & ~7;
  200. break;
  201. case FRAMEBUF_GS4_HMSB:
  202. o->stride = (o->stride + 1) & ~1;
  203. break;
  204. default:
  205. mp_raise_ValueError("invalid format");
  206. }
  207. return MP_OBJ_FROM_PTR(o);
  208. }
  209. STATIC mp_int_t framebuf_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
  210. (void)flags;
  211. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
  212. bufinfo->buf = self->buf;
  213. bufinfo->len = self->stride * self->height * (self->format == FRAMEBUF_RGB565 ? 2 : 1);
  214. bufinfo->typecode = 'B'; // view framebuf as bytes
  215. return 0;
  216. }
  217. STATIC mp_obj_t framebuf_fill(mp_obj_t self_in, mp_obj_t col_in) {
  218. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
  219. mp_int_t col = mp_obj_get_int(col_in);
  220. formats[self->format].fill_rect(self, 0, 0, self->width, self->height, col);
  221. return mp_const_none;
  222. }
  223. STATIC MP_DEFINE_CONST_FUN_OBJ_2(framebuf_fill_obj, framebuf_fill);
  224. STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args) {
  225. (void)n_args;
  226. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  227. mp_int_t x = mp_obj_get_int(args[1]);
  228. mp_int_t y = mp_obj_get_int(args[2]);
  229. mp_int_t width = mp_obj_get_int(args[3]);
  230. mp_int_t height = mp_obj_get_int(args[4]);
  231. mp_int_t col = mp_obj_get_int(args[5]);
  232. fill_rect(self, x, y, width, height, col);
  233. return mp_const_none;
  234. }
  235. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_fill_rect_obj, 6, 6, framebuf_fill_rect);
  236. STATIC mp_obj_t framebuf_pixel(size_t n_args, const mp_obj_t *args) {
  237. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  238. mp_int_t x = mp_obj_get_int(args[1]);
  239. mp_int_t y = mp_obj_get_int(args[2]);
  240. if (0 <= x && x < self->width && 0 <= y && y < self->height) {
  241. if (n_args == 3) {
  242. // get
  243. return MP_OBJ_NEW_SMALL_INT(getpixel(self, x, y));
  244. } else {
  245. // set
  246. setpixel(self, x, y, mp_obj_get_int(args[3]));
  247. }
  248. }
  249. return mp_const_none;
  250. }
  251. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_pixel_obj, 3, 4, framebuf_pixel);
  252. STATIC mp_obj_t framebuf_hline(size_t n_args, const mp_obj_t *args) {
  253. (void)n_args;
  254. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  255. mp_int_t x = mp_obj_get_int(args[1]);
  256. mp_int_t y = mp_obj_get_int(args[2]);
  257. mp_int_t w = mp_obj_get_int(args[3]);
  258. mp_int_t col = mp_obj_get_int(args[4]);
  259. fill_rect(self, x, y, w, 1, col);
  260. return mp_const_none;
  261. }
  262. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_hline_obj, 5, 5, framebuf_hline);
  263. STATIC mp_obj_t framebuf_vline(size_t n_args, const mp_obj_t *args) {
  264. (void)n_args;
  265. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  266. mp_int_t x = mp_obj_get_int(args[1]);
  267. mp_int_t y = mp_obj_get_int(args[2]);
  268. mp_int_t h = mp_obj_get_int(args[3]);
  269. mp_int_t col = mp_obj_get_int(args[4]);
  270. fill_rect(self, x, y, 1, h, col);
  271. return mp_const_none;
  272. }
  273. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_vline_obj, 5, 5, framebuf_vline);
  274. STATIC mp_obj_t framebuf_rect(size_t n_args, const mp_obj_t *args) {
  275. (void)n_args;
  276. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  277. mp_int_t x = mp_obj_get_int(args[1]);
  278. mp_int_t y = mp_obj_get_int(args[2]);
  279. mp_int_t w = mp_obj_get_int(args[3]);
  280. mp_int_t h = mp_obj_get_int(args[4]);
  281. mp_int_t col = mp_obj_get_int(args[5]);
  282. fill_rect(self, x, y, w, 1, col);
  283. fill_rect(self, x, y + h- 1, w, 1, col);
  284. fill_rect(self, x, y, 1, h, col);
  285. fill_rect(self, x + w- 1, y, 1, h, col);
  286. return mp_const_none;
  287. }
  288. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_rect_obj, 6, 6, framebuf_rect);
  289. STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
  290. (void)n_args;
  291. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  292. mp_int_t x1 = mp_obj_get_int(args[1]);
  293. mp_int_t y1 = mp_obj_get_int(args[2]);
  294. mp_int_t x2 = mp_obj_get_int(args[3]);
  295. mp_int_t y2 = mp_obj_get_int(args[4]);
  296. mp_int_t col = mp_obj_get_int(args[5]);
  297. mp_int_t dx = x2 - x1;
  298. mp_int_t sx;
  299. if (dx > 0) {
  300. sx = 1;
  301. } else {
  302. dx = -dx;
  303. sx = -1;
  304. }
  305. mp_int_t dy = y2 - y1;
  306. mp_int_t sy;
  307. if (dy > 0) {
  308. sy = 1;
  309. } else {
  310. dy = -dy;
  311. sy = -1;
  312. }
  313. bool steep;
  314. if (dy > dx) {
  315. mp_int_t temp;
  316. temp = x1; x1 = y1; y1 = temp;
  317. temp = dx; dx = dy; dy = temp;
  318. temp = sx; sx = sy; sy = temp;
  319. steep = true;
  320. } else {
  321. steep = false;
  322. }
  323. mp_int_t e = 2 * dy - dx;
  324. for (mp_int_t i = 0; i < dx; ++i) {
  325. if (steep) {
  326. if (0 <= y1 && y1 < self->width && 0 <= x1 && x1 < self->height) {
  327. setpixel(self, y1, x1, col);
  328. }
  329. } else {
  330. if (0 <= x1 && x1 < self->width && 0 <= y1 && y1 < self->height) {
  331. setpixel(self, x1, y1, col);
  332. }
  333. }
  334. while (e >= 0) {
  335. y1 += sy;
  336. e -= 2 * dx;
  337. }
  338. x1 += sx;
  339. e += 2 * dy;
  340. }
  341. if (0 <= x2 && x2 < self->width && 0 <= y2 && y2 < self->height) {
  342. setpixel(self, x2, y2, col);
  343. }
  344. return mp_const_none;
  345. }
  346. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_line);
  347. STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
  348. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  349. mp_obj_framebuf_t *source = MP_OBJ_TO_PTR(args[1]);
  350. mp_int_t x = mp_obj_get_int(args[2]);
  351. mp_int_t y = mp_obj_get_int(args[3]);
  352. mp_int_t key = -1;
  353. if (n_args > 4) {
  354. key = mp_obj_get_int(args[4]);
  355. }
  356. if (
  357. (x >= self->width) ||
  358. (y >= self->height) ||
  359. (-x >= source->width) ||
  360. (-y >= source->height)
  361. ) {
  362. // Out of bounds, no-op.
  363. return mp_const_none;
  364. }
  365. // Clip.
  366. int x0 = MAX(0, x);
  367. int y0 = MAX(0, y);
  368. int x1 = MAX(0, -x);
  369. int y1 = MAX(0, -y);
  370. int x0end = MIN(self->width, x + source->width);
  371. int y0end = MIN(self->height, y + source->height);
  372. for (; y0 < y0end; ++y0) {
  373. int cx1 = x1;
  374. for (int cx0 = x0; cx0 < x0end; ++cx0) {
  375. uint32_t col = getpixel(source, cx1, y1);
  376. if (col != (uint32_t)key) {
  377. setpixel(self, cx0, y0, col);
  378. }
  379. ++cx1;
  380. }
  381. ++y1;
  382. }
  383. return mp_const_none;
  384. }
  385. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_blit_obj, 4, 5, framebuf_blit);
  386. STATIC mp_obj_t framebuf_scroll(mp_obj_t self_in, mp_obj_t xstep_in, mp_obj_t ystep_in) {
  387. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
  388. mp_int_t xstep = mp_obj_get_int(xstep_in);
  389. mp_int_t ystep = mp_obj_get_int(ystep_in);
  390. int sx, y, xend, yend, dx, dy;
  391. if (xstep < 0) {
  392. sx = 0;
  393. xend = self->width + xstep;
  394. dx = 1;
  395. } else {
  396. sx = self->width - 1;
  397. xend = xstep - 1;
  398. dx = -1;
  399. }
  400. if (ystep < 0) {
  401. y = 0;
  402. yend = self->height + ystep;
  403. dy = 1;
  404. } else {
  405. y = self->height - 1;
  406. yend = ystep - 1;
  407. dy = -1;
  408. }
  409. for (; y != yend; y += dy) {
  410. for (int x = sx; x != xend; x += dx) {
  411. setpixel(self, x, y, getpixel(self, x - xstep, y - ystep));
  412. }
  413. }
  414. return mp_const_none;
  415. }
  416. STATIC MP_DEFINE_CONST_FUN_OBJ_3(framebuf_scroll_obj, framebuf_scroll);
  417. STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
  418. // extract arguments
  419. mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
  420. const char *str = mp_obj_str_get_str(args[1]);
  421. mp_int_t x0 = mp_obj_get_int(args[2]);
  422. mp_int_t y0 = mp_obj_get_int(args[3]);
  423. mp_int_t col = 1;
  424. if (n_args >= 5) {
  425. col = mp_obj_get_int(args[4]);
  426. }
  427. // loop over chars
  428. for (; *str; ++str) {
  429. // get char and make sure its in range of font
  430. int chr = *(uint8_t*)str;
  431. if (chr < 32 || chr > 127) {
  432. chr = 127;
  433. }
  434. // get char data
  435. const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
  436. // loop over char data
  437. for (int j = 0; j < 8; j++, x0++) {
  438. if (0 <= x0 && x0 < self->width) { // clip x
  439. uint vline_data = chr_data[j]; // each byte is a column of 8 pixels, LSB at top
  440. for (int y = y0; vline_data; vline_data >>= 1, y++) { // scan over vertical column
  441. if (vline_data & 1) { // only draw if pixel set
  442. if (0 <= y && y < self->height) { // clip y
  443. setpixel(self, x0, y, col);
  444. }
  445. }
  446. }
  447. }
  448. }
  449. }
  450. return mp_const_none;
  451. }
  452. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_text_obj, 4, 5, framebuf_text);
  453. STATIC const mp_rom_map_elem_t framebuf_locals_dict_table[] = {
  454. { MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&framebuf_fill_obj) },
  455. { MP_ROM_QSTR(MP_QSTR_fill_rect), MP_ROM_PTR(&framebuf_fill_rect_obj) },
  456. { MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&framebuf_pixel_obj) },
  457. { MP_ROM_QSTR(MP_QSTR_hline), MP_ROM_PTR(&framebuf_hline_obj) },
  458. { MP_ROM_QSTR(MP_QSTR_vline), MP_ROM_PTR(&framebuf_vline_obj) },
  459. { MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&framebuf_rect_obj) },
  460. { MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&framebuf_line_obj) },
  461. { MP_ROM_QSTR(MP_QSTR_blit), MP_ROM_PTR(&framebuf_blit_obj) },
  462. { MP_ROM_QSTR(MP_QSTR_scroll), MP_ROM_PTR(&framebuf_scroll_obj) },
  463. { MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&framebuf_text_obj) },
  464. };
  465. STATIC MP_DEFINE_CONST_DICT(framebuf_locals_dict, framebuf_locals_dict_table);
  466. STATIC const mp_obj_type_t mp_type_framebuf = {
  467. { &mp_type_type },
  468. .name = MP_QSTR_FrameBuffer,
  469. .make_new = framebuf_make_new,
  470. .buffer_p = { .get_buffer = framebuf_get_buffer },
  471. .locals_dict = (mp_obj_dict_t*)&framebuf_locals_dict,
  472. };
  473. // this factory function is provided for backwards compatibility with old FrameBuffer1 class
  474. STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args) {
  475. mp_obj_framebuf_t *o = m_new_obj(mp_obj_framebuf_t);
  476. o->base.type = &mp_type_framebuf;
  477. mp_buffer_info_t bufinfo;
  478. mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE);
  479. o->buf = bufinfo.buf;
  480. o->width = mp_obj_get_int(args[1]);
  481. o->height = mp_obj_get_int(args[2]);
  482. o->format = FRAMEBUF_MVLSB;
  483. if (n_args >= 4) {
  484. o->stride = mp_obj_get_int(args[3]);
  485. } else {
  486. o->stride = o->width;
  487. }
  488. return MP_OBJ_FROM_PTR(o);
  489. }
  490. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(legacy_framebuffer1_obj, 3, 4, legacy_framebuffer1);
  491. STATIC const mp_rom_map_elem_t framebuf_module_globals_table[] = {
  492. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_framebuf) },
  493. { MP_ROM_QSTR(MP_QSTR_FrameBuffer), MP_ROM_PTR(&mp_type_framebuf) },
  494. { MP_ROM_QSTR(MP_QSTR_FrameBuffer1), MP_ROM_PTR(&legacy_framebuffer1_obj) },
  495. { MP_ROM_QSTR(MP_QSTR_MVLSB), MP_ROM_INT(FRAMEBUF_MVLSB) },
  496. { MP_ROM_QSTR(MP_QSTR_MONO_VLSB), MP_ROM_INT(FRAMEBUF_MVLSB) },
  497. { MP_ROM_QSTR(MP_QSTR_RGB565), MP_ROM_INT(FRAMEBUF_RGB565) },
  498. { MP_ROM_QSTR(MP_QSTR_GS4_HMSB), MP_ROM_INT(FRAMEBUF_GS4_HMSB) },
  499. { MP_ROM_QSTR(MP_QSTR_MONO_HLSB), MP_ROM_INT(FRAMEBUF_MHLSB) },
  500. { MP_ROM_QSTR(MP_QSTR_MONO_HMSB), MP_ROM_INT(FRAMEBUF_MHMSB) },
  501. };
  502. STATIC MP_DEFINE_CONST_DICT(framebuf_module_globals, framebuf_module_globals_table);
  503. const mp_obj_module_t mp_module_framebuf = {
  504. .base = { &mp_type_module },
  505. .globals = (mp_obj_dict_t*)&framebuf_module_globals,
  506. };
  507. #endif // MICROPY_PY_FRAMEBUF