Skip to content

Commit 26a0008

Browse files
committed
stm: LCD support for PYBv4; fix MMA reading code.
1 parent b979122 commit 26a0008

3 files changed

Lines changed: 82 additions & 22 deletions

File tree

stm/lcd.c

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,35 @@
1313
#include "font_petme128_8x8.h"
1414
#include "lcd.h"
1515

16+
#if defined(PYBOARD)
1617
#define PYB_LCD_PORT (GPIOA)
1718
#define PYB_LCD_CS1_PIN (GPIO_Pin_0)
1819
#define PYB_LCD_RST_PIN (GPIO_Pin_1)
1920
#define PYB_LCD_A0_PIN (GPIO_Pin_2)
2021
#define PYB_LCD_SCL_PIN (GPIO_Pin_3)
2122
#define PYB_LCD_SI_PIN (GPIO_Pin_4)
23+
#elif defined(PYBOARD4)
24+
// X position
25+
#define PYB_LCD_PORT (GPIOA)
26+
#define PYB_LCD_CS1_PIN (GPIO_Pin_2) // X3
27+
#define PYB_LCD_RST_PIN (GPIO_Pin_3) // X4
28+
#define PYB_LCD_A0_PIN (GPIO_Pin_4) // X5
29+
#define PYB_LCD_SCL_PIN (GPIO_Pin_5) // X6
30+
#define PYB_LCD_SI_PIN (GPIO_Pin_7) // X8
31+
#define PYB_LCD_BL_PORT (GPIOC)
32+
#define PYB_LCD_BL_PIN (GPIO_Pin_5) // X12
33+
/*
34+
// Y position
35+
#define PYB_LCD_PORT (GPIOB)
36+
#define PYB_LCD_CS1_PIN (GPIO_Pin_8) // Y3 = PB8
37+
#define PYB_LCD_RST_PIN (GPIO_Pin_9) // Y4 = PB9
38+
#define PYB_LCD_A0_PIN (GPIO_Pin_12) // Y5 = PB12
39+
#define PYB_LCD_SCL_PIN (GPIO_Pin_13) // Y6 = PB13
40+
#define PYB_LCD_SI_PIN (GPIO_Pin_15) // Y8 = PB15
41+
#define PYB_LCD_BL_PORT (GPIOB)
42+
#define PYB_LCD_BL_PIN (GPIO_Pin_1) // Y12 = PB1
43+
*/
44+
#endif
2245

2346
#define LCD_INSTR (0)
2447
#define LCD_DATA (1)
@@ -171,7 +194,25 @@ mp_obj_t lcd_print(mp_obj_t text) {
171194
return mp_const_none;
172195
}
173196

