Skip to content

Commit 2870862

Browse files
committed
Add module object, to be used eventually for import.
1 parent 0ff8839 commit 2870862

File tree

9 files changed

+243
-84
lines changed

9 files changed

+243
-84
lines changed

py/obj.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ struct _mp_obj_type_t {
8787
dynamic_type instance
8888
8989
compare_op
90-
load_attr instance class list
90+
load_attr module instance class list
9191
load_method instance str gen list user
92-
store_attr instance class
92+
store_attr module instance class
9393
store_subscr list dict
9494
9595
len str tuple list map
@@ -147,6 +147,7 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items);
147147
mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth);
148148
mp_obj_t mp_obj_new_class(struct _mp_map_t *class_locals);
149149
mp_obj_t mp_obj_new_instance(mp_obj_t clas);
150+
mp_obj_t mp_obj_new_module(qstr module_name);
150151

151152
const char *mp_obj_get_type_str(mp_obj_t o_in);
152153

@@ -238,5 +239,7 @@ mp_obj_t mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr);
238239
void mp_obj_instance_load_method(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
239240
void mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value);
240241

241-
// temporary way of making C modules
242-
mp_obj_t mp_module_new(void);
242+
// module
243+
extern const mp_obj_type_t module_type;
244+
mp_obj_t mp_obj_new_module(qstr module_name);
245+
struct _mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in);

py/objmodule.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <assert.h>
5+
6+
#include "nlr.h"
7+
#include "misc.h"
8+
#include "mpconfig.h"
9+
#include "obj.h"
10+
#include "runtime.h"
11+
#include "map.h"
12+
13+
typedef struct _mp_obj_module_t {
14+
mp_obj_base_t base;
15+
qstr name;
16+
mp_map_t *globals;
17+
} mp_obj_module_t;
18+
19+
void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
20+
mp_obj_module_t *self = self_in;
21+
print(env, "<module '%s' from '-unknown-file-'>", qstr_str(self->name));
22+
}
23+
24+
const mp_obj_type_t module_type = {
25+
{ &mp_const_type },
26+
"module",
27+
module_print, // print
28+
NULL, // call_n
29+
NULL, // unary_op
30+
NULL, // binary_op
31+
NULL, // getiter
32+
NULL, // iternext
33+
{{NULL, NULL},}, // method list
34+
};
35+
36+
mp_obj_t mp_obj_new_module(qstr module_name) {
37+
mp_obj_module_t *o = m_new_obj(mp_obj_module_t);
38+
o->base.type = &module_type;
39+
o->name = module_name;
40+
o->globals = mp_map_new(MP_MAP_QSTR, 0);
41+
return o;
42+
}
43+
44+
mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in) {
45+
assert(MP_OBJ_IS_TYPE(self_in, &module_type));
46+
mp_obj_module_t *self = self_in;
47+
return self->globals;
48+
}

py/runtime.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,19 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
779779
if (MP_OBJ_IS_TYPE(base, &class_type)) {
780780
mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(base), attr, false);
781781
if (elem == NULL) {
782-
nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
782+
// TODO what about generic method lookup?
783+
goto no_attr;
783784
}
784785
return elem->value;
785786
} else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
786787
return mp_obj_instance_load_attr(base, attr);
788+
} else if (MP_OBJ_IS_TYPE(base, &module_type)) {
789+
mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_module_get_globals(base), attr, false);
790+
if (elem == NULL) {
791+
// TODO what about generic method lookup?
792+
goto no_attr;
793+
}
794+
return elem->value;
787795
} else if (MP_OBJ_IS_OBJ(base)) {
788796
// generic method lookup
789797
mp_obj_base_t *o = base;
@@ -794,6 +802,8 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
794802
}
795803
}
796804
}
805+
806+
no_attr:
797807
nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
798808
}
799809

@@ -832,6 +842,10 @@ void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
832842
mp_qstr_map_lookup(locals, attr, true)->value = value;
833843
} else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
834844
mp_obj_instance_store_attr(base, attr, value);
845+
} else if (MP_OBJ_IS_TYPE(base, &module_type)) {
846+
// TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
847+
mp_map_t *globals = mp_obj_module_get_globals(base);
848+
mp_qstr_map_lookup(globals, attr, true)->value = value;
835849
} else {
836850
nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
837851
}

