summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/obj.c7
-rw-r--r--py/objbool.c14
-rw-r--r--py/objstr.c2
-rw-r--r--py/qstr.c2
-rw-r--r--py/runtime.c10
-rw-r--r--stm/lcd.c83
-rw-r--r--stm/main.c31
-rw-r--r--stm/mma.c17
8 files changed, 119 insertions, 47 deletions
diff --git a/py/obj.c b/py/obj.c
index 660df4dfa1..76a4d8f9b2 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -66,12 +66,7 @@ void mp_obj_print_exception(mp_obj_t exc) {
}
bool mp_obj_is_callable(mp_obj_t o_in) {
- if (!MP_OBJ_IS_OBJ(o_in)) {
- return false;
- } else {
- mp_obj_base_t *o = o_in;
- return o->type->call != NULL;
- }
+ return mp_obj_get_type(o_in)->call != NULL;
}
machine_int_t mp_obj_hash(mp_obj_t o_in) {
diff --git a/py/objbool.c b/py/objbool.c
index 729ffb4e6d..53b2bf8ed7 100644
--- a/py/objbool.c
+++ b/py/objbool.c
@@ -6,6 +6,7 @@
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
+#include "runtime0.h"
#include "runtime.h"
typedef struct _mp_obj_bool_t {
@@ -32,11 +33,24 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
}
}
+static mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
+ machine_int_t value = ((mp_obj_bool_t*)o_in)->value;
+ switch (op) {
+ case RT_UNARY_OP_NOT: if (value) { return mp_const_false; } else { return mp_const_true; }
+ case RT_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
+ case RT_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value);
+ case RT_UNARY_OP_INVERT:
+ default: // no other cases
+ return MP_OBJ_NEW_SMALL_INT(~value);
+ }
+}
+
const mp_obj_type_t bool_type = {
{ &mp_const_type },
"bool",
.print = bool_print,
.make_new = bool_make_new,
+ .unary_op = bool_unary_op,
};
static const mp_obj_bool_t false_obj = {{&bool_type}, false};
diff --git a/py/objstr.c b/py/objstr.c
index 337b42e70e..82e97c0aff 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -447,7 +447,7 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
if (l1 != l2) {
return false;
}
- return strncmp((const char*)d1, (const char*)d2, l1) == 0;
+ return memcmp(d1, d2, l1) == 0;
}
}
diff --git a/py/qstr.c b/py/qstr.c
index 6ce9e8be5a..268b3bafc9 100644
--- a/py/qstr.c
+++ b/py/qstr.c
@@ -106,7 +106,7 @@ qstr qstr_find_strn(const byte *str, uint str_len) {
// search pools for the data
for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
- if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && strncmp((const char*)Q_GET_DATA(*q), (const char*)str, str_len) == 0) {
+ if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) {
return pool->total_prev_len + (q - pool->qstrs);
}
}
diff --git a/py/runtime.c b/py/runtime.c
index d6b2bf7826..0d9906ea60 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -479,16 +479,16 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
return MP_OBJ_NEW_SMALL_INT(val);
}
return mp_obj_new_int(val);
- } else { // will be an object (small ints are caught in previous if)
- mp_obj_base_t *o = arg;
- if (o->type->unary_op != NULL) {
- mp_obj_t result = o->type->unary_op(op, arg);
+ } else {
+ mp_obj_type_t *type = mp_obj_get_type(arg);
+ if (type->unary_op != NULL) {
+ mp_obj_t result = type->unary_op(op, arg);
if (result != NULL) {
return result;
}
}
// TODO specify in error message what the operator is
- nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", o->type->name));
+ nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", type->name));
}
}
diff --git a/stm/lcd.c b/stm/lcd.c
index 0567b43333..a7d473bf6a 100644
--- a/stm/lcd.c
+++ b/stm/lcd.c
@@ -13,12 +13,35 @@
#include "font_petme128_8x8.h"
#include "lcd.h"
+#if defined(PYBOARD)
#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
+*/
+#endif
#define LCD_INSTR (0)
#define LCD_DATA (1)
@@ -171,7 +194,25 @@ mp_obj_t lcd_print(mp_obj_t text) {
return mp_const_none;
}
-void lcd_init(void) {
+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;
@@ -188,6 +229,17 @@ void lcd_init(void) {
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
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.GPIO_Pin = PYB_LCD_BL_PIN;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
+ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
+ GPIO_Init(PYB_LCD_BL_PORT, &GPIO_InitStructure);
+#endif
+
// init the LCD
sys_tick_delay_ms(1); // wait a bit
PYB_LCD_PORT->BSRRH = PYB_LCD_RST_PIN; // RST=0; reset
@@ -221,16 +273,25 @@ void lcd_init(void) {
lcd_column = 0;
lcd_next_line = 0;
- // Python interface
- mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("lcd"));
- rt_store_attr(m, QSTR_FROM_STR_STATIC("lcd8"), rt_make_function_n(2, lcd_draw_pixel_8));
- rt_store_attr(m, QSTR_FROM_STR_STATIC("clear"), rt_make_function_n(0, lcd_pix_clear));
- rt_store_attr(m, QSTR_FROM_STR_STATIC("get"), rt_make_function_n(2, lcd_pix_get));
- rt_store_attr(m, QSTR_FROM_STR_STATIC("set"), rt_make_function_n(2, lcd_pix_set));
- rt_store_attr(m, QSTR_FROM_STR_STATIC("reset"), rt_make_function_n(2, lcd_pix_reset));
- rt_store_attr(m, QSTR_FROM_STR_STATIC("show"), rt_make_function_n(0, lcd_pix_show));
- rt_store_attr(m, QSTR_FROM_STR_STATIC("text"), rt_make_function_n(1, lcd_print));
- rt_store_name(QSTR_FROM_STR_STATIC("lcd"), m);
+ // Micro Python interface
+ mp_obj_t o = mp_obj_new_type("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) {
diff --git a/stm/main.c b/stm/main.c
index a42367984e..20d4278264 100644
--- a/stm/main.c
+++ b/stm/main.c
@@ -648,19 +648,22 @@ int main(void) {
#endif
;
- // configure SDIO pins to be high to start with (apparently makes it more robust)
#if MICROPY_HW_HAS_SDCARD
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- // Configure PD.02 CMD line
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
+ {
+ // configure SDIO pins to be high to start with (apparently makes it more robust)
+ // FIXME this is not making them high, it just makes them outputs...
+ GPIO_InitTypeDef GPIO_InitStructure;
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
+ GPIO_Init(GPIOC, &GPIO_InitStructure);
+
+ // Configure PD.02 CMD line
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
+ GPIO_Init(GPIOD, &GPIO_InitStructure);
+ }
#endif
// basic sub-system init
@@ -696,8 +699,8 @@ soft_reset:
rt_init();
#if MICROPY_HW_HAS_LCD
- // LCD init
- lcd_init(); /* disabled while servos on PA0 PA1 */
+ // LCD init (create in with LCD())
+ lcd_init();
#endif
#if MICROPY_HW_ENABLE_SERVO
diff --git a/stm/mma.c b/stm/mma.c
index a32b0ddebe..8418c5b419 100644
--- a/stm/mma.c
+++ b/stm/mma.c
@@ -263,12 +263,12 @@ mp_obj_t pyb_mma_read(void) {
int jolt_info = mma_read_nack();
mp_obj_t data[4];
- data[0] = mp_obj_new_int(jolt_info);
- data[1] = mp_obj_new_int(mma_buf[2] + mma_buf[5] + mma_buf[8] + mma_buf[11]);
- data[2] = mp_obj_new_int(mma_buf[1] + mma_buf[4] + mma_buf[7] + mma_buf[10]);
- data[3] = mp_obj_new_int(mma_buf[0] + mma_buf[3] + mma_buf[6] + mma_buf[9]);
+ data[0] = mp_obj_new_int(mma_buf[0] + mma_buf[3] + mma_buf[6] + mma_buf[9]);
+ data[1] = mp_obj_new_int(mma_buf[1] + mma_buf[4] + mma_buf[7] + mma_buf[10]);
+ data[2] = mp_obj_new_int(mma_buf[2] + mma_buf[5] + mma_buf[8] + mma_buf[11]);
+ data[3] = mp_obj_new_int(jolt_info);
- return rt_build_tuple(4, data); // items in reverse order in data
+ return rt_build_tuple(4, data);
}
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) {
mma_send_byte(0);
mma_restart(MMA_ADDR, 0);
for (int i = 0; i <= 9; i++) {
- data[10 - i] = mp_obj_new_int(mma_read_ack());
+ data[i] = mp_obj_new_int(mma_read_ack());
}
- data[0] = mp_obj_new_int(mma_read_nack());
+ data[10] = mp_obj_new_int(mma_read_nack());
- return rt_build_tuple(11, data); // items in reverse order in data
+ return rt_build_tuple(11, data);
}
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) {
}
MP_DEFINE_CONST_FUN_OBJ_2(pyb_mma_write_mode_obj, pyb_mma_write_mode);
-