summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-03-19 13:11:59 +0000
committerDamien George <damien.p.george@gmail.com>2014-03-19 13:11:59 +0000
commite2e9011253a61502ac31f144312257c14506a815 (patch)
tree14808f6a344d58eb463be1fd71a7fcc6dae12b0a
parentb92d3e1fded0ad335f7a2c8c0f5dea91d258bae0 (diff)
downloadmicropython-e2e9011253a61502ac31f144312257c14506a815.tar.gz
micropython-e2e9011253a61502ac31f144312257c14506a815.zip
stmhal: Add LCD driver.
-rw-r--r--stmhal/font_petme128_8x8.h98
-rw-r--r--stmhal/lcd.c383
-rw-r--r--stmhal/lcd.h3
3 files changed, 484 insertions, 0 deletions
diff --git a/stmhal/font_petme128_8x8.h b/stmhal/font_petme128_8x8.h
new file mode 100644
index 0000000000..923348ecc8
--- /dev/null
+++ b/stmhal/font_petme128_8x8.h
@@ -0,0 +1,98 @@
+const uint8_t font_petme128_8x8[] = {
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 32=
+ 0x00,0x00,0x00,0x4f,0x4f,0x00,0x00,0x00, // 33=!
+ 0x00,0x07,0x07,0x00,0x00,0x07,0x07,0x00, // 34="
+ 0x14,0x7f,0x7f,0x14,0x14,0x7f,0x7f,0x14, // 35=#
+ 0x00,0x24,0x2e,0x6b,0x6b,0x3a,0x12,0x00, // 36=$
+ 0x00,0x63,0x33,0x18,0x0c,0x66,0x63,0x00, // 37=%
+ 0x00,0x32,0x7f,0x4d,0x4d,0x77,0x72,0x50, // 38=&
+ 0x00,0x00,0x00,0x04,0x06,0x03,0x01,0x00, // 39='
+ 0x00,0x00,0x1c,0x3e,0x63,0x41,0x00,0x00, // 40=(
+ 0x00,0x00,0x41,0x63,0x3e,0x1c,0x00,0x00, // 41=)
+ 0x08,0x2a,0x3e,0x1c,0x1c,0x3e,0x2a,0x08, // 42=*
+ 0x00,0x08,0x08,0x3e,0x3e,0x08,0x08,0x00, // 43=+
+ 0x00,0x00,0x80,0xe0,0x60,0x00,0x00,0x00, // 44=,
+ 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, // 45=-
+ 0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x00, // 46=.
+ 0x00,0x40,0x60,0x30,0x18,0x0c,0x06,0x02, // 47=/
+ 0x00,0x3e,0x7f,0x49,0x45,0x7f,0x3e,0x00, // 48=0
+ 0x00,0x40,0x44,0x7f,0x7f,0x40,0x40,0x00, // 49=1
+ 0x00,0x62,0x73,0x51,0x49,0x4f,0x46,0x00, // 50=2
+ 0x00,0x22,0x63,0x49,0x49,0x7f,0x36,0x00, // 51=3
+ 0x00,0x18,0x18,0x14,0x16,0x7f,0x7f,0x10, // 52=4
+ 0x00,0x27,0x67,0x45,0x45,0x7d,0x39,0x00, // 53=5
+ 0x00,0x3e,0x7f,0x49,0x49,0x7b,0x32,0x00, // 54=6
+ 0x00,0x03,0x03,0x79,0x7d,0x07,0x03,0x00, // 55=7
+ 0x00,0x36,0x7f,0x49,0x49,0x7f,0x36,0x00, // 56=8
+ 0x00,0x26,0x6f,0x49,0x49,0x7f,0x3e,0x00, // 57=9
+ 0x00,0x00,0x00,0x24,0x24,0x00,0x00,0x00, // 58=:
+ 0x00,0x00,0x80,0xe4,0x64,0x00,0x00,0x00, // 59=;
+ 0x00,0x08,0x1c,0x36,0x63,0x41,0x41,0x00, // 60=<
+ 0x00,0x14,0x14,0x14,0x14,0x14,0x14,0x00, // 61==
+ 0x00,0x41,0x41,0x63,0x36,0x1c,0x08,0x00, // 62=>
+ 0x00,0x02,0x03,0x51,0x59,0x0f,0x06,0x00, // 63=?
+ 0x00,0x3e,0x7f,0x41,0x4d,0x4f,0x2e,0x00, // 64=@
+ 0x00,0x7c,0x7e,0x0b,0x0b,0x7e,0x7c,0x00, // 65=A
+ 0x00,0x7f,0x7f,0x49,0x49,0x7f,0x36,0x00, // 66=B
+ 0x00,0x3e,0x7f,0x41,0x41,0x63,0x22,0x00, // 67=C
+ 0x00,0x7f,0x7f,0x41,0x63,0x3e,0x1c,0x00, // 68=D
+ 0x00,0x7f,0x7f,0x49,0x49,0x41,0x41,0x00, // 69=E
+ 0x00,0x7f,0x7f,0x09,0x09,0x01,0x01,0x00, // 70=F
+ 0x00,0x3e,0x7f,0x41,0x49,0x7b,0x3a,0x00, // 71=G
+ 0x00,0x7f,0x7f,0x08,0x08,0x7f,0x7f,0x00, // 72=H
+ 0x00,0x00,0x41,0x7f,0x7f,0x41,0x00,0x00, // 73=I
+ 0x00,0x20,0x60,0x41,0x7f,0x3f,0x01,0x00, // 74=J
+ 0x00,0x7f,0x7f,0x1c,0x36,0x63,0x41,0x00, // 75=K
+ 0x00,0x7f,0x7f,0x40,0x40,0x40,0x40,0x00, // 76=L
+ 0x00,0x7f,0x7f,0x06,0x0c,0x06,0x7f,0x7f, // 77=M
+ 0x00,0x7f,0x7f,0x0e,0x1c,0x7f,0x7f,0x00, // 78=N
+ 0x00,0x3e,0x7f,0x41,0x41,0x7f,0x3e,0x00, // 79=O
+ 0x00,0x7f,0x7f,0x09,0x09,0x0f,0x06,0x00, // 80=P
+ 0x00,0x1e,0x3f,0x21,0x61,0x7f,0x5e,0x00, // 81=Q
+ 0x00,0x7f,0x7f,0x19,0x39,0x6f,0x46,0x00, // 82=R
+ 0x00,0x26,0x6f,0x49,0x49,0x7b,0x32,0x00, // 83=S
+ 0x00,0x01,0x01,0x7f,0x7f,0x01,0x01,0x00, // 84=T
+ 0x00,0x3f,0x7f,0x40,0x40,0x7f,0x3f,0x00, // 85=U
+ 0x00,0x1f,0x3f,0x60,0x60,0x3f,0x1f,0x00, // 86=V
+ 0x00,0x7f,0x7f,0x30,0x18,0x30,0x7f,0x7f, // 87=W
+ 0x00,0x63,0x77,0x1c,0x1c,0x77,0x63,0x00, // 88=X
+ 0x00,0x07,0x0f,0x78,0x78,0x0f,0x07,0x00, // 89=Y
+ 0x00,0x61,0x71,0x59,0x4d,0x47,0x43,0x00, // 90=Z
+ 0x00,0x00,0x7f,0x7f,0x41,0x41,0x00,0x00, // 91=[
+ 0x00,0x02,0x06,0x0c,0x18,0x30,0x60,0x40, // 92='\'
+ 0x00,0x00,0x41,0x41,0x7f,0x7f,0x00,0x00, // 93=]
+ 0x00,0x08,0x0c,0x06,0x06,0x0c,0x08,0x00, // 94=^
+ 0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0, // 95=_
+ 0x00,0x00,0x01,0x03,0x06,0x04,0x00,0x00, // 96=`
+ 0x00,0x20,0x74,0x54,0x54,0x7c,0x78,0x00, // 97=a
+ 0x00,0x7f,0x7f,0x44,0x44,0x7c,0x38,0x00, // 98=b
+ 0x00,0x38,0x7c,0x44,0x44,0x6c,0x28,0x00, // 99=c
+ 0x00,0x38,0x7c,0x44,0x44,0x7f,0x7f,0x00, // 100=d
+ 0x00,0x38,0x7c,0x54,0x54,0x5c,0x58,0x00, // 101=e
+ 0x00,0x08,0x7e,0x7f,0x09,0x03,0x02,0x00, // 102=f
+ 0x00,0x98,0xbc,0xa4,0xa4,0xfc,0x7c,0x00, // 103=g
+ 0x00,0x7f,0x7f,0x04,0x04,0x7c,0x78,0x00, // 104=h
+ 0x00,0x00,0x00,0x7d,0x7d,0x00,0x00,0x00, // 105=i
+ 0x00,0x40,0xc0,0x80,0x80,0xfd,0x7d,0x00, // 106=j
+ 0x00,0x7f,0x7f,0x30,0x38,0x6c,0x44,0x00, // 107=k
+ 0x00,0x00,0x41,0x7f,0x7f,0x40,0x00,0x00, // 108=l
+ 0x00,0x7c,0x7c,0x18,0x30,0x18,0x7c,0x7c, // 109=m
+ 0x00,0x7c,0x7c,0x04,0x04,0x7c,0x78,0x00, // 110=n
+ 0x00,0x38,0x7c,0x44,0x44,0x7c,0x38,0x00, // 111=o
+ 0x00,0xfc,0xfc,0x24,0x24,0x3c,0x18,0x00, // 112=p
+ 0x00,0x18,0x3c,0x24,0x24,0xfc,0xfc,0x00, // 113=q
+ 0x00,0x7c,0x7c,0x04,0x04,0x0c,0x08,0x00, // 114=r
+ 0x00,0x48,0x5c,0x54,0x54,0x74,0x20,0x00, // 115=s
+ 0x04,0x04,0x3f,0x7f,0x44,0x64,0x20,0x00, // 116=t
+ 0x00,0x3c,0x7c,0x40,0x40,0x7c,0x3c,0x00, // 117=u
+ 0x00,0x1c,0x3c,0x60,0x60,0x3c,0x1c,0x00, // 118=v
+ 0x00,0x1c,0x7c,0x30,0x18,0x30,0x7c,0x1c, // 119=w
+ 0x00,0x44,0x6c,0x38,0x38,0x6c,0x44,0x00, // 120=x
+ 0x00,0x9c,0xbc,0xa0,0xa0,0xfc,0x7c,0x00, // 121=y
+ 0x00,0x44,0x64,0x74,0x5c,0x4c,0x44,0x00, // 122=z
+ 0x00,0x08,0x08,0x3e,0x77,0x41,0x41,0x00, // 123={
+ 0x00,0x00,0x00,0xff,0xff,0x00,0x00,0x00, // 124=|
+ 0x00,0x41,0x41,0x77,0x3e,0x08,0x08,0x00, // 125=}
+ 0x00,0x02,0x03,0x01,0x03,0x02,0x03,0x01, // 126=~
+ 0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55, // 127
+};
diff --git a/stmhal/lcd.c b/stmhal/lcd.c
new file mode 100644
index 0000000000..7c15cbcd21
--- /dev/null
+++ b/stmhal/lcd.c
@@ -0,0 +1,383 @@
+#include <string.h>
+#include <stm32f4xx_hal.h>
+
+#include "nlr.h"
+#include "misc.h"
+#include "mpconfig.h"
+
+#if MICROPY_HW_HAS_LCD
+
+#include "qstr.h"
+#include "parse.h"
+#include "obj.h"
+#include "runtime.h"
+
+#include "systick.h"
+#include "font_petme128_8x8.h"
+#include "lcd.h"
+
+#if defined(PYBOARD3)
+#define PYB_LCD_PORT (GPIOA)
+#define PYB_LCD_CS1_PIN (GPIO_PIN_0)
+#define PYB_LCD_RST_PIN (GPIO_PIN_1)
+#define PYB_LCD_A0_PIN (GPIO_PIN_2)
+#define PYB_LCD_SCL_PIN (GPIO_PIN_3)
+#define PYB_LCD_SI_PIN (GPIO_PIN_4)
+#elif defined(PYBOARD4)
+// X position
+#define PYB_LCD_PORT (GPIOA)
+#define PYB_LCD_CS1_PIN (GPIO_PIN_2) // X3
+#define PYB_LCD_RST_PIN (GPIO_PIN_3) // X4
+#define PYB_LCD_A0_PIN (GPIO_PIN_4) // X5
+#define PYB_LCD_SCL_PIN (GPIO_PIN_5) // X6
+#define PYB_LCD_SI_PIN (GPIO_PIN_7) // X8
+#define PYB_LCD_BL_PORT (GPIOC)
+#define PYB_LCD_BL_PIN (GPIO_PIN_5) // X12
+/*
+// Y position
+#define PYB_LCD_PORT (GPIOB)
+#define PYB_LCD_CS1_PIN (GPIO_PIN_8) // Y3 = PB8
+#define PYB_LCD_RST_PIN (GPIO_PIN_9) // Y4 = PB9
+#define PYB_LCD_A0_PIN (GPIO_PIN_12) // Y5 = PB12
+#define PYB_LCD_SCL_PIN (GPIO_PIN_13) // Y6 = PB13
+#define PYB_LCD_SI_PIN (GPIO_PIN_15) // Y8 = PB15
+#define PYB_LCD_BL_PORT (GPIOB)
+#define PYB_LCD_BL_PIN (GPIO_PIN_1) // Y12 = PB1
+*/
+#elif defined(STM32F4DISC)
+/* Configure if needed */
+#define PYB_LCD_PORT (GPIOA)
+#define PYB_LCD_CS1_PIN (GPIO_PIN_2) // X3
+#define PYB_LCD_RST_PIN (GPIO_PIN_3) // X4
+#define PYB_LCD_A0_PIN (GPIO_PIN_4) // X5
+#define PYB_LCD_SCL_PIN (GPIO_PIN_5) // X6
+#define PYB_LCD_SI_PIN (GPIO_PIN_7) // X8
+#define PYB_LCD_BL_PORT (GPIOC)
+#define PYB_LCD_BL_PIN (GPIO_PIN_5) // X12
+#endif
+
+#define LCD_INSTR (0)
+#define LCD_DATA (1)
+
+static void lcd_out(int instr_data, uint8_t i) {
+ HAL_Delay(0);
+ PYB_LCD_PORT->BSRRH = PYB_LCD_CS1_PIN; // CS=0; enable
+ if (instr_data == LCD_INSTR) {
+ PYB_LCD_PORT->BSRRH = PYB_LCD_A0_PIN; // A0=0; select instr reg
+ } else {
+ PYB_LCD_PORT->BSRRL = PYB_LCD_A0_PIN; // A0=1; select data reg
+ }
+ // send byte bigendian, latches on rising clock
+ for (uint32_t n = 0; n < 8; n++) {
+ HAL_Delay(0);
+ PYB_LCD_PORT->BSRRH = PYB_LCD_SCL_PIN; // SCL=0
+ if ((i & 0x80) == 0) {
+ PYB_LCD_PORT->BSRRH = PYB_LCD_SI_PIN; // SI=0
+ } else {
+ PYB_LCD_PORT->BSRRL = PYB_LCD_SI_PIN; // SI=1
+ }
+ i <<= 1;
+ HAL_Delay(0);
+ PYB_LCD_PORT->BSRRL = PYB_LCD_SCL_PIN; // SCL=1
+ }
+ PYB_LCD_PORT->BSRRL = PYB_LCD_CS1_PIN; // CS=1; disable
+
+ /*
+ in Python, native types:
+ CS1_PIN(const) = 0
+ n = int(0)
+ delay_ms(0)
+ PORT[word:BSRRH] = 1 << CS1_PIN
+ for n in range(0, 8):
+ delay_ms(0)
+ PORT[word:BSRRH] = 1 << SCL_PIN
+ if i & 0x80 == 0:
+ PORT[word:BSRRH] = 1 << SI_PIN
+ else:
+ PORT[word:BSRRL] = 1 << SI_PIN
+ i <<= 1
+ delay_ms(0)
+ PORT[word:BSRRL] = 1 << SCL_PIN
+ */
+}
+
+/*
+static void lcd_data_out(uint8_t i) {
+ delay_ms(0);
+ PYB_LCD_PORT->BSRRH = PYB_LCD_CS1_PIN; // CS=0; enable
+ PYB_LCD_PORT->BSRRL = PYB_LCD_A0_PIN; // A0=1; select data reg
+ // send byte bigendian, latches on rising clock
+ for (uint32_t n = 0; n < 8; n++) {
+ delay_ms(0);
+ PYB_LCD_PORT->BSRRH = PYB_LCD_SCL_PIN; // SCL=0
+ if ((i & 0x80) == 0) {
+ PYB_LCD_PORT->BSRRH = PYB_LCD_SI_PIN; // SI=0
+ } else {
+ PYB_LCD_PORT->BSRRL = PYB_LCD_SI_PIN; // SI=1
+ }
+ i <<= 1;
+ delay_ms(0);
+ PYB_LCD_PORT->BSRRL = PYB_LCD_SCL_PIN; // SCL=1
+ }
+ PYB_LCD_PORT->BSRRL = PYB_LCD_CS1_PIN; // CS=1; disable
+}
+*/
+
+// writes 8 vertical pixels
+// pos 0 is upper left, pos 1 is 8 pixels to right of that, pos 128 is 8 pixels below that
+mp_obj_t lcd_draw_pixel_8(mp_obj_t mp_pos, mp_obj_t mp_val) {
+ int pos = mp_obj_get_int(mp_pos);
+ int val = mp_obj_get_int(mp_val);
+ int page = pos / 128;
+ int offset = pos - (page * 128);
+ lcd_out(LCD_INSTR, 0xb0 | page); // page address set
+ lcd_out(LCD_INSTR, 0x10 | ((offset >> 4) & 0x0f)); // column address set upper
+ lcd_out(LCD_INSTR, 0x00 | (offset & 0x0f)); // column address set lower
+ lcd_out(LCD_DATA, val); // write data
+ return mp_const_none;
+}
+
+#define LCD_BUF_W (16)
+#define LCD_BUF_H (4)
+
+char lcd_char_buffer[LCD_BUF_W * LCD_BUF_H];
+int lcd_line;
+int lcd_column;
+int lcd_next_line;
+
+#define LCD_PIX_BUF_SIZE (128 * 32 / 8)
+byte lcd_pix_buf[LCD_PIX_BUF_SIZE];
+byte lcd_pix_buf2[LCD_PIX_BUF_SIZE];
+
+mp_obj_t lcd_pix_clear(void) {
+ memset(lcd_pix_buf, 0, LCD_PIX_BUF_SIZE);
+ memset(lcd_pix_buf2, 0, LCD_PIX_BUF_SIZE);
+ return mp_const_none;
+}
+
+mp_obj_t lcd_pix_get(mp_obj_t mp_x, mp_obj_t mp_y) {
+ int x = mp_obj_get_int(mp_x);
+ int y = mp_obj_get_int(mp_y);
+ if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
+ uint byte_pos = x + 128 * ((uint)y >> 3);
+ if (lcd_pix_buf[byte_pos] & (1 << (y & 7))) {
+ return mp_obj_new_int(1);
+ }
+ }
+ return mp_obj_new_int(0);
+}
+
+mp_obj_t lcd_pix_set(mp_obj_t mp_x, mp_obj_t mp_y) {
+ int x = mp_obj_get_int(mp_x);
+ int y = mp_obj_get_int(mp_y);
+ if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
+ uint byte_pos = x + 128 * ((uint)y >> 3);
+ lcd_pix_buf2[byte_pos] |= 1 << (y & 7);
+ }
+ return mp_const_none;
+}
+
+mp_obj_t lcd_pix_reset(mp_obj_t mp_x, mp_obj_t mp_y) {
+ int x = mp_obj_get_int(mp_x);
+ int y = mp_obj_get_int(mp_y);
+ if (0 <= x && x <= 127 && 0 <= y && y <= 31) {
+ uint byte_pos = x + 128 * ((uint)y >> 3);
+ lcd_pix_buf2[byte_pos] &= ~(1 << (y & 7));
+ }
+ return mp_const_none;
+}
+
+mp_obj_t lcd_pix_show(void) {
+ memcpy(lcd_pix_buf, lcd_pix_buf2, LCD_PIX_BUF_SIZE);
+ for (uint page = 0; page < 4; page++) {
+ lcd_out(LCD_INSTR, 0xb0 | page); // page address set
+ lcd_out(LCD_INSTR, 0x10); // column address set upper; 0
+ lcd_out(LCD_INSTR, 0x00); // column address set lower; 0
+ for (uint i = 0; i < 128; i++) {
+ lcd_out(LCD_DATA, lcd_pix_buf[i + 128 * page]);
+ }
+ }
+ return mp_const_none;
+}
+
+mp_obj_t lcd_print(mp_obj_t text) {
+ uint len;
+ const char *data = mp_obj_str_get_data(text, &len);
+ lcd_print_strn(data, len);
+ return mp_const_none;
+}
+
+mp_obj_t lcd_light(mp_obj_t value) {
+#if defined(PYB_LCD_BL_PORT)
+ if (rt_is_true(value)) {
+ PYB_LCD_BL_PORT->BSRRL = PYB_LCD_BL_PIN; // set pin high to turn backlight on
+ } else {
+ PYB_LCD_BL_PORT->BSRRH = PYB_LCD_BL_PIN; // set pin low to turn backlight off
+ }
+#endif
+ return mp_const_none;
+}
+
+static mp_obj_t mp_lcd = MP_OBJ_NULL;
+
+static mp_obj_t pyb_lcd_init(void) {
+ if (mp_lcd != MP_OBJ_NULL) {
+ // already init'd
+ return mp_lcd;
+ }
+
+ // set the outputs high
+ PYB_LCD_PORT->BSRRL = PYB_LCD_CS1_PIN;
+ PYB_LCD_PORT->BSRRL = PYB_LCD_RST_PIN;
+ PYB_LCD_PORT->BSRRL = PYB_LCD_A0_PIN;
+ PYB_LCD_PORT->BSRRL = PYB_LCD_SCL_PIN;
+ PYB_LCD_PORT->BSRRL = PYB_LCD_SI_PIN;
+
+ // make them push/pull outputs
+ GPIO_InitTypeDef GPIO_InitStructure;
+ GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
+ GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
+ GPIO_InitStructure.Pull = GPIO_NOPULL;
+ GPIO_InitStructure.Pin = PYB_LCD_CS1_PIN | PYB_LCD_RST_PIN | PYB_LCD_A0_PIN | PYB_LCD_SCL_PIN | PYB_LCD_SI_PIN;
+ HAL_GPIO_Init(PYB_LCD_PORT, &GPIO_InitStructure);
+
+#if defined(PYB_LCD_BL_PORT)
+ // backlight drive pin, starts low (off)
+ PYB_LCD_BL_PORT->BSRRH = PYB_LCD_BL_PIN;
+ GPIO_InitStructure.Pin = PYB_LCD_BL_PIN;
+ GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
+ GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
+ GPIO_InitStructure.Pull = GPIO_NOPULL;
+ HAL_GPIO_Init(PYB_LCD_BL_PORT, &GPIO_InitStructure);
+#endif
+
+ // init the LCD
+ HAL_Delay(1); // wait a bit
+ PYB_LCD_PORT->BSRRH = PYB_LCD_RST_PIN; // RST=0; reset
+ HAL_Delay(1); // wait for reset; 2us min
+ PYB_LCD_PORT->BSRRL = PYB_LCD_RST_PIN; // RST=1; enable
+ HAL_Delay(1); // wait for reset; 2us min
+ lcd_out(LCD_INSTR, 0xa0); // ADC select, normal
+ lcd_out(LCD_INSTR, 0xc8); // common output mode select, reverse
+ lcd_out(LCD_INSTR, 0xa2); // LCD bias set, 1/9 bias
+ lcd_out(LCD_INSTR, 0x2f); // power control set, 0b111=(booster on, vreg on, vfollow on)
+ lcd_out(LCD_INSTR, 0x21); // v0 voltage regulator internal resistor ratio set, 0b001=small
+ lcd_out(LCD_INSTR, 0x81); // electronic volume mode set
+ lcd_out(LCD_INSTR, 0x34); // electronic volume register set, 0b110100
+ lcd_out(LCD_INSTR, 0x40); // display start line set, 0
+ lcd_out(LCD_INSTR, 0xaf); // LCD display, on
+
+ // clear display
+ for (int page = 0; page < 4; page++) {
+ lcd_out(LCD_INSTR, 0xb0 | page); // page address set
+ lcd_out(LCD_INSTR, 0x10); // column address set upper
+ lcd_out(LCD_INSTR, 0x00); // column address set lower
+ for (int i = 0; i < 128; i++) {
+ lcd_out(LCD_DATA, 0x00);
+ }
+ }
+
+ for (int i = 0; i < LCD_BUF_H * LCD_BUF_W; i++) {
+ lcd_char_buffer[i] = ' ';
+ }
+ lcd_line = 0;
+ lcd_column = 0;
+ lcd_next_line = 0;
+
+ // Micro Python interface
+ mp_obj_t o = mp_obj_new_type(MP_QSTR_LCD, mp_const_empty_tuple, mp_obj_new_dict(0));
+ rt_store_attr(o, qstr_from_str("lcd8"), rt_make_function_n(2, lcd_draw_pixel_8));
+ rt_store_attr(o, qstr_from_str("clear"), rt_make_function_n(0, lcd_pix_clear));
+ rt_store_attr(o, qstr_from_str("get"), rt_make_function_n(2, lcd_pix_get));
+ rt_store_attr(o, qstr_from_str("set"), rt_make_function_n(2, lcd_pix_set));
+ rt_store_attr(o, qstr_from_str("reset"), rt_make_function_n(2, lcd_pix_reset));
+ rt_store_attr(o, qstr_from_str("show"), rt_make_function_n(0, lcd_pix_show));
+ rt_store_attr(o, qstr_from_str("text"), rt_make_function_n(1, lcd_print));
+ rt_store_attr(o, qstr_from_str("light"), rt_make_function_n(1, lcd_light));
+ mp_lcd = o;
+ return o;
+}
+
+static MP_DEFINE_CONST_FUN_OBJ_0(pyb_lcd_init_obj, pyb_lcd_init);
+
+void lcd_init(void) {
+ mp_lcd = MP_OBJ_NULL;
+ rt_store_name(qstr_from_str("LCD"), (mp_obj_t)&pyb_lcd_init_obj);
+}
+
+void lcd_print_str(const char *str) {
+ lcd_print_strn(str, strlen(str));
+}
+
+void lcd_print_strn(const char *str, unsigned int len) {
+ int redraw_min = lcd_line * LCD_BUF_W + lcd_column;
+ int redraw_max = redraw_min;
+ int did_new_line = 0;
+ for (; len > 0; len--, str++) {
+ // move to next line if needed
+ if (lcd_next_line) {
+ if (lcd_line + 1 < LCD_BUF_H) {
+ lcd_line += 1;
+ } else {
+ lcd_line = LCD_BUF_H - 1;
+ for (int i = 0; i < LCD_BUF_W * (LCD_BUF_H - 1); i++) {
+ lcd_char_buffer[i] = lcd_char_buffer[i + LCD_BUF_W];
+ }
+ for (int i = 0; i < LCD_BUF_W; i++) {
+ lcd_char_buffer[LCD_BUF_W * (LCD_BUF_H - 1) + i] = ' ';
+ }
+ redraw_min = 0;
+ redraw_max = LCD_BUF_W * LCD_BUF_H;
+ }
+ lcd_next_line = 0;
+ lcd_column = 0;
+ did_new_line = 1;
+ }
+ if (*str == '\n') {
+ lcd_next_line = 1;
+ } else if (*str == '\r') {
+ lcd_column = 0;
+ } else if (*str == '\b') {
+ if (lcd_column > 0) {
+ lcd_column--;
+ }
+ } else if (lcd_column >= LCD_BUF_W) {
+ lcd_next_line = 1;
+ str -= 1;
+ len += 1;
+ } else {
+ lcd_char_buffer[lcd_line * LCD_BUF_W + lcd_column] = *str;
+ lcd_column += 1;
+ int max = lcd_line * LCD_BUF_W + lcd_column;
+ if (max > redraw_max) {
+ redraw_max = max;
+ }
+ }
+ }
+
+ int last_page = -1;
+ for (int i = redraw_min; i < redraw_max; i++) {
+ int page = i / LCD_BUF_W;
+ if (page != last_page) {
+ int offset = 8 * (i - (page * LCD_BUF_W));
+ lcd_out(LCD_INSTR, 0xb0 | page); // page address set
+ lcd_out(LCD_INSTR, 0x10 | ((offset >> 4) & 0x0f)); // column address set upper
+ lcd_out(LCD_INSTR, 0x00 | (offset & 0x0f)); // column address set lower
+ last_page = page;
+ }
+ int chr = lcd_char_buffer[i];
+ if (chr < 32 || chr > 126) {
+ chr = 127;
+ }
+ const uint8_t *chr_data = &font_petme128_8x8[(chr - 32) * 8];
+ for (int j = 0; j < 8; j++) {
+ lcd_out(LCD_DATA, chr_data[j]);
+ }
+ }
+
+ if (did_new_line) {
+ HAL_Delay(50);
+ }
+}
+
+#endif // MICROPY_HW_HAS_LCD
diff --git a/stmhal/lcd.h b/stmhal/lcd.h
new file mode 100644
index 0000000000..7b243ee7e3
--- /dev/null
+++ b/stmhal/lcd.h
@@ -0,0 +1,3 @@
+void lcd_init(void);
+void lcd_print_str(const char *str);
+void lcd_print_strn(const char *str, unsigned int len);