stm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ PY_O = \
7272
objgenerator.o \
7373
objinstance.o \
7474
objlist.o \
75+
objmodule.o \
7576
objnone.o \
7677
objrange.o \
7778
objset.o \

stm/main.c

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ int readline(vstr_t *line, const char *prompt) {
453453
}
454454

455455
void do_repl(void) {
456-
stdout_tx_str("Micro Python 0.1; STM32F405RG; PYBv3\r\n");
456+
stdout_tx_str("Micro Python build <git hash> on 2/1/2014; PYBv3 with STM32F405RG\r\n");
457457
stdout_tx_str("Type \"help()\" for more information.\r\n");
458458

459459
vstr_t line;
@@ -587,38 +587,53 @@ mp_obj_t pyb_gc(void) {
587587
return mp_const_none;
588588
}
589589

590-
#define MMA_ADDR (0x4c)
590+
mp_obj_t pyb_gpio(int n_args, mp_obj_t *args) {
591+
//assert(1 <= n_args && n_args <= 2);
591592

592-
int mma_buf[12];
593-
594-
mp_obj_t pyb_mma_read(void) {
595-
for (int i = 0; i <= 6; i += 3) {
596-
mma_buf[0 + i] = mma_buf[0 + i + 3];
597-
mma_buf[1 + i] = mma_buf[1 + i + 3];
598-
mma_buf[2 + i] = mma_buf[2 + i + 3];
593+
const char *pin_name = qstr_str(mp_obj_get_qstr(args[0]));
594+
GPIO_TypeDef *port;
595+
switch (pin_name[0]) {
596+
case 'A': case 'a': port = GPIOA; break;
597+
case 'B': case 'b': port = GPIOB; break;
598+
case 'C': case 'c': port = GPIOC; break;
599+
default: goto pin_error;
599600
}
600-
601-
mma_start(MMA_ADDR, 1);
602-
mma_send_byte(0);
603-
mma_restart(MMA_ADDR, 0);
604-
for (int i = 0; i <= 2; i++) {
605-
int v = mma_read_ack() & 0x3f;
606-
if (v & 0x20) {
607-
v |= ~0x1f;
601+
uint pin_num = 0;
602+
for (const char *s = pin_name + 1; *s; s++) {
603+
if (!('0' <= *s && *s <= '9')) {
604+
goto pin_error;
608605
}
609-
mma_buf[9 + i] = v;
606+
pin_num = 10 * pin_num + *s - '0';
607+
}
608+
if (!(0 <= pin_num && pin_num <= 15)) {
609+
goto pin_error;
610610
}
611-
int jolt_info = mma_read_nack();
612611

613-
mp_obj_t data[4];
614-
data[0] = mp_obj_new_int(jolt_info);
615-
data[1] = mp_obj_new_int(mma_buf[2] + mma_buf[5] + mma_buf[8] + mma_buf[11]);
616-
data[2] = mp_obj_new_int(mma_buf[1] + mma_buf[4] + mma_buf[7] + mma_buf[10]);
617-
data[3] = mp_obj_new_int(mma_buf[0] + mma_buf[3] + mma_buf[6] + mma_buf[9]);
612+
if (n_args == 1) {
613+
// get pin
614+
if ((port->IDR & (1 << pin_num)) != (uint32_t)Bit_RESET) {
615+
return MP_OBJ_NEW_SMALL_INT(1);
616+
} else {
617+
return MP_OBJ_NEW_SMALL_INT(0);
618+
}
619+
} else {
620+
// set pin
621+
if (rt_is_true(args[1])) {
622+
// set pin high
623+
port->BSRRL = 1 << pin_num;
624+
} else {
625+
// set pin low
626+
port->BSRRH = 1 << pin_num;
627+
}
628+
return mp_const_none;
629+
}
618630

619-
return rt_build_tuple(4, data); // items in reverse order in data
631+
pin_error:
632+
nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_ValueError, "pin %s does not exist", pin_name));
620633
}
621634

635+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_obj, 1, 2, pyb_gpio);
636+
622637
mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
623638
mp_obj_t *items = mp_obj_get_array_fixed_n(arg, 4);
624639
uint8_t data[4];
@@ -855,7 +870,7 @@ int main(void) {
855870
{
856871
rt_store_name(qstr_from_str_static("help"), rt_make_function_0(pyb_help));
857872

858-
mp_obj_t m = mp_module_new();
873+
mp_obj_t m = mp_obj_new_module(qstr_from_str_static("pyb"));
859874
rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info));
860875
rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_0(pyb_sd_test));
861876
rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_0(pyb_stop));
@@ -869,7 +884,9 @@ int main(void) {
869884
rt_store_attr(m, qstr_from_str_static("switch"), rt_make_function_0(pyb_sw));
870885
rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_2(pyb_servo_set));
871886
rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_2(pyb_pwm_set));
872-
rt_store_attr(m, qstr_from_str_static("accel"), rt_make_function_0(pyb_mma_read));
887+
rt_store_attr(m, qstr_from_str_static("accel"), (mp_obj_t)&pyb_mma_read_obj);
888+
rt_store_attr(m, qstr_from_str_static("mma_read"), (mp_obj_t)&pyb_mma_read_all_obj);
889+
rt_store_attr(m, qstr_from_str_static("mma_mode"), (mp_obj_t)&pyb_mma_write_mode_obj);
873890
rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report));
874891
rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read));
875892
rt_store_attr(m, qstr_from_str_static("uout"), rt_make_function_1(pyb_usart_send));
@@ -879,6 +896,7 @@ int main(void) {
879896
rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led));
880897
rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo));
881898
rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_2(pyb_I2C));
899+
rt_store_attr(m, qstr_from_str_static("gpio"), (mp_obj_t)&pyb_gpio_obj);
882900
rt_store_name(qstr_from_str_static("pyb"), m);
883901

