forked from Makuna/NeoPixelBus
Merge branch 'UseCycles'
Conflicts: NeoPixelBus.cpp
This commit is contained in:
151
NeoPixelBus.cpp
151
NeoPixelBus.cpp
@@ -75,35 +75,113 @@ void NeoPixelBus::Begin(void)
|
|||||||
|
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR send_ws_0_800(uint8_t gpio) {
|
|
||||||
uint8_t i;
|
#define CYCLES_800_T0H (F_CPU / 2500000 - 2) // 0.4us
|
||||||
i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
#define CYCLES_800_T1H (F_CPU / 1250000 - 2) // 0.8us
|
||||||
i = 7; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
#define CYCLES_800 (F_CPU / 800000 - 2) // 1.25us per bit
|
||||||
|
#define CYCLES_400_T0H (F_CPU / 2000000 - 2)
|
||||||
|
#define CYCLES_400_T1H (F_CPU / 833333 - 2)
|
||||||
|
#define CYCLES_400 (F_CPU / 400000 - 2)
|
||||||
|
|
||||||
|
#define RSR_CCOUNT(r) __asm__ __volatile__("rsr %0,234":"=a" (r))
|
||||||
|
|
||||||
|
static inline ICACHE_FLASH_ATTR uint32_t get_ccount(void)
|
||||||
|
{
|
||||||
|
uint32_t ccount;
|
||||||
|
RSR_CCOUNT(ccount);
|
||||||
|
return ccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR send_ws_1_800(uint8_t gpio) {
|
|
||||||
uint8_t i;
|
static inline void ICACHE_FLASH_ATTR send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin)
|
||||||
i = 9; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
{
|
||||||
i = 2; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
const uint32_t pinRegister = _BV(pin);
|
||||||
|
uint32_t mask; // 32 bit work is optimized for the chip
|
||||||
|
uint32_t subpix; // 32 bit work is optimized for the chip
|
||||||
|
uint32_t cyclesStart;
|
||||||
|
|
||||||
|
cyclesStart = get_ccount() + CYCLES_800;
|
||||||
|
while (pixels < end)
|
||||||
|
{
|
||||||
|
subpix = *pixels++;
|
||||||
|
for (mask = 0x80; mask; mask >>= 1)
|
||||||
|
{
|
||||||
|
uint32_t nextBit = (subpix & mask);
|
||||||
|
uint32_t cyclesNext = cyclesStart;
|
||||||
|
|
||||||
|
// after we have done as much work as needed for this next bit
|
||||||
|
// now wait for the HIGH
|
||||||
|
do
|
||||||
|
{
|
||||||
|
cyclesStart = get_ccount();
|
||||||
|
}
|
||||||
|
while ((cyclesStart - cyclesNext) < CYCLES_800);
|
||||||
|
|
||||||
|
|
||||||
|
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister);
|
||||||
|
|
||||||
|
// wait for the LOW
|
||||||
|
if (nextBit)
|
||||||
|
{
|
||||||
|
while ((get_ccount() - cyclesStart) < CYCLES_800_T1H);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while ((get_ccount() - cyclesStart) < CYCLES_800_T0H);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR send_ws_0_400(uint8_t gpio) {
|
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister);
|
||||||
uint8_t i;
|
}
|
||||||
i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
}
|
||||||
i = 12; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
while ((get_ccount() - cyclesStart) < CYCLES_800);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR send_ws_1_400(uint8_t gpio) {
|
static inline void ICACHE_FLASH_ATTR send_pixels_400(uint8_t* pixels, uint8_t* end, uint8_t pin)
|
||||||
uint8_t i;
|
{
|
||||||
i = 18; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
|
const uint32_t pinRegister = _BV(pin);
|
||||||
i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
|
uint32_t mask; // 32 bit work is optimized for the chip
|
||||||
|
uint32_t subpix; // 32 bit work is optimized for the chip
|
||||||
|
uint32_t cyclesStart;
|
||||||
|
|
||||||
|
cyclesStart = get_ccount() + CYCLES_400;
|
||||||
|
while (pixels < end)
|
||||||
|
{
|
||||||
|
subpix = *pixels++;
|
||||||
|
for (mask = 0x80; mask; mask >>= 1)
|
||||||
|
{
|
||||||
|
uint32_t nextBit = (subpix & mask);
|
||||||
|
uint32_t cyclesNext = cyclesStart;
|
||||||
|
|
||||||
|
// after we have done as much work as needed for this next bit
|
||||||
|
// now wait for the HIGH
|
||||||
|
do
|
||||||
|
{
|
||||||
|
cyclesStart = get_ccount();
|
||||||
|
} while ((cyclesStart - cyclesNext) < CYCLES_400);
|
||||||
|
|
||||||
|
|
||||||
|
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister);
|
||||||
|
|
||||||
|
// wait for the LOW
|
||||||
|
if (nextBit)
|
||||||
|
{
|
||||||
|
while ((get_ccount() - cyclesStart) < CYCLES_400_T1H);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while ((get_ccount() - cyclesStart) < CYCLES_400_T0H);
|
||||||
|
}
|
||||||
|
|
||||||
|
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while ((get_ccount() - cyclesStart) < CYCLES_400);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR NeoPixelBus::Show(void)
|
void ICACHE_FLASH_ATTR NeoPixelBus::Show(void)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@@ -749,41 +827,14 @@ void NeoPixelBus::Show(void)
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
// 800 KHz bitstream
|
// 800 KHz bitstream
|
||||||
while (p < end)
|
send_pixels_800(p, end, _pin);
|
||||||
{
|
|
||||||
uint8_t subpix = *p++;
|
|
||||||
for (uint8_t mask = 0x80; mask; mask >>= 1)
|
|
||||||
{
|
|
||||||
if (subpix & mask)
|
|
||||||
{
|
|
||||||
send_ws_1_800(_pin);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
send_ws_0_800(_pin);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifdef INCLUDE_NEO_KHZ400_SUPPORT
|
#ifdef INCLUDE_NEO_KHZ400_SUPPORT
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 400 kHz bitstream
|
// 400 kHz bitstream
|
||||||
while (p < end)
|
send_pixels_400(p, end, _pin);
|
||||||
{
|
|
||||||
uint8_t subpix = *p++;
|
|
||||||
for (uint8_t mask = 0x80; mask; mask >>= 1)
|
|
||||||
{
|
|
||||||
if (subpix & mask)
|
|
||||||
{
|
|
||||||
send_ws_1_400(_pin);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
send_ws_0_400(_pin);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -794,7 +845,7 @@ void NeoPixelBus::Show(void)
|
|||||||
#if defined(__MK20DX128__) || defined(__MK20DX256__) // Teensy 3.0 & 3.1
|
#if defined(__MK20DX128__) || defined(__MK20DX256__) // Teensy 3.0 & 3.1
|
||||||
#define CYCLES_800_T0H (F_CPU / 2500000) // 0.4us
|
#define CYCLES_800_T0H (F_CPU / 2500000) // 0.4us
|
||||||
#define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us
|
#define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us
|
||||||
#define CYCLES_800 (F_CPU / 800000) // 12.5us per bit
|
#define CYCLES_800 (F_CPU / 800000) // 1.25us per bit
|
||||||
#define CYCLES_400_T0H (F_CPU / 2000000)
|
#define CYCLES_400_T0H (F_CPU / 2000000)
|
||||||
#define CYCLES_400_T1H (F_CPU / 833333)
|
#define CYCLES_400_T1H (F_CPU / 833333)
|
||||||
#define CYCLES_400 (F_CPU / 400000)
|
#define CYCLES_400 (F_CPU / 400000)
|
||||||
@@ -943,9 +994,7 @@ void NeoPixelBus::Show(void)
|
|||||||
ResetDirty();
|
ResetDirty();
|
||||||
_endTime = micros(); // Save EOD time for latch on next call
|
_endTime = micros(); // Save EOD time for latch on next call
|
||||||
}
|
}
|
||||||
#if defined(ESP8266)
|
|
||||||
#pragma optimize( "", on )
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Set the output pin number
|
// Set the output pin number
|
||||||
void NeoPixelBus::setPin(uint8_t p)
|
void NeoPixelBus::setPin(uint8_t p)
|
||||||
|
@@ -15,17 +15,9 @@ You should have received a copy of the GNU Lesser General Public
|
|||||||
License along with NeoPixel. If not, see
|
License along with NeoPixel. If not, see
|
||||||
<http://www.gnu.org/licenses/>.
|
<http://www.gnu.org/licenses/>.
|
||||||
--------------------------------------------------------------------*/
|
--------------------------------------------------------------------*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#ifndef NEOPIXELBUS_H
|
|
||||||
#define NEOPIXELBUS_H
|
|
||||||
|
|
||||||
#if (ARDUINO >= 100)
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#else
|
|
||||||
#include <WProgram.h>
|
|
||||||
#include <pins_arduino.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "RgbColor.h"
|
#include "RgbColor.h"
|
||||||
|
|
||||||
// '_flagsPixels' flags for LED _pixels (third parameter to constructor):
|
// '_flagsPixels' flags for LED _pixels (third parameter to constructor):
|
||||||
@@ -125,4 +117,3 @@ private:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NEOPIXELBUS_H
|
|
||||||
|
11
RgbColor.h
11
RgbColor.h
@@ -13,16 +13,9 @@ You should have received a copy of the GNU Lesser General Public
|
|||||||
License along with NeoPixel. If not, see
|
License along with NeoPixel. If not, see
|
||||||
<http://www.gnu.org/licenses/>.
|
<http://www.gnu.org/licenses/>.
|
||||||
--------------------------------------------------------------------*/
|
--------------------------------------------------------------------*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#ifndef RGBCOLOR_H
|
|
||||||
#define RGBCOLOR_H
|
|
||||||
|
|
||||||
#if (ARDUINO >= 100)
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#else
|
|
||||||
#include <WProgram.h>
|
|
||||||
#include <pins_arduino.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// RgbColor represents a color object that is represented by Red, Green, Blue
|
// RgbColor represents a color object that is represented by Red, Green, Blue
|
||||||
@@ -95,5 +88,3 @@ struct RgbColor
|
|||||||
uint8_t B;
|
uint8_t B;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif // RGBCOLOR_H
|
|
Reference in New Issue
Block a user