2016-11-03 18:50:59 -04:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
|
*
|
|
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2016 Scott Shawcroft 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.
|
|
|
|
|
*/
|
2018-07-31 19:53:54 -04:00
|
|
|
|
#include "shared-bindings/neopixel_write/__init__.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
|
|
|
|
#include "py/mphal.h"
|
|
|
|
|
#include "py/runtime.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2022-01-29 13:13:00 -05:00
|
|
|
|
#include "shared-bindings/util.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
2022-04-13 14:23:26 -04:00
|
|
|
|
// RGB LED timing information:
|
|
|
|
|
|
|
|
|
|
// From the WS2811 datasheet: high speed mode
|
|
|
|
|
// - T0H 0 code,high voltage time 0.25 us +-150ns
|
|
|
|
|
// - T1H 1 code,high voltage time 0.6 us +-150ns
|
|
|
|
|
// - T0L 0 code,low voltage time 1.0 us +-150ns
|
|
|
|
|
// - T1L 1 code,low voltage time 0.65 us +-150ns
|
|
|
|
|
// - RES low voltage time Above 50us
|
|
|
|
|
|
|
|
|
|
// From the SK6812 datasheet:
|
|
|
|
|
// - T0H 0 code, high level time 0.3us +-0.15us
|
|
|
|
|
// - T1H 1 code, high level time 0.6us +-0.15us
|
|
|
|
|
// - T0L 0 code, low level time 0.9us +-0.15us
|
|
|
|
|
// - T1L 1 code, low level time 0.6us +-0.15us
|
|
|
|
|
// - Trst Reset code,low level time 80us
|
|
|
|
|
|
|
|
|
|
// From the WS2812 datasheet:
|
|
|
|
|
// - T0H 0 code, high voltage time 0.35us +-150ns
|
|
|
|
|
// - T1H 1 code, high voltage time 0.7us +-150ns
|
|
|
|
|
// - T0L 0 code, low voltage time 0.8us +-150ns
|
|
|
|
|
// - T1L 1 code, low voltage time 0.6us +-150ns
|
|
|
|
|
// - RES low voltage time Above 50us
|
|
|
|
|
|
|
|
|
|
// From the WS28212B datasheet:
|
|
|
|
|
// - T0H 0 code, high voltage time 0.4us +-150ns
|
|
|
|
|
// - T1H 1 code, high voltage time 0.8us +-150ns
|
|
|
|
|
// - T0L 0 code, low voltage time 0.85us +-150ns
|
|
|
|
|
// - T1L 1 code, low voltage time 0.45us +-150ns
|
|
|
|
|
// - RES low voltage time Above 50us
|
|
|
|
|
|
|
|
|
|
// The timings used in various ports do not always follow the guidelines above.
|
|
|
|
|
// In general, a zero bit is about 300ns high, 900ns low.
|
|
|
|
|
// A one bit is about 700ns high, 500ns low.
|
|
|
|
|
// But the ports vary based on implementation considerations; the proof is in the testing.
|
|
|
|
|
// https://adafru.it/5225 is more sensitive to timing and should be included in testing.
|
|
|
|
|
|
2022-01-29 13:13:00 -05:00
|
|
|
|
STATIC void check_for_deinit(digitalio_digitalinout_obj_t *self) {
|
|
|
|
|
if (common_hal_digitalio_digitalinout_deinited(self)) {
|
|
|
|
|
raise_deinited_error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 20:15:28 -04:00
|
|
|
|
//| """Low-level neopixel implementation
|
2016-11-03 18:50:59 -04:00
|
|
|
|
//|
|
|
|
|
|
//| The `neopixel_write` module contains a helper method to write out bytes in
|
2017-02-19 11:22:42 -05:00
|
|
|
|
//| the 800khz neopixel protocol.
|
2017-03-07 06:58:57 -05:00
|
|
|
|
//|
|
2017-07-12 14:32:46 -04:00
|
|
|
|
//| For example, to turn off a single neopixel (like the status pixel on Express
|
|
|
|
|
//| boards.)
|
|
|
|
|
//|
|
|
|
|
|
//| .. code-block:: python
|
|
|
|
|
//|
|
|
|
|
|
//| import board
|
|
|
|
|
//| import neopixel_write
|
|
|
|
|
//| import digitalio
|
|
|
|
|
//|
|
|
|
|
|
//| pin = digitalio.DigitalInOut(board.NEOPIXEL)
|
2017-07-12 17:59:26 -04:00
|
|
|
|
//| pin.direction = digitalio.Direction.OUTPUT
|
2017-07-12 14:32:46 -04:00
|
|
|
|
//| pixel_off = bytearray([0, 0, 0])
|
2022-05-04 14:20:48 -04:00
|
|
|
|
//| neopixel_write.neopixel_write(pin, pixel_off)
|
|
|
|
|
//|
|
|
|
|
|
//| .. note::
|
|
|
|
|
//|
|
2022-05-13 14:00:09 -04:00
|
|
|
|
//| This module is typically not used by user level code.
|
2022-05-04 14:20:48 -04:00
|
|
|
|
//|
|
2022-05-13 11:51:30 -04:00
|
|
|
|
//| For more information on actually using NeoPixels, refer to the `CircuitPython
|
|
|
|
|
//| Essentials Learn guide <https://learn.adafruit.com/circuitpython-essentials/circuitpython-neopixel>`_
|
2022-05-04 14:20:48 -04:00
|
|
|
|
//|
|
2022-05-13 11:51:30 -04:00
|
|
|
|
//| For a much more thorough guide about using NeoPixels, refer to the `Adafruit NeoPixel Überguide
|
|
|
|
|
//| <https://learn.adafruit.com/adafruit-neopixel-uberguide>`_.
|
2022-05-04 14:20:48 -04:00
|
|
|
|
//|
|
|
|
|
|
//| """
|
2017-07-12 14:32:46 -04:00
|
|
|
|
//|
|
2020-07-25 04:58:37 -04:00
|
|
|
|
//| def neopixel_write(digitalinout: digitalio.DigitalInOut, buf: ReadableBuffer) -> None:
|
2020-07-23 14:22:41 -04:00
|
|
|
|
//| """Write buf out on the given DigitalInOut.
|
2016-11-03 18:50:59 -04:00
|
|
|
|
//|
|
2020-08-03 00:35:43 -04:00
|
|
|
|
//| :param ~digitalio.DigitalInOut digitalinout: the DigitalInOut to output with
|
2023-02-01 03:08:41 -05:00
|
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer buf: The bytes to clock out. No assumption is made about color order
|
|
|
|
|
//| """
|
2020-07-23 14:22:41 -04:00
|
|
|
|
//| ...
|
2022-09-29 20:22:32 -04:00
|
|
|
|
//|
|
2017-02-19 11:22:42 -05:00
|
|
|
|
STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf) {
|
2023-01-10 13:39:10 -05:00
|
|
|
|
const digitalio_digitalinout_obj_t *digitalinout =
|
|
|
|
|
mp_arg_validate_type(digitalinout_obj, &digitalio_digitalinout_type, MP_QSTR_digitalinout);
|
2022-01-29 13:13:00 -05:00
|
|
|
|
|
|
|
|
|
// Check to see if the NeoPixel has been deinited before writing to it.
|
|
|
|
|
check_for_deinit(digitalinout_obj);
|
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
|
|
|
|
// Call platform's neopixel write function with provided buffer and options.
|
2021-03-15 09:57:36 -04:00
|
|
|
|
common_hal_neopixel_write(digitalinout, (uint8_t *)bufinfo.buf, bufinfo.len);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
return mp_const_none;
|
|
|
|
|
}
|
2017-02-19 11:22:42 -05:00
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(neopixel_write_neopixel_write_obj, neopixel_write_neopixel_write_);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t neopixel_write_module_globals_table[] = {
|
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write) },
|
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write), (mp_obj_t)&neopixel_write_neopixel_write_obj },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(neopixel_write_module_globals, neopixel_write_module_globals_table);
|
|
|
|
|
|
|
|
|
|
const mp_obj_module_t neopixel_write_module = {
|
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
|
.globals = (mp_obj_dict_t *)&neopixel_write_module_globals,
|
2016-11-03 18:50:59 -04:00
|
|
|
|
};
|
Convert more modules to use MP_REGISTER_MODULE
Convert neopixel_write, onewireio, ps2io, pulseio, pwmio, rainbowio, random, rgbmatrix, rotaryio, rtc, sdcardio, sharpdisplay, _stage, storage, struct, supervisor, synthio, touchio, traceback, usb_cdc, usb_hid, usb_midi, and vectorio modules to use MP_REGISTER_MODULE.
Related to #5183.
2021-08-30 22:29:51 -04:00
|
|
|
|
|
|
|
|
|
MP_REGISTER_MODULE(MP_QSTR_neopixel_write, neopixel_write_module, CIRCUITPY_NEOPIXEL_WRITE);
|