Skip to content

Commit 9fafd7d

Browse files
committed
Initial commit
1 parent d162bbb commit 9fafd7d

File tree

5 files changed

+202
-7
lines changed

5 files changed

+202
-7
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,10 @@ msgstr ""
14901490
msgid "MOSI pin init failed."
14911491
msgstr ""
14921492

1493+
#: shared-bindings/is31fl3741/__init__.c
1494+
msgid "Mapping must be a tuple"
1495+
msgstr ""
1496+
14931497
#: shared-module/displayio/Shape.c
14941498
#, c-format
14951499
msgid "Maximum x value when mirrored is %d"

shared-bindings/is31fl3741/__init__.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,96 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "shared-bindings/is31fl3741/__init__.h"
28+
2729
#include <stdint.h>
30+
#include <stdbool.h>
2831

2932
#include "py/obj.h"
3033
#include "py/runtime.h"
3134

35+
#include "shared-bindings/busio/I2C.h"
3236
#include "shared-bindings/is31fl3741/IS31FL3741.h"
3337

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+
34112
STATIC const mp_rom_map_elem_t is31fl3741_module_globals_table[] = {
35113
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_is31fl3741) },
36114
{ 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 },
37117
};
38118

39119
STATIC MP_DEFINE_CONST_DICT(is31fl3741_module_globals, is31fl3741_module_globals_table);

shared-bindings/is31fl3741/__init__.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Mark Komus
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_NEOPIXEL_WRITE_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_NEOPIXEL_WRITE_H
29+
30+
#include <stdint.h>
31+
#include <stdbool.h>
32+
33+
#include "shared-bindings/busio/I2C.h"
34+
35+
extern void common_hal_is31fl3741_init(busio_i2c_obj_t *i2c, uint8_t addr);
36+
extern void common_hal_is31fl3741_write(busio_i2c_obj_t *i2c, uint8_t addr, const mp_obj_t *mapping, const uint8_t *pixels, size_t numBytes);
37+
void begin_transaction(busio_i2c_obj_t *i2c);
38+
void end_transaction(busio_i2c_obj_t *i2c);
39+
40+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_NEOPIXEL_WRITE_H

shared-module/is31fl3741/IS31FL3741.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void common_hal_is31fl3741_IS31FL3741_reconstruct(is31fl3741_IS31FL3741_obj_t *s
115115
is31fl3741_set_current(self->i2c, self->device_address, 0xFF);
116116

117117
// set scale (brightness) to max for all LEDs
118-
for (int i; i < 351; i++) {
118+
for (int i = 0; i < 351; i++) {
119119
is31fl3741_set_led(self->i2c, self->device_address, i, 0xFF, 2);
120120
}
121121

@@ -267,23 +267,20 @@ void is31fl3741_IS31FL3741_collect_ptrs(is31fl3741_IS31FL3741_obj_t *self) {
267267
gc_collect_ptr(self->mapping);
268268
}
269269

270-
// The following are routines to manipulate the IS31FL3741 chip
271-
// They are not meant to be called by user code but only used
272-
// internally.
273270

274-
uint8_t cur_page = 99; // set to invalid page to start
271+
uint8_t is31fl3741_cur_page = 99; // set to invalid page to start
275272

276273
void is31fl3741_send_unlock(busio_i2c_obj_t *i2c, uint8_t addr) {
277274
uint8_t unlock[2] = { 0xFE, 0xC5 }; // unlock command
278275
common_hal_busio_i2c_write(i2c, addr, unlock, 2, true);
279276
}
280277

281278
void is31fl3741_set_page(busio_i2c_obj_t *i2c, uint8_t addr, uint8_t p) {
282-
if (p == cur_page) {
279+
if (p == is31fl3741_cur_page) {
283280
return;
284281
}
285282

286-
cur_page = p;
283+
is31fl3741_cur_page = p;
287284
is31fl3741_send_unlock(i2c, addr);
288285

289286
uint8_t page[2] = { 0xFD, 0x00 }; // page command

shared-module/is31fl3741/__init__.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Mark Komus
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
28+
#include "shared-bindings/is31fl3741/__init__.h"
29+
#include "shared-bindings/busio/I2C.h"
30+
#include "shared-bindings/is31fl3741/IS31FL3741.h"
31+
32+
void begin_transaction(busio_i2c_obj_t *i2c) {
33+
while (!common_hal_busio_i2c_try_lock(i2c)) {
34+
RUN_BACKGROUND_TASKS;
35+
if (mp_hal_is_interrupted()) {
36+
break;
37+
}
38+
}
39+
}
40+
41+
void end_transaction(busio_i2c_obj_t *i2c) {
42+
common_hal_busio_i2c_unlock(i2c);
43+
}
44+
45+
void common_hal_is31fl3741_init(busio_i2c_obj_t *i2c, uint8_t addr) {
46+
begin_transaction(i2c);
47+
48+
uint8_t command = 0xFC; // device ID
49+
common_hal_busio_i2c_write(i2c, addr, &command, 1, false);
50+
uint8_t data = 0;
51+
common_hal_busio_i2c_read(i2c, addr, &data, 1);
52+
53+
is31fl3741_send_reset(i2c, addr);
54+
is31fl3741_send_enable(i2c, addr);
55+
is31fl3741_set_current(i2c, addr, 0xFF);
56+
57+
// set scale (brightness) to max for all LEDs
58+
for (int i = 0; i < 351; i++) {
59+
is31fl3741_set_led(i2c, addr, i, 0xFF, 2);
60+
}
61+
62+
end_transaction(i2c);
63+
}
64+
65+
void common_hal_is31fl3741_write(busio_i2c_obj_t *i2c, uint8_t addr, const mp_obj_t *mapping, const uint8_t *pixels, size_t numBytes) {
66+
for (size_t i = 0; i < numBytes; i += 3) {
67+
uint16_t ridx = mp_obj_get_int(mapping[i]);
68+
if (ridx != 65535) {
69+
is31fl3741_set_led(i2c, addr, ridx, IS31GammaTable[pixels[i]], 0); // red
70+
is31fl3741_set_led(i2c, addr, mp_obj_get_int(mapping[i + 1]), IS31GammaTable[pixels[i + 1]], 0); // green
71+
is31fl3741_set_led(i2c, addr, mp_obj_get_int(mapping[i + 2]), IS31GammaTable[pixels[i + 2]], 0); // blue
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)