lcd.c 19.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
#include <stdio.h>
Damien George's avatar
Damien George committed
28
29
#include <string.h>

30
#include "py/mphal.h"
31
32
#include "py/nlr.h"
#include "py/runtime.h"
Damien George's avatar
Damien George committed
33
34
35

#if MICROPY_HW_HAS_LCD

36
37
38
39
#include "pin.h"
#include "genhdr/pins.h"
#include "bufhelper.h"
#include "spi.h"
Damien George's avatar
Damien George committed
40
41
42
#include "font_petme128_8x8.h"
#include "lcd.h"

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/// \moduleref pyb
/// \class LCD - LCD control for the LCD touch-sensor pyskin
///
/// The LCD class is used to control the LCD on the LCD touch-sensor pyskin,
/// LCD32MKv1.0.  The LCD is a 128x32 pixel monochrome screen, part NHD-C12832A1Z.
///
/// The pyskin must be connected in either the X or Y positions, and then
/// an LCD object is made using:
///
///     lcd = pyb.LCD('X')      # if pyskin is in the X position
///     lcd = pyb.LCD('Y')      # if pyskin is in the Y position
///
/// Then you can use:
///
///     lcd.light(True)                 # turn the backlight on
///     lcd.write('Hello world!\n')     # print text to the screen
///
/// This driver implements a double buffer for setting/getting pixels.
/// For example, to make a bouncing dot, try:
///
///     x = y = 0
///     dx = dy = 1
///     while True:
///         # update the dot's position
///         x += dx
///         y += dy
///
///         # make the dot bounce of the edges of the screen
///         if x <= 0 or x >= 127: dx = -dx
///         if y <= 0 or y >= 31: dy = -dy
///
///         lcd.fill(0)                 # clear the buffer
///         lcd.pixel(x, y, 1)          # draw the dot
///         lcd.show()                  # show the buffer
///         pyb.delay(50)               # pause for 50ms

Damien George's avatar
Damien George committed
79
80
81
#define LCD_INSTR (0)
#define LCD_DATA (1)

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#define LCD_CHAR_BUF_W (16)
#define LCD_CHAR_BUF_H (4)

#define LCD_PIX_BUF_W (128)
#define LCD_PIX_BUF_H (32)
#define LCD_PIX_BUF_BYTE_SIZE (LCD_PIX_BUF_W * LCD_PIX_BUF_H / 8)

typedef struct _pyb_lcd_obj_t {
    mp_obj_base_t base;

    // hardware control for the LCD
    SPI_HandleTypeDef *spi;
    const pin_obj_t *pin_cs1;
    const pin_obj_t *pin_rst;
    const pin_obj_t *pin_a0;
    const pin_obj_t *pin_bl;

    // character buffer for stdout-like output
    char char_buffer[LCD_CHAR_BUF_W * LCD_CHAR_BUF_H];
    int line;
    int column;
    int next_line;

    // double buffering for pixel buffer
    byte pix_buf[LCD_PIX_BUF_BYTE_SIZE];
    byte pix_buf2[LCD_PIX_BUF_BYTE_SIZE];
} pyb_lcd_obj_t;

STATIC void lcd_delay(void) {
111
112
113
    __asm volatile ("nop\nnop");
}

