|
1 | | - |
2 | 1 | /* |
3 | | - * This file is part of the Micro Python project, http://micropython.org/ |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
4 | 3 | * |
5 | 4 | * The MIT License (MIT) |
6 | 5 | * |
7 | | - * Copyright (c) 2015 Damien P. George |
| 6 | + * Copyright (c) 2015-2016 Damien P. George |
8 | 7 | * |
9 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | 9 | * of this software and associated documentation files (the "Software"), to deal |
|
30 | 29 |
|
31 | 30 | extern "C" { |
32 | 31 |
|
33 | | -#include "py/mpstate.h" |
| 32 | +#include "py/runtime.h" |
34 | 33 | #include "lib/neopixel.h" |
35 | 34 | #include "microbitobj.h" |
36 | 35 |
|
37 | | -STATIC mp_obj_t mod_neopixel_init(mp_obj_t pin_in, mp_obj_t num_leds_in) { |
38 | | - PinName pin = microbit_obj_get_pin(pin_in)->name; |
39 | | - MP_STATE_PORT(neopixel_strip) = m_new_obj(neopixel_strip_t); |
40 | | - neopixel_init(MP_STATE_PORT(neopixel_strip), pin, mp_obj_get_int(num_leds_in)); |
41 | | - return mp_const_none; |
| 36 | +extern const mp_obj_type_t neopixel_type; |
| 37 | + |
| 38 | +typedef struct _neopixel_obj_t { |
| 39 | + mp_obj_base_t base; |
| 40 | + neopixel_strip_t strip; |
| 41 | +} neopixel_obj_t; |
| 42 | + |
| 43 | +STATIC mp_obj_t neopixel_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
| 44 | + (void)type_in; |
| 45 | + mp_arg_check_num(n_args, n_kw, 2, 2, false); |
| 46 | + |
| 47 | + PinName pin = microbit_obj_get_pin(args[0])->name; |
| 48 | + mp_int_t num_pixels = mp_obj_get_int(args[1]); |
| 49 | + |
| 50 | + if (num_pixels <= 0) { |
| 51 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid number of pixels")); |
| 52 | + } |
| 53 | + |
| 54 | + neopixel_obj_t *self = m_new_obj(neopixel_obj_t); |
| 55 | + self->base.type = &neopixel_type; |
| 56 | + neopixel_init(&self->strip, pin, num_pixels); |
| 57 | + |
| 58 | + return self; |
42 | 59 | } |
43 | | -MP_DEFINE_CONST_FUN_OBJ_2(mod_neopixel_init_obj, mod_neopixel_init); |
44 | 60 |
|
45 | | -STATIC mp_obj_t mod_neopixel_clear(void) { |
46 | | - neopixel_clear(MP_STATE_PORT(neopixel_strip)); |
47 | | - return mp_const_none; |
| 61 | +STATIC mp_obj_t neopixel_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) { |
| 62 | + neopixel_obj_t *self = (neopixel_obj_t*)self_in; |
| 63 | + mp_uint_t index = mp_get_index(self->base.type, self->strip.num_leds, index_in, false); |
| 64 | + if (value == MP_OBJ_NULL) { |
| 65 | + // delete item |
| 66 | + return MP_OBJ_NULL; // op not supported |
| 67 | + } else if (value == MP_OBJ_SENTINEL) { |
| 68 | + // load |
| 69 | + mp_obj_t rgb[3] = { |
| 70 | + MP_OBJ_NEW_SMALL_INT(self->strip.leds[index].simple.r), |
| 71 | + MP_OBJ_NEW_SMALL_INT(self->strip.leds[index].simple.g), |
| 72 | + MP_OBJ_NEW_SMALL_INT(self->strip.leds[index].simple.b), |
| 73 | + }; |
| 74 | + return mp_obj_new_tuple(3, rgb); |
| 75 | + } else { |
| 76 | + // store |
| 77 | + mp_obj_t *rgb; |
| 78 | + mp_obj_get_array_fixed_n(value, 3, &rgb); |
| 79 | + mp_int_t r = mp_obj_get_int(rgb[0]); |
| 80 | + mp_int_t g = mp_obj_get_int(rgb[1]); |
| 81 | + mp_int_t b = mp_obj_get_int(rgb[2]); |
| 82 | + if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) { |
| 83 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid colour")); |
| 84 | + } |
| 85 | + neopixel_set_color(&self->strip, index, r, g, b); |
| 86 | + return mp_const_none; |
| 87 | + } |
48 | 88 | } |
49 | | -MP_DEFINE_CONST_FUN_OBJ_0(mod_neopixel_clear_obj, mod_neopixel_clear); |
50 | 89 |
|
51 | | -STATIC mp_obj_t mod_neopixel_show(void) { |
52 | | - neopixel_show(MP_STATE_PORT(neopixel_strip)); |
| 90 | +STATIC mp_obj_t neopixel_clear_(mp_obj_t self_in) { |
| 91 | + neopixel_obj_t *self = (neopixel_obj_t*)self_in; |
| 92 | + neopixel_clear(&self->strip); |
53 | 93 | return mp_const_none; |
54 | 94 | } |
55 | | -MP_DEFINE_CONST_FUN_OBJ_0(mod_neopixel_show_obj, mod_neopixel_show); |
| 95 | +MP_DEFINE_CONST_FUN_OBJ_1(neopixel_clear_obj, neopixel_clear_); |
56 | 96 |
|
57 | | -STATIC mp_obj_t mod_neopixel_set_colour(mp_uint_t n_args, const mp_obj_t *args) { |
58 | | - (void)n_args; |
59 | | - neopixel_set_color(MP_STATE_PORT(neopixel_strip), mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]), mp_obj_get_int(args[3])); |
| 97 | +STATIC mp_obj_t neopixel_show_(mp_obj_t self_in) { |
| 98 | + neopixel_obj_t *self = (neopixel_obj_t*)self_in; |
| 99 | + neopixel_show(&self->strip); |
60 | 100 | return mp_const_none; |
61 | 101 | } |
62 | | -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_neopixel_set_colour_obj, 4, 4, mod_neopixel_set_colour); |
| 102 | +MP_DEFINE_CONST_FUN_OBJ_1(neopixel_show_obj, neopixel_show_); |
| 103 | + |
| 104 | +STATIC const mp_map_elem_t neopixel_locals_dict_table[] = { |
| 105 | + { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&neopixel_clear_obj }, |
| 106 | + { MP_OBJ_NEW_QSTR(MP_QSTR_show), (mp_obj_t)&neopixel_show_obj }, |
| 107 | +}; |
| 108 | + |
| 109 | +STATIC MP_DEFINE_CONST_DICT(neopixel_locals_dict, neopixel_locals_dict_table); |
| 110 | + |
| 111 | +const mp_obj_type_t neopixel_type = { |
| 112 | + { &mp_type_type }, |
| 113 | + .name = MP_QSTR_NeoPixel, |
| 114 | + .print = NULL, |
| 115 | + .make_new = neopixel_make_new, |
| 116 | + .call = NULL, |
| 117 | + .unary_op = NULL, |
| 118 | + .binary_op = NULL, |
| 119 | + .attr = NULL, |
| 120 | + .subscr = neopixel_subscr, |
| 121 | + .getiter = NULL, |
| 122 | + .iternext = NULL, |
| 123 | + .buffer_p = {NULL}, |
| 124 | + .stream_p = NULL, |
| 125 | + .bases_tuple = MP_OBJ_NULL, |
| 126 | + .locals_dict = (mp_obj_t)&neopixel_locals_dict, |
| 127 | +}; |
63 | 128 |
|
64 | 129 | STATIC const mp_map_elem_t neopixel_module_globals_table[] = { |
65 | 130 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_neopixel) }, |
66 | | - { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&mod_neopixel_init_obj }, |
67 | | - { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&mod_neopixel_clear_obj }, |
68 | | - { MP_OBJ_NEW_QSTR(MP_QSTR_show), (mp_obj_t)&mod_neopixel_show_obj }, |
69 | | - { MP_OBJ_NEW_QSTR(MP_QSTR_set_colour), (mp_obj_t)&mod_neopixel_set_colour_obj }, |
| 131 | + { MP_OBJ_NEW_QSTR(MP_QSTR_NeoPixel), (mp_obj_t)&neopixel_type }, |
70 | 132 | }; |
71 | 133 |
|
72 | 134 | STATIC MP_DEFINE_CONST_DICT(neopixel_module_globals, neopixel_module_globals_table); |
|
0 commit comments