change structure continued
This commit is contained in:
parent
7e1f846dc5
commit
e70087427e
6
.gitmodules
vendored
6
.gitmodules
vendored
@ -4,3 +4,9 @@
|
||||
[submodule "esp8266mqttboilerplate"]
|
||||
path = esp8266mqttboilerplate
|
||||
url = ../esp8266mqttboilerplate
|
||||
[submodule "libraries/esp8266boilerplate"]
|
||||
path = libraries/esp8266boilerplate
|
||||
url = ../esp8266boilerplate.git
|
||||
[submodule "libraries/esp8266mqttboilerplate"]
|
||||
path = libraries/esp8266mqttboilerplate
|
||||
url = ../esp8266mqttboilerplate.git
|
||||
|
2435
libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp
Normal file
2435
libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp
Normal file
File diff suppressed because it is too large
Load Diff
351
libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.h
Normal file
351
libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.h
Normal file
@ -0,0 +1,351 @@
|
||||
/*!
|
||||
* @file Adafruit_NeoPixel.h
|
||||
*
|
||||
* This is part of Adafruit's NeoPixel library for the Arduino platform,
|
||||
* allowing a broad range of microcontroller boards (most AVR boards,
|
||||
* many ARM devices, ESP8266 and ESP32, among others) to control Adafruit
|
||||
* NeoPixels, FLORA RGB Smart Pixels and compatible devices -- WS2811,
|
||||
* WS2812, WS2812B, SK6812, etc.
|
||||
*
|
||||
* Adafruit invests time and resources providing this open source code,
|
||||
* please support Adafruit and open-source hardware by purchasing products
|
||||
* from Adafruit!
|
||||
*
|
||||
* Written by Phil "Paint Your Dragon" Burgess for Adafruit Industries,
|
||||
* with contributions by PJRC, Michael Miller and other members of the
|
||||
* open source community.
|
||||
*
|
||||
* This file is part of the Adafruit_NeoPixel library.
|
||||
*
|
||||
* Adafruit_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.
|
||||
*
|
||||
* Adafruit_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
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ADAFRUIT_NEOPIXEL_H
|
||||
#define ADAFRUIT_NEOPIXEL_H
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#include <pins_arduino.h>
|
||||
#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 transmitted 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.
|
||||
// Most of these values won't exist in real-world devices, but it's done
|
||||
// this way so we're ready for it (also, if using the WS2811 driver IC,
|
||||
// one might have their pixels set up in any weird permutation).
|
||||
|
||||
// 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)) ///< Transmit as R,G,B
|
||||
#define NEO_RBG ((0<<6) | (0<<4) | (2<<2) | (1)) ///< Transmit as R,B,G
|
||||
#define NEO_GRB ((1<<6) | (1<<4) | (0<<2) | (2)) ///< Transmit as G,R,B
|
||||
#define NEO_GBR ((2<<6) | (2<<4) | (0<<2) | (1)) ///< Transmit as G,B,R
|
||||
#define NEO_BRG ((1<<6) | (1<<4) | (2<<2) | (0)) ///< Transmit as B,R,G
|
||||
#define NEO_BGR ((2<<6) | (2<<4) | (1<<2) | (0)) ///< Transmit as B,G,R
|
||||
|
||||
// RGBW NeoPixel permutations; all 4 offsets are distinct
|
||||
// Offset: W R G B
|
||||
#define NEO_WRGB ((0<<6) | (1<<4) | (2<<2) | (3)) ///< Transmit as W,R,G,B
|
||||
#define NEO_WRBG ((0<<6) | (1<<4) | (3<<2) | (2)) ///< Transmit as W,R,B,G
|
||||
#define NEO_WGRB ((0<<6) | (2<<4) | (1<<2) | (3)) ///< Transmit as W,G,R,B
|
||||
#define NEO_WGBR ((0<<6) | (3<<4) | (1<<2) | (2)) ///< Transmit as W,G,B,R
|
||||
#define NEO_WBRG ((0<<6) | (2<<4) | (3<<2) | (1)) ///< Transmit as W,B,R,G
|
||||
#define NEO_WBGR ((0<<6) | (3<<4) | (2<<2) | (1)) ///< Transmit as W,B,G,R
|
||||
|
||||
#define NEO_RWGB ((1<<6) | (0<<4) | (2<<2) | (3)) ///< Transmit as R,W,G,B
|
||||
#define NEO_RWBG ((1<<6) | (0<<4) | (3<<2) | (2)) ///< Transmit as R,W,B,G
|
||||
#define NEO_RGWB ((2<<6) | (0<<4) | (1<<2) | (3)) ///< Transmit as R,G,W,B
|
||||
#define NEO_RGBW ((3<<6) | (0<<4) | (1<<2) | (2)) ///< Transmit as R,G,B,W
|
||||
#define NEO_RBWG ((2<<6) | (0<<4) | (3<<2) | (1)) ///< Transmit as R,B,W,G
|
||||
#define NEO_RBGW ((3<<6) | (0<<4) | (2<<2) | (1)) ///< Transmit as R,B,G,W
|
||||
|
||||
#define NEO_GWRB ((1<<6) | (2<<4) | (0<<2) | (3)) ///< Transmit as G,W,R,B
|
||||
#define NEO_GWBR ((1<<6) | (3<<4) | (0<<2) | (2)) ///< Transmit as G,W,B,R
|
||||
#define NEO_GRWB ((2<<6) | (1<<4) | (0<<2) | (3)) ///< Transmit as G,R,W,B
|
||||
#define NEO_GRBW ((3<<6) | (1<<4) | (0<<2) | (2)) ///< Transmit as G,R,B,W
|
||||
#define NEO_GBWR ((2<<6) | (3<<4) | (0<<2) | (1)) ///< Transmit as G,B,W,R
|
||||
#define NEO_GBRW ((3<<6) | (2<<4) | (0<<2) | (1)) ///< Transmit as G,B,R,W
|
||||
|
||||
#define NEO_BWRG ((1<<6) | (2<<4) | (3<<2) | (0)) ///< Transmit as B,W,R,G
|
||||
#define NEO_BWGR ((1<<6) | (3<<4) | (2<<2) | (0)) ///< Transmit as B,W,G,R
|
||||
#define NEO_BRWG ((2<<6) | (1<<4) | (3<<2) | (0)) ///< Transmit as B,R,W,G
|
||||
#define NEO_BRGW ((3<<6) | (1<<4) | (2<<2) | (0)) ///< Transmit as B,R,G,W
|
||||
#define NEO_BGWR ((2<<6) | (3<<4) | (1<<2) | (0)) ///< Transmit as B,G,W,R
|
||||
#define NEO_BGRW ((3<<6) | (2<<4) | (1<<2) | (0)) ///< Transmit as B,G,R,W
|
||||
|
||||
// Add NEO_KHZ400 to the color order value to indicate a 400 KHz device.
|
||||
// All but the earliest v1 NeoPixels expect an 800 KHz data stream, this is
|
||||
// the default if unspecified. Because flash space is very limited on ATtiny
|
||||
// devices (e.g. Trinket, Gemma), v1 NeoPixels aren't handled by default on
|
||||
// those chips, though it can be enabled by removing the ifndef/endif below,
|
||||
// but code will be bigger. Conversely, can disable the NEO_KHZ400 line on
|
||||
// other MCUs to remove v1 support and save a little space.
|
||||
|
||||
#define NEO_KHZ800 0x0000 ///< 800 KHz data transmission
|
||||
#ifndef __AVR_ATtiny85__
|
||||
#define NEO_KHZ400 0x0100 ///< 400 KHz data transmission
|
||||
#endif
|
||||
|
||||
// If 400 KHz support is enabled, the third parameter to the constructor
|
||||
// requires a 16-bit value (in order to select 400 vs 800 KHz speed).
|
||||
// If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value
|
||||
// is sufficient to encode pixel color order, saving some space.
|
||||
|
||||
#ifdef NEO_KHZ400
|
||||
typedef uint16_t neoPixelType; ///< 3rd arg to Adafruit_NeoPixel constructor
|
||||
#else
|
||||
typedef uint8_t neoPixelType; ///< 3rd arg to Adafruit_NeoPixel constructor
|
||||
#endif
|
||||
|
||||
// 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 PROGMEM (flash mem) table containing 8-bit unsigned sine wave (0-255).
|
||||
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
|
||||
*/
|
||||
static const uint8_t PROGMEM _NeoPixelSineTable[256] = {
|
||||
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
|
||||
*/
|
||||
static const uint8_t PROGMEM _NeoPixelGammaTable[256] = {
|
||||
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};
|
||||
|
||||
/*!
|
||||
@brief Class that stores state and functions for interacting with
|
||||
Adafruit NeoPixels and compatible devices.
|
||||
*/
|
||||
class Adafruit_NeoPixel {
|
||||
|
||||
public:
|
||||
|
||||
// Constructor: number of LEDs, pin number, LED type
|
||||
Adafruit_NeoPixel(uint16_t n, uint8_t pin=6,
|
||||
neoPixelType type=NEO_GRB + NEO_KHZ800);
|
||||
Adafruit_NeoPixel(void);
|
||||
~Adafruit_NeoPixel();
|
||||
|
||||
void begin(void);
|
||||
void show(void);
|
||||
void setPin(uint8_t p);
|
||||
void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b);
|
||||
void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b,
|
||||
uint8_t w);
|
||||
void setPixelColor(uint16_t n, uint32_t c);
|
||||
void fill(uint32_t c=0, uint16_t first=0, uint16_t count=0);
|
||||
void setBrightness(uint8_t);
|
||||
void clear(void);
|
||||
void updateLength(uint16_t n);
|
||||
void updateType(neoPixelType t);
|
||||
/*!
|
||||
@brief Check whether a call to show() will start sending data
|
||||
immediately or will 'block' for a required interval. NeoPixels
|
||||
require a short quiet time (about 300 microseconds) after the
|
||||
last bit is received before the data 'latches' and new data can
|
||||
start being received. Usually one's sketch is implicitly using
|
||||
this time to generate a new frame of animation...but if it
|
||||
finishes very quickly, this function could be used to see if
|
||||
there's some idle time available for some low-priority
|
||||
concurrent task.
|
||||
@return 1 or true if show() will start sending immediately, 0 or false
|
||||
if show() would block (meaning some idle time is available).
|
||||
*/
|
||||
boolean canShow(void) { return (micros() - endTime) >= 300L; }
|
||||
/*!
|
||||
@brief Get a pointer directly to the NeoPixel data buffer in RAM.
|
||||
Pixel data is stored in a device-native format (a la the NEO_*
|
||||
constants) and is not translated here. Applications that access
|
||||
this buffer will need to be aware of the specific data format
|
||||
and handle colors appropriately.
|
||||
@return Pointer to NeoPixel buffer (uint8_t* array).
|
||||
@note This is for high-performance applications where calling
|
||||
setPixelColor() on every single pixel would be too slow (e.g.
|
||||
POV or light-painting projects). There is no bounds checking
|
||||
on the array, creating tremendous potential for mayhem if one
|
||||
writes past the ends of the buffer. Great power, great
|
||||
responsibility and all that.
|
||||
*/
|
||||
uint8_t *getPixels(void) const { return pixels; };
|
||||
uint8_t getBrightness(void) const;
|
||||
/*!
|
||||
@brief Retrieve the pin number used for NeoPixel data output.
|
||||
@return Arduino pin number (-1 if not set).
|
||||
*/
|
||||
int8_t getPin(void) const { return pin; };
|
||||
/*!
|
||||
@brief Return the number of pixels in an Adafruit_NeoPixel strip object.
|
||||
@return Pixel count (0 if not set).
|
||||
*/
|
||||
uint16_t numPixels(void) const { return numLEDs; }
|
||||
uint32_t getPixelColor(uint16_t n) const;
|
||||
/*!
|
||||
@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) {
|
||||
return pgm_read_byte(&_NeoPixelSineTable[x]); // 0-255 in, 0-255 out
|
||||
}
|
||||
/*!
|
||||
@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) {
|
||||
return pgm_read_byte(&_NeoPixelGammaTable[x]); // 0-255 in, 0-255 out
|
||||
}
|
||||
/*!
|
||||
@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);
|
||||
|
||||
protected:
|
||||
|
||||
#ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled...
|
||||
boolean is800KHz; ///< true if 800 KHz pixels
|
||||
#endif
|
||||
boolean begun; ///< true if begin() previously called
|
||||
uint16_t numLEDs; ///< Number of RGB LEDs in strip
|
||||
uint16_t numBytes; ///< Size of 'pixels' buffer below
|
||||
int8_t pin; ///< Output pin number (-1 if not yet set)
|
||||
uint8_t brightness; ///< Strip brightness 0-255 (stored as +1)
|
||||
uint8_t *pixels; ///< Holds LED color values (3 or 4 bytes each)
|
||||
uint8_t rOffset; ///< Red index within each 3- or 4-byte pixel
|
||||
uint8_t gOffset; ///< Index of green byte
|
||||
uint8_t bOffset; ///< Index of blue byte
|
||||
uint8_t wOffset; ///< Index of white (==rOffset if no white)
|
||||
uint32_t endTime; ///< Latch timing reference
|
||||
#ifdef __AVR__
|
||||
volatile uint8_t *port; ///< Output PORT register
|
||||
uint8_t pinMask; ///< Output PORT bitmask
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // ADAFRUIT_NEOPIXEL_H
|
165
libraries/Adafruit_NeoPixel/COPYING
Normal file
165
libraries/Adafruit_NeoPixel/COPYING
Normal file
@ -0,0 +1,165 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
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.
|
47
libraries/Adafruit_NeoPixel/README.md
Normal file
47
libraries/Adafruit_NeoPixel/README.md
Normal file
@ -0,0 +1,47 @@
|
||||
# Adafruit NeoPixel Library [](https://travis-ci.org/adafruit/Adafruit_NeoPixel)
|
||||
|
||||
Arduino library for controlling single-wire-based LED pixels and strip such as the [Adafruit 60 LED/meter Digital LED strip][strip], the [Adafruit FLORA RGB Smart Pixel][flora], the [Adafruit Breadboard-friendly RGB Smart Pixel][pixel], the [Adafruit NeoPixel Stick][stick], and the [Adafruit NeoPixel Shield][shield].
|
||||
|
||||
After downloading, rename folder to 'Adafruit_NeoPixel' and install in Arduino Libraries folder. Restart Arduino IDE, then open File->Sketchbook->Library->Adafruit_NeoPixel->strandtest sketch.
|
||||
|
||||
Compatibility notes: Port A is not supported on any AVR processors at this time
|
||||
|
||||
[flora]: http://adafruit.com/products/1060
|
||||
[strip]: http://adafruit.com/products/1138
|
||||
[pixel]: http://adafruit.com/products/1312
|
||||
[stick]: http://adafruit.com/products/1426
|
||||
[shield]: http://adafruit.com/products/1430
|
||||
|
||||
---
|
||||
|
||||
## Supported chipsets
|
||||
|
||||
We have included code for the following chips - *sometimes these break for exciting reasons that we can't control* in which case please open an issue!
|
||||
|
||||
* AVR ATmega and ATtiny (any 8-bit) - 8 MHz, 12 MHz and 16 MHz
|
||||
* Teensy 3.x and LC
|
||||
* Arduino Due
|
||||
* Arduino 101
|
||||
* ATSAMD21 (Arduino Zero/M0 and other SAMD21 boards) @ 48 MHz
|
||||
* ATSAMD51 @ 120 MHz
|
||||
* Adafruit STM32 Feather @ 120 MHz
|
||||
* ESP8266 any speed
|
||||
* ESP32 any speed
|
||||
* Nordic nRF52 (Adafruit Feather nRF52), nRF51 (micro:bit)
|
||||
|
||||
Check forks for other architectures not listed here!
|
||||
|
||||
---
|
||||
|
||||
### Roadmap
|
||||
|
||||
The PRIME DIRECTIVE is to maintain backward compatibility with existing Arduino sketches -- many are hosted elsewhere and don't track changes here, some are in print and can never be changed!
|
||||
|
||||
Please don't reformat code for the sake of reformatting code. The resulting large "visual diff" makes it impossible to untangle actual bug fixes from merely rearranged lines. (Exception for first item in wishlist below.)
|
||||
|
||||
Things I'd Like To Do But There's No Official Timeline So Please Don't Count On Any Of This Ever Being Canonical:
|
||||
|
||||
* For the show() function (with all the delicate pixel timing stuff), break out each architecture into separate source files rather than the current unmaintainable tangle of #ifdef statements!
|
||||
* Please don't use updateLength() or updateType() in new code. They should not have been implemented this way (use the C++ 'new' operator with the regular constructor instead) and are only sticking around because of the Prime Directive. setPin() is OK for now though, it's a trick we can use to 'recycle' pixel memory across multiple strips.
|
||||
* In the M0 and M4 code, use the hardware systick counter for bit timing rather than hand-tweaked NOPs (a temporary kludge at the time because I wasn't reading systick correctly).
|
||||
* As currently written, brightness scaling is still a "destructive" operation -- pixel values are altered in RAM and the original value as set can't be accurately read back, only approximated, which has been confusing and frustrating to users. It was done this way at the time because NeoPixel timing is strict, AVR microcontrollers (all we had at the time) are limited, and assembly language is hard. All the 32-bit architectures should have no problem handling nondestructive brightness scaling -- calculating each byte immediately before it's sent out the wire, maintaining the original set value in RAM -- the work just hasn't been done. There's a fair chance even the AVR code could manage it with some intense focus. (The DotStar library achieves nondestructive brightness scaling because it doesn't have to manage data timing so carefully...every architecture, even ATtiny, just takes whatever cycles it needs for the multiply/shift operations.)
|
82
libraries/Adafruit_NeoPixel/esp8266.c
Normal file
82
libraries/Adafruit_NeoPixel/esp8266.c
Normal file
@ -0,0 +1,82 @@
|
||||
// This is a mash-up of the Due show() code + insights from Michael Miller's
|
||||
// ESP8266 work for the NeoPixelBus library: github.com/Makuna/NeoPixelBus
|
||||
// Needs to be a separate .c file to enforce ICACHE_RAM_ATTR execution.
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP8266
|
||||
#include <eagle_soc.h>
|
||||
#endif
|
||||
|
||||
static uint32_t _getCycleCount(void) __attribute__((always_inline));
|
||||
static inline uint32_t _getCycleCount(void) {
|
||||
uint32_t ccount;
|
||||
__asm__ __volatile__("rsr %0,ccount":"=a" (ccount));
|
||||
return ccount;
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
void ICACHE_RAM_ATTR espShow(
|
||||
uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) {
|
||||
#else
|
||||
void espShow(
|
||||
uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) {
|
||||
#endif
|
||||
|
||||
#define CYCLES_800_T0H (F_CPU / 2500000) // 0.4us
|
||||
#define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us
|
||||
#define CYCLES_800 (F_CPU / 800000) // 1.25us per bit
|
||||
#define CYCLES_400_T0H (F_CPU / 2000000) // 0.5uS
|
||||
#define CYCLES_400_T1H (F_CPU / 833333) // 1.2us
|
||||
#define CYCLES_400 (F_CPU / 400000) // 2.5us per bit
|
||||
|
||||
uint8_t *p, *end, pix, mask;
|
||||
uint32_t t, time0, time1, period, c, startTime, pinMask;
|
||||
|
||||
pinMask = _BV(pin);
|
||||
p = pixels;
|
||||
end = p + numBytes;
|
||||
pix = *p++;
|
||||
mask = 0x80;
|
||||
startTime = 0;
|
||||
|
||||
#ifdef NEO_KHZ400
|
||||
if(is800KHz) {
|
||||
#endif
|
||||
time0 = CYCLES_800_T0H;
|
||||
time1 = CYCLES_800_T1H;
|
||||
period = CYCLES_800;
|
||||
#ifdef NEO_KHZ400
|
||||
} else { // 400 KHz bitstream
|
||||
time0 = CYCLES_400_T0H;
|
||||
time1 = CYCLES_400_T1H;
|
||||
period = CYCLES_400;
|
||||
}
|
||||
#endif
|
||||
|
||||
for(t = time0;; t = time0) {
|
||||
if(pix & mask) t = time1; // Bit high duration
|
||||
while(((c = _getCycleCount()) - startTime) < period); // Wait for bit start
|
||||
#ifdef ESP8266
|
||||
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinMask); // Set high
|
||||
#else
|
||||
gpio_set_level(pin, HIGH);
|
||||
#endif
|
||||
startTime = c; // Save start time
|
||||
while(((c = _getCycleCount()) - startTime) < t); // Wait high duration
|
||||
#ifdef ESP8266
|
||||
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinMask); // Set low
|
||||
#else
|
||||
gpio_set_level(pin, LOW);
|
||||
#endif
|
||||
if(!(mask >>= 1)) { // Next bit/byte
|
||||
if(p >= end) break;
|
||||
pix = *p++;
|
||||
mask = 0x80;
|
||||
}
|
||||
}
|
||||
while((_getCycleCount() - startTime) < period); // Wait for last bit
|
||||
}
|
||||
|
||||
#endif // ESP8266
|
@ -0,0 +1,177 @@
|
||||
// NeoPixel test program showing use of the WHITE channel for RGBW
|
||||
// pixels only (won't look correct on regular RGB NeoPixel strips).
|
||||
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#ifdef __AVR__
|
||||
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
|
||||
#endif
|
||||
|
||||
// Which pin on the Arduino is connected to the NeoPixels?
|
||||
// On a Trinket or Gemma we suggest changing this to 1:
|
||||
#define LED_PIN 6
|
||||
|
||||
// How many NeoPixels are attached to the Arduino?
|
||||
#define LED_COUNT 60
|
||||
|
||||
// NeoPixel brightness, 0 (min) to 255 (max)
|
||||
#define BRIGHTNESS 50
|
||||
|
||||
// Declare our NeoPixel strip object:
|
||||
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
|
||||
// Argument 1 = Number of pixels in NeoPixel strip
|
||||
// Argument 2 = Arduino pin number (most are valid)
|
||||
// Argument 3 = Pixel type flags, add together as needed:
|
||||
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
|
||||
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
|
||||
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
|
||||
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
|
||||
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
|
||||
|
||||
void setup() {
|
||||
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
|
||||
// Any other board, you can remove this part (but no harm leaving it):
|
||||
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
|
||||
clock_prescale_set(clock_div_1);
|
||||
#endif
|
||||
// END of Trinket-specific code.
|
||||
|
||||
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
|
||||
strip.show(); // Turn OFF all pixels ASAP
|
||||
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Fill along the length of the strip in various colors...
|
||||
colorWipe(strip.Color(255, 0, 0) , 50); // Red
|
||||
colorWipe(strip.Color( 0, 255, 0) , 50); // Green
|
||||
colorWipe(strip.Color( 0, 0, 255) , 50); // Blue
|
||||
colorWipe(strip.Color( 0, 0, 0, 255), 50); // True white (not RGB white)
|
||||
|
||||
whiteOverRainbow(75, 5);
|
||||
|
||||
pulseWhite(5);
|
||||
|
||||
rainbowFade2White(3, 3, 1);
|
||||
}
|
||||
|
||||
// Fill strip pixels one after another with a color. Strip is NOT cleared
|
||||
// first; anything there will be covered pixel by pixel. Pass in color
|
||||
// (as a single 'packed' 32-bit value, which you can get by calling
|
||||
// strip.Color(red, green, blue) as shown in the loop() function above),
|
||||
// and a delay time (in milliseconds) between pixels.
|
||||
void colorWipe(uint32_t color, int wait) {
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
|
||||
strip.show(); // Update strip to match
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
|
||||
void whiteOverRainbow(int whiteSpeed, int whiteLength) {
|
||||
|
||||
if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
|
||||
|
||||
int head = whiteLength - 1;
|
||||
int tail = 0;
|
||||
int loops = 3;
|
||||
int loopNum = 0;
|
||||
uint32_t lastTime = millis();
|
||||
uint32_t firstPixelHue = 0;
|
||||
|
||||
for(;;) { // Repeat forever (or until a 'break' or 'return')
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
if(((i >= tail) && (i <= head)) || // If between head & tail...
|
||||
((tail > head) && ((i >= tail) || (i <= head)))) {
|
||||
strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
|
||||
} else { // else set rainbow
|
||||
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
|
||||
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
|
||||
}
|
||||
}
|
||||
|
||||
strip.show(); // Update strip with new contents
|
||||
// There's no delay here, it just runs full-tilt until the timer and
|
||||
// counter combination below runs out.
|
||||
|
||||
firstPixelHue += 40; // Advance just a little along the color wheel
|
||||
|
||||
if((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
|
||||
if(++head >= strip.numPixels()) { // Advance head, wrap around
|
||||
head = 0;
|
||||
if(++loopNum >= loops) return;
|
||||
}
|
||||
if(++tail >= strip.numPixels()) { // Advance tail, wrap around
|
||||
tail = 0;
|
||||
}
|
||||
lastTime = millis(); // Save time of last movement
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void pulseWhite(uint8_t wait) {
|
||||
for(int j=0; j<256; j++) { // Ramp up from 0 to 255
|
||||
// Fill entire strip with white at gamma-corrected brightness level 'j':
|
||||
strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
|
||||
strip.show();
|
||||
delay(wait);
|
||||
}
|
||||
|
||||
for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
|
||||
strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
|
||||
strip.show();
|
||||
delay(wait);
|
||||
}
|
||||
}
|
||||
|
||||
void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {
|
||||
int fadeVal=0, fadeMax=100;
|
||||
|
||||
// Hue of first pixel runs 'rainbowLoops' complete loops through the color
|
||||
// wheel. Color wheel has a range of 65536 but it's OK if we roll over, so
|
||||
// just count from 0 to rainbowLoops*65536, using steps of 256 so we
|
||||
// advance around the wheel at a decent clip.
|
||||
for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536;
|
||||
firstPixelHue += 256) {
|
||||
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
|
||||
// Offset pixel hue by an amount to make one full revolution of the
|
||||
// color wheel (range of 65536) along the length of the strip
|
||||
// (strip.numPixels() steps):
|
||||
uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
|
||||
|
||||
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
|
||||
// optionally add saturation and value (brightness) (each 0 to 255).
|
||||
// Here we're using just the three-argument variant, though the
|
||||
// second value (saturation) is a constant 255.
|
||||
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
|
||||
255 * fadeVal / fadeMax)));
|
||||
}
|
||||
|
||||
strip.show();
|
||||
delay(wait);
|
||||
|
||||
if(firstPixelHue < 65536) { // First loop,
|
||||
if(fadeVal < fadeMax) fadeVal++; // fade in
|
||||
} else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,
|
||||
if(fadeVal > 0) fadeVal--; // fade out
|
||||
} else {
|
||||
fadeVal = fadeMax; // Interim loop, make sure fade is at max
|
||||
}
|
||||
}
|
||||
|
||||
for(int k=0; k<whiteLoops; k++) {
|
||||
for(int j=0; j<256; j++) { // Ramp up 0 to 255
|
||||
// Fill entire strip with white at gamma-corrected brightness level 'j':
|
||||
strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
|
||||
strip.show();
|
||||
}
|
||||
delay(1000); // Pause 1 second
|
||||
for(int j=255; j>=0; j--) { // Ramp down 255 to 0
|
||||
strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
|
||||
strip.show();
|
||||
}
|
||||
}
|
||||
|
||||
delay(500); // Pause 1/2 second
|
||||
}
|
133
libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.cpp
Normal file
133
libraries/Adafruit_NeoPixel/examples/StrandtestBLE/BLESerial.cpp
Normal file
@ -0,0 +1,133 @@
|
||||
#include "BLESerial.h"
|
||||
|
||||
// #define BLE_SERIAL_DEBUG
|
||||
|
||||
BLESerial* BLESerial::_instance = NULL;
|
||||
|
||||
BLESerial::BLESerial(unsigned char req, unsigned char rdy, unsigned char rst) :
|
||||
BLEPeripheral(req, rdy, rst)
|
||||
{
|
||||
this->_txCount = 0;
|
||||
this->_rxHead = this->_rxTail = 0;
|
||||
this->_flushed = 0;
|
||||
BLESerial::_instance = this;
|
||||
|
||||
addAttribute(this->_uartService);
|
||||
addAttribute(this->_uartNameDescriptor);
|
||||
setAdvertisedServiceUuid(this->_uartService.uuid());
|
||||
addAttribute(this->_rxCharacteristic);
|
||||
addAttribute(this->_rxNameDescriptor);
|
||||
this->_rxCharacteristic.setEventHandler(BLEWritten, BLESerial::_received);
|
||||
addAttribute(this->_txCharacteristic);
|
||||
addAttribute(this->_txNameDescriptor);
|
||||
}
|
||||
|
||||
void BLESerial::begin(...) {
|
||||
BLEPeripheral::begin();
|
||||
#ifdef BLE_SERIAL_DEBUG
|
||||
Serial.println(F("BLESerial::begin()"));
|
||||
#endif
|
||||
}
|
||||
|
||||
void BLESerial::poll() {
|
||||
if (millis() < this->_flushed + 100) {
|
||||
BLEPeripheral::poll();
|
||||
} else {
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
void BLESerial::end() {
|
||||
this->_rxCharacteristic.setEventHandler(BLEWritten, NULL);
|
||||
this->_rxHead = this->_rxTail = 0;
|
||||
flush();
|
||||
BLEPeripheral::disconnect();
|
||||
}
|
||||
|
||||
int BLESerial::available(void) {
|
||||
BLEPeripheral::poll();
|
||||
int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer);
|
||||
#ifdef BLE_SERIAL_DEBUG
|
||||
Serial.print(F("BLESerial::available() = "));
|
||||
Serial.println(retval);
|
||||
#endif
|
||||
return retval;
|
||||
}
|
||||
|
||||
int BLESerial::peek(void) {
|
||||
BLEPeripheral::poll();
|
||||
if (this->_rxTail == this->_rxHead) return -1;
|
||||
uint8_t byte = this->_rxBuffer[this->_rxTail];
|
||||
#ifdef BLE_SERIAL_DEBUG
|
||||
Serial.print(F("BLESerial::peek() = "));
|
||||
Serial.print((char) byte);
|
||||
Serial.print(F(" 0x"));
|
||||
Serial.println(byte, HEX);
|
||||
#endif
|
||||
return byte;
|
||||
}
|
||||
|
||||
int BLESerial::read(void) {
|
||||
BLEPeripheral::poll();
|
||||
if (this->_rxTail == this->_rxHead) return -1;
|
||||
this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer);
|
||||
uint8_t byte = this->_rxBuffer[this->_rxTail];
|
||||
#ifdef BLE_SERIAL_DEBUG
|
||||
Serial.print(F("BLESerial::read() = "));
|
||||
Serial.print((char) byte);
|
||||
Serial.print(F(" 0x"));
|
||||
Serial.println(byte, HEX);
|
||||
#endif
|
||||
return byte;
|
||||
}
|
||||
|
||||
void BLESerial::flush(void) {
|
||||
if (this->_txCount == 0) return;
|
||||
this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount);
|
||||
this->_flushed = millis();
|
||||
this->_txCount = 0;
|
||||
BLEPeripheral::poll();
|
||||
#ifdef BLE_SERIAL_DEBUG
|
||||
Serial.println(F("BLESerial::flush()"));
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t BLESerial::write(uint8_t byte) {
|
||||
BLEPeripheral::poll();
|
||||
if (this->_txCharacteristic.subscribed() == false) return 0;
|
||||
this->_txBuffer[this->_txCount++] = byte;
|
||||
if (this->_txCount == sizeof(this->_txBuffer)) flush();
|
||||
#ifdef BLE_SERIAL_DEBUG
|
||||
Serial.print(F("BLESerial::write("));
|
||||
Serial.print((char) byte);
|
||||
Serial.print(F(" 0x"));
|
||||
Serial.print(byte, HEX);
|
||||
Serial.println(F(") = 1"));
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
BLESerial::operator bool() {
|
||||
bool retval = BLEPeripheral::connected();
|
||||
#ifdef BLE_SERIAL_DEBUG
|
||||
Serial.print(F("BLESerial::operator bool() = "));
|
||||
Serial.println(retval);
|
||||
#endif
|
||||
return retval;
|
||||
}
|
||||
|
||||
void BLESerial::_received(const uint8_t* data, size_t size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
this->_rxHead = (this->_rxHead + 1) % sizeof(this->_rxBuffer);
|
||||
this->_rxBuffer[this->_rxHead] = data[i];
|
||||
}
|
||||
#ifdef BLE_SERIAL_DEBUG
|
||||
Serial.print(F("BLESerial::received("));
|
||||
for (int i = 0; i < size; i++) Serial.print((char) data[i]);
|
||||
Serial.println(F(")"));
|
||||
#endif
|
||||
}
|
||||
|
||||
void BLESerial::_received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic) {
|
||||
BLESerial::_instance->_received(rxCharacteristic.value(), rxCharacteristic.valueLength());
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
#ifndef _BLE_SERIAL_H_
|
||||
#define _BLE_SERIAL_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <BLEPeripheral.h>
|
||||
|
||||
class BLESerial : public BLEPeripheral, public Stream
|
||||
{
|
||||
public:
|
||||
BLESerial(unsigned char req, unsigned char rdy, unsigned char rst);
|
||||
|
||||
void begin(...);
|
||||
void poll();
|
||||
void end();
|
||||
|
||||
virtual int available(void);
|
||||
virtual int peek(void);
|
||||
virtual int read(void);
|
||||
virtual void flush(void);
|
||||
virtual size_t write(uint8_t byte);
|
||||
using Print::write;
|
||||
virtual operator bool();
|
||||
|
||||
private:
|
||||
unsigned long _flushed;
|
||||
static BLESerial* _instance;
|
||||
|
||||
size_t _rxHead;
|
||||
size_t _rxTail;
|
||||
size_t _rxCount() const;
|
||||
uint8_t _rxBuffer[BLE_ATTRIBUTE_MAX_VALUE_LENGTH];
|
||||
size_t _txCount;
|
||||
uint8_t _txBuffer[BLE_ATTRIBUTE_MAX_VALUE_LENGTH];
|
||||
|
||||
BLEService _uartService = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
|
||||
BLEDescriptor _uartNameDescriptor = BLEDescriptor("2901", "UART");
|
||||
BLECharacteristic _rxCharacteristic = BLECharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWriteWithoutResponse, BLE_ATTRIBUTE_MAX_VALUE_LENGTH);
|
||||
BLEDescriptor _rxNameDescriptor = BLEDescriptor("2901", "RX - Receive Data (Write)");
|
||||
BLECharacteristic _txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLENotify, BLE_ATTRIBUTE_MAX_VALUE_LENGTH);
|
||||
BLEDescriptor _txNameDescriptor = BLEDescriptor("2901", "TX - Transfer Data (Notify)");
|
||||
|
||||
void _received(const uint8_t* data, size_t size);
|
||||
static void _received(BLECentral& /*central*/, BLECharacteristic& rxCharacteristic);
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,192 @@
|
||||
/****************************************************************************
|
||||
* This example was developed by the Hackerspace San Salvador to demonstrate
|
||||
* the simultaneous use of the NeoPixel library and the Bluetooth SoftDevice.
|
||||
* To compile this example you'll need to add support for the NRF52 based
|
||||
* following the instructions at:
|
||||
* https://github.com/sandeepmistry/arduino-nRF5
|
||||
* Or adding the following URL to the board manager URLs on Arduino preferences:
|
||||
* https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json
|
||||
* Then you can install the BLEPeripheral library avaiable at:
|
||||
* https://github.com/sandeepmistry/arduino-BLEPeripheral
|
||||
* To test it, compile this example and use the UART module from the nRF
|
||||
* Toolbox App for Android. Edit the interface and send the characters
|
||||
* 'a' to 'i' to switch the animation.
|
||||
* There is a delay because this example blocks the thread of execution but
|
||||
* the change will be shown after the current animation ends. (This might
|
||||
* take a couple of seconds)
|
||||
* For more info write us at: info _at- teubi.co
|
||||
*/
|
||||
#include <SPI.h>
|
||||
#include <BLEPeripheral.h>
|
||||
#include "BLESerial.h"
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
|
||||
#define PIN 15 // Pin where NeoPixels are connected
|
||||
|
||||
// Declare our NeoPixel strip object:
|
||||
Adafruit_NeoPixel strip(64, PIN, NEO_GRB + NEO_KHZ800);
|
||||
// Argument 1 = Number of pixels in NeoPixel strip
|
||||
// Argument 2 = Arduino pin number (most are valid)
|
||||
// Argument 3 = Pixel type flags, add together as needed:
|
||||
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
|
||||
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
|
||||
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
|
||||
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
|
||||
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
|
||||
|
||||
// NEOPIXEL BEST PRACTICES for most reliable operation:
|
||||
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
|
||||
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
|
||||
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
|
||||
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
|
||||
// connect GROUND (-) first, then +, then data.
|
||||
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
|
||||
// a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
|
||||
// (Skipping these may work OK on your workbench but can fail in the field)
|
||||
|
||||
// define pins (varies per shield/board)
|
||||
#define BLE_REQ 10
|
||||
#define BLE_RDY 2
|
||||
#define BLE_RST 9
|
||||
|
||||
// create ble serial instance, see pinouts above
|
||||
BLESerial BLESerial(BLE_REQ, BLE_RDY, BLE_RST);
|
||||
|
||||
uint8_t current_state = 0;
|
||||
uint8_t rgb_values[3];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Hello World!");
|
||||
// custom services and characteristics can be added as well
|
||||
BLESerial.setLocalName("UART_HS");
|
||||
BLESerial.begin();
|
||||
|
||||
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
|
||||
strip.show(); // Turn OFF all pixels ASAP
|
||||
|
||||
//pinMode(PIN, OUTPUT);
|
||||
//digitalWrite(PIN, LOW);
|
||||
|
||||
current_state = 'a';
|
||||
}
|
||||
|
||||
void loop() {
|
||||
while(BLESerial.available()) {
|
||||
uint8_t character = BLESerial.read();
|
||||
switch(character) {
|
||||
case 'a':
|
||||
case 'b':
|
||||
case 'c':
|
||||
case 'd':
|
||||
case 'e':
|
||||
case 'f':
|
||||
case 'g':
|
||||
case 'h':
|
||||
current_state = character;
|
||||
break;
|
||||
};
|
||||
}
|
||||
switch(current_state) {
|
||||
case 'a':
|
||||
colorWipe(strip.Color(255, 0, 0), 20); // Red
|
||||
break;
|
||||
case 'b':
|
||||
colorWipe(strip.Color( 0, 255, 0), 20); // Green
|
||||
break;
|
||||
case 'c':
|
||||
colorWipe(strip.Color( 0, 0, 255), 20); // Blue
|
||||
break;
|
||||
case 'd':
|
||||
theaterChase(strip.Color(255, 0, 0), 20); // Red
|
||||
break;
|
||||
case 'e':
|
||||
theaterChase(strip.Color( 0, 255, 0), 20); // Green
|
||||
break;
|
||||
case 'f':
|
||||
theaterChase(strip.Color(255, 0, 255), 20); // Cyan
|
||||
break;
|
||||
case 'g':
|
||||
rainbow(10);
|
||||
break;
|
||||
case 'h':
|
||||
theaterChaseRainbow(20);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Fill strip pixels one after another with a color. Strip is NOT cleared
|
||||
// first; anything there will be covered pixel by pixel. Pass in color
|
||||
// (as a single 'packed' 32-bit value, which you can get by calling
|
||||
// strip.Color(red, green, blue) as shown in the loop() function above),
|
||||
// and a delay time (in milliseconds) between pixels.
|
||||
void colorWipe(uint32_t color, int wait) {
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
|
||||
strip.show(); // Update strip to match
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
|
||||
// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
|
||||
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
|
||||
// between frames.
|
||||
void theaterChase(uint32_t color, int wait) {
|
||||
for(int a=0; a<10; a++) { // Repeat 10 times...
|
||||
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
|
||||
strip.clear(); // Set all pixels in RAM to 0 (off)
|
||||
// 'c' counts up from 'b' to end of strip in steps of 3...
|
||||
for(int c=b; c<strip.numPixels(); c += 3) {
|
||||
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
|
||||
void rainbow(int wait) {
|
||||
// Hue of first pixel runs 5 complete loops through the color wheel.
|
||||
// Color wheel has a range of 65536 but it's OK if we roll over, so
|
||||
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
|
||||
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
|
||||
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
// Offset pixel hue by an amount to make one full revolution of the
|
||||
// color wheel (range of 65536) along the length of the strip
|
||||
// (strip.numPixels() steps):
|
||||
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
|
||||
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
|
||||
// optionally add saturation and value (brightness) (each 0 to 255).
|
||||
// Here we're using just the single-argument hue variant. The result
|
||||
// is passed through strip.gamma32() to provide 'truer' colors
|
||||
// before assigning to each pixel:
|
||||
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
|
||||
// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
|
||||
void theaterChaseRainbow(int wait) {
|
||||
int firstPixelHue = 0; // First pixel starts at red (hue 0)
|
||||
for(int a=0; a<30; a++) { // Repeat 30 times...
|
||||
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
|
||||
strip.clear(); // Set all pixels in RAM to 0 (off)
|
||||
// 'c' counts up from 'b' to end of strip in increments of 3...
|
||||
for(int c=b; c<strip.numPixels(); c += 3) {
|
||||
// hue of pixel 'c' is offset by an amount to make one full
|
||||
// revolution of the color wheel (range 65536) along the length
|
||||
// of the strip (strip.numPixels() steps):
|
||||
int hue = firstPixelHue + c * 65536L / strip.numPixels();
|
||||
uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
|
||||
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
// Simple demonstration on using an input device to trigger changes on your
|
||||
// NeoPixels. Wire a momentary push button to connect from ground to a
|
||||
// digital IO pin. When the button is pressed it will change to a new pixel
|
||||
// animation. Initial state has all pixels off -- press the button once to
|
||||
// start the first animation. As written, the button does not interrupt an
|
||||
// animation in-progress, it works only when idle.
|
||||
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#ifdef __AVR__
|
||||
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
|
||||
#endif
|
||||
|
||||
// Digital IO pin connected to the button. This will be driven with a
|
||||
// pull-up resistor so the switch pulls the pin to ground momentarily.
|
||||
// On a high -> low transition the button press logic will execute.
|
||||
#define BUTTON_PIN 2
|
||||
|
||||
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
|
||||
|
||||
#define PIXEL_COUNT 16 // Number of NeoPixels
|
||||
|
||||
// Declare our NeoPixel strip object:
|
||||
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
|
||||
// Argument 1 = Number of pixels in NeoPixel strip
|
||||
// Argument 2 = Arduino pin number (most are valid)
|
||||
// Argument 3 = Pixel type flags, add together as needed:
|
||||
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
|
||||
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
|
||||
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
|
||||
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
|
||||
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
|
||||
|
||||
boolean oldState = HIGH;
|
||||
int mode = 0; // Currently-active animation mode, 0-9
|
||||
|
||||
void setup() {
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
|
||||
strip.show(); // Initialize all pixels to 'off'
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Get current button state.
|
||||
boolean newState = digitalRead(BUTTON_PIN);
|
||||
|
||||
// Check if state changed from high to low (button press).
|
||||
if((newState == LOW) && (oldState == HIGH)) {
|
||||
// Short delay to debounce button.
|
||||
delay(20);
|
||||
// Check if button is still low after debounce.
|
||||
newState = digitalRead(BUTTON_PIN);
|
||||
if(newState == LOW) { // Yes, still low
|
||||
if(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8
|
||||
switch(mode) { // Start the new animation...
|
||||
case 0:
|
||||
colorWipe(strip.Color( 0, 0, 0), 50); // Black/off
|
||||
break;
|
||||
case 1:
|
||||
colorWipe(strip.Color(255, 0, 0), 50); // Red
|
||||
break;
|
||||
case 2:
|
||||
colorWipe(strip.Color( 0, 255, 0), 50); // Green
|
||||
break;
|
||||
case 3:
|
||||
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
|
||||
break;
|
||||
case 4:
|
||||
theaterChase(strip.Color(127, 127, 127), 50); // White
|
||||
break;
|
||||
case 5:
|
||||
theaterChase(strip.Color(127, 0, 0), 50); // Red
|
||||
break;
|
||||
case 6:
|
||||
theaterChase(strip.Color( 0, 0, 127), 50); // Blue
|
||||
break;
|
||||
case 7:
|
||||
rainbow(10);
|
||||
break;
|
||||
case 8:
|
||||
theaterChaseRainbow(50);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set the last-read button state to the old state.
|
||||
oldState = newState;
|
||||
}
|
||||
|
||||
// Fill strip pixels one after another with a color. Strip is NOT cleared
|
||||
// first; anything there will be covered pixel by pixel. Pass in color
|
||||
// (as a single 'packed' 32-bit value, which you can get by calling
|
||||
// strip.Color(red, green, blue) as shown in the loop() function above),
|
||||
// and a delay time (in milliseconds) between pixels.
|
||||
void colorWipe(uint32_t color, int wait) {
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
|
||||
strip.show(); // Update strip to match
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
|
||||
// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
|
||||
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
|
||||
// between frames.
|
||||
void theaterChase(uint32_t color, int wait) {
|
||||
for(int a=0; a<10; a++) { // Repeat 10 times...
|
||||
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
|
||||
strip.clear(); // Set all pixels in RAM to 0 (off)
|
||||
// 'c' counts up from 'b' to end of strip in steps of 3...
|
||||
for(int c=b; c<strip.numPixels(); c += 3) {
|
||||
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
|
||||
void rainbow(int wait) {
|
||||
// Hue of first pixel runs 3 complete loops through the color wheel.
|
||||
// Color wheel has a range of 65536 but it's OK if we roll over, so
|
||||
// just count from 0 to 3*65536. Adding 256 to firstPixelHue each time
|
||||
// means we'll make 3*65536/256 = 768 passes through this outer loop:
|
||||
for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
// Offset pixel hue by an amount to make one full revolution of the
|
||||
// color wheel (range of 65536) along the length of the strip
|
||||
// (strip.numPixels() steps):
|
||||
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
|
||||
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
|
||||
// optionally add saturation and value (brightness) (each 0 to 255).
|
||||
// Here we're using just the single-argument hue variant. The result
|
||||
// is passed through strip.gamma32() to provide 'truer' colors
|
||||
// before assigning to each pixel:
|
||||
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
|
||||
// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
|
||||
void theaterChaseRainbow(int wait) {
|
||||
int firstPixelHue = 0; // First pixel starts at red (hue 0)
|
||||
for(int a=0; a<30; a++) { // Repeat 30 times...
|
||||
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
|
||||
strip.clear(); // Set all pixels in RAM to 0 (off)
|
||||
// 'c' counts up from 'b' to end of strip in increments of 3...
|
||||
for(int c=b; c<strip.numPixels(); c += 3) {
|
||||
// hue of pixel 'c' is offset by an amount to make one full
|
||||
// revolution of the color wheel (range 65536) along the length
|
||||
// of the strip (strip.numPixels() steps):
|
||||
int hue = firstPixelHue + c * 65536L / strip.numPixels();
|
||||
uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
|
||||
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
|
||||
}
|
||||
}
|
||||
}
|
50
libraries/Adafruit_NeoPixel/examples/simple/simple.ino
Normal file
50
libraries/Adafruit_NeoPixel/examples/simple/simple.ino
Normal file
@ -0,0 +1,50 @@
|
||||
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
|
||||
// Released under the GPLv3 license to match the rest of the
|
||||
// Adafruit NeoPixel library
|
||||
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#ifdef __AVR__
|
||||
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
|
||||
#endif
|
||||
|
||||
// Which pin on the Arduino is connected to the NeoPixels?
|
||||
#define PIN 6 // On Trinket or Gemma, suggest changing this to 1
|
||||
|
||||
// How many NeoPixels are attached to the Arduino?
|
||||
#define NUMPIXELS 16 // Popular NeoPixel ring size
|
||||
|
||||
// When setting up the NeoPixel library, we tell it how many pixels,
|
||||
// and which pin to use to send signals. Note that for older NeoPixel
|
||||
// strips you might need to change the third parameter -- see the
|
||||
// strandtest example for more information on possible values.
|
||||
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
|
||||
|
||||
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
|
||||
|
||||
void setup() {
|
||||
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
|
||||
// Any other board, you can remove this part (but no harm leaving it):
|
||||
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
|
||||
clock_prescale_set(clock_div_1);
|
||||
#endif
|
||||
// END of Trinket-specific code.
|
||||
|
||||
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
|
||||
}
|
||||
|
||||
void loop() {
|
||||
pixels.clear(); // Set all pixel colors to 'off'
|
||||
|
||||
// The first NeoPixel in a strand is #0, second is 1, all the way up
|
||||
// to the count of pixels minus one.
|
||||
for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
|
||||
|
||||
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
|
||||
// Here we're using a moderately bright green color:
|
||||
pixels.setPixelColor(i, pixels.Color(0, 150, 0));
|
||||
|
||||
pixels.show(); // Send the updated pixel colors to the hardware.
|
||||
|
||||
delay(DELAYVAL); // Pause before next pass through loop
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
|
||||
// Released under the GPLv3 license to match the rest of the
|
||||
// Adafruit NeoPixel library
|
||||
// This sketch shows use of the "new" operator with Adafruit_NeoPixel.
|
||||
// It's helpful if you don't know NeoPixel settings at compile time or
|
||||
// just want to store this settings in EEPROM or a file on an SD card.
|
||||
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#ifdef __AVR__
|
||||
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
|
||||
#endif
|
||||
|
||||
// Which pin on the Arduino is connected to the NeoPixels?
|
||||
int pin = 6; // On Trinket or Gemma, suggest changing this to 1
|
||||
|
||||
// How many NeoPixels are attached to the Arduino?
|
||||
int numPixels = 16; // Popular NeoPixel ring size
|
||||
|
||||
// NeoPixel color format & data rate. See the strandtest example for
|
||||
// information on possible values.
|
||||
int pixelFormat = NEO_GRB + NEO_KHZ800;
|
||||
|
||||
// Rather than declaring the whole NeoPixel object here, we just create
|
||||
// a pointer for one, which we'll then allocate later...
|
||||
Adafruit_NeoPixel *pixels;
|
||||
|
||||
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
|
||||
|
||||
void setup() {
|
||||
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
|
||||
// Any other board, you can remove this part (but no harm leaving it):
|
||||
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
|
||||
clock_prescale_set(clock_div_1);
|
||||
#endif
|
||||
// END of Trinket-specific code.
|
||||
|
||||
// Right about here is where we could read 'pin', 'numPixels' and/or
|
||||
// 'pixelFormat' from EEPROM or a file on SD or whatever. This is a simple
|
||||
// example and doesn't do that -- those variables are just set to fixed
|
||||
// values at the top of this code -- but this is where it would happen.
|
||||
|
||||
// Then create a new NeoPixel object dynamically with these values:
|
||||
pixels = new Adafruit_NeoPixel(numPixels, pin, pixelFormat);
|
||||
|
||||
// Going forward from here, code works almost identically to any other
|
||||
// NeoPixel example, but instead of the dot operator on function calls
|
||||
// (e.g. pixels.begin()), we instead use pointer indirection (->) like so:
|
||||
pixels->begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
|
||||
// You'll see more of this in the loop() function below.
|
||||
}
|
||||
|
||||
void loop() {
|
||||
pixels->clear(); // Set all pixel colors to 'off'
|
||||
|
||||
// The first NeoPixel in a strand is #0, second is 1, all the way up
|
||||
// to the count of pixels minus one.
|
||||
for(int i=0; i<numPixels; i++) { // For each pixel...
|
||||
|
||||
// pixels->Color() takes RGB values, from 0,0,0 up to 255,255,255
|
||||
// Here we're using a moderately bright green color:
|
||||
pixels->setPixelColor(i, pixels->Color(0, 150, 0));
|
||||
|
||||
pixels->show(); // Send the updated pixel colors to the hardware.
|
||||
|
||||
delay(DELAYVAL); // Pause before next pass through loop
|
||||
}
|
||||
}
|
147
libraries/Adafruit_NeoPixel/examples/strandtest/strandtest.ino
Normal file
147
libraries/Adafruit_NeoPixel/examples/strandtest/strandtest.ino
Normal file
@ -0,0 +1,147 @@
|
||||
// A basic everyday NeoPixel strip test program.
|
||||
|
||||
// NEOPIXEL BEST PRACTICES for most reliable operation:
|
||||
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
|
||||
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
|
||||
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
|
||||
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
|
||||
// connect GROUND (-) first, then +, then data.
|
||||
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
|
||||
// a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
|
||||
// (Skipping these may work OK on your workbench but can fail in the field)
|
||||
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#ifdef __AVR__
|
||||
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
|
||||
#endif
|
||||
|
||||
// Which pin on the Arduino is connected to the NeoPixels?
|
||||
// On a Trinket or Gemma we suggest changing this to 1:
|
||||
#define LED_PIN 6
|
||||
|
||||
// How many NeoPixels are attached to the Arduino?
|
||||
#define LED_COUNT 60
|
||||
|
||||
// Declare our NeoPixel strip object:
|
||||
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
|
||||
// Argument 1 = Number of pixels in NeoPixel strip
|
||||
// Argument 2 = Arduino pin number (most are valid)
|
||||
// Argument 3 = Pixel type flags, add together as needed:
|
||||
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
|
||||
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
|
||||
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
|
||||
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
|
||||
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
|
||||
|
||||
|
||||
// setup() function -- runs once at startup --------------------------------
|
||||
|
||||
void setup() {
|
||||
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
|
||||
// Any other board, you can remove this part (but no harm leaving it):
|
||||
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
|
||||
clock_prescale_set(clock_div_1);
|
||||
#endif
|
||||
// END of Trinket-specific code.
|
||||
|
||||
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
|
||||
strip.show(); // Turn OFF all pixels ASAP
|
||||
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
|
||||
}
|
||||
|
||||
|
||||
// loop() function -- runs repeatedly as long as board is on ---------------
|
||||
|
||||
void loop() {
|
||||
// Fill along the length of the strip in various colors...
|
||||
colorWipe(strip.Color(255, 0, 0), 50); // Red
|
||||
colorWipe(strip.Color( 0, 255, 0), 50); // Green
|
||||
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
|
||||
|
||||
// Do a theater marquee effect in various colors...
|
||||
theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
|
||||
theaterChase(strip.Color(127, 0, 0), 50); // Red, half brightness
|
||||
theaterChase(strip.Color( 0, 0, 127), 50); // Blue, half brightness
|
||||
|
||||
rainbow(10); // Flowing rainbow cycle along the whole strip
|
||||
theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
|
||||
}
|
||||
|
||||
|
||||
// Some functions of our own for creating animated effects -----------------
|
||||
|
||||
// Fill strip pixels one after another with a color. Strip is NOT cleared
|
||||
// first; anything there will be covered pixel by pixel. Pass in color
|
||||
// (as a single 'packed' 32-bit value, which you can get by calling
|
||||
// strip.Color(red, green, blue) as shown in the loop() function above),
|
||||
// and a delay time (in milliseconds) between pixels.
|
||||
void colorWipe(uint32_t color, int wait) {
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
|
||||
strip.show(); // Update strip to match
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
|
||||
// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
|
||||
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
|
||||
// between frames.
|
||||
void theaterChase(uint32_t color, int wait) {
|
||||
for(int a=0; a<10; a++) { // Repeat 10 times...
|
||||
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
|
||||
strip.clear(); // Set all pixels in RAM to 0 (off)
|
||||
// 'c' counts up from 'b' to end of strip in steps of 3...
|
||||
for(int c=b; c<strip.numPixels(); c += 3) {
|
||||
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
|
||||
void rainbow(int wait) {
|
||||
// Hue of first pixel runs 5 complete loops through the color wheel.
|
||||
// Color wheel has a range of 65536 but it's OK if we roll over, so
|
||||
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
|
||||
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
|
||||
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
|
||||
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
|
||||
// Offset pixel hue by an amount to make one full revolution of the
|
||||
// color wheel (range of 65536) along the length of the strip
|
||||
// (strip.numPixels() steps):
|
||||
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
|
||||
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
|
||||
// optionally add saturation and value (brightness) (each 0 to 255).
|
||||
// Here we're using just the single-argument hue variant. The result
|
||||
// is passed through strip.gamma32() to provide 'truer' colors
|
||||
// before assigning to each pixel:
|
||||
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
}
|
||||
}
|
||||
|
||||
// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
|
||||
void theaterChaseRainbow(int wait) {
|
||||
int firstPixelHue = 0; // First pixel starts at red (hue 0)
|
||||
for(int a=0; a<30; a++) { // Repeat 30 times...
|
||||
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
|
||||
strip.clear(); // Set all pixels in RAM to 0 (off)
|
||||
// 'c' counts up from 'b' to end of strip in increments of 3...
|
||||
for(int c=b; c<strip.numPixels(); c += 3) {
|
||||
// hue of pixel 'c' is offset by an amount to make one full
|
||||
// revolution of the color wheel (range 65536) along the length
|
||||
// of the strip (strip.numPixels() steps):
|
||||
int hue = firstPixelHue + c * 65536L / strip.numPixels();
|
||||
uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
|
||||
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
|
||||
}
|
||||
strip.show(); // Update strip with new contents
|
||||
delay(wait); // Pause for a moment
|
||||
firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
|
||||
}
|
||||
}
|
||||
}
|
72
libraries/Adafruit_NeoPixel/keywords.txt
Normal file
72
libraries/Adafruit_NeoPixel/keywords.txt
Normal file
@ -0,0 +1,72 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For Adafruit_NeoPixel
|
||||
#######################################
|
||||
# Class
|
||||
#######################################
|
||||
|
||||
Adafruit_NeoPixel KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
show KEYWORD2
|
||||
setPin KEYWORD2
|
||||
setPixelColor KEYWORD2
|
||||
fill KEYWORD2
|
||||
setBrightness KEYWORD2
|
||||
clear KEYWORD2
|
||||
updateLength KEYWORD2
|
||||
updateType KEYWORD2
|
||||
canShow KEYWORD2
|
||||
getPixels KEYWORD2
|
||||
getBrightness KEYWORD2
|
||||
getPin KEYWORD2
|
||||
numPixels KEYWORD2
|
||||
getPixelColor KEYWORD2
|
||||
sine8 KEYWORD2
|
||||
gamma8 KEYWORD2
|
||||
Color KEYWORD2
|
||||
ColorHSV KEYWORD2
|
||||
gamma32 KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants
|
||||
#######################################
|
||||
|
||||
NEO_COLMASK LITERAL1
|
||||
NEO_SPDMASK LITERAL1
|
||||
NEO_KHZ800 LITERAL1
|
||||
NEO_KHZ400 LITERAL1
|
||||
NEO_RGB LITERAL1
|
||||
NEO_RBG LITERAL1
|
||||
NEO_GRB LITERAL1
|
||||
NEO_GBR LITERAL1
|
||||
NEO_BRG LITERAL1
|
||||
NEO_BGR LITERAL1
|
||||
NEO_WRGB LITERAL1
|
||||
NEO_WRBG LITERAL1
|
||||
NEO_WGRB LITERAL1
|
||||
NEO_WGBR LITERAL1
|
||||
NEO_WBRG LITERAL1
|
||||
NEO_WBGR LITERAL1
|
||||
NEO_RWGB LITERAL1
|
||||
NEO_RWBG LITERAL1
|
||||
NEO_RGWB LITERAL1
|
||||
NEO_RGBW LITERAL1
|
||||
NEO_RBWG LITERAL1
|
||||
NEO_RBGW LITERAL1
|
||||
NEO_GWRB LITERAL1
|
||||
NEO_GWBR LITERAL1
|
||||
NEO_GRWB LITERAL1
|
||||
NEO_GRBW LITERAL1
|
||||
NEO_GBWR LITERAL1
|
||||
NEO_GBRW LITERAL1
|
||||
NEO_BWRG LITERAL1
|
||||
NEO_BWGR LITERAL1
|
||||
NEO_BRWG LITERAL1
|
||||
NEO_BRGW LITERAL1
|
||||
NEO_BGWR LITERAL1
|
||||
NEO_BGRW LITERAL1
|
||||
|
9
libraries/Adafruit_NeoPixel/library.properties
Normal file
9
libraries/Adafruit_NeoPixel/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=Adafruit NeoPixel
|
||||
version=1.2.1
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=Arduino library for controlling single-wire-based LED pixels and strip.
|
||||
paragraph=Arduino library for controlling single-wire-based LED pixels and strip.
|
||||
category=Display
|
||||
url=https://github.com/adafruit/Adafruit_NeoPixel
|
||||
architectures=*
|
1
libraries/esp8266boilerplate
Submodule
1
libraries/esp8266boilerplate
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 749b1653a8774f5bce576f6d91513c36382c8d5d
|
1
libraries/esp8266mqttboilerplate
Submodule
1
libraries/esp8266mqttboilerplate
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit b20b46b2ca317730ee429eb2a6b07769d1312de3
|
670
libraries/pubsubclient/PubSubClient.cpp
Normal file
670
libraries/pubsubclient/PubSubClient.cpp
Normal file
@ -0,0 +1,670 @@
|
||||
/*
|
||||
PubSubClient.cpp - A simple client for MQTT.
|
||||
Nick O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#include "PubSubClient.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
#ifdef ESP8266
|
||||
#define INIT_FINGERPRINT() this->fingerprint = NULL;
|
||||
#else
|
||||
#define INIT_FINGERPRINT()
|
||||
#endif
|
||||
|
||||
PubSubClient::PubSubClient() {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
this->_client = NULL;
|
||||
this->stream = NULL;
|
||||
setCallback(NULL);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
PubSubClient::PubSubClient(WiFiClientSecure& client, const char* fingerprint) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
this->fingerprint = fingerprint;
|
||||
}
|
||||
#endif
|
||||
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr, port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr, port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr,port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip, port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip, port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip,port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setCallback(callback);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->_available = 0;
|
||||
INIT_FINGERPRINT()
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id) {
|
||||
return connect(id,NULL,NULL,0,0,0,0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char *user, const char *pass) {
|
||||
return connect(id,user,pass,0,0,0,0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
|
||||
return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
|
||||
if (!connected()) {
|
||||
int result = 0;
|
||||
|
||||
if (domain != NULL) {
|
||||
result = _client->connect(this->domain, this->port);
|
||||
} else {
|
||||
result = _client->connect(this->ip, this->port);
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
if (fingerprint != NULL) {
|
||||
if (domain != NULL) {
|
||||
// there's only one way to set fingerprint: using the WiFiClientSecure-based constructor, so this cast is safe
|
||||
if (!static_cast<WiFiClientSecure*>(_client)->verify(fingerprint, domain)) {
|
||||
_state = MQTT_TLS_BAD_SERVER_CREDENTIALS;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
char buffer[16]; // IPv4 only (which is what IPAddress supports anyway)
|
||||
|
||||
ip.toString().toCharArray(buffer, 16);
|
||||
|
||||
if (!static_cast<WiFiClientSecure*>(_client)->verify(fingerprint, buffer)) {
|
||||
_state = MQTT_TLS_BAD_SERVER_CREDENTIALS;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (result == 1) {
|
||||
nextMsgId = 1;
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 5;
|
||||
unsigned int j;
|
||||
|
||||
#if MQTT_VERSION == MQTT_VERSION_3_1
|
||||
uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p', MQTT_VERSION};
|
||||
#define MQTT_HEADER_VERSION_LENGTH 9
|
||||
#elif MQTT_VERSION == MQTT_VERSION_3_1_1
|
||||
uint8_t d[7] = {0x00,0x04,'M','Q','T','T',MQTT_VERSION};
|
||||
#define MQTT_HEADER_VERSION_LENGTH 7
|
||||
#endif
|
||||
for (j = 0;j<MQTT_HEADER_VERSION_LENGTH;j++) {
|
||||
buffer[length++] = d[j];
|
||||
}
|
||||
|
||||
uint8_t v;
|
||||
if (willTopic) {
|
||||
v = 0x06|(willQos<<3)|(willRetain<<5);
|
||||
} else {
|
||||
v = 0x02;
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
v = v|0x80;
|
||||
|
||||
if(pass != NULL) {
|
||||
v = v|(0x80>>1);
|
||||
}
|
||||
}
|
||||
|
||||
buffer[length++] = v;
|
||||
|
||||
buffer[length++] = ((MQTT_KEEPALIVE) >> 8);
|
||||
buffer[length++] = ((MQTT_KEEPALIVE) & 0xFF);
|
||||
length = writeString(id,buffer,length);
|
||||
if (willTopic) {
|
||||
length = writeString(willTopic,buffer,length);
|
||||
length = writeString(willMessage,buffer,length);
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
length = writeString(user,buffer,length);
|
||||
if(pass != NULL) {
|
||||
length = writeString(pass,buffer,length);
|
||||
}
|
||||
}
|
||||
|
||||
write(MQTTCONNECT,buffer,length-5);
|
||||
|
||||
lastInActivity = lastOutActivity = millis();
|
||||
|
||||
while (!available()) {
|
||||
unsigned long t = millis();
|
||||
if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) {
|
||||
_state = MQTT_CONNECTION_TIMEOUT;
|
||||
_client->stop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
uint8_t llen;
|
||||
uint16_t len = readPacket(&llen);
|
||||
|
||||
if (len == 4) {
|
||||
if (buffer[3] == 0) {
|
||||
lastInActivity = millis();
|
||||
pingOutstanding = false;
|
||||
_state = MQTT_CONNECTED;
|
||||
return true;
|
||||
} else {
|
||||
_state = buffer[3];
|
||||
}
|
||||
}
|
||||
_client->stop();
|
||||
} else {
|
||||
_state = MQTT_CONNECT_FAILED;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// return and cache the available number of bytes in the client;
|
||||
// remember to reduce the available count when consuming the buffer
|
||||
int PubSubClient::available() {
|
||||
if (_available == 0) {
|
||||
_available = _client->available();
|
||||
}
|
||||
return _available;
|
||||
}
|
||||
|
||||
// reads a byte into result
|
||||
boolean PubSubClient::readByte(uint8_t * result) {
|
||||
uint32_t previousMillis = millis();
|
||||
while(!available()) {
|
||||
uint32_t currentMillis = millis();
|
||||
if(currentMillis - previousMillis >= ((int32_t) MQTT_SOCKET_TIMEOUT * 1000)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
*result = _client->read();
|
||||
_available -= 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
// reads a byte into result[*index] and increments index
|
||||
boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
|
||||
uint16_t current_index = *index;
|
||||
uint8_t * write_address = &(result[current_index]);
|
||||
if(readByte(write_address)){
|
||||
*index = current_index + 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
||||
uint16_t len = 0;
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH;
|
||||
uint32_t multiplier = 1;
|
||||
uint16_t length = 0;
|
||||
uint8_t digit = 0;
|
||||
uint16_t skip = 0;
|
||||
uint8_t start = 0;
|
||||
|
||||
do {
|
||||
if(!readByte(&digit)) return 0;
|
||||
buffer[len++] = digit;
|
||||
length += (digit & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
} while ((digit & 128) != 0);
|
||||
*lengthLength = len-1;
|
||||
|
||||
if (isPublish) {
|
||||
// Read in topic length to calculate bytes to skip over for Stream writing
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
if(!readByte(buffer, &len)) return 0;
|
||||
skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2];
|
||||
start = 2;
|
||||
if (buffer[0]&MQTTQOS1) {
|
||||
// skip message id
|
||||
skip += 2;
|
||||
}
|
||||
}
|
||||
|
||||
for (uint16_t i = start;i<length;i++) {
|
||||
if(!readByte(&digit)) return 0;
|
||||
if (this->stream) {
|
||||
if (isPublish && len-*lengthLength-2>skip) {
|
||||
this->stream->write(digit);
|
||||
}
|
||||
}
|
||||
if (len < MQTT_MAX_PACKET_SIZE) {
|
||||
buffer[len] = digit;
|
||||
}
|
||||
len++;
|
||||
}
|
||||
|
||||
if (!this->stream && len > MQTT_MAX_PACKET_SIZE) {
|
||||
len = 0; // This will cause the packet to be ignored.
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
boolean PubSubClient::loop() {
|
||||
if (connected()) {
|
||||
do {
|
||||
unsigned long t = millis();
|
||||
if ((t - lastInActivity > MQTT_KEEPALIVE*1000UL) || (t - lastOutActivity > MQTT_KEEPALIVE*1000UL)) {
|
||||
if (pingOutstanding) {
|
||||
this->_state = MQTT_CONNECTION_TIMEOUT;
|
||||
_client->stop();
|
||||
return false;
|
||||
} else {
|
||||
buffer[0] = MQTTPINGREQ;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
lastOutActivity = t;
|
||||
lastInActivity = t;
|
||||
pingOutstanding = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (available()) {
|
||||
uint8_t llen;
|
||||
uint16_t len = readPacket(&llen);
|
||||
uint16_t msgId = 0;
|
||||
uint8_t *payload;
|
||||
if (len > 0) {
|
||||
lastInActivity = t;
|
||||
uint8_t type = buffer[0]&0xF0;
|
||||
if (type == MQTTPUBLISH) {
|
||||
if (callback) {
|
||||
uint16_t tl = (buffer[llen+1]<<8)+buffer[llen+2]; /* topic length in bytes */
|
||||
memmove(buffer+llen+2,buffer+llen+3,tl); /* move topic inside buffer 1 byte to front */
|
||||
buffer[llen+2+tl] = 0; /* end the topic as a 'C' string with \x00 */
|
||||
char *topic = (char*) buffer+llen+2;
|
||||
// msgId only present for QOS>0
|
||||
if ((buffer[0]&0x06) == MQTTQOS1) {
|
||||
msgId = (buffer[llen+3+tl]<<8)+buffer[llen+3+tl+1];
|
||||
payload = buffer+llen+3+tl+2;
|
||||
callback(topic,payload,len-llen-3-tl-2);
|
||||
|
||||
buffer[0] = MQTTPUBACK;
|
||||
buffer[1] = 2;
|
||||
buffer[2] = (msgId >> 8);
|
||||
buffer[3] = (msgId & 0xFF);
|
||||
_client->write(buffer,4);
|
||||
lastOutActivity = t;
|
||||
|
||||
} else {
|
||||
payload = buffer+llen+3+tl;
|
||||
callback(topic,payload,len-llen-3-tl);
|
||||
}
|
||||
}
|
||||
} else if (type == MQTTPINGREQ) {
|
||||
buffer[0] = MQTTPINGRESP;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
} else if (type == MQTTPINGRESP) {
|
||||
pingOutstanding = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (_available > 0); // can't leave data in the buffer, or subsequent publish() calls
|
||||
// may fail (axTLS is only half-duplex, so writes will fail, to
|
||||
// avoid losing information)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const char* payload) {
|
||||
return publish(topic,(const uint8_t*)payload,strlen(payload),false);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) {
|
||||
return publish(topic,(const uint8_t*)payload,strlen(payload),retained);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
|
||||
return publish(topic, payload, plength, false);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
|
||||
if (connected()) {
|
||||
if (MQTT_MAX_PACKET_SIZE < 5 + 2+strlen(topic) + plength) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 5;
|
||||
length = writeString(topic,buffer,length);
|
||||
uint16_t i;
|
||||
for (i=0;i<plength;i++) {
|
||||
buffer[length++] = payload[i];
|
||||
}
|
||||
uint8_t header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
return write(header,buffer,length-5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsigned int plength, boolean retained) {
|
||||
uint8_t llen = 0;
|
||||
uint8_t digit;
|
||||
unsigned int rc = 0;
|
||||
uint16_t tlen;
|
||||
unsigned int pos = 0;
|
||||
unsigned int i;
|
||||
uint8_t header;
|
||||
unsigned int len;
|
||||
|
||||
if (!connected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
tlen = strlen(topic);
|
||||
|
||||
header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
buffer[pos++] = header;
|
||||
len = plength + 2 + tlen;
|
||||
do {
|
||||
digit = len % 128;
|
||||
len = len / 128;
|
||||
if (len > 0) {
|
||||
digit |= 0x80;
|
||||
}
|
||||
buffer[pos++] = digit;
|
||||
llen++;
|
||||
} while(len>0);
|
||||
|
||||
pos = writeString(topic,buffer,pos);
|
||||
|
||||
rc += _client->write(buffer,pos);
|
||||
|
||||
for (i=0;i<plength;i++) {
|
||||
rc += _client->write((char)pgm_read_byte_near(payload + i));
|
||||
}
|
||||
|
||||
lastOutActivity = millis();
|
||||
|
||||
return rc == tlen + 4 + plength;
|
||||
}
|
||||
|
||||
boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
||||
uint8_t lenBuf[4];
|
||||
uint8_t llen = 0;
|
||||
uint8_t digit;
|
||||
uint8_t pos = 0;
|
||||
uint16_t rc;
|
||||
uint16_t len = length;
|
||||
do {
|
||||
digit = len % 128;
|
||||
len = len / 128;
|
||||
if (len > 0) {
|
||||
digit |= 0x80;
|
||||
}
|
||||
lenBuf[pos++] = digit;
|
||||
llen++;
|
||||
} while(len>0);
|
||||
|
||||
buf[4-llen] = header;
|
||||
for (int i=0;i<llen;i++) {
|
||||
buf[5-llen+i] = lenBuf[i];
|
||||
}
|
||||
|
||||
#ifdef MQTT_MAX_TRANSFER_SIZE
|
||||
uint8_t* writeBuf = buf+(4-llen);
|
||||
uint16_t bytesRemaining = length+1+llen; //Match the length type
|
||||
uint8_t bytesToWrite;
|
||||
boolean result = true;
|
||||
while((bytesRemaining > 0) && result) {
|
||||
bytesToWrite = (bytesRemaining > MQTT_MAX_TRANSFER_SIZE)?MQTT_MAX_TRANSFER_SIZE:bytesRemaining;
|
||||
rc = _client->write(writeBuf,bytesToWrite);
|
||||
result = (rc == bytesToWrite);
|
||||
bytesRemaining -= rc;
|
||||
writeBuf += rc;
|
||||
}
|
||||
return result;
|
||||
#else
|
||||
rc = _client->write(buf+(4-llen),length+1+llen);
|
||||
lastOutActivity = millis();
|
||||
return (rc == 1+llen+length);
|
||||
#endif
|
||||
}
|
||||
|
||||
boolean PubSubClient::subscribe(const char* topic) {
|
||||
return subscribe(topic, 0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
|
||||
if (qos < 0 || qos > 1) {
|
||||
return false;
|
||||
}
|
||||
if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
if (connected()) {
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 5;
|
||||
nextMsgId++;
|
||||
if (nextMsgId == 0) {
|
||||
nextMsgId = 1;
|
||||
}
|
||||
buffer[length++] = (nextMsgId >> 8);
|
||||
buffer[length++] = (nextMsgId & 0xFF);
|
||||
length = writeString((char*)topic, buffer,length);
|
||||
buffer[length++] = qos;
|
||||
return write(MQTTSUBSCRIBE|MQTTQOS1,buffer,length-5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::unsubscribe(const char* topic) {
|
||||
if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
if (connected()) {
|
||||
uint16_t length = 5;
|
||||
nextMsgId++;
|
||||
if (nextMsgId == 0) {
|
||||
nextMsgId = 1;
|
||||
}
|
||||
buffer[length++] = (nextMsgId >> 8);
|
||||
buffer[length++] = (nextMsgId & 0xFF);
|
||||
length = writeString(topic, buffer,length);
|
||||
return write(MQTTUNSUBSCRIBE|MQTTQOS1,buffer,length-5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void PubSubClient::disconnect() {
|
||||
buffer[0] = MQTTDISCONNECT;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
_state = MQTT_DISCONNECTED;
|
||||
_client->stop();
|
||||
lastInActivity = lastOutActivity = millis();
|
||||
}
|
||||
|
||||
uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) {
|
||||
const char* idp = string;
|
||||
uint16_t i = 0;
|
||||
pos += 2;
|
||||
while (*idp) {
|
||||
buf[pos++] = *idp++;
|
||||
i++;
|
||||
}
|
||||
buf[pos-i-2] = (i >> 8);
|
||||
buf[pos-i-1] = (i & 0xFF);
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
boolean PubSubClient::connected() {
|
||||
boolean rc;
|
||||
if (_client == NULL ) {
|
||||
rc = false;
|
||||
} else {
|
||||
rc = (int)_client->connected();
|
||||
if (!rc) {
|
||||
if (this->_state == MQTT_CONNECTED) {
|
||||
this->_state = MQTT_CONNECTION_LOST;
|
||||
_client->flush();
|
||||
_client->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setServer(uint8_t * ip, uint16_t port) {
|
||||
IPAddress addr(ip[0],ip[1],ip[2],ip[3]);
|
||||
return setServer(addr,port);
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setServer(IPAddress ip, uint16_t port) {
|
||||
this->ip = ip;
|
||||
this->port = port;
|
||||
this->domain = NULL;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setServer(const char * domain, uint16_t port) {
|
||||
this->domain = domain;
|
||||
this->port = port;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setCallback(MQTT_CALLBACK_SIGNATURE) {
|
||||
this->callback = callback;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setClient(Client& client){
|
||||
this->_client = &client;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PubSubClient& PubSubClient::setStream(Stream& stream){
|
||||
this->stream = &stream;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int PubSubClient::state() {
|
||||
return this->_state;
|
||||
}
|
160
libraries/pubsubclient/PubSubClient.h
Normal file
160
libraries/pubsubclient/PubSubClient.h
Normal file
@ -0,0 +1,160 @@
|
||||
/*
|
||||
PubSubClient.h - A simple client for MQTT.
|
||||
Nick O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#ifndef PubSubClient_h
|
||||
#define PubSubClient_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "IPAddress.h"
|
||||
#include "Client.h"
|
||||
#include "Stream.h"
|
||||
|
||||
#ifdef ESP8266
|
||||
#include "WiFiClientSecure.h"
|
||||
#endif
|
||||
|
||||
#define MQTT_VERSION_3_1 3
|
||||
#define MQTT_VERSION_3_1_1 4
|
||||
|
||||
// MQTT_VERSION : Pick the version
|
||||
//#define MQTT_VERSION MQTT_VERSION_3_1
|
||||
#ifndef MQTT_VERSION
|
||||
#define MQTT_VERSION MQTT_VERSION_3_1_1
|
||||
#endif
|
||||
|
||||
// MQTT_MAX_PACKET_SIZE : Maximum packet size
|
||||
#ifndef MQTT_MAX_PACKET_SIZE
|
||||
#define MQTT_MAX_PACKET_SIZE 128
|
||||
#endif
|
||||
|
||||
// MQTT_KEEPALIVE : keepAlive interval in Seconds
|
||||
#ifndef MQTT_KEEPALIVE
|
||||
#define MQTT_KEEPALIVE 15
|
||||
#endif
|
||||
|
||||
// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
|
||||
#ifndef MQTT_SOCKET_TIMEOUT
|
||||
#define MQTT_SOCKET_TIMEOUT 15
|
||||
#endif
|
||||
|
||||
// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
|
||||
// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
|
||||
// pass the entire MQTT packet in each write call.
|
||||
//#define MQTT_MAX_TRANSFER_SIZE 80
|
||||
|
||||
// Possible values for client.state()
|
||||
#define MQTT_TLS_BAD_SERVER_CREDENTIALS -5
|
||||
#define MQTT_CONNECTION_TIMEOUT -4
|
||||
#define MQTT_CONNECTION_LOST -3
|
||||
#define MQTT_CONNECT_FAILED -2
|
||||
#define MQTT_DISCONNECTED -1
|
||||
#define MQTT_CONNECTED 0
|
||||
#define MQTT_CONNECT_BAD_PROTOCOL 1
|
||||
#define MQTT_CONNECT_BAD_CLIENT_ID 2
|
||||
#define MQTT_CONNECT_UNAVAILABLE 3
|
||||
#define MQTT_CONNECT_BAD_CREDENTIALS 4
|
||||
#define MQTT_CONNECT_UNAUTHORIZED 5
|
||||
|
||||
#define MQTTCONNECT 1 << 4 // Client request to connect to Server
|
||||
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
|
||||
#define MQTTPUBLISH 3 << 4 // Publish message
|
||||
#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
|
||||
#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
|
||||
#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
|
||||
#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
|
||||
#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
|
||||
#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
|
||||
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
|
||||
#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
|
||||
#define MQTTPINGREQ 12 << 4 // PING Request
|
||||
#define MQTTPINGRESP 13 << 4 // PING Response
|
||||
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
|
||||
#define MQTTReserved 15 << 4 // Reserved
|
||||
|
||||
#define MQTTQOS0 (0 << 1)
|
||||
#define MQTTQOS1 (1 << 1)
|
||||
#define MQTTQOS2 (2 << 1)
|
||||
|
||||
#ifdef ESP8266
|
||||
#include <functional>
|
||||
#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
|
||||
#else
|
||||
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
|
||||
#endif
|
||||
|
||||
class PubSubClient {
|
||||
private:
|
||||
Client* _client;
|
||||
uint8_t buffer[MQTT_MAX_PACKET_SIZE];
|
||||
uint16_t nextMsgId;
|
||||
unsigned long lastOutActivity;
|
||||
unsigned long lastInActivity;
|
||||
int _available;
|
||||
bool pingOutstanding;
|
||||
MQTT_CALLBACK_SIGNATURE;
|
||||
int available();
|
||||
uint16_t readPacket(uint8_t*);
|
||||
boolean readByte(uint8_t * result);
|
||||
boolean readByte(uint8_t * result, uint16_t * index);
|
||||
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
|
||||
uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
|
||||
IPAddress ip;
|
||||
const char* domain;
|
||||
uint16_t port;
|
||||
Stream* stream;
|
||||
int _state;
|
||||
|
||||
#ifdef ESP8266
|
||||
const char* fingerprint;
|
||||
#endif
|
||||
|
||||
public:
|
||||
PubSubClient();
|
||||
PubSubClient(Client& client);
|
||||
PubSubClient(IPAddress, uint16_t, Client& client);
|
||||
PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
|
||||
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||
PubSubClient(uint8_t *, uint16_t, Client& client);
|
||||
PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
|
||||
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||
PubSubClient(const char*, uint16_t, Client& client);
|
||||
PubSubClient(const char*, uint16_t, Client& client, Stream&);
|
||||
PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||
PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||
|
||||
#ifdef ESP8266
|
||||
PubSubClient(WiFiClientSecure& client, const char* fingerprint);
|
||||
#endif
|
||||
|
||||
PubSubClient& setServer(IPAddress ip, uint16_t port);
|
||||
PubSubClient& setServer(uint8_t * ip, uint16_t port);
|
||||
PubSubClient& setServer(const char * domain, uint16_t port);
|
||||
PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
|
||||
PubSubClient& setClient(Client& client);
|
||||
PubSubClient& setStream(Stream& stream);
|
||||
|
||||
boolean connect(const char* id);
|
||||
boolean connect(const char* id, const char* user, const char* pass);
|
||||
boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
|
||||
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
|
||||
void disconnect();
|
||||
boolean publish(const char* topic, const char* payload);
|
||||
boolean publish(const char* topic, const char* payload, boolean retained);
|
||||
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
|
||||
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
||||
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
||||
boolean subscribe(const char* topic);
|
||||
boolean subscribe(const char* topic, uint8_t qos);
|
||||
boolean unsubscribe(const char* topic);
|
||||
boolean loop();
|
||||
boolean connected();
|
||||
int state();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user