lcd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 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/mphal.h"
  29. #include "py/runtime.h"
  30. #if MICROPY_HW_HAS_LCD
  31. #include "pin.h"
  32. #include "bufhelper.h"
  33. #include "spi.h"
  34. #include "font_petme128_8x8.h"
  35. #include "lcd.h"
  36. /// \moduleref pyb
  37. /// \class LCD - LCD control for the LCD touch-sensor pyskin
  38. ///
  39. /// The LCD class is used to control the LCD on the LCD touch-sensor pyskin,
  40. /// LCD32MKv1.0. The LCD is a 128x32 pixel monochrome screen, part NHD-C12832A1Z.
  41. ///
  42. /// The pyskin must be connected in either the X or Y positions, and then
  43. /// an LCD object is made using:
  44. ///
  45. /// lcd = pyb.LCD('X') # if pyskin is in the X position
  46. /// lcd = pyb.LCD('Y') # if pyskin is in the Y position
  47. ///
  48. /// Then you can use:
  49. ///
  50. /// lcd.light(True) # turn the backlight on
  51. /// lcd.write('Hello world!\n') # print text to the screen
  52. ///
  53. /// This driver implements a double buffer for setting/getting pixels.
  54. /// For example, to make a bouncing dot, try:
  55. ///
  56. /// x = y = 0
  57. /// dx = dy = 1
  58. /// while True:
  59. /// # update the dot's position
  60. /// x += dx
  61. /// y += dy
  62. ///
  63. /// # make the dot bounce of the edges of the screen
  64. /// if x <= 0 or x >= 127: dx = -dx
  65. /// if y <= 0 or y >= 31: dy = -dy
  66. ///
  67. /// lcd.fill(0) # clear the buffer
  68. /// lcd.pixel(x, y, 1) # draw the dot
  69. /// lcd.show() # show the buffer
  70. /// pyb.delay(50) # pause for 50ms
  71. #define LCD_INSTR (0)
  72. #define LCD_DATA (1)
  73. #define LCD_CHAR_BUF_W (16)
  74. #define LCD_CHAR_BUF_H (4)
  75. #define LCD_PIX_BUF_W (128)
  76. #define LCD_PIX_BUF_H (32)
  77. #define LCD_PIX_BUF_BYTE_SIZE (LCD_PIX_BUF_W * LCD_PIX_BUF_H / 8)
  78. typedef struct _pyb_lcd_obj_t {
  79. mp_obj_base_t base;
  80. // hardware control for the LCD
  81. const spi_t *spi;
  82. const pin_obj_t *pin_cs1;
  83. const pin_obj_t *pin_rst;
  84. const pin_obj_t *pin_a0;
  85. const pin_obj_t *pin_bl;
  86. // character buffer for stdout-like output
  87. char char_buffer[LCD_CHAR_BUF_W * LCD_CHAR_BUF_H];
  88. int line;
  89. int column;
  90. int next_line;
  91. // double buffering for pixel buffer
  92. byte pix_buf[LCD_PIX_BUF_BYTE_SIZE];
  93. byte pix_buf2[LCD_PIX_BUF_BYTE_SIZE];
  94. } pyb_lcd_obj_t;
  95. STATIC void lcd_delay(void) {
  96. __asm volatile ("nop\nnop");
  97. }
  98. STATIC void lcd_out(pyb_lcd_obj_t *lcd, int instr_data, uint8_t i) {
  99. lcd_delay();
  100. mp_hal_pin_low(lcd->pin_cs1); // CS=0; enable
  101. if (instr_data == LCD_INSTR) {
  102. mp_hal_pin_low(lcd->pin_a0); // A0=0; select instr reg
  103. } else {
  104. mp_hal_pin_high(lcd->pin_a0); // A0=1; select data reg
  105. }
  106. lcd_delay();
  107. HAL_SPI_Transmit(lcd->spi->spi, &i, 1, 1000);
  108. lcd_delay();
  109. mp_hal_pin_high(lcd->pin_cs1); // CS=1; disable
  110. }
  111. // write a string to the LCD at the current cursor location
  112. // output it straight away (doesn't use the pixel buffer)
  113. STATIC void lcd_write_strn(pyb_lcd_obj_t *lcd, const char *str, unsigned int len) {
  114. int redraw_min = lcd->line * LCD_CHAR_BUF_W + lcd->column;
  115. int redraw_max = redraw_min;
  116. for (; len > 0; len--, str++) {
  117. // move to next line if needed
  118. if (lcd->next_line) {
  119. if (lcd->line + 1 < LCD_CHAR_BUF_H) {
  120. lcd->line += 1;
  121. } else {
  122. lcd->line = LCD_CHAR_BUF_H - 1;
  123. for (int i = 0; i < LCD_CHAR_BUF_W * (LCD_CHAR_BUF_H - 1); i++) {
  124. lcd->char_buffer[i] = lcd->char_buffer[i + LCD_CHAR_BUF_W];
  125. }
  126. for (int i = 0; i < LCD_CHAR_BUF_W; i++) {
  127. lcd->char_buffer[LCD_CHAR_BUF_W * (LCD_CHAR_BUF_H - 1) + i] = ' ';
  128. }
  129. redraw_min = 0;
  130. redraw_max = LCD_CHAR_BUF_W * LCD_CHAR_BUF_H;
  131. }
  132. lcd->next_line = 0;
  133. lcd->column = 0;
  134. }
  135. if (*str == '\n') {
  136. lcd->next_line = 1;
  137. } else if (*str == '\r') {
  138. lcd->column = 0;
  139. } else if (*str == '\b') {
  140. if (lcd->column > 0) {
  141. lcd->column--;
  142. redraw_min = 0; // could optimise this to not redraw everything
  143. }
  144. } else if (lcd->column >= LCD_CHAR_BUF_W) {
  145. lcd->next_line = 1;
  146. str -= 1;
  147. len += 1;
  148. } else {
  149. lcd->char_buffer[lcd->line * LCD_CHAR_BUF_W + lcd->column] = *str;
  150. lcd->column += 1;
  151. int max = lcd->line * LCD_CHAR_BUF_W + lcd->column;
  152. if (max > redraw_max) {
  153. redraw_max = max;
  154. }
  155. }
  156. }
  157. // we must draw upside down, because the LCD is upside down
  158. for (int i = redraw_min; i < redraw_max; i++) {
  159. uint page = i / LCD_CHAR_BUF_W;
  160. uint offset = 8 * (LCD_CHAR_BUF_W - 1 - (i - (page * LCD_CHAR_BUF_W)));
  161. lcd_out(lcd, LCD_INSTR, 0xb0 | page); // page address set
  162. lcd_out(lcd, LCD_INSTR, 0x10 | ((offset >> 4) & 0x0f)); // column address set upper
  163. lcd_out(lcd, LCD_INSTR, 0x00 | (offset & 0x0f)); // column address set lower
  164. int chr = lcd->char_buffer[i];
  165. if (chr < 32 || chr > 126) {
  166. chr = 127;
  167. }
  168. const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
  169. for (int j = 7; j >= 0; j--) {
  170. lcd_out(lcd, LCD_DATA, chr_data[j]);
  171. }
  172. }
  173. }
  174. /// \classmethod \constructor(skin_position)
  175. ///
  176. /// Construct an LCD object in the given skin position. `skin_position` can be 'X' or 'Y', and
  177. /// should match the position where the LCD pyskin is plugged in.
  178. STATIC mp_obj_t pyb_lcd_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  179. // check arguments
  180. mp_arg_check_num(n_args, n_kw, 1, 1, false);
  181. // get LCD position
  182. const char *lcd_id = mp_obj_str_get_str(args[0]);
  183. // create lcd object
  184. pyb_lcd_obj_t *lcd = m_new_obj(pyb_lcd_obj_t);
  185. lcd->base.type = &pyb_lcd_type;
  186. // configure pins
  187. // TODO accept an SPI object and pin objects for full customisation
  188. if ((lcd_id[0] | 0x20) == 'x' && lcd_id[1] == '\0') {
  189. lcd->spi = &spi_obj[0];
  190. lcd->pin_cs1 = pyb_pin_X3;
  191. lcd->pin_rst = pyb_pin_X4;
  192. lcd->pin_a0 = pyb_pin_X5;
  193. lcd->pin_bl = pyb_pin_X12;
  194. } else if ((lcd_id[0] | 0x20) == 'y' && lcd_id[1] == '\0') {
  195. lcd->spi = &spi_obj[1];
  196. lcd->pin_cs1 = pyb_pin_Y3;
  197. lcd->pin_rst = pyb_pin_Y4;
  198. lcd->pin_a0 = pyb_pin_Y5;
  199. lcd->pin_bl = pyb_pin_Y12;
  200. } else {
  201. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LCD(%s) doesn't exist", lcd_id));
  202. }
  203. // init the SPI bus
  204. SPI_InitTypeDef *init = &lcd->spi->spi->Init;
  205. init->Mode = SPI_MODE_MASTER;
  206. // compute the baudrate prescaler from the desired baudrate
  207. // select a prescaler that yields at most the desired baudrate
  208. uint spi_clock;
  209. if (lcd->spi->spi->Instance == SPI1) {
  210. // SPI1 is on APB2
  211. spi_clock = HAL_RCC_GetPCLK2Freq();
  212. } else {
  213. // SPI2 and SPI3 are on APB1
  214. spi_clock = HAL_RCC_GetPCLK1Freq();
  215. }
  216. uint br_prescale = spi_clock / 16000000; // datasheet says LCD can run at 20MHz, but we go for 16MHz
  217. if (br_prescale <= 2) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; }
  218. else if (br_prescale <= 4) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; }
  219. else if (br_prescale <= 8) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; }
  220. else if (br_prescale <= 16) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; }
  221. else if (br_prescale <= 32) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; }
  222. else if (br_prescale <= 64) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; }
  223. else if (br_prescale <= 128) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; }
  224. else { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; }
  225. // data is sent bigendian, latches on rising clock
  226. init->CLKPolarity = SPI_POLARITY_HIGH;
  227. init->CLKPhase = SPI_PHASE_2EDGE;
  228. init->Direction = SPI_DIRECTION_2LINES;
  229. init->DataSize = SPI_DATASIZE_8BIT;
  230. init->NSS = SPI_NSS_SOFT;
  231. init->FirstBit = SPI_FIRSTBIT_MSB;
  232. init->TIMode = SPI_TIMODE_DISABLED;
  233. init->CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  234. init->CRCPolynomial = 0;
  235. // init the SPI bus
  236. spi_init(lcd->spi, false);
  237. // set the pins to default values
  238. mp_hal_pin_high(lcd->pin_cs1);
  239. mp_hal_pin_high(lcd->pin_rst);
  240. mp_hal_pin_high(lcd->pin_a0);
  241. mp_hal_pin_low(lcd->pin_bl);
  242. // init the pins to be push/pull outputs
  243. mp_hal_pin_output(lcd->pin_cs1);
  244. mp_hal_pin_output(lcd->pin_rst);
  245. mp_hal_pin_output(lcd->pin_a0);
  246. mp_hal_pin_output(lcd->pin_bl);
  247. // init the LCD
  248. mp_hal_delay_ms(1); // wait a bit
  249. mp_hal_pin_low(lcd->pin_rst); // RST=0; reset
  250. mp_hal_delay_ms(1); // wait for reset; 2us min
  251. mp_hal_pin_high(lcd->pin_rst); // RST=1; enable
  252. mp_hal_delay_ms(1); // wait for reset; 2us min
  253. lcd_out(lcd, LCD_INSTR, 0xa0); // ADC select, normal
  254. lcd_out(lcd, LCD_INSTR, 0xc0); // common output mode select, normal (this flips the display)
  255. lcd_out(lcd, LCD_INSTR, 0xa2); // LCD bias set, 1/9 bias
  256. lcd_out(lcd, LCD_INSTR, 0x2f); // power control set, 0b111=(booster on, vreg on, vfollow on)
  257. lcd_out(lcd, LCD_INSTR, 0x21); // v0 voltage regulator internal resistor ratio set, 0b001=small
  258. lcd_out(lcd, LCD_INSTR, 0x81); // electronic volume mode set
  259. lcd_out(lcd, LCD_INSTR, 0x28); // electronic volume register set
  260. lcd_out(lcd, LCD_INSTR, 0x40); // display start line set, 0
  261. lcd_out(lcd, LCD_INSTR, 0xaf); // LCD display, on
  262. // clear LCD RAM
  263. for (int page = 0; page < 4; page++) {
  264. lcd_out(lcd, LCD_INSTR, 0xb0 | page); // page address set
  265. lcd_out(lcd, LCD_INSTR, 0x10); // column address set upper
  266. lcd_out(lcd, LCD_INSTR, 0x00); // column address set lower
  267. for (int i = 0; i < 128; i++) {
  268. lcd_out(lcd, LCD_DATA, 0x00);
  269. }
  270. }
  271. // clear local char buffer
  272. memset(lcd->char_buffer, ' ', LCD_CHAR_BUF_H * LCD_CHAR_BUF_W);
  273. lcd->line = 0;
  274. lcd->column = 0;
  275. lcd->next_line = 0;
  276. // clear local pixel buffer
  277. memset(lcd->pix_buf, 0, LCD_PIX_BUF_BYTE_SIZE);
  278. memset(lcd->pix_buf2, 0, LCD_PIX_BUF_BYTE_SIZE);
  279. return MP_OBJ_FROM_PTR(lcd);
  280. }
  281. /// \method command(instr_data, buf)
  282. ///
  283. /// Send an arbitrary command to the LCD. Pass 0 for `instr_data` to send an
  284. /// instruction, otherwise pass 1 to send data. `buf` is a buffer with the
  285. /// instructions/data to send.
  286. STATIC mp_obj_t pyb_lcd_command(mp_obj_t self_in, mp_obj_t instr_data_in, mp_obj_t val) {
  287. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(self_in);
  288. // get whether instr or data
  289. int instr_data = mp_obj_get_int(instr_data_in);
  290. // get the buffer to send from
  291. mp_buffer_info_t bufinfo;
  292. uint8_t data[1];
  293. pyb_buf_get_for_send(val, &bufinfo, data);
  294. // send the data
  295. for (uint i = 0; i < bufinfo.len; i++) {
  296. lcd_out(self, instr_data, ((byte*)bufinfo.buf)[i]);
  297. }
  298. return mp_const_none;
  299. }
  300. STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_lcd_command_obj, pyb_lcd_command);
  301. /// \method contrast(value)
  302. ///
  303. /// Set the contrast of the LCD. Valid values are between 0 and 47.
  304. STATIC mp_obj_t pyb_lcd_contrast(mp_obj_t self_in, mp_obj_t contrast_in) {
  305. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(self_in);
  306. int contrast = mp_obj_get_int(contrast_in);
  307. if (contrast < 0) {
  308. contrast = 0;
  309. } else if (contrast > 0x2f) {
  310. contrast = 0x2f;
  311. }
  312. lcd_out(self, LCD_INSTR, 0x81); // electronic volume mode set
  313. lcd_out(self, LCD_INSTR, contrast); // electronic volume register set
  314. return mp_const_none;
  315. }
  316. STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_contrast_obj, pyb_lcd_contrast);
  317. /// \method light(value)
  318. ///
  319. /// Turn the backlight on/off. True or 1 turns it on, False or 0 turns it off.
  320. STATIC mp_obj_t pyb_lcd_light(mp_obj_t self_in, mp_obj_t value) {
  321. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(self_in);
  322. if (mp_obj_is_true(value)) {
  323. mp_hal_pin_high(self->pin_bl); // set pin high to turn backlight on
  324. } else {
  325. mp_hal_pin_low(self->pin_bl); // set pin low to turn backlight off
  326. }
  327. return mp_const_none;
  328. }
  329. STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_light_obj, pyb_lcd_light);
  330. /// \method write(str)
  331. ///
  332. /// Write the string `str` to the screen. It will appear immediately.
  333. STATIC mp_obj_t pyb_lcd_write(mp_obj_t self_in, mp_obj_t str) {
  334. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(self_in);
  335. size_t len;
  336. const char *data = mp_obj_str_get_data(str, &len);
  337. lcd_write_strn(self, data, len);
  338. return mp_const_none;
  339. }
  340. STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_write_obj, pyb_lcd_write);
  341. /// \method fill(colour)
  342. ///
  343. /// Fill the screen with the given colour (0 or 1 for white or black).
  344. ///
  345. /// This method writes to the hidden buffer. Use `show()` to show the buffer.
  346. STATIC mp_obj_t pyb_lcd_fill(mp_obj_t self_in, mp_obj_t col_in) {
  347. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(self_in);
  348. int col = mp_obj_get_int(col_in);
  349. if (col) {
  350. col = 0xff;
  351. }
  352. memset(self->pix_buf, col, LCD_PIX_BUF_BYTE_SIZE);
  353. memset(self->pix_buf2, col, LCD_PIX_BUF_BYTE_SIZE);
  354. return mp_const_none;
  355. }
  356. STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_fill_obj, pyb_lcd_fill);
  357. /// \method get(x, y)
  358. ///
  359. /// Get the pixel at the position `(x, y)`. Returns 0 or 1.
  360. ///
  361. /// This method reads from the visible buffer.
  362. STATIC mp_obj_t pyb_lcd_get(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
  363. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(self_in);
  364. int x = mp_obj_get_int(x_in);
  365. int y = mp_obj_get_int(y_in);
  366. if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
  367. uint byte_pos = x + 128 * ((uint)y >> 3);
  368. if (self->pix_buf[byte_pos] & (1 << (y & 7))) {
  369. return mp_obj_new_int(1);
  370. }
  371. }
  372. return mp_obj_new_int(0);
  373. }
  374. STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_lcd_get_obj, pyb_lcd_get);
  375. /// \method pixel(x, y, colour)
  376. ///
  377. /// Set the pixel at `(x, y)` to the given colour (0 or 1).
  378. ///
  379. /// This method writes to the hidden buffer. Use `show()` to show the buffer.
  380. STATIC mp_obj_t pyb_lcd_pixel(size_t n_args, const mp_obj_t *args) {
  381. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  382. int x = mp_obj_get_int(args[1]);
  383. int y = mp_obj_get_int(args[2]);
  384. if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
  385. uint byte_pos = x + 128 * ((uint)y >> 3);
  386. if (mp_obj_get_int(args[3]) == 0) {
  387. self->pix_buf2[byte_pos] &= ~(1 << (y & 7));
  388. } else {
  389. self->pix_buf2[byte_pos] |= 1 << (y & 7);
  390. }
  391. }
  392. return mp_const_none;
  393. }
  394. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_lcd_pixel_obj, 4, 4, pyb_lcd_pixel);
  395. /// \method text(str, x, y, colour)
  396. ///
  397. /// Draw the given text to the position `(x, y)` using the given colour (0 or 1).
  398. ///
  399. /// This method writes to the hidden buffer. Use `show()` to show the buffer.
  400. STATIC mp_obj_t pyb_lcd_text(size_t n_args, const mp_obj_t *args) {
  401. // extract arguments
  402. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  403. size_t len;
  404. const char *data = mp_obj_str_get_data(args[1], &len);
  405. int x0 = mp_obj_get_int(args[2]);
  406. int y0 = mp_obj_get_int(args[3]);
  407. int col = mp_obj_get_int(args[4]);
  408. // loop over chars
  409. for (const char *top = data + len; data < top; data++) {
  410. // get char and make sure its in range of font
  411. uint chr = *(byte*)data;
  412. if (chr < 32 || chr > 127) {
  413. chr = 127;
  414. }
  415. // get char data
  416. const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
  417. // loop over char data
  418. for (uint j = 0; j < 8; j++, x0++) {
  419. if (0 <= x0 && x0 < LCD_PIX_BUF_W) { // clip x
  420. uint vline_data = chr_data[j]; // each byte of char data is a vertical column of 8 pixels, LSB at top
  421. for (int y = y0; vline_data; vline_data >>= 1, y++) { // scan over vertical column
  422. if (vline_data & 1) { // only draw if pixel set
  423. if (0 <= y && y < LCD_PIX_BUF_H) { // clip y
  424. uint byte_pos = x0 + LCD_PIX_BUF_W * ((uint)y >> 3);
  425. if (col == 0) {
  426. // clear pixel
  427. self->pix_buf2[byte_pos] &= ~(1 << (y & 7));
  428. } else {
  429. // set pixel
  430. self->pix_buf2[byte_pos] |= 1 << (y & 7);
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }
  438. return mp_const_none;
  439. }
  440. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_lcd_text_obj, 5, 5, pyb_lcd_text);
  441. /// \method show()
  442. ///
  443. /// Show the hidden buffer on the screen.
  444. STATIC mp_obj_t pyb_lcd_show(mp_obj_t self_in) {
  445. pyb_lcd_obj_t *self = MP_OBJ_TO_PTR(self_in);
  446. memcpy(self->pix_buf, self->pix_buf2, LCD_PIX_BUF_BYTE_SIZE);
  447. for (uint page = 0; page < 4; page++) {
  448. lcd_out(self, LCD_INSTR, 0xb0 | page); // page address set
  449. lcd_out(self, LCD_INSTR, 0x10); // column address set upper; 0
  450. lcd_out(self, LCD_INSTR, 0x00); // column address set lower; 0
  451. for (uint i = 0; i < 128; i++) {
  452. lcd_out(self, LCD_DATA, self->pix_buf[128 * page + 127 - i]);
  453. }
  454. }
  455. return mp_const_none;
  456. }
  457. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_lcd_show_obj, pyb_lcd_show);
  458. STATIC const mp_rom_map_elem_t pyb_lcd_locals_dict_table[] = {
  459. // instance methods
  460. { MP_ROM_QSTR(MP_QSTR_command), MP_ROM_PTR(&pyb_lcd_command_obj) },
  461. { MP_ROM_QSTR(MP_QSTR_contrast), MP_ROM_PTR(&pyb_lcd_contrast_obj) },
  462. { MP_ROM_QSTR(MP_QSTR_light), MP_ROM_PTR(&pyb_lcd_light_obj) },
  463. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_lcd_write_obj) },
  464. { MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&pyb_lcd_fill_obj) },
  465. { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&pyb_lcd_get_obj) },
  466. { MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&pyb_lcd_pixel_obj) },
  467. { MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&pyb_lcd_text_obj) },
  468. { MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&pyb_lcd_show_obj) },
  469. };
  470. STATIC MP_DEFINE_CONST_DICT(pyb_lcd_locals_dict, pyb_lcd_locals_dict_table);
  471. const mp_obj_type_t pyb_lcd_type = {
  472. { &mp_type_type },
  473. .name = MP_QSTR_LCD,
  474. .make_new = pyb_lcd_make_new,
  475. .locals_dict = (mp_obj_dict_t*)&pyb_lcd_locals_dict,
  476. };
  477. #endif // MICROPY_HW_HAS_LCD