114
STATIC void lcd_out(pyb_lcd_obj_t *lcd, int instr_data, uint8_t i) {
115
    lcd_delay();
116
    mp_hal_pin_low(lcd->pin_cs1); // CS=0; enable
Damien George's avatar
Damien George committed
117
    if (instr_data == LCD_INSTR) {
118
        mp_hal_pin_low(lcd->pin_a0); // A0=0; select instr reg
Damien George's avatar
Damien George committed
119
    } else {
120
        mp_hal_pin_high(lcd->pin_a0); // A0=1; select data reg
Damien George's avatar
Damien George committed
121
    }
122
123
    lcd_delay();
    HAL_SPI_Transmit(lcd->spi, &i, 1, 1000);
124
    lcd_delay();
125
    mp_hal_pin_high(lcd->pin_cs1); // CS=1; disable
Damien George's avatar
Damien George committed
126
127
}

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// write a string to the LCD at the current cursor location
// output it straight away (doesn't use the pixel buffer)
STATIC void lcd_write_strn(pyb_lcd_obj_t *lcd, const char *str, unsigned int len) {
    int redraw_min = lcd->line * LCD_CHAR_BUF_W + lcd->column;
    int redraw_max = redraw_min;
    for (; len > 0; len--, str++) {
        // move to next line if needed
        if (lcd->next_line) {
            if (lcd->line + 1 < LCD_CHAR_BUF_H) {
                lcd->line += 1;
            } else {
                lcd->line = LCD_CHAR_BUF_H - 1;
                for (int i = 0; i < LCD_CHAR_BUF_W * (LCD_CHAR_BUF_H - 1); i++) {
                    lcd->char_buffer[i] = lcd->char_buffer[i + LCD_CHAR_BUF_W];
                }
                for (int i = 0; i < LCD_CHAR_BUF_W; i++) {
                    lcd->char_buffer[LCD_CHAR_BUF_W * (LCD_CHAR_BUF_H - 1) + i] = ' ';
                }
                redraw_min = 0;
                redraw_max = LCD_CHAR_BUF_W * LCD_CHAR_BUF_H;
            }
            lcd->next_line = 0;
            lcd->column = 0;
        }
        if (*str == '\n') {
            lcd->next_line = 1;
        } else if (*str == '\r') {
            lcd->column = 0;
        } else if (*str == '\b') {
            if (lcd->column > 0) {
                lcd->column--;
                redraw_min = 0; // could optimise this to not redraw everything
            }
        } else if (lcd->column >= LCD_CHAR_BUF_W) {
            lcd->next_line = 1;
            str -= 1;
            len += 1;
Damien George's avatar
Damien George committed
165
        } else {
166
167
168
169
170
171
            lcd->char_buffer[lcd->line * LCD_CHAR_BUF_W + lcd->column] = *str;
            lcd->column += 1;
            int max = lcd->line * LCD_CHAR_BUF_W + lcd->column;
            if (max > redraw_max) {
                redraw_max = max;
            }
Damien George's avatar
Damien George committed
172
173
174
        }
    }

175
176
177
178
179
180
181
182
183
184
    // we must draw upside down, because the LCD is upside down
    for (int i = redraw_min; i < redraw_max; i++) {
        uint page = i / LCD_CHAR_BUF_W;
        uint offset = 8 * (LCD_CHAR_BUF_W - 1 - (i - (page * LCD_CHAR_BUF_W)));
        lcd_out(lcd, LCD_INSTR, 0xb0 | page); // page address set
        lcd_out(lcd, LCD_INSTR, 0x10 | ((offset >> 4) & 0x0f)); // column address set upper
        lcd_out(lcd, LCD_INSTR, 0x00 | (offset & 0x0f)); // column address set lower
        int chr = lcd->char_buffer[i];
        if (chr < 32 || chr > 126) {
            chr = 127;
Damien George's avatar
Damien George committed
185
        }
186
187
188
        const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
        for (int j = 7; j >= 0; j--) {
            lcd_out(lcd, LCD_DATA, chr_data[j]);
Damien George's avatar
Damien George committed
189
190
191
192
        }
    }
}