174-
void lcd_init(void) {
197+
mp_obj_t lcd_light(mp_obj_t value) {
198+
#if defined(PYB_LCD_BL_PORT)
199+
if (rt_is_true(value)) {
200+
PYB_LCD_BL_PORT->BSRRL = PYB_LCD_BL_PIN; // set pin high to turn backlight on
201+
} else {
202+
PYB_LCD_BL_PORT->BSRRH = PYB_LCD_BL_PIN; // set pin low to turn backlight off
203+
}
204+
#endif
205+
return mp_const_none;
206+
}
207+
208+
static mp_obj_t mp_lcd = MP_OBJ_NULL;
209+
210+
static mp_obj_t pyb_lcd_init(void) {
211+
if (mp_lcd != MP_OBJ_NULL) {
212+
// already init'd
213+
return mp_lcd;
214+
}
215+
175216
// set the outputs high
176217
PYB_LCD_PORT->BSRRL = PYB_LCD_CS1_PIN;
177218
PYB_LCD_PORT->BSRRL = PYB_LCD_RST_PIN;
@@ -188,6 +229,17 @@ void lcd_init(void) {
188229
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
189230
GPIO_Init(PYB_LCD_PORT, &GPIO_InitStructure);
190231

232+
#if defined(PYB_LCD_BL_PORT)
233+
// backlight drive pin, starts low (off)
234+
PYB_LCD_BL_PORT->BSRRH = PYB_LCD_BL_PIN;
235+
GPIO_InitStructure.GPIO_Pin = PYB_LCD_BL_PIN;
236+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
237+
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
238+
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
239+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
240+
GPIO_Init(PYB_LCD_BL_PORT, &GPIO_InitStructure);
241+
#endif
242+
191243
// init the LCD
192244
sys_tick_delay_ms(1); // wait a bit
193245
PYB_LCD_PORT->BSRRH = PYB_LCD_RST_PIN; // RST=0; reset
@@ -221,16 +273,25 @@ void lcd_init(void) {
221273
lcd_column = 0;
222274
lcd_next_line = 0;
223275

224-
// Python interface
225-
mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("lcd"));
226-
rt_store_attr(m, QSTR_FROM_STR_STATIC("lcd8"), rt_make_function_n(2, lcd_draw_pixel_8));
227-
rt_store_attr(m, QSTR_FROM_STR_STATIC("clear"), rt_make_function_n(0, lcd_pix_clear));
228-
rt_store_attr(m, QSTR_FROM_STR_STATIC("get"), rt_make_function_n(2, lcd_pix_get));
229-
rt_store_attr(m, QSTR_FROM_STR_STATIC("set"), rt_make_function_n(2, lcd_pix_set));
230-
rt_store_attr(m, QSTR_FROM_STR_STATIC("reset"), rt_make_function_n(2, lcd_pix_reset));
231-
rt_store_attr(m, QSTR_FROM_STR_STATIC("show"), rt_make_function_n(0, lcd_pix_show));
232-
rt_store_attr(m, QSTR_FROM_STR_STATIC("text"), rt_make_function_n(1, lcd_print));
233-
rt_store_name(QSTR_FROM_STR_STATIC("lcd"), m);
276+
// Micro Python interface
277+
mp_obj_t o = mp_obj_new_type("LCD", mp_const_empty_tuple, mp_obj_new_dict(0));
278+
rt_store_attr(o, qstr_from_str("lcd8"), rt_make_function_n(2, lcd_draw_pixel_8));
279+
rt_store_attr(o, qstr_from_str("clear"), rt_make_function_n(0, lcd_pix_clear));
280+
rt_store_attr(o, qstr_from_str("get"), rt_make_function_n(2, lcd_pix_get));
281+
rt_store_attr(o, qstr_from_str("set"), rt_make_function_n(2, lcd_pix_set));
282+
rt_store_attr(o, qstr_from_str("reset"), rt_make_function_n(2, lcd_pix_reset));
283+
rt_store_attr(o, qstr_from_str("show"), rt_make_function_n(0, lcd_pix_show));
284+
rt_store_attr(o, qstr_from_str("text"), rt_make_function_n(1, lcd_print));
285+
rt_store_attr(o, qstr_from_str("light"), rt_make_function_n(1, lcd_light));
286+
mp_lcd = o;
287+
return o;
288+
}
289+
290+
static MP_DEFINE_CONST_FUN_OBJ_0(pyb_lcd_init_obj, pyb_lcd_init);
291+
292+
void lcd_init(void) {
293+
mp_lcd = MP_OBJ_NULL;
294+
rt_store_name(qstr_from_str("LCD"), (mp_obj_t)&pyb_lcd_init_obj);
234295
}
235296

236297
void lcd_print_str(const char *str) {

stm/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,8 @@ int main(void) {
768768
qstr_init();
769769
rt_init();
770770

771-
// LCD init
772-
//lcd_init(); disabled while servos on PA0 PA1
771+
// LCD init (create in with LCD())
772+
lcd_init();
773773

774774
// servo
775775
servo_init();

stm/mma.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ mp_obj_t pyb_mma_read(void) {
263263
int jolt_info = mma_read_nack();
264264

265265
mp_obj_t data[4];
266-
data[0] = mp_obj_new_int(jolt_info);
267-
data[1] = mp_obj_new_int(mma_buf[2] + mma_buf[5] + mma_buf[8] + mma_buf[11]);
268-
data[2] = mp_obj_new_int(mma_buf[1] + mma_buf[4] + mma_buf[7] + mma_buf[10]);
269-
data[3] = mp_obj_new_int(mma_buf[0] + mma_buf[3] + mma_buf[6] + mma_buf[9]);
266+
data[0] = mp_obj_new_int(mma_buf[0] + mma_buf[3] + mma_buf[6] + mma_buf[9]);
267+
data[1] = mp_obj_new_int(mma_buf[1] + mma_buf[4] + mma_buf[7] + mma_buf[10]);
268+
data[2] = mp_obj_new_int(mma_buf[2] + mma_buf[5] + mma_buf[8] + mma_buf[11]);
269+
data[3] = mp_obj_new_int(jolt_info);
270270

271-
return rt_build_tuple(4, data); // items in reverse order in data
271+
return rt_build_tuple(4, data);
272272
}
273273

274274
MP_DEFINE_CONST_FUN_OBJ_0(pyb_mma_read_obj, pyb_mma_read);
@@ -279,11 +279,11 @@ mp_obj_t pyb_mma_read_all(void) {
279279
mma_send_byte(0);
280280
mma_restart(MMA_ADDR, 0);
281281
for (int i = 0; i <= 9; i++) {
282-
data[10 - i] = mp_obj_new_int(mma_read_ack());
282+
data[i] = mp_obj_new_int(mma_read_ack());
283283
}
284-
data[0] = mp_obj_new_int(mma_read_nack());
284+
data[10] = mp_obj_new_int(mma_read_nack());
285285

286-
return rt_build_tuple(11, data); // items in reverse order in data
286+
return rt_build_tuple(11, data);
287287
}
288288

289289
MP_DEFINE_CONST_FUN_OBJ_0(pyb_mma_read_all_obj, pyb_mma_read_all);
@@ -298,4 +298,3 @@ mp_obj_t pyb_mma_write_mode(mp_obj_t o_int, mp_obj_t o_mode) {
298298
}
299299

300300
MP_DEFINE_CONST_FUN_OBJ_2(pyb_mma_write_mode_obj, pyb_mma_write_mode);
301-

0 commit comments

Comments
 (0)