|
24 | 24 | * THE SOFTWARE.
|
25 | 25 | */
|
26 | 26 |
|
| 27 | +#include "shared-bindings/is31fl3741/__init__.h" |
| 28 | + |
27 | 29 | #include <stdint.h>
|
| 30 | +#include <stdbool.h> |
28 | 31 |
|
29 | 32 | #include "py/obj.h"
|
30 | 33 | #include "py/runtime.h"
|
31 | 34 |
|
| 35 | +#include "shared-bindings/busio/I2C.h" |
32 | 36 | #include "shared-bindings/is31fl3741/IS31FL3741.h"
|
33 | 37 |
|
| 38 | + |
| 39 | +//| """Low-level neopixel implementation |
| 40 | +//| |
| 41 | +//| The `neopixel_write` module contains a helper method to write out bytes in |
| 42 | +//| the 800khz neopixel protocol. |
| 43 | +//| |
| 44 | +//| For example, to turn off a single neopixel (like the status pixel on Express |
| 45 | +//| boards.) |
| 46 | +//| |
| 47 | +//| .. code-block:: python |
| 48 | +//| |
| 49 | +//| import board |
| 50 | +//| import neopixel_write |
| 51 | +//| import digitalio |
| 52 | +//| |
| 53 | +//| pin = digitalio.DigitalInOut(board.NEOPIXEL) |
| 54 | +//| pin.direction = digitalio.Direction.OUTPUT |
| 55 | +//| pixel_off = bytearray([0, 0, 0]) |
| 56 | +//| neopixel_write.neopixel_write(pin, pixel_off)""" |
| 57 | +//| |
| 58 | +//| def neopixel_write(digitalinout: digitalio.DigitalInOut, buf: ReadableBuffer) -> None: |
| 59 | +//| """Write buf out on the given DigitalInOut. |
| 60 | +//| |
| 61 | +//| :param ~digitalio.DigitalInOut digitalinout: the DigitalInOut to output with |
| 62 | +//| :param ~_typing.ReadableBuffer buf: The bytes to clock out. No assumption is made about color order""" |
| 63 | +//| ... |
| 64 | +// STATIC mp_obj_t is31fl3741_is31fl3741_write(mp_obj_t i2c_obj, mp_obj_t device_addr_obj, mp_obj_t mapping, mp_obj_t buf) { |
| 65 | +STATIC mp_obj_t is31fl3741_is31fl3741_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 66 | + enum { ARG_i2c, ARG_addr, ARG_mapping, ARG_buffer }; |
| 67 | + static const mp_arg_t allowed_args[] = { |
| 68 | + { MP_QSTR_i2c, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 69 | + { MP_QSTR_addr, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0x30 } }, |
| 70 | + { MP_QSTR_mapping, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 71 | + { MP_QSTR_buffer, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 72 | + }; |
| 73 | + |
| 74 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 75 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 76 | + |
| 77 | + mp_obj_t i2c = mp_arg_validate_type(args[ARG_i2c].u_obj, &busio_i2c_type, MP_QSTR_i2c_bus); |
| 78 | + |
| 79 | + if (!mp_obj_is_tuple_compatible(args[ARG_mapping].u_obj)) { |
| 80 | + mp_raise_ValueError(translate("Mapping must be a tuple")); |
| 81 | + } |
| 82 | + |
| 83 | + mp_obj_t *map_items; |
| 84 | + size_t map_len; |
| 85 | + mp_obj_tuple_get(args[ARG_mapping].u_obj, &map_len, &map_items); |
| 86 | + |
| 87 | + mp_buffer_info_t bufinfo; |
| 88 | + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); |
| 89 | + |
| 90 | + common_hal_is31fl3741_write(i2c, args[ARG_addr].u_int, map_items, (uint8_t *)bufinfo.buf, bufinfo.len); |
| 91 | + return mp_const_none; |
| 92 | +} |
| 93 | +MP_DEFINE_CONST_FUN_OBJ_KW(is31fl3741_is31fl3741_write_obj, 0, is31fl3741_is31fl3741_write); |
| 94 | + |
| 95 | +STATIC mp_obj_t is31fl3741_is31fl3741_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 96 | + enum { ARG_i2c, ARG_addr }; |
| 97 | + static const mp_arg_t allowed_args[] = { |
| 98 | + { MP_QSTR_i2c, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 99 | + { MP_QSTR_addr, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0x30 } }, |
| 100 | + }; |
| 101 | + |
| 102 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 103 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 104 | + |
| 105 | + mp_obj_t i2c = mp_arg_validate_type(args[ARG_i2c].u_obj, &busio_i2c_type, MP_QSTR_i2c_bus); |
| 106 | + |
| 107 | + common_hal_is31fl3741_init(i2c, args[ARG_addr].u_int); |
| 108 | + return mp_const_none; |
| 109 | +} |
| 110 | +MP_DEFINE_CONST_FUN_OBJ_KW(is31fl3741_is31fl3741_init_obj, 0, is31fl3741_is31fl3741_init); |
| 111 | + |
34 | 112 | STATIC const mp_rom_map_elem_t is31fl3741_module_globals_table[] = {
|
35 | 113 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_is31fl3741) },
|
36 | 114 | { MP_ROM_QSTR(MP_QSTR_IS31FL3741), MP_ROM_PTR(&is31fl3741_IS31FL3741_type) },
|
| 115 | + { MP_OBJ_NEW_QSTR(MP_QSTR_is31fl3741_init), (mp_obj_t)&is31fl3741_is31fl3741_init_obj }, |
| 116 | + { MP_OBJ_NEW_QSTR(MP_QSTR_is31fl3741_write), (mp_obj_t)&is31fl3741_is31fl3741_write_obj }, |
37 | 117 | };
|
38 | 118 |
|
39 | 119 | STATIC MP_DEFINE_CONST_DICT(is31fl3741_module_globals, is31fl3741_module_globals_table);
|
|
0 commit comments