193
194
195
196
/// \classmethod \constructor(skin_position)
///
/// Construct an LCD object in the given skin position.  `skin_position` can be 'X' or 'Y', and
/// should match the position where the LCD pyskin is plugged in.
197
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) {
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    // check arguments
    mp_arg_check_num(n_args, n_kw, 1, 1, false);

    // get LCD position
    const char *lcd_id = mp_obj_str_get_str(args[0]);

    // create lcd object
    pyb_lcd_obj_t *lcd = m_new_obj(pyb_lcd_obj_t);
    lcd->base.type = &pyb_lcd_type;

    // configure pins
    // TODO accept an SPI object and pin objects for full customisation
    if ((lcd_id[0] | 0x20) == 'x' && lcd_id[1] == '\0') {
        lcd->spi = &SPIHandle1;
212
213
214
215
        lcd->pin_cs1 = &pyb_pin_X3;
        lcd->pin_rst = &pyb_pin_X4;
        lcd->pin_a0 = &pyb_pin_X5;
        lcd->pin_bl = &pyb_pin_X12;
216
217
    } else if ((lcd_id[0] | 0x20) == 'y' && lcd_id[1] == '\0') {
        lcd->spi = &SPIHandle2;
218
219
220
221
        lcd->pin_cs1 = &pyb_pin_Y3;
        lcd->pin_rst = &pyb_pin_Y4;
        lcd->pin_a0 = &pyb_pin_Y5;
        lcd->pin_bl = &pyb_pin_Y12;
Damien George's avatar
Damien George committed
222
    } else {
223
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LCD(%s) doesn't exist", lcd_id));
Damien George's avatar
Damien George committed
224
225
    }

226
227
228
    // init the SPI bus
    SPI_InitTypeDef *init = &lcd->spi->Init;
    init->Mode = SPI_MODE_MASTER;
Damien George's avatar
Damien George committed
229

230
231
232
233
234
235
236
237
238
    // compute the baudrate prescaler from the desired baudrate
    // select a prescaler that yields at most the desired baudrate
    uint spi_clock;
    if (lcd->spi->Instance == SPI1) {
        // SPI1 is on APB2
        spi_clock = HAL_RCC_GetPCLK2Freq();
    } else {
        // SPI2 and SPI3 are on APB1
        spi_clock = HAL_RCC_GetPCLK1Freq();
Damien George's avatar
Damien George committed
239
    }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
    uint br_prescale = spi_clock / 16000000; // datasheet says LCD can run at 20MHz, but we go for 16MHz
    if (br_prescale <= 2) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; }
    else if (br_prescale <= 4) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; }
    else if (br_prescale <= 8) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; }
    else if (br_prescale <= 16) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; }
    else if (br_prescale <= 32) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; }
    else if (br_prescale <= 64) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; }
    else if (br_prescale <= 128) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; }
    else { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; }

    // data is sent bigendian, latches on rising clock
    init->CLKPolarity = SPI_POLARITY_HIGH;
    init->CLKPhase = SPI_PHASE_2EDGE;
    init->Direction = SPI_DIRECTION_2LINES;
    init->DataSize = SPI_DATASIZE_8BIT;
    init->NSS = SPI_NSS_SOFT;
    init->FirstBit = SPI_FIRSTBIT_MSB;
    init->TIMode = SPI_TIMODE_DISABLED;
    init->CRCCalculation = SPI_CRCCALCULATION_DISABLED;
    init->CRCPolynomial = 0;

    // init the SPI bus
262
    spi_init(lcd->spi, false);
263
264

    // set the pins to default values
265
266
267
268
    mp_hal_pin_high(lcd->pin_cs1);
    mp_hal_pin_high(lcd->pin_rst);
    mp_hal_pin_high(lcd->pin_a0);
    mp_hal_pin_low(lcd->pin_bl);
269
270

    // init the pins to be push/pull outputs
271
272
273
274
    mp_hal_pin_output(lcd->pin_cs1);
    mp_hal_pin_output(lcd->pin_rst);
    mp_hal_pin_output(lcd->pin_a0);
    mp_hal_pin_output(lcd->pin_bl);
Damien George's avatar
Damien George committed
275
276

    // init the LCD
277
    mp_hal_delay_ms(1); // wait a bit
278
    mp_hal_pin_low(lcd->pin_rst); // RST=0; reset
279
    mp_hal_delay_ms(1); // wait for reset; 2us min
280
    mp_hal_pin_high(lcd->pin_rst); // RST=1; enable
281
    mp_hal_delay_ms(1); // wait for reset; 2us min
