From 67ad313af229d8b5f660cb56fc4a900475d574e8 Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Tue, 2 Mar 2021 17:57:20 +0100 Subject: [PATCH 1/3] Initial NeoPixel Support --- lib/tinyNeoPixel_Static/COPYING | 165 +++ .../tinyNeoPixel_Static.cpp | 1151 +++++++++++++++++ lib/tinyNeoPixel_Static/tinyNeoPixel_Static.h | 327 +++++ platformio.ini | 4 +- src/main.cpp | 50 +- 5 files changed, 1693 insertions(+), 4 deletions(-) create mode 100644 lib/tinyNeoPixel_Static/COPYING create mode 100644 lib/tinyNeoPixel_Static/tinyNeoPixel_Static.cpp create mode 100644 lib/tinyNeoPixel_Static/tinyNeoPixel_Static.h diff --git a/lib/tinyNeoPixel_Static/COPYING b/lib/tinyNeoPixel_Static/COPYING new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/lib/tinyNeoPixel_Static/COPYING @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/lib/tinyNeoPixel_Static/tinyNeoPixel_Static.cpp b/lib/tinyNeoPixel_Static/tinyNeoPixel_Static.cpp new file mode 100644 index 0000000..d87ed7d --- /dev/null +++ b/lib/tinyNeoPixel_Static/tinyNeoPixel_Static.cpp @@ -0,0 +1,1151 @@ +/*------------------------------------------------------------------------- + Arduino library to control a wide variety of WS2811- and WS2812-based RGB + LED devices such as Adafruit FLORA RGB Smart Pixels and NeoPixel strips. + + Currently handles 800 KHz bitstreams on 8, 10, 12, 16, and 20 MHz ATtiny + MCUs with ATTinyCore 1.30+, 8, 10, 12, 16, 20, 24, 28, and 32 MHz AVRxt + tinyAVR 0/1/2-series parts with megaTinyCore 1.0.3+ and those speeds + plus the ridiculously overclocked 36, 40, 44, and 48 MHz speeds with + AVR Dx-series parts. Note that the highest speeds have not been tested + and it would be surprising if the parts could be pushed that far. + + Like the Adafruit original version, it supports LEDs wired + for various color orders. 400 kHz support was never included. + Nobody has ever asked about it, nor have I seen any 400 kHz LEDs for sale. + Ever. + + Written by Phil Burgess / Paint Your Dragon for Adafruit Industries, + contributions by PJRC, Michael Miller and other members of the open + source community. + + Extensive porting to additional parts and different clock speeds by + Spence Konde. + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing products + from Adafruit! + + Same goes for Spence too! + + ------------------------------------------------------------------------- + This file is part of the tinyNeoPixel library derived from + Adafruit_NeoPixel. + + NeoPixel is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + NeoPixel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with NeoPixel. If not, see + . + -------------------------------------------------------------------------*/ + +#include "tinyNeoPixel_Static.h" + +// Constructor when length, pin and type are known at compile-time: +tinyNeoPixel::tinyNeoPixel(uint16_t n, uint8_t p, neoPixelType t, uint8_t *pxl) : + brightness(0), pixels(pxl), endTime(0) { + //boolean oldThreeBytesPerPixel = (wOffset == rOffset); // false if RGBW + wOffset = (t >> 6) & 0b11; // See notes in header file + rOffset = (t >> 4) & 0b11; // regarding R/G/B/W offsets + gOffset = (t >> 2) & 0b11; + bOffset = t & 0b11; + numBytes = n * ((wOffset == rOffset) ? 3 : 4); + numLEDs = n; + pin = p; + port = portOutputRegister(digitalPinToPort(p)); + pinMask = digitalPinToBitMask(p); +} + + + +tinyNeoPixel::~tinyNeoPixel() { + //if (pixels) free(pixels); + //if (pin >= 0) pinMode(pin, INPUT); +} + +// *INDENT-OFF* astyle don't like assembly +void tinyNeoPixel::show(void) { + + if ((!pixels) || pin >= NUM_DIGITAL_PINS) { + return; + } + + // Data latch = 50+ microsecond pause in the output stream. Rather than + // put a delay at the end of the function, the ending time is noted and + // the function will simply hold off (if needed) on issuing the + // subsequent round of data until the latch time has elapsed. This + // allows the mainline code to start generating the next frame of data + // rather than stalling for the latch. + while (!canShow()); + // endTime is a private member (rather than global var) so that multiple + // instances on different pins can be quickly issued in succession (each + // instance doesn't delay the next). + + // In order to make this code runtime-configurable to work with any pin, + // SBI/CBI instructions are eschewed in favor of full PORT writes via the + // OUT or ST instructions. It relies on two facts: that peripheral + // functions (such as PWM) take precedence on output pins, so our PORT- + // wide writes won't interfere, and that interrupts are globally disabled + // while data is being issued to the LEDs, so no other code will be + // accessing the PORT. The code takes an initial 'snapshot' of the PORT + // state, computes 'pin high' and 'pin low' values, and writes these back + // to the PORT register as needed. + + noInterrupts(); // Need 100% focus on instruction timing + + + // AVRxt MCUs -- tinyAVR 0/1/2, megaAVR 0, AVR Dx ---------------------- + // with extended maximum speeds to support vigorously overclocked + // Dx-series parts. This is by no means intended to imply that they will + // run at those speeds, only that - if they do - you can control WS2812s + // with them. + + volatile uint16_t + i = numBytes; // Loop counter + volatile uint8_t + *ptr = pixels, // Pointer to next byte + b = *ptr++, // Current byte value + hi, // PORT w/output bit set high + lo; // PORT w/output bit set low + + // Hand-tuned assembly code issues data to the LED drivers at a specific + // rate. There's separate code for different CPU speeds (8, 12, 16 MHz) + // for both the WS2811 (400 KHz) and WS2812 (800 KHz) drivers. The + // datastream timing for the LED drivers allows a little wiggle room each + // way (listed in the datasheets), so the conditions for compiling each + // case are set up for a range of frequencies rather than just the exact + // 8, 12 or 16 MHz values, permitting use with some close-but-not-spot-on + // devices (e.g. 16.5 MHz DigiSpark). The ranges were arrived at based + // on the datasheet figures and have not been extensively tested outside + // the canonical 8/12/16 MHz speeds; there's no guarantee these will work + // close to the extremes (or possibly they could be pushed further). + // Keep in mind only one CPU speed case actually gets compiled; the + // resulting program isn't as massive as it might look from source here. + + // 8 MHz(ish) AVRxt --------------------------------------------------------- + #if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL) + + volatile uint8_t n1, n2 = 0; // First, next bits out + + // We need to be able to write to the port register in one clock + // to meet timing constraints here. + + // 10 instruction clocks per bit: HHxxxxxLLL + // OUT instructions: ^ ^ ^ (T=0,2,7) + + hi = *port | pinMask; + lo = *port & ~pinMask; + n1 = lo; + if (b & 0x80) n1 = hi; + + // Dirty trick: RJMPs proceeding to the next instruction are used + // to delay two clock cycles in one instruction word (rather than + // using two NOPs). This was necessary in order to squeeze the + // loop down to exactly 64 words -- the maximum possible for a + // relative branch. + + asm volatile( + "headD:" "\n\t" // Clk Pseudocode + // Bit 7: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "st %a[port], %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 6" "\n\t" // 1-2 if (b & 0x40) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 6: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "st %a[port], %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 5" "\n\t" // 1-2 if (b & 0x20) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 5: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "st %a[port], %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 4" "\n\t" // 1-2 if (b & 0x10) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 4: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "st %a[port], %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 3" "\n\t" // 1-2 if (b & 0x08) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 3: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "st %a[port], %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 2" "\n\t" // 1-2 if (b & 0x04) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 2: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "st %a[port], %[n2]" "\n\t" // 1 PORT = n2 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 1" "\n\t" // 1-2 if (b & 0x02) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo + "rjmp .+0" "\n\t" // 2 nop nop + // Bit 1: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi + "mov %[n2] , %[lo]" "\n\t" // 1 n2 = lo + "st %a[port], %[n1]" "\n\t" // 1 PORT = n1 + "rjmp .+0" "\n\t" // 2 nop nop + "sbrc %[byte] , 0" "\n\t" // 1-2 if (b & 0x01) + "mov %[n2] , %[hi]" "\n\t" // 0-1 n2 = hi + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo + "sbiw %[count], 1" "\n\t" // 2 i-- (don't act on Z flag yet) + // Bit 0: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi + "mov %[n1] , %[lo]" "\n\t" // 1 n1 = lo + "st %a[port], %[n2]" "\n\t" // 1 PORT = n2 + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ + "sbrc %[byte] , 7" "\n\t" // 1-2 if (b & 0x80) + "mov %[n1] , %[hi]" "\n\t" // 0-1 n1 = hi + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo + "brne headD" "\n" // 2 while(i) (Z flag set above) + : [port] "+e" (port), + [byte] "+r" (b), + [n1] "+r" (n1), + [n2] "+r" (n2), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + + #elif (F_CPU >= 9500000UL) && (F_CPU <= 11100000UL) + /* + volatile uint8_t n1, n2 = 0; // First, next bits out + + */ + // 14 instruction clocks per bit: HHHHxxxxLLLLL + // ST instructions: ^ ^ ^ (T=0,4,7) + volatile uint8_t next; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + if (b & 0x80) { + next = hi; + } + + // Don't "optimize" the OUT calls into the bitTime subroutine; + // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs! + asm volatile( + "headD:" "\n\t" // (T = 0) + "st %a[port], %[hi]" "\n\t" // (T = 1) + "rcall bitTimeD" "\n\t" // Bit 7 (T = 14) + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 6 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 5 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 4 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 3 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 2 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 1 + // Bit 0: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "rjmp .+0" "\n\t" // 2 nop nop (T = 3) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 5) + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 6) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7) + "sbrc %[byte] , 7" "\n\t" // 1-2 if (b & 0x80) (T = 8) + "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 9) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 10) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 12) + "brne headD" "\n\t" // 2 if (i != 0) -> (next byte) + "rjmp doneD" "\n\t" + "bitTimeD:" "\n\t" // nop nop nop (T = 4) + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 5) + "mov %[next], %[lo]" "\n\t" // 1 next = lo (T = 6) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 7) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 0x80) (T = 8) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 9) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 10) + "ret" "\n\t" // 4 nop nop nop nop (T = 14) + "doneD:" "\n" + : [port] "+e" (port), + [byte] "+r" (b), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + + + +// 12 MHz(ish) AVRxt -------------------------------------------------------- +#elif (F_CPU >= 11100000UL) && (F_CPU <= 14300000UL) + + // In the 12 MHz case, an optimized 800 KHz datastream (no dead time + // between bytes) requires a PORT-specific loop similar to the 8 MHz + // code (but a little more relaxed in this case). + + // 15 instruction clocks per bit: HHHHxxxxxxLLLLL + // OUT instructions: ^ ^ ^ (T=0,4,10) + + volatile uint8_t next; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + if (b & 0x80) { + next = hi; + } + + // Don't "optimize" the OUT calls into the bitTime subroutine; + // we're exploiting the RCALL and RET as 3- and 4-cycle NOPs! + asm volatile( + "headD:" "\n\t" // (T = 0) + "st %a[port], %[hi]" "\n\t" // (T = 1) + "rcall bitTimeD" "\n\t" // Bit 7 (T = 15) + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 6 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 5 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 4 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 3 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 2 + "st %a[port], %[hi]" "\n\t" + "rcall bitTimeD" "\n\t" // Bit 1 + // Bit 0: + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "rjmp .+0" "\n\t" // 2 nop nop (T = 3) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 5) + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 6) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 7) + "sbrc %[byte] , 7" "\n\t" // 1-2 if (b & 0x80) (T = 8) + "mov %[next] , %[hi]" "\n\t" // 0-1 next = hi (T = 9) + "nop" "\n\t" // 1 (T = 10) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 11) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 13) + "brne headD" "\n\t" // 2 if (i != 0) -> (next byte) + "rjmp doneD" "\n\t" + "bitTimeD:" "\n\t" // nop nop nop (T = 4) + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 5) + "mov %[next], %[lo]" "\n\t" // 1 next = lo (T = 6) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 7) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 0x80) (T = 8) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 9) + "nop" "\n\t" // 1 (T = 10) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 11) + "ret" "\n\t" // 4 nop nop nop nop (T = 15) + "doneD:" "\n" + : [port] "+e" (port), + [byte] "+r" (b), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + + +// 16 MHz(ish) AVRxt ------------------------------------------------------ +#elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L) + + // WS2811 and WS2812 have different hi/lo duty cycles; this is + // similar but NOT an exact copy of the prior 400-on-8 code. + + // 20 inst. clocks per bit: HHHHHxxxxxxxxLLLLLLL + // ST instructions: ^ ^ ^ (T=0,5,13) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head20:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "nop" "\n\t" // 1 nop (T = 2) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 4) + "dec %[bit]" "\n\t" // 1 bit-- (T = 5) + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 6) + "nop" "\n\t" // 1 nop (T = 7) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 8) + "breq nextbyte20" "\n\t" // 1-2 if (bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 10) + "rjmp .+0" "\n\t" // 2 nop nop (T = 12) + "nop" "\n\t" // 1 nop (T = 13) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 14) + "rjmp .+0" "\n\t" // 2 nop nop (T = 16) + "rjmp .+0" "\n\t" // 2 nop nop (T = 18) + "rjmp head20" "\n\t" // 2 -> head20 (next bit out) (T=20) + "nextbyte20:" "\n\t" // (T = 10) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 11) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 13) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 14) + "rjmp .+0" "\n\t" // 2 nop nop (T = 16) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 18) + "brne head20" "\n" // 2 if (i != 0) -> (next byte) (T=20) + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + +// 20 MHz(ish) AVRxt ------------------------------------------------------ +#elif (F_CPU >= 19000000UL) && (F_CPU <= 22000000L) + + + // 25 inst. clocks per bit: HHHHHHHxxxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,7,15) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head20:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 3) + "dec %[bit]" "\n\t" // 1 bit-- (T = 4) + "nop" "\n\t" // 1 nop (T = 5) + "rjmp .+0" "\n\t" // 2 nop nop (T = 7) + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 8) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 9) + "breq nextbyte20" "\n\t" // 1-2 if (bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 11) + "rjmp .+0" "\n\t" // 2 nop nop (T = 13) + "rjmp .+0" "\n\t" // 2 nop nop (T = 15) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 16) + "nop" "\n\t" // 1 nop (T = 17) + "rjmp .+0" "\n\t" // 2 nop nop (T = 19) + "rjmp .+0" "\n\t" // 2 nop nop (T = 21) + "rjmp .+0" "\n\t" // 2 nop nop (T = 23) + "rjmp head20" "\n\t" // 2 -> head20 (next bit out) + "nextbyte20:" "\n\t" // (T = 11) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 12) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 14) + "nop" "\n\t" // 1 nop (T = 15) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 16) + "nop" "\n\t" // 1 nop (T = 17) + "rjmp .+0" "\n\t" // 2 nop nop (T = 19) + "rjmp .+0" "\n\t" // 2 nop nop (T = 21) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 23) + "brne head20" "\n" // 2 if (i != 0) -> (next byte) () + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + +// 24 (22~26) MHz AVRxt ------------------------------------------------------ +#elif (F_CPU >= 22000000UL) && (F_CPU <= 26000000L) + + + // 30 inst. clocks per bit: HHHHHHHxxxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,9,18) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + + asm volatile( + "head24:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 3) + "dec %[bit]" "\n\t" // 1 bit-- (T = 4) + "nop" "\n\t" // 1 nop (T = 5) + "rjmp .+0" "\n\t" // 2 nop nop (T = 7) + "rjmp .+0" "\n\t" // 2 nop nop (T = 9) + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 10) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 11) + "nop" "\n\t" // 1 nop (T = 12) + "rjmp .+0" "\n\t" // 2 nop nop (T = 14) + "rjmp .+0" "\n\t" // 2 nop nop (T = 16) + "breq nextbyte24" "\n\t" // 1-2 if (bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 18) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 19) + "rcall seconddelay24" "\n\t" // 2+4+3=9 (T = 28) + "rjmp head24" "\n\t" // 2 -> head20 (next bit out) + "seconddelay24:" "\n\t" // + "nop" "\n\t" // 1 + "rjmp .+0" "\n\t" // 2 + "ret" "\n\t" // 4 + "nextbyte24:" "\n\t" // last bit of a byte (T = 18) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 19) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 20) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 22) + "rjmp .+0" "\n\t" // 2 nop nop (T = 24) + "rjmp .+0" "\n\t" // 2 nop nop (T = 26) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 28) + "brne head24" "\n" // 2 if (i != 0) -> (next byte) () + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + + + + +// 28 (26~30) MHz AVRxt ------------------------------------------------------ +#elif (F_CPU >= 26000000UL) && (F_CPU <= 30000000L) + + + // 35 inst. clocks per bit: HHHHHHHxxxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,10,21) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head28:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 3) + "dec %[bit]" "\n\t" // 1 bit-- (T = 4) + "rcall zerothdelay32" "\n\t" // 2+4=6 + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 11) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 12) + "rcall firstdelay28" "\n\t" // 2+4 = 7 (T = 19) + "breq nextbyte28" "\n\t" // 1-2 if (bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 21) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 22) + "rcall seconddelay28" "\n\t" // 2+4+1+4=11 (T = 33) + "rjmp head28" "\n\t" // 2 -> head20 (next bit out) + "seconddelay28:" "\n\t" // + "rjmp .+0" "\n\t" // 2 + "rjmp .+0" "\n\t" // 2 + "firstdelay28:" "\n\t" // first delay + "nop" "\n\t" // 1 nop + "thirddelay28:" "\n\t" // third delay + "zerothdelay28:" "\n\t" + "ret" "\n\t" // 4 + "nextbyte28:" "\n\t" // last bit of a byte (T = 21) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 22) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 23) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 25) + "rcall thirddelay28" "\n\t" // 2+4 = 6 (T = 31) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 33) + "brne head28" "\n" // 2 if (i != 0) -> (next byte) () + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + + +// 32 (30~34) MHz AVRxt ------------------------------------------------------ +#elif (F_CPU > 30000000UL) && (F_CPU <= 34000000L) + + + // 40 inst. clocks per bit: HHHHHHHxxxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,11,24) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head32:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 3) + "dec %[bit]" "\n\t" // 1 bit-- (T = 4) + "rcall zerothdelay32" "\n\t" // 2+4+1=7 + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 12) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 13) + "rcall firstdelay32" "\n\t" // 2+4+1+2 = 9 (T = 22) + "breq nextbyte32" "\n\t" // 1-2 if (bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 24) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 25) + "rcall seconddelay32" "\n\t" // 2+4+3+2+3=13 (T = 38) + "rjmp head32" "\n\t" // 2 -> head20 (next bit out) + "seconddelay32:" "\n\t" // second delay 13 cycles + "rjmp .+0" "\n\t" // 2 + "rjmp .+0" "\n\t" // 2 + "firstdelay32:" "\n\t" // first delay 9 cycles + "nop" "\n\t" // 1 nop + "thirddelay32:" "\n\t" // third delay 8 cycles + "nop" "\n\t" // 1 nop + "zerothdelay32:" "\n\t" // zeroth delay 7 cycles + "nop" "\n\t" // 1 nop + "ret" "\n\t" // 4 + "nextbyte32:" "\n\t" // last bit of a byte (T = 24) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 25) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 26) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 28) + "rcall thirddelay32" "\n\t" // 2+4+1+1 = 8 (T = 36) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 38) + "brne head32" "\n" // 2 if (i != 0) -> (next byte) () + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + +// 36 (34~38) MHz AVRxt ------------------------------------------------------ +#elif (F_CPU > 3400000UL) && (F_CPU <= 38000000L) + + + // 45 inst. clocks per bit: HHHHHHHxxxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,12,27) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head36:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 3) + "dec %[bit]" "\n\t" // 1 bit-- (T = 4) + "rcall zerothdelay36" "\n\t" // 2+4+2=8 + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 13) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 14) + "rcall firstdelay36" "\n\t" // 2+4+3 = 11 (T = 25) + "breq nextbyte36" "\n\t" // 1-2 if (bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 27) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 28) + "rcall seconddelay36" "\n\t" // 2+4+3+2+2=15 (T = 43) + "rjmp head36" "\n\t" // 2 -> head20 (next bit out) + "seconddelay36:" "\n\t" // second delay 15 cycles + "rjmp .+0" "\n\t" // 2 + "rjmp .+0" "\n\t" // 2 + "firstdelay36:" "\n\t" // first delay 11 cycles + "nop" "\n\t" // 1 nop + "thirddelay36:" "\n\t" // third delay 10 cycles + "rjmp .+0" "\n\t" // 2 nop nop + "zerothdelay36:" "\n\t" // zeroth delay 8 cycles + "rjmp .+0" "\n\t" // 2 nop nop + "ret" "\n\t" // 4 + "nextbyte36:" "\n\t" // last bit of a byte (T = 27) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 28) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 29) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 31) + "rcall thirddelay36" "\n\t" // 2+4 = 10 (T = 41) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 43) + "brne head36" "\n" // 2 if (i != 0) -> (next byte) () + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + + +// 40 (38-44) MHz AVRxt ------------------------------------------------------ +#elif (F_CPU > 3800000UL) && (F_CPU <= 44000000L) + + + // 50 inst. clocks per bit: HHHHHHHxxxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,14,30) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + + asm volatile( + "head40:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 3) + "dec %[bit]" "\n\t" // 1 bit-- (T = 4) + "rcall zerothdelay40" "\n\t" // 2+4+4=10 + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 15) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 16) + "rcall firstdelay40" "\n\t" // 2+4+4+2 = 12 (T = 28) + "breq nextbyte40" "\n\t" // 1-2 if (bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 30) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 31) + "rcall seconddelay40" "\n\t" // 2+4+3+2+3=17 (T = 48) + "rjmp head40" "\n\t" // 2 -> head20 (next bit out) + "seconddelay40:" "\n\t" // second delay 17 cycles + "nop" "\n\t" // 1 nop + "rjmp .+0" "\n\t" // 2 + "rjmp .+0" "\n\t" // 2 + "thirddelay40:" "\n\t" // third delay 12 cycles + "firstdelay40:" "\n\t" // first delay 12 cycles + "rjmp .+0" "\n\t" // 2 nop nop + "zerothdelay40:" "\n\t" // zeroth delay 10 cycles + "rjmp .+0" "\n\t" // 2 nop nop + "rjmp .+0" "\n\t" // 2 nop nop + "ret" "\n\t" // 4 + "nextbyte40:" "\n\t" // last bit of a byte (T = 30) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 31) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 32) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 34) + "rcall thirddelay40" "\n\t" // 2+4+4+2 = 12 (T = 46) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 48) + "brne head40" "\n" // 2 if (i != 0) -> (next byte) () + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + +// 48 (44-50) MHz AVRxt ------------------------------------------------------ +#elif (F_CPU > 4400000UL) && (F_CPU <= 50000000L) + + + // 60 inst. clocks per bit: HHHHHHHxxxxxxxxLLLLLLLLLL + // ST instructions: ^ ^ ^ (T=0,16,35) + + volatile uint8_t next, bit; + + hi = *port | pinMask; + lo = *port & ~pinMask; + next = lo; + bit = 8; + asm volatile( + "head48:" "\n\t" // Clk Pseudocode (T = 0) + "st %a[port], %[hi]" "\n\t" // 1 PORT = hi (T = 1) + "sbrc %[byte], 7" "\n\t" // 1-2 if (b & 128) + "mov %[next], %[hi]" "\n\t" // 0-1 next = hi (T = 3) + "dec %[bit]" "\n\t" // 1 bit-- (T = 4) + "rcall zerothdelay48" "\n\t" // 2+4=13 + "st %a[port], %[next]" "\n\t" // 1 PORT = next (T = 17) + "mov %[next] , %[lo]" "\n\t" // 1 next = lo (T = 18) + "rcall firstdelay48" "\n\t" // 2+4+3 = 15 (T = 33) + "breq nextbyte48" "\n\t" // 1-2 if (bit == 0) (from dec above) + "rol %[byte]" "\n\t" // 1 b <<= 1 (T = 35) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 36) + "rcall seconddelay48" "\n\t" // 2+4+3+2+3=22 (T = 58) + "rjmp head48" "\n\t" // 2 -> head20 (next bit out) + "seconddelay48:" "\n\t" // second delay 22 cycles + "rjmp .+0" "\n\t" // 2 + "rjmp .+0" "\n\t" // 2 + "nop" "\n\t" // 1 nop + "thirddelay48:" "\n\t" // third delay 17 cycles + "rjmp .+0" "\n\t" // 2 + "firstdelay48:" "\n\t" // first delay 15 cycles + "rjmp .+0" "\n\t" // 2 nop nop + "zerothdelay48:" "\n\t" // zeroth delay 13 cycles + "nop" "\n\t" // 1 nop + "rcall emptydelay48" "\n\t" // 2+4 + "ret" "\n\t" // 4 + "emptydelay48:" "\n\t" // immediately returns: 2+4 = 6 cycles, for 2 words! + "ret" "\n\t" // 4 + "nextbyte48:" "\n\t" // last bit of a byte (T = 35) + "st %a[port], %[lo]" "\n\t" // 1 PORT = lo (T = 36) + "ldi %[bit] , 8" "\n\t" // 1 bit = 8 (T = 37) + "ld %[byte] , %a[ptr]+" "\n\t" // 2 b = *ptr++ (T = 39) + "rcall thirddelay48" "\n\t" // 2+4 = 17 (T = 56) + "sbiw %[count], 1" "\n\t" // 2 i-- (T = 58) + "brne head48" "\n" // 2 if (i != 0) -> (next byte) () + : [port] "+e" (port), + [byte] "+r" (b), + [bit] "+r" (bit), + [next] "+r" (next), + [count] "+w" (i) + : [ptr] "e" (ptr), + [hi] "r" (hi), + [lo] "r" (lo)); + +#else + #error "CPU SPEED NOT SUPPORTED" +#endif + + // END AVR ---------------------------------------------------------------- + + interrupts(); + #if (!defined(DISABLEMILLIS) && !defined(MILLIS_USE_TIMERRTC) && !defined(MILLIS_USE_TIMERRTC_XTAL) && !defined(MILLIS_USE_TIMERRTC_XOSC)) + endTime = micros(); + // Save EOD time for latch on next call + #else + #warning "micros is not available based on timer settings. You must ensure at least 50us between calls to show() or the pixels will never latch" + #endif +} + +// Set the output pin number +void tinyNeoPixel::setPin(uint8_t p) { + pin = p; + port = portOutputRegister(digitalPinToPort(p)); + pinMask = digitalPinToBitMask(p); +} + +// Set pixel color from separate R,G,B components: +void tinyNeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) { + if (n < numLEDs) { + if (brightness) { // See notes in setBrightness() + r = (r * brightness) >> 8; + g = (g * brightness) >> 8; + b = (b * brightness) >> 8; + } + uint8_t *p; + if (wOffset == rOffset) { // Is an RGB-type strip + p = &pixels[n * 3]; // 3 bytes per pixel + } else { // Is a WRGB-type strip + p = &pixels[n * 4]; // 4 bytes per pixel + p[wOffset] = 0; // But only R,G,B passed -- set W to 0 + } + p[rOffset] = r; // R,G,B always stored + p[gOffset] = g; + p[bOffset] = b; + } +} + +void tinyNeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w) { + if (n < numLEDs) { + if (brightness) { // See notes in setBrightness() + r = (r * brightness) >> 8; + g = (g * brightness) >> 8; + b = (b * brightness) >> 8; + w = (w * brightness) >> 8; + } + uint8_t *p; + if (wOffset == rOffset) { // Is an RGB-type strip + p = &pixels[n * 3]; // 3 bytes per pixel (ignore W) + } else { // Is a WRGB-type strip + p = &pixels[n * 4]; // 4 bytes per pixel + p[wOffset] = w; // Store W + } + p[rOffset] = r; // Store R,G,B + p[gOffset] = g; + p[bOffset] = b; + } +} + +// Set pixel color from 'packed' 32-bit RGB color: +void tinyNeoPixel::setPixelColor(uint16_t n, uint32_t c) { + if (n < numLEDs) { + uint8_t *p, + r = (uint8_t)(c >> 16), + g = (uint8_t)(c >> 8), + b = (uint8_t)c; + if (brightness) { // See notes in setBrightness() + r = (r * brightness) >> 8; + g = (g * brightness) >> 8; + b = (b * brightness) >> 8; + } + if (wOffset == rOffset) { + p = &pixels[n * 3]; + } else { + p = &pixels[n * 4]; + uint8_t w = (uint8_t)(c >> 24); + p[wOffset] = brightness ? ((w * brightness) >> 8) : w; + } + p[rOffset] = r; + p[gOffset] = g; + p[bOffset] = b; + } +} + +/*! + @brief Fill all or part of the NeoPixel strip with a color. + @param c 32-bit color value. Most significant byte is white (for + RGBW pixels) or ignored (for RGB pixels), next is red, + then green, and least significant byte is blue. If all + arguments are unspecified, this will be 0 (off). + @param first Index of first pixel to fill, starting from 0. Must be + in-bounds, no clipping is performed. 0 if unspecified. + @param count Number of pixels to fill, as a positive value. Passing + 0 or leaving unspecified will fill to end of strip. +*/ +void tinyNeoPixel::fill(uint32_t c, uint16_t first, uint16_t count) { + uint16_t i, end; + + if (first >= numLEDs) { + return; // If first LED is past end of strip, nothing to do + } + + // Calculate the index ONE AFTER the last pixel to fill + if (count == 0) { + // Fill to end of strip + end = numLEDs; + } else { + // Ensure that the loop won't go past the last pixel + end = first + count; + if (end > numLEDs) end = numLEDs; + } + + for (i = first; i < end; i++) { + this->setPixelColor(i, c); + } +} + + +/*! + @brief Convert hue, saturation and value into a packed 32-bit RGB color + that can be passed to setPixelColor() or other RGB-compatible + functions. + @param hue An unsigned 16-bit value, 0 to 65535, representing one full + loop of the color wheel, which allows 16-bit hues to "roll + over" while still doing the expected thing (and allowing + more precision than the wheel() function that was common to + prior NeoPixel examples). + @param sat Saturation, 8-bit value, 0 (min or pure grayscale) to 255 + (max or pure hue). Default of 255 if unspecified. + @param val Value (brightness), 8-bit value, 0 (min / black / off) to + 255 (max or full brightness). Default of 255 if unspecified. + @return Packed 32-bit RGB with the most significant byte set to 0 -- the + white element of WRGB pixels is NOT utilized. Result is linearly + but not perceptually correct, so you may want to pass the result + through the gamma32() function (or your own gamma-correction + operation) else colors may appear washed out. This is not done + automatically by this function because coders may desire a more + refined gamma-correction function than the simplified + one-size-fits-all operation of gamma32(). Diffusing the LEDs also + really seems to help when using low-saturation colors. +*/ +uint32_t tinyNeoPixel::ColorHSV(uint16_t hue, uint8_t sat, uint8_t val) { + + uint8_t r, g, b; + + // Remap 0-65535 to 0-1529. Pure red is CENTERED on the 64K rollover; + // 0 is not the start of pure red, but the midpoint...a few values above + // zero and a few below 65536 all yield pure red (similarly, 32768 is the + // midpoint, not start, of pure cyan). The 8-bit RGB hexcone (256 values + // each for red, green, blue) really only allows for 1530 distinct hues + // (not 1536, more on that below), but the full unsigned 16-bit type was + // chosen for hue so that one's code can easily handle a contiguous color + // wheel by allowing hue to roll over in either direction. + hue = (hue * 1530L + 32768) / 65536; + // Because red is centered on the rollover point (the +32768 above, + // essentially a fixed-point +0.5), the above actually yields 0 to 1530, + // where 0 and 1530 would yield the same thing. Rather than apply a + // costly modulo operator, 1530 is handled as a special case below. + + // So you'd think that the color "hexcone" (the thing that ramps from + // pure red, to pure yellow, to pure green and so forth back to red, + // yielding six slices), and with each color component having 256 + // possible values (0-255), might have 1536 possible items (6*256), + // but in reality there's 1530. This is because the last element in + // each 256-element slice is equal to the first element of the next + // slice, and keeping those in there this would create small + // discontinuities in the color wheel. So the last element of each + // slice is dropped...we regard only elements 0-254, with item 255 + // being picked up as element 0 of the next slice. Like this: + // Red to not-quite-pure-yellow is: 255, 0, 0 to 255, 254, 0 + // Pure yellow to not-quite-pure-green is: 255, 255, 0 to 1, 255, 0 + // Pure green to not-quite-pure-cyan is: 0, 255, 0 to 0, 255, 254 + // and so forth. Hence, 1530 distinct hues (0 to 1529), and hence why + // the constants below are not the multiples of 256 you might expect. + + // Convert hue to R,G,B (nested ifs faster than divide+mod+switch): + if (hue < 510) { // Red to Green-1 + b = 0; + if (hue < 255) { // Red to Yellow-1 + r = 255; + g = hue; // g = 0 to 254 + } else { // Yellow to Green-1 + r = 510 - hue; // r = 255 to 1 + g = 255; + } + } else if (hue < 1020) { // Green to Blue-1 + r = 0; + if (hue < 765) { // Green to Cyan-1 + g = 255; + b = hue - 510; // b = 0 to 254 + } else { // Cyan to Blue-1 + g = 1020 - hue; // g = 255 to 1 + b = 255; + } + } else if (hue < 1530) { // Blue to Red-1 + g = 0; + if (hue < 1275) { // Blue to Magenta-1 + r = hue - 1020; // r = 0 to 254 + b = 255; + } else { // Magenta to Red-1 + r = 255; + b = 1530 - hue; // b = 255 to 1 + } + } else { // Last 0.5 Red (quicker than % operator) + r = 255; + g = b = 0; + } + + // Apply saturation and value to R,G,B, pack into 32-bit result: + uint32_t v1 = 1 + val; // 1 to 256; allows >>8 instead of /255 + uint16_t s1 = 1 + sat; // 1 to 256; same reason + uint8_t s2 = 255 - sat; // 255 to 0 + return ((((((r * s1) >> 8) + s2) * v1) & 0xff00) << 8) | + (((((g * s1) >> 8) + s2) * v1) & 0xff00) | + (((((b * s1) >> 8) + s2) * v1) >> 8); +} + + +// Query color from previously-set pixel (returns packed 32-bit RGB value) +uint32_t tinyNeoPixel::getPixelColor(uint16_t n) const { + if (n >= numLEDs) { + return 0; // Out of bounds, return no color. + } + + uint8_t *p; + + if (wOffset == rOffset) { // Is RGB-type device + p = &pixels[n * 3]; + if (brightness) { + // Stored color was decimated by setBrightness(). Returned value + // attempts to scale back to an approximation of the original 24-bit + // value used when setting the pixel color, but there will always be + // some error -- those bits are simply gone. Issue is most + // pronounced at low brightness levels. + return (((uint32_t)(p[rOffset] << 8) / brightness) << 16) | + (((uint32_t)(p[gOffset] << 8) / brightness) << 8) | + ((uint32_t)(p[bOffset] << 8) / brightness); + } else { + // No brightness adjustment has been made -- return 'raw' color + return ((uint32_t)p[rOffset] << 16) | + ((uint32_t)p[gOffset] << 8) | + (uint32_t)p[bOffset]; + } + } else { // Is RGBW-type device + p = &pixels[n * 4]; + if (brightness) { // Return scaled color + return (((uint32_t)(p[wOffset] << 8) / brightness) << 24) | + (((uint32_t)(p[rOffset] << 8) / brightness) << 16) | + (((uint32_t)(p[gOffset] << 8) / brightness) << 8) | + ((uint32_t)(p[bOffset] << 8) / brightness); + } else { // Return raw color + return ((uint32_t)p[wOffset] << 24) | + ((uint32_t)p[rOffset] << 16) | + ((uint32_t)p[gOffset] << 8) | + (uint32_t)p[bOffset]; + } + } +} + +// Returns pointer to pixels[] array. Pixel data is stored in device- +// native format and is not translated here. Application will need to be +// aware of specific pixel data format and handle colors appropriately. +uint8_t *tinyNeoPixel::getPixels(void) const { + return pixels; +} + +uint16_t tinyNeoPixel::numPixels(void) const { + return numLEDs; +} + +// Adjust output brightness; 0=darkest (off), 255=brightest. This does +// NOT immediately affect what's currently displayed on the LEDs. The +// next call to show() will refresh the LEDs at this level. However, +// this process is potentially "lossy," especially when increasing +// brightness. The tight timing in the WS2811/WS2812 code means there +// aren't enough free cycles to perform this scaling on the fly as data +// is issued. So we make a pass through the existing color data in RAM +// and scale it (subsequent graphics commands also work at this +// brightness level). If there's a significant step up in brightness, +// the limited number of steps (quantization) in the old data will be +// quite visible in the re-scaled version. For a non-destructive +// change, you'll need to re-render the full strip data. +void tinyNeoPixel::setBrightness(uint8_t b) { + // Stored brightness value is different than what's passed. + // This simplifies the actual scaling math later, allowing a fast + // 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t, + // adding 1 here may (intentionally) roll over...so 0 = max brightness + // (color values are interpreted literally; no scaling), 1 = min + // brightness (off), 255 = just below max brightness. + uint8_t newBrightness = b + 1; + if (newBrightness != brightness) { // Compare against prior value + // Brightness has changed -- re-scale existing data in RAM + uint8_t c, + *ptr = pixels, + oldBrightness = brightness - 1; // De-wrap old brightness value + uint16_t scale; + if (oldBrightness == 0) { + scale = 0; // Avoid /0 + } else if (b == 255) { + scale = 65535 / oldBrightness; + } else { + scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness; + } + for (uint16_t i = 0; i < numBytes; i++) { + c = *ptr; + *ptr++ = (c * scale) >> 8; + } + brightness = newBrightness; + } +} + +//Return the brightness value +uint8_t tinyNeoPixel::getBrightness(void) const { + return brightness - 1; +} + +void tinyNeoPixel::clear() { + memset(pixels, 0, numBytes); +} + +// A 32-bit variant of gamma8() that applies the same function +// to all components of a packed RGB or WRGB value. +uint32_t tinyNeoPixel::gamma32(uint32_t x) { + uint8_t *y = (uint8_t *)&x; + // All four bytes of a 32-bit value are filtered even if RGB (not WRGB), + // to avoid a bunch of shifting and masking that would be necessary for + // properly handling different endianisms (and each byte is a fairly + // trivial operation, so it might not even be wasting cycles vs a check + // and branch for the RGB case). In theory this might cause trouble *if* + // someone's storing information in the unused most significant byte + // of an RGB value, but this seems exceedingly rare and if it's + // encountered in reality they can mask values going in or coming out. + for (uint8_t i=0; i<4; i++) y[i] = gamma8(y[i]); + return x; // Packed 32-bit return +} +// *INDENT-ON* diff --git a/lib/tinyNeoPixel_Static/tinyNeoPixel_Static.h b/lib/tinyNeoPixel_Static/tinyNeoPixel_Static.h new file mode 100644 index 0000000..a7fddba --- /dev/null +++ b/lib/tinyNeoPixel_Static/tinyNeoPixel_Static.h @@ -0,0 +1,327 @@ +/*-------------------------------------------------------------------- + This file is part of the tinyNeoPixel library, derived from + Adafruit_NeoPixel. + + NeoPixel is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + NeoPixel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with NeoPixel. If not, see + . + --------------------------------------------------------------------*/ +// *INDENT-OFF* astyle hates this file +// *PAD-OFF* and destroys the lookup tables! + +#ifndef TINYNEOPIXEL_H +#define TINYNEOPIXEL_H + +#include + +#if (__AVR_ARCH__ < 100) + #error "This version of the library only supports AVRxt parts (tinyAVR 0/1/2-series, megaAVR 0-series and the AVR DA/DB/DD parts. For tinyNeoPixel, for classic AVR, get from ATTinyCore package" +#endif + +// The order of primary colors in the NeoPixel data stream can vary +// among device types, manufacturers and even different revisions of +// the same item. The third parameter to the Adafruit_NeoPixel +// constructor encodes the per-pixel byte offsets of the red, green +// and blue primaries (plus white, if present) in the data stream -- +// the following #defines provide an easier-to-use named version for +// each permutation. e.g. NEO_GRB indicates a NeoPixel-compatible +// device expecting three bytes per pixel, with the first byte +// containing the green value, second containing red and third +// containing blue. The in-memory representation of a chain of +// NeoPixels is the same as the data-stream order; no re-ordering of +// bytes is required when issuing data to the chain. + +// Bits 5,4 of this value are the offset (0-3) from the first byte of +// a pixel to the location of the red color byte. Bits 3,2 are the +// green offset and 1,0 are the blue offset. If it is an RGBW-type +// device (supporting a white primary in addition to R,G,B), bits 7,6 +// are the offset to the white byte...otherwise, bits 7,6 are set to +// the same value as 5,4 (red) to indicate an RGB (not RGBW) device. +// i.e. binary representation: +// 0bWWRRGGBB for RGBW devices +// 0bRRRRGGBB for RGB + +// RGB NeoPixel permutations; white and red offsets are always same +// Offset: W R G B +#define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2)) +#define NEO_RBG ((0 << 6) | (0 << 4) | (2 << 2) | (1)) +#define NEO_GRB ((1 << 6) | (1 << 4) | (0 << 2) | (2)) +#define NEO_GBR ((2 << 6) | (2 << 4) | (0 << 2) | (1)) +#define NEO_BRG ((1 << 6) | (1 << 4) | (2 << 2) | (0)) +#define NEO_BGR ((2 << 6) | (2 << 4) | (1 << 2) | (0)) + +// RGBW NeoPixel permutations; all 4 offsets are distinct +// Offset: W R G B +#define NEO_WRGB ((0 << 6) | (1 << 4) | (2 << 2) | (3)) +#define NEO_WRBG ((0 << 6) | (1 << 4) | (3 << 2) | (2)) +#define NEO_WGRB ((0 << 6) | (2 << 4) | (1 << 2) | (3)) +#define NEO_WGBR ((0 << 6) | (3 << 4) | (1 << 2) | (2)) +#define NEO_WBRG ((0 << 6) | (2 << 4) | (3 << 2) | (1)) +#define NEO_WBGR ((0 << 6) | (3 << 4) | (2 << 2) | (1)) + +#define NEO_RWGB ((1 << 6) | (0 << 4) | (2 << 2) | (3)) +#define NEO_RWBG ((1 << 6) | (0 << 4) | (3 << 2) | (2)) +#define NEO_RGWB ((2 << 6) | (0 << 4) | (1 << 2) | (3)) +#define NEO_RGBW ((3 << 6) | (0 << 4) | (1 << 2) | (2)) +#define NEO_RBWG ((2 << 6) | (0 << 4) | (3 << 2) | (1)) +#define NEO_RBGW ((3 << 6) | (0 << 4) | (2 << 2) | (1)) + +#define NEO_GWRB ((1 << 6) | (2 << 4) | (0 << 2) | (3)) +#define NEO_GWBR ((1 << 6) | (3 << 4) | (0 << 2) | (2)) +#define NEO_GRWB ((2 << 6) | (1 << 4) | (0 << 2) | (3)) +#define NEO_GRBW ((3 << 6) | (1 << 4) | (0 << 2) | (2)) +#define NEO_GBWR ((2 << 6) | (3 << 4) | (0 << 2) | (1)) +#define NEO_GBRW ((3 << 6) | (2 << 4) | (0 << 2) | (1)) + +#define NEO_BWRG ((1 << 6) | (2 << 4) | (3 << 2) | (0)) +#define NEO_BWGR ((1 << 6) | (3 << 4) | (2 << 2) | (0)) +#define NEO_BRWG ((2 << 6) | (1 << 4) | (3 << 2) | (0)) +#define NEO_BRGW ((3 << 6) | (1 << 4) | (2 << 2) | (0)) +#define NEO_BGWR ((2 << 6) | (3 << 4) | (1 << 2) | (0)) +#define NEO_BGRW ((3 << 6) | (2 << 4) | (1 << 2) | (0)) + +#define NEO_KHZ800 0x0000 ///< 800 KHz data transmission + +// 400 kHz neopixels are virtually absent from the market today +// They are not supported. + +// These two tables are declared outside the Adafruit_NeoPixel class +// because some boards may require oldschool compilers that don't +// handle the C++11 constexpr keyword. + +/* A pre-calculated 8-bit sine look-up table stored in flash for use +with the sine8() function. This is apparently of use in some animation +algorithms. If __AVR_ARCH__==103, then all of the flash is memory +mapped, and we can simply declare it const, access it like a +normal variable, and it won't be copied to RAM. + +AVRxt devices with too much flash for all of it to be mapped +which includes the AVR64Dx and AVR128Dx parts. DxCore defines a +.section for the area of PROGMEM that is mapped by default, and +a MAPPED_PROGMEM macro. A variable declared const MAPPED_PROGMEM can +be accessed normally, but will be stored in the flash and not copied to RAM. + +Finally, if neither of those are an option - it gets declared with PROGMEM + + + Copy & paste this snippet into a Python REPL to regenerate: +import math +for x in range(256): + print("{:3},".format(int((math.sin(x/128.0*math.pi)+1.0)*127.5+0.5))), + if x&15 == 15: print +*/ +#if (__AVR_ARCH__==103) + // All out flash is mapped - yay! + static const uint8_t _NeoPixelSineTable[256] = { +#elif defined(MAPPED_PROGMEM) + // Some of it is - but we can put stuff there - yay! + static const uint8_t MAPPED_PROGMEM _NeoPixelSineTable[256] = { +#else + // Back to progmem... + static const uint8_t PROGMEM _NeoPixelSineTable[256] = { +#endif + 128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173, + 176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215, + 218,220,222,224,226,228,230,232,234,235,237,238,240,241,243,244, + 245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255, + 255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246, + 245,244,243,241,240,238,237,235,234,232,230,228,226,224,222,220, + 218,215,213,211,208,206,203,201,198,196,193,190,188,185,182,179, + 176,173,170,167,165,162,158,155,152,149,146,143,140,137,134,131, + 128,124,121,118,115,112,109,106,103,100, 97, 93, 90, 88, 85, 82, + 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40, + 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, + 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, + 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, + 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76, + 79, 82, 85, 88, 90, 93, 97,100,103,106,109,112,115,118,121,124}; + +/* Similar to above, but for an 8-bit gamma-correction table. + Copy & paste this snippet into a Python REPL to regenerate: +import math +gamma=2.6 +for x in range(256): + print("{:3},".format(int(math.pow((x)/255.0,gamma)*255.0+0.5))), + if x&15 == 15: print +*/ +#if (__AVR_ARCH__==103) + // All our flash is mapped - yay! + static const uint8_t _NeoPixelGammaTable[256] = { +#elif defined(MAPPED_PROGMEM) + // Some of it is - but we can put stuff there - yay! + static const uint8_t MAPPED_PROGMEM _NeoPixelGammaTable[256] = { +#else + // Back to progmem... + static const uint8_t PROGMEM _NeoPixelGammaTable[256] = { +#endif + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, + 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, + 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, + 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, + 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29, + 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, + 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 75, + 76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96, + 97, 99,100,102,103,105,106,108,109,111,112,114,115,117,119,120, + 122,124,125,127,129,130,132,134,136,137,139,141,143,145,146,148, + 150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180, + 182,184,186,188,191,193,195,197,199,202,204,206,209,211,213,215, + 218,220,223,225,227,230,232,235,237,240,242,245,247,250,252,255}; + + +typedef uint8_t neoPixelType; + +class tinyNeoPixel { + + public: + + // Constructor: number of LEDs, pin number, LED type + tinyNeoPixel(uint16_t n, uint8_t p, neoPixelType t, uint8_t *pxl); + ~tinyNeoPixel(); + + void + show(void), + setPin(uint8_t p), + setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), + setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w), + setPixelColor(uint16_t n, uint32_t c), + fill(uint32_t c=0, uint16_t first=0, uint16_t count=0), + setBrightness(uint8_t b), + clear(); + uint8_t + *getPixels(void) const, + getBrightness(void) const; + uint16_t + numPixels(void) const; + uint32_t + getPixelColor(uint16_t n) const; + uint8_t getPin(void) { return pin; } + void begin(void) {return;} + /*! + @brief An 8-bit integer sine wave function, not directly compatible + with standard trigonometric units like radians or degrees. + @param x Input angle, 0-255; 256 would loop back to zero, completing + the circle (equivalent to 360 degrees or 2 pi radians). + One can therefore use an unsigned 8-bit variable and simply + add or subtract, allowing it to overflow/underflow and it + still does the expected contiguous thing. + @return Sine result, 0 to 255, or -128 to +127 if type-converted to + a signed int8_t, but you'll most likely want unsigned as this + output is often used for pixel brightness in animation effects. + */ + static uint8_t sine8(uint8_t x) { // 0-255 in, 0-255 out + #if (__AVR_ARCH__==103 || defined(MAPPED_PROGMEM)) + return _NeoPixelSineTable[x]; + #else // We had to put it in PROGMEM, and that's how we get it out + return pgm_read_byte(&_NeoPixelSineTable[x]); // 0-255 in, 0-255 out + #endif + } + + /*! + @brief An 8-bit gamma-correction function for basic pixel brightness + adjustment. Makes color transitions appear more perceptially + correct. + @param x Input brightness, 0 (minimum or off/black) to 255 (maximum). + @return Gamma-adjusted brightness, can then be passed to one of the + setPixelColor() functions. This uses a fixed gamma correction + exponent of 2.6, which seems reasonably okay for average + NeoPixels in average tasks. If you need finer control you'll + need to provide your own gamma-correction function instead. + */ + + static uint8_t gamma8(uint8_t x) { + #if (__AVR_ARCH__==103 || defined(MAPPED_PROGMEM)) + return _NeoPixelGammaTable[x]; + #else + return pgm_read_byte(&_NeoPixelGammaTable[x]); + #endif + } + /*! + @brief Convert separate red, green and blue values into a single + "packed" 32-bit RGB color. + @param r Red brightness, 0 to 255. + @param g Green brightness, 0 to 255. + @param b Blue brightness, 0 to 255. + @return 32-bit packed RGB value, which can then be assigned to a + variable for later use or passed to the setPixelColor() + function. Packed RGB format is predictable, regardless of + LED strand color order. + */ + static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) { + return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; + } + /*! + @brief Convert separate red, green, blue and white values into a + single "packed" 32-bit WRGB color. + @param r Red brightness, 0 to 255. + @param g Green brightness, 0 to 255. + @param b Blue brightness, 0 to 255. + @param w White brightness, 0 to 255. + @return 32-bit packed WRGB value, which can then be assigned to a + variable for later use or passed to the setPixelColor() + function. Packed WRGB format is predictable, regardless of + LED strand color order. + */ + static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { + return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; + } + static uint32_t ColorHSV(uint16_t hue, uint8_t sat=255, uint8_t val=255); + /*! + @brief A gamma-correction function for 32-bit packed RGB or WRGB + colors. Makes color transitions appear more perceptially + correct. + @param x 32-bit packed RGB or WRGB color. + @return Gamma-adjusted packed color, can then be passed in one of the + setPixelColor() functions. Like gamma8(), this uses a fixed + gamma correction exponent of 2.6, which seems reasonably okay + for average NeoPixels in average tasks. If you need finer + control you'll need to provide your own gamma-correction + function instead. + */ + static uint32_t gamma32(uint32_t x); + + #if (!defined(DISABLEMILLIS) && !defined(MILLIS_USE_TIMERRTC) && !defined(MILLIS_USE_TIMERRTC_XTAL) && !defined(MILLIS_USE_TIMERRTC_XOSC)) + inline bool canShow(void) { return (micros() - endTime) >= 50L; } + #else + inline bool canShow(void) {return 1;} //we don't have micros here; + #endif + + + private: + + uint16_t + numLEDs, // Number of RGB LEDs in strip + numBytes; // Size of 'pixels' buffer below (3 or 4 bytes/pixel) + int8_t + pin; // Output pin number (-1 if not yet set) + uint8_t + brightness, + *pixels, // Holds LED color values (3 or 4 bytes each) + rOffset, // Index of red byte within each 3- or 4-byte pixel + gOffset, // Index of green byte + bOffset, // Index of blue byte + wOffset; // Index of white byte (same as rOffset if no white) + uint32_t + endTime; // Latch timing reference + volatile uint8_t + *port; // Output PORT register + uint8_t + pinMask; // Output PORT bitmask + +}; + +#endif // TINYNEOPIXEL_H diff --git a/platformio.ini b/platformio.ini index 9bc3ed1..2ff1680 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,7 +14,7 @@ board = ATtiny3216 framework = arduino # Board Config -board_build.f_cpu = 5000000L +board_build.f_cpu = 8000000L board_hardware.oscillator = internal board_hardware.bod = disabled @@ -38,4 +38,4 @@ upload_flags = -c$UPLOAD_PROTOCOL lib_deps = - mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 + mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 339372d..83b186a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,14 @@ void blink(uint8_t num) { #define BLINK_LED(COUNT) #endif +// WS2812B +#include + +#define WS_NUM_PIXELS 2 + +byte pixels[WS_NUM_PIXELS * 3]; +tinyNeoPixel leds = tinyNeoPixel(WS_NUM_PIXELS, PIN_PC1, NEO_GRB, pixels); + #if defined HAS_NO_SENSOR struct lora_data { uint8_t bat; @@ -75,7 +83,7 @@ const lmic_pinmap lmic_pins = { // List of unused Pins - will be disabled for Power Saving #if defined DEBUG || defined HAS_SG112A || defined HAS_MHZ19C -const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0}; +const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC0}; #else const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB3, PIN_PB2, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0}; #endif @@ -109,12 +117,21 @@ void onEvent(ev_t ev) { case EV_JOINED: // Disable LinkCheck LMIC_setLinkCheckMode(0); - BLINK_LED(2); + leds.setPixelColor(1, leds.Color(0,127,0)); + leds.show(); + delay(1000); + leds.setPixelColor(1, leds.Color(0,0,0)); + leds.show(); DEBUG_PRINTLN("OTAA Join Succeeded"); break; case EV_TXCOMPLETE: // Check for Downlink DEBUG_PRINTLN("LoRa Packet Sent"); + leds.setPixelColor(1, leds.Color(0,127,0)); + leds.show(); + delay(100); + leds.setPixelColor(1, leds.Color(0,0,0)); + leds.show(); if ((int)LMIC.dataLen == 2) { // We got a Packet with the right size - lets assemble it into a uint16_t DEBUG_PRINTLN("Received Downlink") @@ -123,6 +140,11 @@ void onEvent(ev_t ev) { DEBUG_PRINTLN(tmpslp); sleep_time = tmpslp; EEPROM.put(ADDR_SLP, tmpslp); + leds.setPixelColor(1, leds.Color(0,0,127)); + leds.show(); + delay(250); + leds.setPixelColor(1, leds.Color(0,0,0)); + leds.show(); } // Got to sleep for specified Time @@ -169,6 +191,25 @@ void do_send(osjob_t* j) { // Get Sensor Readings Into Data Paket #ifndef HAS_NO_SENSOR sensor.getSensorData(data); + + // ppm_level + // 0 -> < 1000 ppm green + // 1 -> < 1800 ppm yellow + // 2 -> > 1000 ppm red + + if (data.ppm > 0 && data.ppm <= 1000) { + leds.setPixelColor(0, leds.Color(0,127,0)); + leds.show(); + } else if (data.ppm > 1000 && data.ppm <= 1800) { + leds.setPixelColor(0, leds.Color(127,127,0)); + leds.show(); + } else if (data.ppm > 1800) { + leds.setPixelColor(0, leds.Color(127,0,0)); + leds.show(); + } else { + leds.setPixelColor(0, leds.Color(0,0,0)); + leds.show(); + } #endif // Queue Packet for Sending @@ -186,6 +227,9 @@ void setup() Wire.begin(); SPI.begin(); + pinMode(PIN_PC1, OUTPUT); + leds.setBrightness(127); + // Disable unused Pins (for power saving) for (int i = 0; i < (sizeof(disabledPins) / sizeof(disabledPins[0])) - 1; i++) pinMode(disabledPins[i], INPUT_PULLUP); @@ -225,6 +269,8 @@ void setup() DEBUG_PRINTLN("Setup Finished"); // Schedule First Send (Triggers OTAA Join as well) + leds.setPixelColor(1, leds.Color(127, 127, 0)); + leds.show(); do_send(&sendjob); } From c573254d0666b3205ed86427472b22f1ccbc31a5 Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Tue, 2 Mar 2021 18:32:56 +0100 Subject: [PATCH 2/3] Reduce LED Brightness --- src/main.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 83b186a..8c28646 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -192,10 +192,14 @@ void do_send(osjob_t* j) { #ifndef HAS_NO_SENSOR sensor.getSensorData(data); - // ppm_level - // 0 -> < 1000 ppm green - // 1 -> < 1800 ppm yellow - // 2 -> > 1000 ppm red + // Queue Packet for Sending + DEBUG_PRINTLN("LoRa-Packet Queued"); + LMIC_setTxData2(1, (unsigned char *)&data, sizeof(data), 0); + + // ppm level + // < 1000 ppm green + // < 1800 ppm yellow + // > 1000 ppm red if (data.ppm > 0 && data.ppm <= 1000) { leds.setPixelColor(0, leds.Color(0,127,0)); @@ -212,9 +216,7 @@ void do_send(osjob_t* j) { } #endif - // Queue Packet for Sending - DEBUG_PRINTLN("LoRa-Packet Queued"); - LMIC_setTxData2(1, (unsigned char *)&data, sizeof(data), 0); + } } @@ -228,7 +230,7 @@ void setup() SPI.begin(); pinMode(PIN_PC1, OUTPUT); - leds.setBrightness(127); + leds.setBrightness(64); // Disable unused Pins (for power saving) for (int i = 0; i < (sizeof(disabledPins) / sizeof(disabledPins[0])) - 1; i++) From 55952369273618f53fb7a371b71741be14449a48 Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Tue, 9 Mar 2021 17:25:35 +0100 Subject: [PATCH 3/3] WS2812B Code Cleanup, Configuration via config.h --- platformio.ini | 13 +++++--- src/config.h.example | 9 ++++++ src/main.cpp | 72 ++++++++++++++++++++------------------------ 3 files changed, 50 insertions(+), 44 deletions(-) diff --git a/platformio.ini b/platformio.ini index 2ff1680..2d2c790 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,15 +13,17 @@ platform = atmelmegaavr board = ATtiny3216 framework = arduino -# Board Config -board_build.f_cpu = 8000000L +## Board Config ## +# You might want to set f_cpu to 5MHz (5000000L) to allow operation at lower Battery Voltage - Use "Burn Fuses" after changing f_cpu +# Be aware that some Functions (like WS2812B LED Support) will not work at 5 HMz +board_build.f_cpu = 8000000L board_hardware.oscillator = internal board_hardware.bod = disabled -# Debug Port Config +## Debug Port Config ## monitor_speed = 115200 -# LMIC Config via Build Flags +## LMIC Config via Build Flags ## build_flags = -D CFG_eu868 -D CFG_sx1276_radio @@ -29,7 +31,7 @@ build_flags = -D DISABLE_BEACONS -D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS -# Programmer Config (MicroUPDI) +## Programmer Config (MicroUPDI) ## upload_port = usb upload_protocol = xplainedmini_updi upload_flags = @@ -37,5 +39,6 @@ upload_flags = -P$UPLOAD_PORT -c$UPLOAD_PROTOCOL +## External Libraries ## lib_deps = mcci-catena/MCCI LoRaWAN LMIC library @ ^3.3.0 \ No newline at end of file diff --git a/src/config.h.example b/src/config.h.example index 6791f96..3674f35 100644 --- a/src/config.h.example +++ b/src/config.h.example @@ -4,6 +4,15 @@ // ATTNode v3 Onboard LED is on PIN_PA7 #define LED_PIN PIN_PA7 +// Enable WS2812B RGB LED Support for the CO2 Addon Board +// * First LED shows CO2-Level (green: <1000, yellow: 1000-1800, red: >=1800) +// * Second LED shows LoRa Status (yellow: Joining, green 1s: Joined, green 100ms: Sending, blue 250ms: Received Downlink) +// WS2812B_BRIGHT can be set to the desired brightness value for the LEDs (0=off, 255=brightest) +// Uncomment the 3 following lines to get the default behaviour +// #define WS2812B_PIN PIN_PC1 +// #define WS2812B_NUM 2 +// #define WS2812B_BRIGHT 32 + // Enable Serial Debugging. Parameters for the Serial Port are 115200 // Please be aware that the SG112A/B CO2 Sensor uses the HW-UART, so // Serial Debug Output is not available with this Sensor. diff --git a/src/main.cpp b/src/main.cpp index 8c28646..59da31c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,14 +31,20 @@ void blink(uint8_t num) { #define BLINK_LED(COUNT) #endif -// WS2812B -#include - -#define WS_NUM_PIXELS 2 - -byte pixels[WS_NUM_PIXELS * 3]; -tinyNeoPixel leds = tinyNeoPixel(WS_NUM_PIXELS, PIN_PC1, NEO_GRB, pixels); +// WS2812B RGB LEDs on the CO2 Addon Board +// Defines the Macro Function WS2812B_SETLED so we don't need #ifdefs everywhere +#ifdef WS2812B_PIN + #include + byte pixels[WS2812B_NUM * 3]; + tinyNeoPixel leds = tinyNeoPixel(WS2812B_NUM, WS2812B_PIN, NEO_GRB, pixels); + #define WS2812B_SETLED(led,r,g,b) leds.setPixelColor(led,r,g,b); leds.show() + #define WS2812B_BLINK(led,r,g,b,ms) leds.setPixelColor(led,r,g,b); leds.show(); delay(ms); leds.setPixelColor(led,0,0,0); leds.show() +#else + #define WS2812B_SETLED(led,r,g,b) + #define WS2812B_BLINK(led,r,g,b,ms) +#endif +// Create the Sensor Objects #if defined HAS_NO_SENSOR struct lora_data { uint8_t bat; @@ -83,7 +89,7 @@ const lmic_pinmap lmic_pins = { // List of unused Pins - will be disabled for Power Saving #if defined DEBUG || defined HAS_SG112A || defined HAS_MHZ19C -const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC0}; +const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0}; #else const int disabledPins[] = {PIN_PB5, PIN_PB4, PIN_PB3, PIN_PB2, PIN_PB1, PIN_PB0, PIN_PC3, PIN_PC2, PIN_PC1, PIN_PC0}; #endif @@ -117,21 +123,13 @@ void onEvent(ev_t ev) { case EV_JOINED: // Disable LinkCheck LMIC_setLinkCheckMode(0); - leds.setPixelColor(1, leds.Color(0,127,0)); - leds.show(); - delay(1000); - leds.setPixelColor(1, leds.Color(0,0,0)); - leds.show(); + WS2812B_BLINK(1,0,127,0,1000); DEBUG_PRINTLN("OTAA Join Succeeded"); break; case EV_TXCOMPLETE: // Check for Downlink DEBUG_PRINTLN("LoRa Packet Sent"); - leds.setPixelColor(1, leds.Color(0,127,0)); - leds.show(); - delay(100); - leds.setPixelColor(1, leds.Color(0,0,0)); - leds.show(); + WS2812B_BLINK(1,0,127,0,1000); if ((int)LMIC.dataLen == 2) { // We got a Packet with the right size - lets assemble it into a uint16_t DEBUG_PRINTLN("Received Downlink") @@ -140,11 +138,7 @@ void onEvent(ev_t ev) { DEBUG_PRINTLN(tmpslp); sleep_time = tmpslp; EEPROM.put(ADDR_SLP, tmpslp); - leds.setPixelColor(1, leds.Color(0,0,127)); - leds.show(); - delay(250); - leds.setPixelColor(1, leds.Color(0,0,0)); - leds.show(); + WS2812B_BLINK(1,0,0,127,250); } // Got to sleep for specified Time @@ -196,25 +190,24 @@ void do_send(osjob_t* j) { DEBUG_PRINTLN("LoRa-Packet Queued"); LMIC_setTxData2(1, (unsigned char *)&data, sizeof(data), 0); - // ppm level + #if defined WS2812B_PIN && (defined HAS_SG112A || defined HAS_MHZ19C) + + // CO2 PPM Levels and LED Colors // < 1000 ppm green // < 1800 ppm yellow // > 1000 ppm red if (data.ppm > 0 && data.ppm <= 1000) { - leds.setPixelColor(0, leds.Color(0,127,0)); - leds.show(); + WS2812B_SETLED(0,0,127,0); } else if (data.ppm > 1000 && data.ppm <= 1800) { - leds.setPixelColor(0, leds.Color(127,127,0)); - leds.show(); + WS2812B_SETLED(0,127,127,0); } else if (data.ppm > 1800) { - leds.setPixelColor(0, leds.Color(127,0,0)); - leds.show(); + WS2812B_SETLED(0,127,0,0); } else { - leds.setPixelColor(0, leds.Color(0,0,0)); - leds.show(); + WS2812B_SETLED(0,0,0,0); } - #endif + #endif // WS2812B + #endif // #infdef HAS_NO_SENSOR } @@ -229,13 +222,15 @@ void setup() Wire.begin(); SPI.begin(); - pinMode(PIN_PC1, OUTPUT); - leds.setBrightness(64); - - // Disable unused Pins (for power saving) + // Disable unused Pins (for power saving) for (int i = 0; i < (sizeof(disabledPins) / sizeof(disabledPins[0])) - 1; i++) pinMode(disabledPins[i], INPUT_PULLUP); + #ifdef WS2812B_PIN + pinMode(WS2812B_PIN, OUTPUT); + leds.setBrightness(WS2812B_BRIGHT); + #endif + // Set RTC while (RTC.STATUS > 0) {} RTC.CLKSEL = RTC_CLKSEL_INT1K_gc; @@ -271,8 +266,7 @@ void setup() DEBUG_PRINTLN("Setup Finished"); // Schedule First Send (Triggers OTAA Join as well) - leds.setPixelColor(1, leds.Color(127, 127, 0)); - leds.show(); + WS2812B_SETLED(1,127,127,0); do_send(&sendjob); }