/* * This file is part of Espruino, a JavaScript interpreter for Microcontrollers * * Copyright (C) 2013 Gordon Williams * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * ---------------------------------------------------------------------------- * This file is designed to be parsed during the build process * * Contains JavaScript interface for Neopixel/WS281x/APA10x devices * ---------------------------------------------------------------------------- */ #ifdef NRF52 #include "i2s_ws2812b_drive.h" #endif #ifdef ESP8266 #include #include #endif #ifdef ESP32 #include "esp32_neopixel.h" #endif #ifdef WIO_LTE #include "stm32_ws2812b_driver.h" #endif #include #include "jsvariterator.h" #include "jspininfo.h" #include "jshardware.h" #include "jsspi.h" /** Send the given RGB pixel data out to neopixel/WS2811/APA104/etc */ bool neopixelWrite(Pin pin, unsigned char *rgbData, size_t rgbSize); /*JSON{ "type" : "library", "class" : "neopixel" } This library allows you to write to Neopixel/WS281x/APA10x LED strips These use a high speed single-wire protocol which needs platform-specific implementation on some devices - hence this library to simplify things. */ /*JSON{ "type" : "staticmethod", "class" : "neopixel", "name" : "write", "generate" : "jswrap_neopixel_write", "params" : [ ["pin", "pin", "The Pin the LEDs are connected to"], ["data","JsVar","The data to write to the LED strip"] ] } Write to a strip of NeoPixel/WS281x/APA104/APA106/SK6812-style LEDs attached to the given pin. ``` // set just one pixel, red, green, blue require("neopixel").write(B15, [255,0,0]); ``` ``` // Produce an animated rainbow over 25 LEDs var rgb = new Uint8ClampedArray(25*3); var pos = 0; function getPattern() { pos++; for (var i=0;i>= 1)) { // Next bit/byte? if (p >= end) break; // at end, we're done pix = *p++; mask = 0x80; pinMask = _BV(pin); } while (_getCycleCount()-start < tLow) ; // busy-wait } while (_getCycleCount()-start < tLow) ; // wait for last bit ets_intr_unlock(); #else // this is the code without preloading the first bit uint32_t pinMask = _BV(pin); // bit mask for GPIO pin to write to reg uint8_t *p = (uint8_t *)rgbData; // pointer to walk through pixel array uint8_t *end = p + rgbSize; // pointer to end of array uint8_t pix = *p++; // current byte being shifted out uint8_t mask = 0x80; // mask for current bit uint32_t start; // start time of bit // iterate through all bits while(1) { uint32_t t; if (pix & mask) t = 56; // one bit, high typ 800ns (56 cyc = 700ns) else t = 14; // zero bit, high typ 300ns (14 cycl = 175ns) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinMask); // Set high start = _getCycleCount(); // get start time of this bit while (_getCycleCount()-start < t) ; // busy-wait GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinMask); // Set low if (!(mask >>= 1)) { // Next bit/byte? if (p >= end) break; // at end, we're done pix = *p++; mask = 0x80; } while (_getCycleCount()-start < 100) ; // busy-wait, 100 cyc = 1.25us } while (_getCycleCount()-start < 100) ; // Wait for last bit #endif #undef _BV return true; } #elif defined(ESP32) bool neopixelWrite(Pin pin, unsigned char *rgbData, size_t rgbSize){ return esp32_neopixelWrite(pin,rgbData, rgbSize); } #else bool neopixelWrite(Pin pin, unsigned char *rgbData, size_t rgbSize) { jsExceptionHere(JSET_ERROR, "Neopixel writing not implemented"); return false; } #endif