282
283
284
285
286
287
288
289
290
291
292
    lcd_out(lcd, LCD_INSTR, 0xa0); // ADC select, normal
    lcd_out(lcd, LCD_INSTR, 0xc0); // common output mode select, normal (this flips the display)
    lcd_out(lcd, LCD_INSTR, 0xa2); // LCD bias set, 1/9 bias
    lcd_out(lcd, LCD_INSTR, 0x2f); // power control set, 0b111=(booster on, vreg on, vfollow on)
    lcd_out(lcd, LCD_INSTR, 0x21); // v0 voltage regulator internal resistor ratio set, 0b001=small
    lcd_out(lcd, LCD_INSTR, 0x81); // electronic volume mode set
    lcd_out(lcd, LCD_INSTR, 0x28); // electronic volume register set
    lcd_out(lcd, LCD_INSTR, 0x40); // display start line set, 0
    lcd_out(lcd, LCD_INSTR, 0xaf); // LCD display, on

    // clear LCD RAM
Damien George's avatar
Damien George committed
293
    for (int page = 0; page < 4; page++) {
294
295
296
        lcd_out(lcd, LCD_INSTR, 0xb0 | page); // page address set
        lcd_out(lcd, LCD_INSTR, 0x10); // column address set upper
        lcd_out(lcd, LCD_INSTR, 0x00); // column address set lower
Damien George's avatar
Damien George committed
297
        for (int i = 0; i < 128; i++) {
298
            lcd_out(lcd, LCD_DATA, 0x00);
Damien George's avatar
Damien George committed
299
300
301
        }
    }

302
303
304
305
306
307
308
309
310
311
312
313
314
    // clear local char buffer
    memset(lcd->char_buffer, ' ', LCD_CHAR_BUF_H * LCD_CHAR_BUF_W);
    lcd->line = 0;
    lcd->column = 0;
    lcd->next_line = 0;

    // clear local pixel buffer
    memset(lcd->pix_buf, 0, LCD_PIX_BUF_BYTE_SIZE);
    memset(lcd->pix_buf2, 0, LCD_PIX_BUF_BYTE_SIZE);

    return lcd;
}

315
316
317
318
319
/// \method command(instr_data, buf)
///
/// Send an arbitrary command to the LCD.  Pass 0 for `instr_data` to send an
/// instruction, otherwise pass 1 to send data.  `buf` is a buffer with the
/// instructions/data to send.
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
STATIC mp_obj_t pyb_lcd_command(mp_obj_t self_in, mp_obj_t instr_data_in, mp_obj_t val) {
    pyb_lcd_obj_t *self = self_in;

    // get whether instr or data
    int instr_data = mp_obj_get_int(instr_data_in);

    // get the buffer to send from
    mp_buffer_info_t bufinfo;
    uint8_t data[1];
    pyb_buf_get_for_send(val, &bufinfo, data);

    // send the data
    for (uint i = 0; i < bufinfo.len; i++) {
        lcd_out(self, instr_data, ((byte*)bufinfo.buf)[i]);
    }

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_lcd_command_obj, pyb_lcd_command);

340
341
342
/// \method contrast(value)
///
/// Set the contrast of the LCD.  Valid values are between 0 and 47.
343
344
345
346
347
348
349
STATIC mp_obj_t pyb_lcd_contrast(mp_obj_t self_in, mp_obj_t contrast_in) {
    pyb_lcd_obj_t *self = self_in;
    int contrast = mp_obj_get_int(contrast_in);
    if (contrast < 0) {
        contrast = 0;
    } else if (contrast > 0x2f) {
        contrast = 0x2f;
Damien George's avatar
Damien George committed
350
    }
351
352
353
    lcd_out(self, LCD_INSTR, 0x81); // electronic volume mode set
    lcd_out(self, LCD_INSTR, contrast); // electronic volume register set
    return mp_const_none;
Damien George's avatar
Damien George committed
354
}
355
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_contrast_obj, pyb_lcd_contrast);
Damien George's avatar
Damien George committed
356

357
358
359
/// \method light(value)
///
/// Turn the backlight on/off.  True or 1 turns it on, False or 0 turns it off.
360
361
362
STATIC mp_obj_t pyb_lcd_light(mp_obj_t self_in, mp_obj_t value) {
    pyb_lcd_obj_t *self = self_in;
    if (mp_obj_is_true(value)) {
363
        mp_hal_pin_high(self->pin_bl); // set pin high to turn backlight on
364
    } else {
365
        mp_hal_pin_low(self->pin_bl); // set pin low to turn backlight off
366
367
368
369
    }
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_light_obj, pyb_lcd_light);
Damien George's avatar
Damien George committed
370