884902
rt_store_name(qstr_from_str_static("open"), rt_make_function_2(pyb_io_open));
@@ -985,56 +1003,6 @@ int main(void) {
9851003
if (first_soft_reset) {
9861004
// init and reset address to zero
9871005
mma_init();
988-
mma_start(MMA_ADDR, 1);
989-
mma_send_byte(0);
990-
mma_stop();
991-
992-
/*
993-
// read and print all 11 registers
994-
mma_start(MMA_ADDR, 1);
995-
mma_send_byte(0);
996-
mma_restart(MMA_ADDR, 0);
997-
for (int i = 0; i <= 0xa; i++) {
998-
int data;
999-
if (i == 0xa) {
1000-
data = mma_read_nack();
1001-
} else {
1002-
data = mma_read_ack();
1003-
}
1004-
printf(" %02x", data);
1005-
}
1006-
printf("\n");
1007-
*/
1008-
1009-
// put into active mode
1010-
mma_start(MMA_ADDR, 1);
1011-
mma_send_byte(7); // mode
1012-
mma_send_byte(1); // active mode
1013-
mma_stop();
1014-
1015-
/*
1016-
// infinite loop to read values
1017-
for (;;) {
1018-
sys_tick_delay_ms(500);
1019-
1020-
mma_start(MMA_ADDR, 1);
1021-
mma_send_byte(0);
1022-
mma_restart(MMA_ADDR, 0);
1023-
for (int i = 0; i <= 3; i++) {
1024-
int data;
1025-
if (i == 3) {
1026-
data = mma_read_nack();
1027-
printf(" %02x\n", data);
1028-
} else {
1029-
data = mma_read_ack() & 0x3f;
1030-
if (data & 0x20) {
1031-
data |= ~0x1f;
1032-
}
1033-
printf(" % 2d", data);
1034-
}
1035-
}
1036-
}
1037-
*/
10381006
}
10391007

10401008
// turn boot-up LED off
@@ -1220,9 +1188,9 @@ int main(void) {
12201188
} else {
12211189
data[0] = 0x00;
12221190
}
1223-
mma_start(MMA_ADDR, 1);
1191+
mma_start(0x4c /* MMA_ADDR */, 1);
12241192
mma_send_byte(0);
1225-
mma_restart(MMA_ADDR, 0);
1193+
mma_restart(0x4c /* MMA_ADDR */, 0);
12261194
for (int i = 0; i <= 1; i++) {
12271195
int v = mma_read_ack() & 0x3f;
12281196
if (v & 0x20) {

0 commit comments

Comments
 (0)