From a32337718d48fa83e5ebd8c7168019b4196acd0e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 15 Apr 2020 09:21:04 -0500 Subject: Rename _protomatter -> protomatter I originally believed that there would be a wrapper library around it, like with _pixelbuf; but this proves not to be the case, as there's too little for the library to do. --- .../common-hal/_protomatter/Protomatter.c | 78 ---- .../common-hal/_protomatter/Protomatter.h | 35 -- .../atmel-samd/common-hal/_protomatter/__init__.c | 0 .../common-hal/protomatter/Protomatter.c | 78 ++++ .../common-hal/protomatter/Protomatter.h | 35 ++ ports/atmel-samd/common-hal/protomatter/__init__.c | 0 ports/nrf/common-hal/_protomatter/Protomatter.c | 66 ---- ports/nrf/common-hal/_protomatter/Protomatter.h | 35 -- ports/nrf/common-hal/_protomatter/__init__.c | 0 ports/nrf/common-hal/protomatter/Protomatter.c | 66 ++++ ports/nrf/common-hal/protomatter/Protomatter.h | 35 ++ ports/nrf/common-hal/protomatter/__init__.c | 0 py/circuitpy_defns.mk | 12 +- py/circuitpy_mpconfig.h | 2 +- shared-bindings/_protomatter/Protomatter.c | 415 --------------------- shared-bindings/_protomatter/Protomatter.h | 59 --- shared-bindings/_protomatter/__init__.c | 55 --- shared-bindings/protomatter/Protomatter.c | 415 +++++++++++++++++++++ shared-bindings/protomatter/Protomatter.h | 59 +++ shared-bindings/protomatter/__init__.c | 55 +++ shared-module/_protomatter/Protomatter.c | 200 ---------- shared-module/_protomatter/Protomatter.h | 0 shared-module/_protomatter/__init__.c | 0 shared-module/_protomatter/__init__.h | 0 shared-module/_protomatter/allocator.h | 29 -- shared-module/displayio/__init__.h | 2 +- shared-module/protomatter/Protomatter.c | 200 ++++++++++ shared-module/protomatter/Protomatter.h | 0 shared-module/protomatter/__init__.c | 0 shared-module/protomatter/__init__.h | 0 shared-module/protomatter/allocator.h | 29 ++ 31 files changed, 980 insertions(+), 980 deletions(-) delete mode 100644 ports/atmel-samd/common-hal/_protomatter/Protomatter.c delete mode 100644 ports/atmel-samd/common-hal/_protomatter/Protomatter.h delete mode 100644 ports/atmel-samd/common-hal/_protomatter/__init__.c create mode 100644 ports/atmel-samd/common-hal/protomatter/Protomatter.c create mode 100644 ports/atmel-samd/common-hal/protomatter/Protomatter.h create mode 100644 ports/atmel-samd/common-hal/protomatter/__init__.c delete mode 100644 ports/nrf/common-hal/_protomatter/Protomatter.c delete mode 100644 ports/nrf/common-hal/_protomatter/Protomatter.h delete mode 100644 ports/nrf/common-hal/_protomatter/__init__.c create mode 100644 ports/nrf/common-hal/protomatter/Protomatter.c create mode 100644 ports/nrf/common-hal/protomatter/Protomatter.h create mode 100644 ports/nrf/common-hal/protomatter/__init__.c delete mode 100644 shared-bindings/_protomatter/Protomatter.c delete mode 100644 shared-bindings/_protomatter/Protomatter.h delete mode 100644 shared-bindings/_protomatter/__init__.c create mode 100644 shared-bindings/protomatter/Protomatter.c create mode 100644 shared-bindings/protomatter/Protomatter.h create mode 100644 shared-bindings/protomatter/__init__.c delete mode 100644 shared-module/_protomatter/Protomatter.c delete mode 100644 shared-module/_protomatter/Protomatter.h delete mode 100644 shared-module/_protomatter/__init__.c delete mode 100644 shared-module/_protomatter/__init__.h delete mode 100644 shared-module/_protomatter/allocator.h create mode 100644 shared-module/protomatter/Protomatter.c create mode 100644 shared-module/protomatter/Protomatter.h create mode 100644 shared-module/protomatter/__init__.c create mode 100644 shared-module/protomatter/__init__.h create mode 100644 shared-module/protomatter/allocator.h diff --git a/ports/atmel-samd/common-hal/_protomatter/Protomatter.c b/ports/atmel-samd/common-hal/_protomatter/Protomatter.c deleted file mode 100644 index bf9cafb6b..000000000 --- a/ports/atmel-samd/common-hal/_protomatter/Protomatter.c +++ /dev/null @@ -1,78 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include - -#include "common-hal/_protomatter/Protomatter.h" - -#include "samd/timers.h" -#include "timer_handler.h" - -void *common_hal_protomatter_timer_allocate() { - uint8_t timer_index = find_free_timer(); - if (timer_index == 0xff) { - return NULL; - } - timer_never_reset(timer_index, true); - return tc_insts[timer_index]; -} - -static uint8_t tc_index_from_ptr(void* ptr) { - for (uint8_t i = TC_INST_NUM; i > 0; i--) { - if (tc_insts[i] == ptr) { - return i; - } - } - return 0xff; -} - -void common_hal_protomatter_timer_enable(void* ptr) { - uint8_t timer_index = tc_index_from_ptr(ptr); - if (timer_index == 0xff) { - return; - } - set_timer_handler(true, timer_index, TC_HANDLER_PROTOMATTER); - turn_on_clocks(true, timer_index, 1); -} - -void common_hal_protomatter_timer_disable(void* ptr) { - uint8_t timer_index = tc_index_from_ptr(ptr); - if (timer_index == 0xff) { - return; - } - set_timer_handler(true, timer_index, TC_HANDLER_NO_INTERRUPT); - tc_set_enable(ptr, false); -} - -void common_hal_protomatter_timer_free(void* ptr) { - uint8_t timer_index = tc_index_from_ptr(ptr); - if (timer_index == 0xff) { - return; - } - tc_set_enable(ptr, false); - tc_reset(ptr); - timer_reset_ok(timer_index, true); -} diff --git a/ports/atmel-samd/common-hal/_protomatter/Protomatter.h b/ports/atmel-samd/common-hal/_protomatter/Protomatter.h deleted file mode 100644 index 8185bed0e..000000000 --- a/ports/atmel-samd/common-hal/_protomatter/Protomatter.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H - -void *common_hal_protomatter_timer_allocate(void); -void common_hal_protomatter_timer_enable(void*); -void common_hal_protomatter_timer_disable(void*); -void common_hal_protomatter_timer_free(void*); - -#endif diff --git a/ports/atmel-samd/common-hal/_protomatter/__init__.c b/ports/atmel-samd/common-hal/_protomatter/__init__.c deleted file mode 100644 index e69de29bb..000000000 diff --git a/ports/atmel-samd/common-hal/protomatter/Protomatter.c b/ports/atmel-samd/common-hal/protomatter/Protomatter.c new file mode 100644 index 000000000..c9e2b0ce1 --- /dev/null +++ b/ports/atmel-samd/common-hal/protomatter/Protomatter.c @@ -0,0 +1,78 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "common-hal/protomatter/Protomatter.h" + +#include "samd/timers.h" +#include "timer_handler.h" + +void *common_hal_protomatter_timer_allocate() { + uint8_t timer_index = find_free_timer(); + if (timer_index == 0xff) { + return NULL; + } + timer_never_reset(timer_index, true); + return tc_insts[timer_index]; +} + +static uint8_t tc_index_from_ptr(void* ptr) { + for (uint8_t i = TC_INST_NUM; i > 0; i--) { + if (tc_insts[i] == ptr) { + return i; + } + } + return 0xff; +} + +void common_hal_protomatter_timer_enable(void* ptr) { + uint8_t timer_index = tc_index_from_ptr(ptr); + if (timer_index == 0xff) { + return; + } + set_timer_handler(true, timer_index, TC_HANDLER_PROTOMATTER); + turn_on_clocks(true, timer_index, 1); +} + +void common_hal_protomatter_timer_disable(void* ptr) { + uint8_t timer_index = tc_index_from_ptr(ptr); + if (timer_index == 0xff) { + return; + } + set_timer_handler(true, timer_index, TC_HANDLER_NO_INTERRUPT); + tc_set_enable(ptr, false); +} + +void common_hal_protomatter_timer_free(void* ptr) { + uint8_t timer_index = tc_index_from_ptr(ptr); + if (timer_index == 0xff) { + return; + } + tc_set_enable(ptr, false); + tc_reset(ptr); + timer_reset_ok(timer_index, true); +} diff --git a/ports/atmel-samd/common-hal/protomatter/Protomatter.h b/ports/atmel-samd/common-hal/protomatter/Protomatter.h new file mode 100644 index 000000000..8185bed0e --- /dev/null +++ b/ports/atmel-samd/common-hal/protomatter/Protomatter.h @@ -0,0 +1,35 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H + +void *common_hal_protomatter_timer_allocate(void); +void common_hal_protomatter_timer_enable(void*); +void common_hal_protomatter_timer_disable(void*); +void common_hal_protomatter_timer_free(void*); + +#endif diff --git a/ports/atmel-samd/common-hal/protomatter/__init__.c b/ports/atmel-samd/common-hal/protomatter/__init__.c new file mode 100644 index 000000000..e69de29bb diff --git a/ports/nrf/common-hal/_protomatter/Protomatter.c b/ports/nrf/common-hal/_protomatter/Protomatter.c deleted file mode 100644 index b6e55bbd4..000000000 --- a/ports/nrf/common-hal/_protomatter/Protomatter.c +++ /dev/null @@ -1,66 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include - -#include "common-hal/_protomatter/Protomatter.h" - -#include "peripherals/nrf/timers.h" - -extern void _PM_IRQ_HANDLER(void); - -void *common_hal_protomatter_timer_allocate() { - nrfx_timer_t *timer = nrf_peripherals_allocate_timer_or_throw(); - nrf_peripherals_timer_never_reset(timer); - return timer->p_reg; -} - - -static void protomatter_event_handler(nrf_timer_event_t event_type, void *p_context) { - _PM_IRQ_HANDLER(); -} - -void common_hal_protomatter_timer_enable(void* ptr) { - nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr); - static const nrfx_timer_config_t timer_config = { - .frequency = NRF_TIMER_FREQ_16MHz, - .mode = NRF_TIMER_MODE_TIMER, - .bit_width = NRF_TIMER_BIT_WIDTH_16, - .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, - .p_context = NULL, - }; - nrfx_timer_init(timer, &timer_config, &protomatter_event_handler); -} - -void common_hal_protomatter_timer_disable(void* ptr) { - nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr); - nrfx_timer_uninit(timer); -} - -void common_hal_protomatter_timer_free(void* ptr) { - nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr); - nrf_peripherals_free_timer(timer); -} diff --git a/ports/nrf/common-hal/_protomatter/Protomatter.h b/ports/nrf/common-hal/_protomatter/Protomatter.h deleted file mode 100644 index 8185bed0e..000000000 --- a/ports/nrf/common-hal/_protomatter/Protomatter.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H - -void *common_hal_protomatter_timer_allocate(void); -void common_hal_protomatter_timer_enable(void*); -void common_hal_protomatter_timer_disable(void*); -void common_hal_protomatter_timer_free(void*); - -#endif diff --git a/ports/nrf/common-hal/_protomatter/__init__.c b/ports/nrf/common-hal/_protomatter/__init__.c deleted file mode 100644 index e69de29bb..000000000 diff --git a/ports/nrf/common-hal/protomatter/Protomatter.c b/ports/nrf/common-hal/protomatter/Protomatter.c new file mode 100644 index 000000000..45d75e912 --- /dev/null +++ b/ports/nrf/common-hal/protomatter/Protomatter.c @@ -0,0 +1,66 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "common-hal/protomatter/Protomatter.h" + +#include "peripherals/nrf/timers.h" + +extern void _PM_IRQ_HANDLER(void); + +void *common_hal_protomatter_timer_allocate() { + nrfx_timer_t *timer = nrf_peripherals_allocate_timer_or_throw(); + nrf_peripherals_timer_never_reset(timer); + return timer->p_reg; +} + + +static void protomatter_event_handler(nrf_timer_event_t event_type, void *p_context) { + _PM_IRQ_HANDLER(); +} + +void common_hal_protomatter_timer_enable(void* ptr) { + nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr); + static const nrfx_timer_config_t timer_config = { + .frequency = NRF_TIMER_FREQ_16MHz, + .mode = NRF_TIMER_MODE_TIMER, + .bit_width = NRF_TIMER_BIT_WIDTH_16, + .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, + .p_context = NULL, + }; + nrfx_timer_init(timer, &timer_config, &protomatter_event_handler); +} + +void common_hal_protomatter_timer_disable(void* ptr) { + nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr); + nrfx_timer_uninit(timer); +} + +void common_hal_protomatter_timer_free(void* ptr) { + nrfx_timer_t *timer = nrf_peripherals_timer_from_reg(ptr); + nrf_peripherals_free_timer(timer); +} diff --git a/ports/nrf/common-hal/protomatter/Protomatter.h b/ports/nrf/common-hal/protomatter/Protomatter.h new file mode 100644 index 000000000..8185bed0e --- /dev/null +++ b/ports/nrf/common-hal/protomatter/Protomatter.h @@ -0,0 +1,35 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PROTOMATTER_PROTOMATTER_H + +void *common_hal_protomatter_timer_allocate(void); +void common_hal_protomatter_timer_enable(void*); +void common_hal_protomatter_timer_disable(void*); +void common_hal_protomatter_timer_free(void*); + +#endif diff --git a/ports/nrf/common-hal/protomatter/__init__.c b/ports/nrf/common-hal/protomatter/__init__.c new file mode 100644 index 000000000..e69de29bb diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index b6b0cacaf..995412139 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -182,7 +182,7 @@ ifeq ($(CIRCUITPY_PIXELBUF),1) SRC_PATTERNS += _pixelbuf/% endif ifeq ($(CIRCUITPY_PROTOMATTER),1) -SRC_PATTERNS += _protomatter/% +SRC_PATTERNS += protomatter/% endif ifeq ($(CIRCUITPY_PULSEIO),1) SRC_PATTERNS += pulseio/% @@ -248,8 +248,6 @@ SRC_COMMON_HAL_ALL = \ _bleio/PacketBuffer.c \ _bleio/Service.c \ _bleio/UUID.c \ - _protomatter/Protomatter.c \ - _protomatter/__init__.c \ analogio/AnalogIn.c \ analogio/AnalogOut.c \ analogio/__init__.c \ @@ -279,6 +277,8 @@ SRC_COMMON_HAL_ALL = \ nvm/ByteArray.c \ nvm/__init__.c \ os/__init__.c \ + protomatter/Protomatter.c \ + protomatter/__init__.c \ pulseio/PWMOut.c \ pulseio/PulseIn.c \ pulseio/PulseOut.c \ @@ -323,8 +323,6 @@ SRC_SHARED_MODULE_ALL = \ _bleio/ScanResults.c \ _pixelbuf/PixelBuf.c \ _pixelbuf/__init__.c \ - _protomatter/Protomatter.c \ - _protomatter/__init__.c \ _stage/Layer.c \ _stage/Text.c \ _stage/__init__.c \ @@ -368,6 +366,8 @@ SRC_SHARED_MODULE_ALL = \ random/__init__.c \ socket/__init__.c \ network/__init__.c \ + protomatter/Protomatter.c \ + protomatter/__init__.c \ storage/__init__.c \ struct/__init__.c \ terminalio/Terminal.c \ @@ -417,7 +417,7 @@ ifeq ($(CIRCUITPY_PROTOMATTER),1) SRC_MOD += $(addprefix lib/protomatter/, \ core.c \ ) -$(BUILD)/lib/protomatter/core.o: CFLAGS += -include "shared-module/_protomatter/allocator.h" -DCIRCUITPY -Wno-missing-braces +$(BUILD)/lib/protomatter/core.o: CFLAGS += -include "shared-module/protomatter/allocator.h" -DCIRCUITPY -Wno-missing-braces endif # All possible sources are listed here, and are filtered by SRC_PATTERNS. diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 06289258d..55e04b293 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -463,7 +463,7 @@ extern const struct _mp_obj_module_t pixelbuf_module; #if CIRCUITPY_PROTOMATTER extern const struct _mp_obj_module_t protomatter_module; -#define PROTOMATTER_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__protomatter),(mp_obj_t)&protomatter_module }, +#define PROTOMATTER_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_protomatter),(mp_obj_t)&protomatter_module }, #else #define PROTOMATTER_MODULE #endif diff --git a/shared-bindings/_protomatter/Protomatter.c b/shared-bindings/_protomatter/Protomatter.c deleted file mode 100644 index 1efdb2c0c..000000000 --- a/shared-bindings/_protomatter/Protomatter.c +++ /dev/null @@ -1,415 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "py/obj.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "py/objarray.h" - -#include "common-hal/_protomatter/Protomatter.h" -#include "shared-bindings/_protomatter/Protomatter.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/util.h" -#include "shared-module/displayio/__init__.h" -#include "shared-module/framebufferio/__init__.h" -#include "shared-module/framebufferio/FramebufferDisplay.h" - -//| .. currentmodule:: _protomatter -//| -//| :class:`protomatter` -- Driver for HUB75-style RGB LED matrices -//| ================================================================ -//| - -extern Protomatter_core *_PM_protoPtr; - -STATIC uint8_t validate_pin(mp_obj_t obj) { - mcu_pin_obj_t *result = validate_obj_is_free_pin(obj); - return common_hal_mcu_pin_number(result); -} - -STATIC void validate_pins(qstr what, uint8_t* pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) { - mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq)); - if (len > max_pins) { - mp_raise_ValueError_varg(translate("At most %d %q may be specified (not %d)"), max_pins, what, len); - } - *count_out = len; - for (mp_int_t i=0; iprotomatter; - self->base.type = &protomatter_Protomatter_type; - - uint8_t rgb_count, addr_count; - uint8_t rgb_pins[MP_ARRAY_SIZE(self->rgb_pins)]; - uint8_t addr_pins[MP_ARRAY_SIZE(self->addr_pins)]; - uint8_t clock_pin = validate_pin(args[ARG_clock_pin].u_obj); - uint8_t latch_pin = validate_pin(args[ARG_latch_pin].u_obj); - uint8_t output_enable_pin = validate_pin(args[ARG_output_enable_pin].u_obj); - - validate_pins(MP_QSTR_rgb_pins, rgb_pins, MP_ARRAY_SIZE(self->rgb_pins), args[ARG_rgb_list].u_obj, &rgb_count); - validate_pins(MP_QSTR_addr_pins, addr_pins, MP_ARRAY_SIZE(self->addr_pins), args[ARG_addr_list].u_obj, &addr_count); - - if (rgb_count % 6) { - mp_raise_ValueError_varg(translate("Must use a multiple of 6 rgb pins, not %d"), rgb_count); - } - - // TODO(@jepler) Use fewer than all rows of pixels if height < computed_height - if (args[ARG_height].u_int != 0) { - int computed_height = (rgb_count / 3) << (addr_count); - if (computed_height != args[ARG_height].u_int) { - mp_raise_ValueError_varg( - translate("%d address pins and %d rgb pins indicate a height of %d, not %d"), addr_count, rgb_count, computed_height, args[ARG_height].u_int); - } - } - - preflight_pins_or_throw(clock_pin, rgb_pins, rgb_count, true); - - mp_obj_t framebuffer = args[ARG_framebuffer].u_obj; - if (framebuffer == mp_const_none) { - int width = args[ARG_width].u_int; - int bufsize = 2 * width * rgb_count / 3 * (1 << addr_count); - framebuffer = mp_obj_new_bytearray_of_zeros(bufsize); - } - - common_hal_protomatter_protomatter_construct(self, - args[ARG_width].u_int, - args[ARG_bit_depth].u_int, - rgb_count, rgb_pins, - addr_count, addr_pins, - clock_pin, latch_pin, output_enable_pin, - args[ARG_doublebuffer].u_bool, - framebuffer, NULL); - - claim_and_never_reset_pins(args[ARG_rgb_list].u_obj); - claim_and_never_reset_pins(args[ARG_addr_list].u_obj); - claim_and_never_reset_pin(args[ARG_clock_pin].u_obj); - claim_and_never_reset_pin(args[ARG_output_enable_pin].u_obj); - claim_and_never_reset_pin(args[ARG_latch_pin].u_obj); - - return MP_OBJ_FROM_PTR(self); -} - -//| .. method:: deinit -//| -//| Free the resources (pins, timers, etc.) associated with this -//| protomatter instance. After deinitialization, no further operations -//| may be performed. -//| -STATIC mp_obj_t protomatter_protomatter_deinit(mp_obj_t self_in) { - protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; - common_hal_protomatter_protomatter_deinit(self); - return mp_const_none; -} - -STATIC MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_deinit_obj, protomatter_protomatter_deinit); - -static void check_for_deinit(protomatter_protomatter_obj_t *self) { - if (!self->core.rgbPins) { - raise_deinited_error(); - } -} - -//| .. attribute:: brightness -//| -//| In the current implementation, 0.0 turns the display off entirely -//| and any other value up to 1.0 turns the display on fully. -//| -STATIC mp_obj_t protomatter_protomatter_get_brightness(mp_obj_t self_in) { - protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; - check_for_deinit(self); - return mp_obj_new_float(common_hal_protomatter_protomatter_get_paused(self)? 0.0f : 1.0f); -} -MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_get_brightness_obj, protomatter_protomatter_get_brightness); - -STATIC mp_obj_t protomatter_protomatter_set_brightness(mp_obj_t self_in, mp_obj_t value_in) { - protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; - check_for_deinit(self); - mp_float_t brightness = mp_obj_get_float(value_in); - if (brightness < 0.0f || brightness > 1.0f) { - mp_raise_ValueError(translate("Brightness must be 0-1.0")); - } - common_hal_protomatter_protomatter_set_paused(self_in, brightness <= 0); - - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(protomatter_protomatter_set_brightness_obj, protomatter_protomatter_set_brightness); - -const mp_obj_property_t protomatter_protomatter_brightness_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&protomatter_protomatter_get_brightness_obj, - (mp_obj_t)&protomatter_protomatter_set_brightness_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. method:: refresh() -//| -//| Transmits the color data in the buffer to the pixels so that -//| they are shown. -//| -STATIC mp_obj_t protomatter_protomatter_refresh(mp_obj_t self_in) { - protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; - check_for_deinit(self); - common_hal_protomatter_protomatter_refresh(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_refresh_obj, protomatter_protomatter_refresh); - -//| .. attribute:: width -//| -//| The width of the display, in pixels -//| -STATIC mp_obj_t protomatter_protomatter_get_width(mp_obj_t self_in) { - protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; - check_for_deinit(self); - return MP_OBJ_NEW_SMALL_INT(self->width); -} -MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_get_width_obj, protomatter_protomatter_get_width); -const mp_obj_property_t protomatter_protomatter_width_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&protomatter_protomatter_get_width_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - -//| .. attribute:: height -//| -//| The height of the display, in pixels -//| -STATIC mp_obj_t protomatter_protomatter_get_height(mp_obj_t self_in) { - protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; - check_for_deinit(self); - int computed_get_height = (self->rgb_count / 3) << (self->addr_count); - return MP_OBJ_NEW_SMALL_INT(computed_get_height); -} -MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_get_height_obj, protomatter_protomatter_get_height); - -const mp_obj_property_t protomatter_protomatter_height_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&protomatter_protomatter_get_height_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - - -STATIC const mp_rom_map_elem_t protomatter_protomatter_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&protomatter_protomatter_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&protomatter_protomatter_brightness_obj) }, - { MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&protomatter_protomatter_refresh_obj) }, - { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&protomatter_protomatter_width_obj) }, - { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&protomatter_protomatter_height_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(protomatter_protomatter_locals_dict, protomatter_protomatter_locals_dict_table); - -STATIC void protomatter_protomatter_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) { - protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; - check_for_deinit(self); - - *bufinfo = self->bufinfo; -} - -// These version exists so that the prototype matches the protocol, -// avoiding a type cast that can hide errors -STATIC void protomatter_protomatter_swapbuffers(mp_obj_t self_in) { - common_hal_protomatter_protomatter_refresh(self_in); -} - -STATIC void protomatter_protomatter_deinit_proto(mp_obj_t self_in) { - common_hal_protomatter_protomatter_deinit(self_in); -} - -STATIC float protomatter_protomatter_get_brightness_proto(mp_obj_t self_in) { - return common_hal_protomatter_protomatter_get_paused(self_in) ? 0.0f : 1.0f; -} - -STATIC bool protomatter_protomatter_set_brightness_proto(mp_obj_t self_in, mp_float_t value) { - common_hal_protomatter_protomatter_set_paused(self_in, value <= 0); - return true; -} - -STATIC const framebuffer_p_t protomatter_protomatter_proto = { - MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer) - .get_bufinfo = protomatter_protomatter_get_bufinfo, - .set_brightness = protomatter_protomatter_set_brightness_proto, - .get_brightness = protomatter_protomatter_get_brightness_proto, - .swapbuffers = protomatter_protomatter_swapbuffers, - .deinit = protomatter_protomatter_deinit_proto, -}; - -STATIC mp_int_t protomatter_protomatter_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { - protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; - // a readonly framebuffer would be unusual but not impossible - if ((flags & MP_BUFFER_WRITE) && !(self->bufinfo.typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) { - return 1; - } - *bufinfo = self->bufinfo; - return 0; -} - -const mp_obj_type_t protomatter_Protomatter_type = { - { &mp_type_type }, - .name = MP_QSTR_Protomatter, - .buffer_p = { .get_buffer = protomatter_protomatter_get_buffer, }, - .make_new = protomatter_protomatter_make_new, - .protocol = &protomatter_protomatter_proto, - .locals_dict = (mp_obj_dict_t*)&protomatter_protomatter_locals_dict, -}; diff --git a/shared-bindings/_protomatter/Protomatter.h b/shared-bindings/_protomatter/Protomatter.h deleted file mode 100644 index 9499da22c..000000000 --- a/shared-bindings/_protomatter/Protomatter.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_PROTOMATTER_PROTOMATTER_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_PROTOMATTER_PROTOMATTER_H - -#include "shared-module/_protomatter/Protomatter.h" -#include "lib/protomatter/core.h" - -extern const mp_obj_type_t protomatter_Protomatter_type; -typedef struct { - mp_obj_base_t base; - mp_obj_t framebuffer; - mp_buffer_info_t bufinfo; - Protomatter_core core; - void *timer; - uint16_t bufsize, width; - uint8_t rgb_pins[30]; - uint8_t addr_pins[10]; - uint8_t clock_pin, latch_pin, oe_pin; - uint8_t rgb_count, addr_count; - uint8_t bit_depth; - bool core_is_initialized; - bool paused; - bool doublebuffer; -} protomatter_protomatter_obj_t; - -void common_hal_protomatter_protomatter_construct(protomatter_protomatter_obj_t* self, int width, int bit_depth, uint8_t rgb_count, uint8_t* rgb_pins, uint8_t addr_count, uint8_t* addr_pins, uint8_t clock_pin, uint8_t latch_pin, uint8_t oe_pin, bool doublebuffer, mp_obj_t framebuffer, void* timer); -void common_hal_protomatter_protomatter_deinit(protomatter_protomatter_obj_t*); -void protomatter_protomatter_collect_ptrs(protomatter_protomatter_obj_t*); -void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_t* self, mp_obj_t framebuffer); -void common_hal_protomatter_protomatter_set_paused(protomatter_protomatter_obj_t* self, bool paused); -bool common_hal_protomatter_protomatter_get_paused(protomatter_protomatter_obj_t* self); -void common_hal_protomatter_protomatter_refresh(protomatter_protomatter_obj_t* self); - -#endif diff --git a/shared-bindings/_protomatter/__init__.c b/shared-bindings/_protomatter/__init__.c deleted file mode 100644 index 2281b15f1..000000000 --- a/shared-bindings/_protomatter/__init__.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include - -#include "py/obj.h" -#include "py/runtime.h" - -#include "shared-bindings/_protomatter/Protomatter.h" - -//| :mod:`_protomatter` --- Low-level routines for bitbanged LED matrices -//| ===================================================================== -//| -//| .. module:: _protomatter -//| :synopsis: Low-level routines for bitbanged LED matrices -//| -//| .. toctree:: -//| :maxdepth: 3 -//| -//| Protomatter - -STATIC const mp_rom_map_elem_t protomatter_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__protomatter) }, - { MP_ROM_QSTR(MP_QSTR_Protomatter), MP_ROM_PTR(&protomatter_Protomatter_type) }, -}; - -STATIC MP_DEFINE_CONST_DICT(protomatter_module_globals, protomatter_module_globals_table); - -const mp_obj_module_t protomatter_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&protomatter_module_globals, -}; diff --git a/shared-bindings/protomatter/Protomatter.c b/shared-bindings/protomatter/Protomatter.c new file mode 100644 index 000000000..cb4618634 --- /dev/null +++ b/shared-bindings/protomatter/Protomatter.c @@ -0,0 +1,415 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/obj.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/objarray.h" + +#include "common-hal/protomatter/Protomatter.h" +#include "shared-bindings/protomatter/Protomatter.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/util.h" +#include "shared-module/displayio/__init__.h" +#include "shared-module/framebufferio/__init__.h" +#include "shared-module/framebufferio/FramebufferDisplay.h" + +//| .. currentmodule:: _protomatter +//| +//| :class:`protomatter` -- Driver for HUB75-style RGB LED matrices +//| ================================================================ +//| + +extern Protomatter_core *_PM_protoPtr; + +STATIC uint8_t validate_pin(mp_obj_t obj) { + mcu_pin_obj_t *result = validate_obj_is_free_pin(obj); + return common_hal_mcu_pin_number(result); +} + +STATIC void validate_pins(qstr what, uint8_t* pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) { + mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq)); + if (len > max_pins) { + mp_raise_ValueError_varg(translate("At most %d %q may be specified (not %d)"), max_pins, what, len); + } + *count_out = len; + for (mp_int_t i=0; iprotomatter; + self->base.type = &protomatter_Protomatter_type; + + uint8_t rgb_count, addr_count; + uint8_t rgb_pins[MP_ARRAY_SIZE(self->rgb_pins)]; + uint8_t addr_pins[MP_ARRAY_SIZE(self->addr_pins)]; + uint8_t clock_pin = validate_pin(args[ARG_clock_pin].u_obj); + uint8_t latch_pin = validate_pin(args[ARG_latch_pin].u_obj); + uint8_t output_enable_pin = validate_pin(args[ARG_output_enable_pin].u_obj); + + validate_pins(MP_QSTR_rgb_pins, rgb_pins, MP_ARRAY_SIZE(self->rgb_pins), args[ARG_rgb_list].u_obj, &rgb_count); + validate_pins(MP_QSTR_addr_pins, addr_pins, MP_ARRAY_SIZE(self->addr_pins), args[ARG_addr_list].u_obj, &addr_count); + + if (rgb_count % 6) { + mp_raise_ValueError_varg(translate("Must use a multiple of 6 rgb pins, not %d"), rgb_count); + } + + // TODO(@jepler) Use fewer than all rows of pixels if height < computed_height + if (args[ARG_height].u_int != 0) { + int computed_height = (rgb_count / 3) << (addr_count); + if (computed_height != args[ARG_height].u_int) { + mp_raise_ValueError_varg( + translate("%d address pins and %d rgb pins indicate a height of %d, not %d"), addr_count, rgb_count, computed_height, args[ARG_height].u_int); + } + } + + preflight_pins_or_throw(clock_pin, rgb_pins, rgb_count, true); + + mp_obj_t framebuffer = args[ARG_framebuffer].u_obj; + if (framebuffer == mp_const_none) { + int width = args[ARG_width].u_int; + int bufsize = 2 * width * rgb_count / 3 * (1 << addr_count); + framebuffer = mp_obj_new_bytearray_of_zeros(bufsize); + } + + common_hal_protomatter_protomatter_construct(self, + args[ARG_width].u_int, + args[ARG_bit_depth].u_int, + rgb_count, rgb_pins, + addr_count, addr_pins, + clock_pin, latch_pin, output_enable_pin, + args[ARG_doublebuffer].u_bool, + framebuffer, NULL); + + claim_and_never_reset_pins(args[ARG_rgb_list].u_obj); + claim_and_never_reset_pins(args[ARG_addr_list].u_obj); + claim_and_never_reset_pin(args[ARG_clock_pin].u_obj); + claim_and_never_reset_pin(args[ARG_output_enable_pin].u_obj); + claim_and_never_reset_pin(args[ARG_latch_pin].u_obj); + + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit +//| +//| Free the resources (pins, timers, etc.) associated with this +//| protomatter instance. After deinitialization, no further operations +//| may be performed. +//| +STATIC mp_obj_t protomatter_protomatter_deinit(mp_obj_t self_in) { + protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; + common_hal_protomatter_protomatter_deinit(self); + return mp_const_none; +} + +STATIC MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_deinit_obj, protomatter_protomatter_deinit); + +static void check_for_deinit(protomatter_protomatter_obj_t *self) { + if (!self->core.rgbPins) { + raise_deinited_error(); + } +} + +//| .. attribute:: brightness +//| +//| In the current implementation, 0.0 turns the display off entirely +//| and any other value up to 1.0 turns the display on fully. +//| +STATIC mp_obj_t protomatter_protomatter_get_brightness(mp_obj_t self_in) { + protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; + check_for_deinit(self); + return mp_obj_new_float(common_hal_protomatter_protomatter_get_paused(self)? 0.0f : 1.0f); +} +MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_get_brightness_obj, protomatter_protomatter_get_brightness); + +STATIC mp_obj_t protomatter_protomatter_set_brightness(mp_obj_t self_in, mp_obj_t value_in) { + protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; + check_for_deinit(self); + mp_float_t brightness = mp_obj_get_float(value_in); + if (brightness < 0.0f || brightness > 1.0f) { + mp_raise_ValueError(translate("Brightness must be 0-1.0")); + } + common_hal_protomatter_protomatter_set_paused(self_in, brightness <= 0); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(protomatter_protomatter_set_brightness_obj, protomatter_protomatter_set_brightness); + +const mp_obj_property_t protomatter_protomatter_brightness_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&protomatter_protomatter_get_brightness_obj, + (mp_obj_t)&protomatter_protomatter_set_brightness_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. method:: refresh() +//| +//| Transmits the color data in the buffer to the pixels so that +//| they are shown. +//| +STATIC mp_obj_t protomatter_protomatter_refresh(mp_obj_t self_in) { + protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; + check_for_deinit(self); + common_hal_protomatter_protomatter_refresh(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_refresh_obj, protomatter_protomatter_refresh); + +//| .. attribute:: width +//| +//| The width of the display, in pixels +//| +STATIC mp_obj_t protomatter_protomatter_get_width(mp_obj_t self_in) { + protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; + check_for_deinit(self); + return MP_OBJ_NEW_SMALL_INT(self->width); +} +MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_get_width_obj, protomatter_protomatter_get_width); +const mp_obj_property_t protomatter_protomatter_width_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&protomatter_protomatter_get_width_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: height +//| +//| The height of the display, in pixels +//| +STATIC mp_obj_t protomatter_protomatter_get_height(mp_obj_t self_in) { + protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; + check_for_deinit(self); + int computed_get_height = (self->rgb_count / 3) << (self->addr_count); + return MP_OBJ_NEW_SMALL_INT(computed_get_height); +} +MP_DEFINE_CONST_FUN_OBJ_1(protomatter_protomatter_get_height_obj, protomatter_protomatter_get_height); + +const mp_obj_property_t protomatter_protomatter_height_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&protomatter_protomatter_get_height_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + + +STATIC const mp_rom_map_elem_t protomatter_protomatter_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&protomatter_protomatter_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&protomatter_protomatter_brightness_obj) }, + { MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&protomatter_protomatter_refresh_obj) }, + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&protomatter_protomatter_width_obj) }, + { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&protomatter_protomatter_height_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(protomatter_protomatter_locals_dict, protomatter_protomatter_locals_dict_table); + +STATIC void protomatter_protomatter_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) { + protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; + check_for_deinit(self); + + *bufinfo = self->bufinfo; +} + +// These version exists so that the prototype matches the protocol, +// avoiding a type cast that can hide errors +STATIC void protomatter_protomatter_swapbuffers(mp_obj_t self_in) { + common_hal_protomatter_protomatter_refresh(self_in); +} + +STATIC void protomatter_protomatter_deinit_proto(mp_obj_t self_in) { + common_hal_protomatter_protomatter_deinit(self_in); +} + +STATIC float protomatter_protomatter_get_brightness_proto(mp_obj_t self_in) { + return common_hal_protomatter_protomatter_get_paused(self_in) ? 0.0f : 1.0f; +} + +STATIC bool protomatter_protomatter_set_brightness_proto(mp_obj_t self_in, mp_float_t value) { + common_hal_protomatter_protomatter_set_paused(self_in, value <= 0); + return true; +} + +STATIC const framebuffer_p_t protomatter_protomatter_proto = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer) + .get_bufinfo = protomatter_protomatter_get_bufinfo, + .set_brightness = protomatter_protomatter_set_brightness_proto, + .get_brightness = protomatter_protomatter_get_brightness_proto, + .swapbuffers = protomatter_protomatter_swapbuffers, + .deinit = protomatter_protomatter_deinit_proto, +}; + +STATIC mp_int_t protomatter_protomatter_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { + protomatter_protomatter_obj_t *self = (protomatter_protomatter_obj_t*)self_in; + // a readonly framebuffer would be unusual but not impossible + if ((flags & MP_BUFFER_WRITE) && !(self->bufinfo.typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) { + return 1; + } + *bufinfo = self->bufinfo; + return 0; +} + +const mp_obj_type_t protomatter_Protomatter_type = { + { &mp_type_type }, + .name = MP_QSTR_Protomatter, + .buffer_p = { .get_buffer = protomatter_protomatter_get_buffer, }, + .make_new = protomatter_protomatter_make_new, + .protocol = &protomatter_protomatter_proto, + .locals_dict = (mp_obj_dict_t*)&protomatter_protomatter_locals_dict, +}; diff --git a/shared-bindings/protomatter/Protomatter.h b/shared-bindings/protomatter/Protomatter.h new file mode 100644 index 000000000..4704a626f --- /dev/null +++ b/shared-bindings/protomatter/Protomatter.h @@ -0,0 +1,59 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_PROTOMATTER_PROTOMATTER_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_PROTOMATTER_PROTOMATTER_H + +#include "shared-module/protomatter/Protomatter.h" +#include "lib/protomatter/core.h" + +extern const mp_obj_type_t protomatter_Protomatter_type; +typedef struct { + mp_obj_base_t base; + mp_obj_t framebuffer; + mp_buffer_info_t bufinfo; + Protomatter_core core; + void *timer; + uint16_t bufsize, width; + uint8_t rgb_pins[30]; + uint8_t addr_pins[10]; + uint8_t clock_pin, latch_pin, oe_pin; + uint8_t rgb_count, addr_count; + uint8_t bit_depth; + bool core_is_initialized; + bool paused; + bool doublebuffer; +} protomatter_protomatter_obj_t; + +void common_hal_protomatter_protomatter_construct(protomatter_protomatter_obj_t* self, int width, int bit_depth, uint8_t rgb_count, uint8_t* rgb_pins, uint8_t addr_count, uint8_t* addr_pins, uint8_t clock_pin, uint8_t latch_pin, uint8_t oe_pin, bool doublebuffer, mp_obj_t framebuffer, void* timer); +void common_hal_protomatter_protomatter_deinit(protomatter_protomatter_obj_t*); +void protomatter_protomatter_collect_ptrs(protomatter_protomatter_obj_t*); +void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_t* self, mp_obj_t framebuffer); +void common_hal_protomatter_protomatter_set_paused(protomatter_protomatter_obj_t* self, bool paused); +bool common_hal_protomatter_protomatter_get_paused(protomatter_protomatter_obj_t* self); +void common_hal_protomatter_protomatter_refresh(protomatter_protomatter_obj_t* self); + +#endif diff --git a/shared-bindings/protomatter/__init__.c b/shared-bindings/protomatter/__init__.c new file mode 100644 index 000000000..fc429898f --- /dev/null +++ b/shared-bindings/protomatter/__init__.c @@ -0,0 +1,55 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/protomatter/Protomatter.h" + +//| :mod:`_protomatter` --- Low-level routines for bitbanged LED matrices +//| ===================================================================== +//| +//| .. module:: _protomatter +//| :synopsis: Low-level routines for bitbanged LED matrices +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| Protomatter + +STATIC const mp_rom_map_elem_t protomatter_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_protomatter) }, + { MP_ROM_QSTR(MP_QSTR_Protomatter), MP_ROM_PTR(&protomatter_Protomatter_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(protomatter_module_globals, protomatter_module_globals_table); + +const mp_obj_module_t protomatter_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&protomatter_module_globals, +}; diff --git a/shared-module/_protomatter/Protomatter.c b/shared-module/_protomatter/Protomatter.c deleted file mode 100644 index 77ca63476..000000000 --- a/shared-module/_protomatter/Protomatter.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2020 Jeff Epler for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include - -#include "py/gc.h" -#include "py/obj.h" -#include "py/objarray.h" -#include "py/objproperty.h" -#include "py/runtime.h" - -#include "common-hal/_protomatter/Protomatter.h" -#include "shared-module/_protomatter/allocator.h" -#include "shared-bindings/_protomatter/Protomatter.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/util.h" -#include "shared-module/framebufferio/FramebufferDisplay.h" - -extern Protomatter_core *_PM_protoPtr; - -void common_hal_protomatter_protomatter_construct(protomatter_protomatter_obj_t *self, int width, int bit_depth, uint8_t rgb_count, uint8_t *rgb_pins, uint8_t addr_count, uint8_t *addr_pins, uint8_t clock_pin, uint8_t latch_pin, uint8_t oe_pin, bool doublebuffer, mp_obj_t framebuffer, void *timer) { - self->width = width; - self->bit_depth = bit_depth; - self->rgb_count = rgb_count; - memcpy(self->rgb_pins, rgb_pins, rgb_count); - self->addr_count = addr_count; - memcpy(self->addr_pins, addr_pins, addr_count); - self->clock_pin = clock_pin; - self->oe_pin = oe_pin; - self->latch_pin = latch_pin; - self->doublebuffer = doublebuffer; - - self->timer = timer ? timer : common_hal_protomatter_timer_allocate(); - if (self->timer == NULL) { - mp_raise_ValueError(translate("No timer available")); - } - - self->width = width; - self->bufsize = 2 * width * rgb_count / 3 * (1 << addr_count); - - common_hal_protomatter_protomatter_reconstruct(self, framebuffer); -} - -void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_t* self, mp_obj_t framebuffer) { - if (framebuffer) { - self->framebuffer = framebuffer; - framebuffer = mp_obj_new_bytearray_of_zeros(self->bufsize); - mp_get_buffer_raise(self->framebuffer, &self->bufinfo, MP_BUFFER_READ); - if (mp_get_buffer(self->framebuffer, &self->bufinfo, MP_BUFFER_RW)) { - self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW; - } else { - self->bufinfo.typecode = 'H'; - } - // verify that the matrix is big enough - mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize-1), false); - } else { - _PM_FREE(self->bufinfo.buf); - _PM_FREE(self->core.rgbPins); - _PM_FREE(self->core.addr); - _PM_FREE(self->core.screenData); - - self->framebuffer = NULL; - self->bufinfo.buf = _PM_allocator_impl(self->bufsize); - self->bufinfo.len = self->bufsize; - self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW; - } - - ProtomatterStatus stat = _PM_init(&self->core, - self->width, self->bit_depth, - self->rgb_count/6, self->rgb_pins, - self->addr_count, self->addr_pins, - self->clock_pin, self->latch_pin, self->oe_pin, - self->doublebuffer, self->timer); - - if (stat == PROTOMATTER_OK) { - _PM_protoPtr = &self->core; - common_hal_mcu_disable_interrupts(); - common_hal_protomatter_timer_enable(self->timer); - stat = _PM_begin(&self->core); - _PM_convert_565(&self->core, self->bufinfo.buf, self->width); - common_hal_mcu_enable_interrupts(); - _PM_swapbuffer_maybe(&self->core); - } - - if (stat != PROTOMATTER_OK) { - // XXX this deinit() actually makes crashy-crashy - // can trigger it by sending inappropriate pins - common_hal_protomatter_protomatter_deinit(self); - switch (stat) { - case PROTOMATTER_ERR_PINS: - mp_raise_ValueError(translate("Invalid pin")); - break; - case PROTOMATTER_ERR_ARG: - mp_raise_ValueError(translate("Invalid argument")); - break; - case PROTOMATTER_ERR_MALLOC: /// should have already been signaled as NLR - default: - mp_raise_msg_varg(&mp_type_RuntimeError, - translate("Protomatter internal error #%d"), (int)stat); - break; - } - } - - self->paused = 0; - -} - -STATIC void free_pin(uint8_t *pin) { - if (*pin != COMMON_HAL_MCU_NO_PIN) { - common_hal_mcu_pin_reset_number(*pin); - } - *pin = COMMON_HAL_MCU_NO_PIN; -} - -STATIC void free_pin_seq(uint8_t *seq, int count) { - for (int i=0; itimer) { - common_hal_protomatter_timer_free(self->timer); - self->timer = 0; - } - - if (_PM_protoPtr == &self->core) { - _PM_protoPtr = NULL; - } - - free_pin_seq(self->rgb_pins, self->rgb_count); - free_pin_seq(self->addr_pins, self->addr_count); - free_pin(&self->clock_pin); - free_pin(&self->latch_pin); - free_pin(&self->oe_pin); - - if (self->core.rgbPins) { - _PM_free(&self->core); - } - memset(&self->core, 0, sizeof(self->core)); - - // If it was supervisor-allocated, it is supervisor-freed and the pointer - // is zeroed, otherwise the pointer is just zeroed - _PM_FREE(self->bufinfo.buf); - self->base.type = NULL; - - // If a framebuffer was passed in to the constructor, NULL the reference - // here so that it will become GC'able - self->framebuffer = NULL; -} - -void protomatter_protomatter_collect_ptrs(protomatter_protomatter_obj_t* self) { - gc_collect_ptr(self->framebuffer); - gc_collect_ptr(self->core.rgbPins); - gc_collect_ptr(self->core.addr); - gc_collect_ptr(self->core.screenData); -} - -void common_hal_protomatter_protomatter_set_paused(protomatter_protomatter_obj_t* self, bool paused) { - if (paused && !self->paused) { - _PM_stop(&self->core); - } else if (!paused && self->paused) { - _PM_resume(&self->core); - } - self->paused = paused; -} - -bool common_hal_protomatter_protomatter_get_paused(protomatter_protomatter_obj_t* self) { - return self->paused; -} - -void common_hal_protomatter_protomatter_refresh(protomatter_protomatter_obj_t* self) { - _PM_convert_565(&self->core, self->bufinfo.buf, self->width); - _PM_swapbuffer_maybe(&self->core); -} - diff --git a/shared-module/_protomatter/Protomatter.h b/shared-module/_protomatter/Protomatter.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/shared-module/_protomatter/__init__.c b/shared-module/_protomatter/__init__.c deleted file mode 100644 index e69de29bb..000000000 diff --git a/shared-module/_protomatter/__init__.h b/shared-module/_protomatter/__init__.h deleted file mode 100644 index e69de29bb..000000000 diff --git a/shared-module/_protomatter/allocator.h b/shared-module/_protomatter/allocator.h deleted file mode 100644 index b7f517ce5..000000000 --- a/shared-module/_protomatter/allocator.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H -#define MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H - -#include -#include "py/gc.h" -#include "py/misc.h" -#include "supervisor/memory.h" - -#define _PM_ALLOCATOR _PM_allocator_impl -#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0) - -static inline void *_PM_allocator_impl(size_t sz) { - if (gc_alloc_possible()) { - return m_malloc(sz + sizeof(void*), true); - } else { - supervisor_allocation *allocation = allocate_memory(align32_size(sz), false); - return allocation ? allocation->ptr : NULL; - } -} - -static inline void _PM_free_impl(void *ptr_in) { - supervisor_allocation *allocation = allocation_from_ptr(ptr_in); - - if (allocation) { - free_memory(allocation); - } -} - -#endif diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h index 278a5e78f..f5045b19a 100644 --- a/shared-module/displayio/__init__.h +++ b/shared-module/displayio/__init__.h @@ -36,7 +36,7 @@ #include "shared-bindings/displayio/Group.h" #include "shared-bindings/displayio/I2CDisplay.h" #include "shared-bindings/displayio/ParallelBus.h" -#include "shared-bindings/_protomatter/Protomatter.h" +#include "shared-bindings/protomatter/Protomatter.h" typedef struct { union { diff --git a/shared-module/protomatter/Protomatter.c b/shared-module/protomatter/Protomatter.c new file mode 100644 index 000000000..37721cb1c --- /dev/null +++ b/shared-module/protomatter/Protomatter.c @@ -0,0 +1,200 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jeff Epler for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "py/gc.h" +#include "py/obj.h" +#include "py/objarray.h" +#include "py/objproperty.h" +#include "py/runtime.h" + +#include "common-hal/protomatter/Protomatter.h" +#include "shared-module/protomatter/allocator.h" +#include "shared-bindings/protomatter/Protomatter.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/util.h" +#include "shared-module/framebufferio/FramebufferDisplay.h" + +extern Protomatter_core *_PM_protoPtr; + +void common_hal_protomatter_protomatter_construct(protomatter_protomatter_obj_t *self, int width, int bit_depth, uint8_t rgb_count, uint8_t *rgb_pins, uint8_t addr_count, uint8_t *addr_pins, uint8_t clock_pin, uint8_t latch_pin, uint8_t oe_pin, bool doublebuffer, mp_obj_t framebuffer, void *timer) { + self->width = width; + self->bit_depth = bit_depth; + self->rgb_count = rgb_count; + memcpy(self->rgb_pins, rgb_pins, rgb_count); + self->addr_count = addr_count; + memcpy(self->addr_pins, addr_pins, addr_count); + self->clock_pin = clock_pin; + self->oe_pin = oe_pin; + self->latch_pin = latch_pin; + self->doublebuffer = doublebuffer; + + self->timer = timer ? timer : common_hal_protomatter_timer_allocate(); + if (self->timer == NULL) { + mp_raise_ValueError(translate("No timer available")); + } + + self->width = width; + self->bufsize = 2 * width * rgb_count / 3 * (1 << addr_count); + + common_hal_protomatter_protomatter_reconstruct(self, framebuffer); +} + +void common_hal_protomatter_protomatter_reconstruct(protomatter_protomatter_obj_t* self, mp_obj_t framebuffer) { + if (framebuffer) { + self->framebuffer = framebuffer; + framebuffer = mp_obj_new_bytearray_of_zeros(self->bufsize); + mp_get_buffer_raise(self->framebuffer, &self->bufinfo, MP_BUFFER_READ); + if (mp_get_buffer(self->framebuffer, &self->bufinfo, MP_BUFFER_RW)) { + self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW; + } else { + self->bufinfo.typecode = 'H'; + } + // verify that the matrix is big enough + mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize-1), false); + } else { + _PM_FREE(self->bufinfo.buf); + _PM_FREE(self->core.rgbPins); + _PM_FREE(self->core.addr); + _PM_FREE(self->core.screenData); + + self->framebuffer = NULL; + self->bufinfo.buf = _PM_allocator_impl(self->bufsize); + self->bufinfo.len = self->bufsize; + self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW; + } + + ProtomatterStatus stat = _PM_init(&self->core, + self->width, self->bit_depth, + self->rgb_count/6, self->rgb_pins, + self->addr_count, self->addr_pins, + self->clock_pin, self->latch_pin, self->oe_pin, + self->doublebuffer, self->timer); + + if (stat == PROTOMATTER_OK) { + _PM_protoPtr = &self->core; + common_hal_mcu_disable_interrupts(); + common_hal_protomatter_timer_enable(self->timer); + stat = _PM_begin(&self->core); + _PM_convert_565(&self->core, self->bufinfo.buf, self->width); + common_hal_mcu_enable_interrupts(); + _PM_swapbuffer_maybe(&self->core); + } + + if (stat != PROTOMATTER_OK) { + // XXX this deinit() actually makes crashy-crashy + // can trigger it by sending inappropriate pins + common_hal_protomatter_protomatter_deinit(self); + switch (stat) { + case PROTOMATTER_ERR_PINS: + mp_raise_ValueError(translate("Invalid pin")); + break; + case PROTOMATTER_ERR_ARG: + mp_raise_ValueError(translate("Invalid argument")); + break; + case PROTOMATTER_ERR_MALLOC: /// should have already been signaled as NLR + default: + mp_raise_msg_varg(&mp_type_RuntimeError, + translate("Protomatter internal error #%d"), (int)stat); + break; + } + } + + self->paused = 0; + +} + +STATIC void free_pin(uint8_t *pin) { + if (*pin != COMMON_HAL_MCU_NO_PIN) { + common_hal_mcu_pin_reset_number(*pin); + } + *pin = COMMON_HAL_MCU_NO_PIN; +} + +STATIC void free_pin_seq(uint8_t *seq, int count) { + for (int i=0; itimer) { + common_hal_protomatter_timer_free(self->timer); + self->timer = 0; + } + + if (_PM_protoPtr == &self->core) { + _PM_protoPtr = NULL; + } + + free_pin_seq(self->rgb_pins, self->rgb_count); + free_pin_seq(self->addr_pins, self->addr_count); + free_pin(&self->clock_pin); + free_pin(&self->latch_pin); + free_pin(&self->oe_pin); + + if (self->core.rgbPins) { + _PM_free(&self->core); + } + memset(&self->core, 0, sizeof(self->core)); + + // If it was supervisor-allocated, it is supervisor-freed and the pointer + // is zeroed, otherwise the pointer is just zeroed + _PM_FREE(self->bufinfo.buf); + self->base.type = NULL; + + // If a framebuffer was passed in to the constructor, NULL the reference + // here so that it will become GC'able + self->framebuffer = NULL; +} + +void protomatter_protomatter_collect_ptrs(protomatter_protomatter_obj_t* self) { + gc_collect_ptr(self->framebuffer); + gc_collect_ptr(self->core.rgbPins); + gc_collect_ptr(self->core.addr); + gc_collect_ptr(self->core.screenData); +} + +void common_hal_protomatter_protomatter_set_paused(protomatter_protomatter_obj_t* self, bool paused) { + if (paused && !self->paused) { + _PM_stop(&self->core); + } else if (!paused && self->paused) { + _PM_resume(&self->core); + } + self->paused = paused; +} + +bool common_hal_protomatter_protomatter_get_paused(protomatter_protomatter_obj_t* self) { + return self->paused; +} + +void common_hal_protomatter_protomatter_refresh(protomatter_protomatter_obj_t* self) { + _PM_convert_565(&self->core, self->bufinfo.buf, self->width); + _PM_swapbuffer_maybe(&self->core); +} + diff --git a/shared-module/protomatter/Protomatter.h b/shared-module/protomatter/Protomatter.h new file mode 100644 index 000000000..e69de29bb diff --git a/shared-module/protomatter/__init__.c b/shared-module/protomatter/__init__.c new file mode 100644 index 000000000..e69de29bb diff --git a/shared-module/protomatter/__init__.h b/shared-module/protomatter/__init__.h new file mode 100644 index 000000000..e69de29bb diff --git a/shared-module/protomatter/allocator.h b/shared-module/protomatter/allocator.h new file mode 100644 index 000000000..b7f517ce5 --- /dev/null +++ b/shared-module/protomatter/allocator.h @@ -0,0 +1,29 @@ +#ifndef MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H +#define MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H + +#include +#include "py/gc.h" +#include "py/misc.h" +#include "supervisor/memory.h" + +#define _PM_ALLOCATOR _PM_allocator_impl +#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0) + +static inline void *_PM_allocator_impl(size_t sz) { + if (gc_alloc_possible()) { + return m_malloc(sz + sizeof(void*), true); + } else { + supervisor_allocation *allocation = allocate_memory(align32_size(sz), false); + return allocation ? allocation->ptr : NULL; + } +} + +static inline void _PM_free_impl(void *ptr_in) { + supervisor_allocation *allocation = allocation_from_ptr(ptr_in); + + if (allocation) { + free_memory(allocation); + } +} + +#endif -- cgit v1.2.3