mirror of
https://github.com/micropython/micropython.git
synced 2026-01-08 13:10:21 +01:00
Change object representation from 1 big union to individual structs.
A big change. Micro Python objects are allocated as individual structs with the first element being a pointer to the type information (which is itself an object). This scheme follows CPython. Much more flexible, not necessarily slower, uses same heap memory, and can allocate objects statically. Also change name prefix, from py_ to mp_ (mp for Micro Python).
This commit is contained in:
62
stm/led.c
62
stm/led.c
@@ -1,5 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stm32f4xx.h>
|
||||
#include <stm32f4xx_gpio.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "obj.h"
|
||||
#include "led.h"
|
||||
|
||||
#define PYB_LED_R_PORT (GPIOA)
|
||||
@@ -64,3 +69,60 @@ void led_toggle(pyb_led_t led) {
|
||||
port->BSRRH = pin;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* Micro Python bindings */
|
||||
|
||||
typedef struct _pyb_led_obj_t {
|
||||
mp_obj_base_t base;
|
||||
uint led_id;
|
||||
} pyb_led_obj_t;
|
||||
|
||||
void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
||||
pyb_led_obj_t *self = self_in;
|
||||
print(env, "<LED %lu>", self->led_id);
|
||||
}
|
||||
|
||||
mp_obj_t led_obj_on(mp_obj_t self_in) {
|
||||
pyb_led_obj_t *self = self_in;
|
||||
switch (self->led_id) {
|
||||
case 1: led_state(PYB_LED_G1, 1); break;
|
||||
case 2: led_state(PYB_LED_G2, 1); break;
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t led_obj_off(mp_obj_t self_in) {
|
||||
pyb_led_obj_t *self = self_in;
|
||||
switch (self->led_id) {
|
||||
case 1: led_state(PYB_LED_G1, 0); break;
|
||||
case 2: led_state(PYB_LED_G2, 0); break;
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on);
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);
|
||||
|
||||
static const mp_obj_type_t led_obj_type = {
|
||||
{ &mp_const_type },
|
||||
"Led",
|
||||
led_obj_print, // print
|
||||
NULL, // call_n
|
||||
NULL, // unary_op
|
||||
NULL, // binary_op
|
||||
NULL, // getiter
|
||||
NULL, // iternext
|
||||
{ // method list
|
||||
{ "on", &led_obj_on_obj },
|
||||
{ "off", &led_obj_off_obj },
|
||||
{ NULL, NULL },
|
||||
}
|
||||
};
|
||||
|
||||
mp_obj_t pyb_Led(mp_obj_t led_id) {
|
||||
pyb_led_obj_t *o = m_new_obj(pyb_led_obj_t);
|
||||
o->base.type = &led_obj_type;
|
||||
o->led_id = mp_obj_get_int(led_id);
|
||||
return o;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user