371
372
373
/// \method write(str)
///
/// Write the string `str` to the screen.  It will appear immediately.
374
375
STATIC mp_obj_t pyb_lcd_write(mp_obj_t self_in, mp_obj_t str) {
    pyb_lcd_obj_t *self = self_in;
376
    size_t len;
377
378
379
    const char *data = mp_obj_str_get_data(str, &len);
    lcd_write_strn(self, data, len);
    return mp_const_none;
Damien George's avatar
Damien George committed
380
}
381
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_write_obj, pyb_lcd_write);
Damien George's avatar
Damien George committed
382

383
384
385
386
387
/// \method fill(colour)
///
/// Fill the screen with the given colour (0 or 1 for white or black).
///
/// This method writes to the hidden buffer.  Use `show()` to show the buffer.
388
389
390
391
392
393
394
395
396
STATIC mp_obj_t pyb_lcd_fill(mp_obj_t self_in, mp_obj_t col_in) {
    pyb_lcd_obj_t *self = self_in;
    int col = mp_obj_get_int(col_in);
    if (col) {
        col = 0xff;
    }
    memset(self->pix_buf, col, LCD_PIX_BUF_BYTE_SIZE);
    memset(self->pix_buf2, col, LCD_PIX_BUF_BYTE_SIZE);
    return mp_const_none;
Damien George's avatar
Damien George committed
397
}
398
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_lcd_fill_obj, pyb_lcd_fill);
Damien George's avatar
Damien George committed
399

400
401
402
403
404
/// \method get(x, y)
///
/// Get the pixel at the position `(x, y)`.  Returns 0 or 1.
///
/// This method reads from the visible buffer.
405
406
407
408
409
410
411
412
STATIC mp_obj_t pyb_lcd_get(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
    pyb_lcd_obj_t *self = self_in;
    int x = mp_obj_get_int(x_in);
    int y = mp_obj_get_int(y_in);
    if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
        uint byte_pos = x + 128 * ((uint)y >> 3);
        if (self->pix_buf[byte_pos] & (1 << (y & 7))) {
            return mp_obj_new_int(1);
Damien George's avatar
Damien George committed
413
        }
414
415
416
417
418
    }
    return mp_obj_new_int(0);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_lcd_get_obj, pyb_lcd_get);

419
420
421
422
423
/// \method pixel(x, y, colour)
///
/// Set the pixel at `(x, y)` to the given colour (0 or 1).
///
/// This method writes to the hidden buffer.  Use `show()` to show the buffer.
424
STATIC mp_obj_t pyb_lcd_pixel(mp_uint_t n_args, const mp_obj_t *args) {
425
426
427
428
429
430
431
    pyb_lcd_obj_t *self = args[0];
    int x = mp_obj_get_int(args[1]);
    int y = mp_obj_get_int(args[2]);
    if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
        uint byte_pos = x + 128 * ((uint)y >> 3);
        if (mp_obj_get_int(args[3]) == 0) {
            self->pix_buf2[byte_pos] &= ~(1 << (y & 7));
Damien George's avatar
Damien George committed
432
        } else {
433
            self->pix_buf2[byte_pos] |= 1 << (y & 7);
Damien George's avatar
Damien George committed
434
435
        }
    }
436
437
438
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_lcd_pixel_obj, 4, 4, pyb_lcd_pixel);
Damien George's avatar
Damien George committed
439

