mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Export LED as a Python module
This commit is contained in:
parent
d175c69d19
commit
160b727809
@ -1,60 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "qstr.h"
|
||||
#include "obj.h"
|
||||
#include "led.h"
|
||||
#include "led_py.h"
|
||||
|
||||
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, mp_print_kind_t kind) {
|
||||
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;
|
||||
led_state(self->led_id, 1);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t led_obj_off(mp_obj_t self_in) {
|
||||
pyb_led_obj_t *self = self_in;
|
||||
led_state(self->led_id, 0);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t led_obj_toggle(mp_obj_t self_in) {
|
||||
pyb_led_obj_t *self = self_in;
|
||||
// led_toggle(self->led_id);
|
||||
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_method_t led_methods[] = {
|
||||
{ "on", &led_obj_on_obj },
|
||||
{ "off", &led_obj_off_obj },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
|
||||
static const mp_obj_type_t led_obj_type = {
|
||||
{ &mp_const_type },
|
||||
"Led",
|
||||
.print = led_obj_print,
|
||||
.methods = led_methods,
|
||||
};
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(pyb_Led_obj, pyb_Led);
|
||||
@ -1,3 +0,0 @@
|
||||
#include "led.h"
|
||||
MP_DECLARE_CONST_FUN_OBJ(pyb_Led_obj);
|
||||
|
||||
47
src/py/py_led.c
Normal file
47
src/py/py_led.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include <libmp.h>
|
||||
#include "led.h"
|
||||
#include "py_led.h"
|
||||
|
||||
struct sym_entry {
|
||||
const char *sym;
|
||||
int val;
|
||||
} static constants[] = {
|
||||
{"RED", LED_RED},
|
||||
{"GREEN", LED_GREEN},
|
||||
{"BLUE", LED_BLUE},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
static mp_obj_t py_led_on(mp_obj_t led_id) {
|
||||
led_state(mp_obj_get_int(led_id), 1);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t py_led_off(mp_obj_t led_id) {
|
||||
led_state(mp_obj_get_int(led_id), 0);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t py_led_toggle(mp_obj_t led_id) {
|
||||
led_toggle(mp_obj_get_int(led_id));
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t py_led_init()
|
||||
{
|
||||
/* Init LED */
|
||||
led_init(LED_BLUE);
|
||||
|
||||
/* Create module */
|
||||
mp_obj_t m = mp_obj_new_module(qstr_from_str("led"));
|
||||
rt_store_attr(m, qstr_from_str("on"), rt_make_function_n(1, py_led_on));
|
||||
rt_store_attr(m, qstr_from_str("off"), rt_make_function_n(1, py_led_off));
|
||||
rt_store_attr(m, qstr_from_str("toggle"), rt_make_function_n(1, py_led_toggle));
|
||||
|
||||
/* Store module attributes */
|
||||
for (struct sym_entry *p = constants; p->sym != NULL; p++) {
|
||||
rt_store_attr(m, QSTR_FROM_STR_STATIC(p->sym), MP_OBJ_NEW_SMALL_INT((machine_int_t)p->val));
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
4
src/py/py_led.h
Normal file
4
src/py/py_led.h
Normal file
@ -0,0 +1,4 @@
|
||||
#ifndef __PY_LED_H__
|
||||
#define __PY_LED_H__
|
||||
mp_obj_t py_led_init();
|
||||
#endif /* __PY_LED_H__ */
|
||||
Loading…
Reference in New Issue
Block a user