440
441
442
443
444
/// \method text(str, x, y, colour)
///
/// Draw the given text to the position `(x, y)` using the given colour (0 or 1).
///
/// This method writes to the hidden buffer.  Use `show()` to show the buffer.
445
STATIC mp_obj_t pyb_lcd_text(mp_uint_t n_args, const mp_obj_t *args) {
446
447
    // extract arguments
    pyb_lcd_obj_t *self = args[0];
448
    size_t len;
449
450
451
452
453
454
455
456
457
458
    const char *data = mp_obj_str_get_data(args[1], &len);
    int x0 = mp_obj_get_int(args[2]);
    int y0 = mp_obj_get_int(args[3]);
    int col = mp_obj_get_int(args[4]);

    // loop over chars
    for (const char *top = data + len; data < top; data++) {
        // get char and make sure its in range of font
        uint chr = *(byte*)data;
        if (chr < 32 || chr > 127) {
Damien George's avatar
Damien George committed
459
460
            chr = 127;
        }
461
        // get char data
Damien George's avatar
Damien George committed
462
        const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
        // loop over char data
        for (uint j = 0; j < 8; j++, x0++) {
            if (0 <= x0 && x0 < LCD_PIX_BUF_W) { // clip x
                uint vline_data = chr_data[j]; // each byte of char data is a vertical column of 8 pixels, LSB at top
                for (int y = y0; vline_data; vline_data >>= 1, y++) { // scan over vertical column
                    if (vline_data & 1) { // only draw if pixel set
                        if (0 <= y && y < LCD_PIX_BUF_H) { // clip y
                            uint byte_pos = x0 + LCD_PIX_BUF_W * ((uint)y >> 3);
                            if (col == 0) {
                                // clear pixel
                                self->pix_buf2[byte_pos] &= ~(1 << (y & 7));
                            } else {
                                // set pixel
                                self->pix_buf2[byte_pos] |= 1 << (y & 7);
                            }
                        }
                    }
                }
            }
Damien George's avatar
Damien George committed
482
483
484
        }
    }

485
486
487
488
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_lcd_text_obj, 5, 5, pyb_lcd_text);

489
490
491
/// \method show()
///
/// Show the hidden buffer on the screen.
492
493
494
495
496
497
498
499
500
501
STATIC mp_obj_t pyb_lcd_show(mp_obj_t self_in) {
    pyb_lcd_obj_t *self = self_in;
    memcpy(self->pix_buf, self->pix_buf2, LCD_PIX_BUF_BYTE_SIZE);
    for (uint page = 0; page < 4; page++) {
        lcd_out(self, LCD_INSTR, 0xb0 | page); // page address set
        lcd_out(self, LCD_INSTR, 0x10); // column address set upper; 0
        lcd_out(self, LCD_INSTR, 0x00); // column address set lower; 0
        for (uint i = 0; i < 128; i++) {
            lcd_out(self, LCD_DATA, self->pix_buf[128 * page + 127 - i]);
        }
Damien George's avatar
Damien George committed
502
    }
503
    return mp_const_none;
Damien George's avatar
Damien George committed
504
}
505
506
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_lcd_show_obj, pyb_lcd_show);

507
STATIC const mp_rom_map_elem_t pyb_lcd_locals_dict_table[] = {
508
    // instance methods
509
510
511
512
513
514
515
516
517
    { MP_ROM_QSTR(MP_QSTR_command), MP_ROM_PTR(&pyb_lcd_command_obj) },
    { MP_ROM_QSTR(MP_QSTR_contrast), MP_ROM_PTR(&pyb_lcd_contrast_obj) },
    { MP_ROM_QSTR(MP_QSTR_light), MP_ROM_PTR(&pyb_lcd_light_obj) },
    { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_lcd_write_obj) },
    { MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&pyb_lcd_fill_obj) },
    { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&pyb_lcd_get_obj) },
    { MP_ROM_QSTR(MP_QSTR_pixel), MP_ROM_PTR(&pyb_lcd_pixel_obj) },
    { MP_ROM_QSTR(MP_QSTR_text), MP_ROM_PTR(&pyb_lcd_text_obj) },
    { MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&pyb_lcd_show_obj) },
518
519
520
521
522
523
524
525
};

STATIC MP_DEFINE_CONST_DICT(pyb_lcd_locals_dict, pyb_lcd_locals_dict_table);

const mp_obj_type_t pyb_lcd_type = {
    { &mp_type_type },
    .name = MP_QSTR_LCD,
    .make_new = pyb_lcd_make_new,
526
    .locals_dict = (mp_obj_dict_t*)&pyb_lcd_locals_dict,
527
};
Damien George's avatar
Damien George committed
528
529

#endif // MICROPY_HW_HAS_LCD