Initial commit
This commit is contained in:
parent
ae6ef92b49
commit
2926c431a2
56 changed files with 8787 additions and 2 deletions
2601
ampel-firmware/src/lib/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp
Normal file
2601
ampel-firmware/src/lib/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp
Normal file
File diff suppressed because it is too large
Load diff
366
ampel-firmware/src/lib/Adafruit_NeoPixel/Adafruit_NeoPixel.h
Normal file
366
ampel-firmware/src/lib/Adafruit_NeoPixel/Adafruit_NeoPixel.h
Normal file
|
@ -0,0 +1,366 @@
|
|||
/*!
|
||||
* @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
|
||||
|
||||
#ifdef ARDUINO
|
||||
#if (ARDUINO >= 100)
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#include <pins_arduino.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef TARGET_LPC1768
|
||||
#include <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, uint16_t pin=6,
|
||||
neoPixelType type=NEO_GRB + NEO_KHZ800);
|
||||
Adafruit_NeoPixel(void);
|
||||
~Adafruit_NeoPixel();
|
||||
|
||||
void begin(void);
|
||||
void show(void);
|
||||
void setPin(uint16_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).
|
||||
*/
|
||||
bool canShow(void) {
|
||||
if (endTime > micros()) {
|
||||
endTime = micros();
|
||||
}
|
||||
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).
|
||||
*/
|
||||
int16_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...
|
||||
bool is800KHz; ///< true if 800 KHz pixels
|
||||
#endif
|
||||
bool begun; ///< true if begin() previously called
|
||||
uint16_t numLEDs; ///< Number of RGB LEDs in strip
|
||||
uint16_t numBytes; ///< Size of 'pixels' buffer below
|
||||
int16_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
|
||||
#if defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_ARDUINO_CORE_STM32)
|
||||
GPIO_TypeDef *gpioPort; ///< Output GPIO PORT
|
||||
uint32_t gpioPin; ///< Output GPIO PIN
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // ADAFRUIT_NEOPIXEL_H
|
13
ampel-firmware/src/lib/Adafruit_NeoPixel/CONTRIBUTING.md
Normal file
13
ampel-firmware/src/lib/Adafruit_NeoPixel/CONTRIBUTING.md
Normal file
|
@ -0,0 +1,13 @@
|
|||
# Contribution Guidelines
|
||||
|
||||
This library is the culmination of the expertise of many members of the open source community who have dedicated their time and hard work. The best way to ask for help or propose a new idea is to [create a new issue](https://github.com/adafruit/Adafruit_NeoPixel/issues/new) while creating a Pull Request with your code changes allows you to share your own innovations with the rest of the community.
|
||||
|
||||
The following are some guidelines to observe when creating issues or PRs:
|
||||
|
||||
- Be friendly; it is important that we can all enjoy a safe space as we are all working on the same project and it is okay for people to have different ideas
|
||||
|
||||
- [Use code blocks](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code); it helps us help you when we can read your code! On that note also refrain from pasting more than 30 lines of code in a post, instead [create a gist](https://gist.github.com/) if you need to share large snippets
|
||||
|
||||
- Use reasonable titles; refrain from using overly long or capitalized titles as they are usually annoying and do little to encourage others to help :smile:
|
||||
|
||||
- Be detailed; refrain from mentioning code problems without sharing your source code and always give information regarding your board and version of the library
|
165
ampel-firmware/src/lib/Adafruit_NeoPixel/COPYING
Normal file
165
ampel-firmware/src/lib/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.
|
153
ampel-firmware/src/lib/Adafruit_NeoPixel/README.md
Normal file
153
ampel-firmware/src/lib/Adafruit_NeoPixel/README.md
Normal file
|
@ -0,0 +1,153 @@
|
|||
# Adafruit NeoPixel Library [](https://github.com/adafruit/Adafruit_NeoPixel/actions)[](http://adafruit.github.io/Adafruit_NeoPixel/html/index.html)
|
||||
|
||||
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
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
### First Method
|
||||
|
||||

|
||||
|
||||
1. In the Arduino IDE, navigate to Sketch > Include Library > Manage Libraries
|
||||
1. Then the Library Manager will open and you will find a list of libraries that are already installed or ready for installation.
|
||||
1. Then search for Neopixel strip using the search bar.
|
||||
1. Click on the text area and then select the specific version and install it.
|
||||
|
||||
### Second Method
|
||||
|
||||
1. Navigate to the [Releases page](https://github.com/adafruit/Adafruit_NeoPixel/releases).
|
||||
1. Download the latest release.
|
||||
1. Extract the zip file
|
||||
1. In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library
|
||||
|
||||
## Features
|
||||
|
||||
- ### Simple to use
|
||||
|
||||
Controlling NeoPixels “from scratch” is quite a challenge, so we provide a library letting you focus on the fun and interesting bits.
|
||||
|
||||
- ### Give back
|
||||
|
||||
The library is free; you don’t have to pay for anything. Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
|
||||
|
||||
- ### 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!
|
||||
|
||||
- ### GNU Lesser General Public License
|
||||
|
||||
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.
|
||||
|
||||
## Functions
|
||||
|
||||
- begin()
|
||||
- updateLength()
|
||||
- updateType()
|
||||
- show()
|
||||
- delay_ns()
|
||||
- setPin()
|
||||
- setPixelColor()
|
||||
- fill()
|
||||
- ColorHSV()
|
||||
- getPixelColor()
|
||||
- setBrightness()
|
||||
- getBrightness()
|
||||
- clear()
|
||||
- gamma32()
|
||||
|
||||
## Examples
|
||||
|
||||
There are many examples implemented in this library. One of the examples is below. You can find other examples [here](https://github.com/adafruit/Adafruit_NeoPixel/tree/master/examples)
|
||||
|
||||
### Simple
|
||||
|
||||
```Cpp
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#ifdef __AVR__
|
||||
#include <avr/power.h>
|
||||
#endif
|
||||
#define PIN 6
|
||||
#define NUMPIXELS 16
|
||||
|
||||
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
|
||||
#define DELAYVAL 500
|
||||
|
||||
void setup() {
|
||||
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
|
||||
clock_prescale_set(clock_div_1);
|
||||
#endif
|
||||
|
||||
pixels.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
pixels.clear();
|
||||
|
||||
for(int i=0; i<NUMPIXELS; i++) {
|
||||
|
||||
pixels.setPixelColor(i, pixels.Color(0, 150, 0));
|
||||
pixels.show();
|
||||
delay(DELAYVAL);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
If you want to contribute to this project:
|
||||
|
||||
- Report bugs and errors
|
||||
- Ask for enhancements
|
||||
- Create issues and pull requests
|
||||
- Tell others about this library
|
||||
- Contribute new protocols
|
||||
|
||||
Please read [CONTRIBUTING.md](https://github.com/adafruit/Adafruit_NeoPixel/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
|
||||
|
||||
### 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 of 1.4.2, systick is used on M4 devices and it appears to be overclock-compatible. Not for M0 yet, which is why this item is still here.)
|
||||
- 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.)
|
||||
|
||||
## Credits
|
||||
|
||||
This library is written by Phil "Paint Your Dragon" Burgess for Adafruit Industries, with contributions by PJRC, Michael Miller and other members of the open source community.
|
||||
|
||||
## License
|
||||
|
||||
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](https://www.gnu.org/licenses/lgpl-3.0.en.html) for more details.
|
||||
You should have received a copy of the GNU Lesser General Public License along with NeoPixel. If not, see [this](https://www.gnu.org/licenses/)
|
162
ampel-firmware/src/lib/Adafruit_NeoPixel/esp.c
Normal file
162
ampel-firmware/src/lib/Adafruit_NeoPixel/esp.c
Normal file
|
@ -0,0 +1,162 @@
|
|||
// Implements the RMT peripheral on Espressif SoCs
|
||||
// Copyright (c) 2020 Lucian Copeland for Adafruit Industries
|
||||
|
||||
/* Uses code from Espressif RGB LED Strip demo and drivers
|
||||
* Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if defined(ESP32)
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "driver/rmt.h"
|
||||
|
||||
// This code is adapted from the ESP-IDF v3.4 RMT "led_strip" example, altered
|
||||
// to work with the Arduino version of the ESP-IDF (3.2)
|
||||
|
||||
#define WS2812_T0H_NS (400)
|
||||
#define WS2812_T0L_NS (850)
|
||||
#define WS2812_T1H_NS (800)
|
||||
#define WS2812_T1L_NS (450)
|
||||
|
||||
#define WS2811_T0H_NS (500)
|
||||
#define WS2811_T0L_NS (2000)
|
||||
#define WS2811_T1H_NS (1200)
|
||||
#define WS2811_T1L_NS (1300)
|
||||
|
||||
static uint32_t t0h_ticks = 0;
|
||||
static uint32_t t1h_ticks = 0;
|
||||
static uint32_t t0l_ticks = 0;
|
||||
static uint32_t t1l_ticks = 0;
|
||||
|
||||
// Limit the number of RMT channels available for the Neopixels. Defaults to all
|
||||
// channels (8 on ESP32, 4 on ESP32-S2 and S3). Redefining this value will free
|
||||
// any channels with a higher number for other uses, such as IR send-and-recieve
|
||||
// libraries. Redefine as 1 to restrict Neopixels to only a single channel.
|
||||
#define ADAFRUIT_RMT_CHANNEL_MAX RMT_CHANNEL_MAX
|
||||
|
||||
#define RMT_LL_HW_BASE (&RMT)
|
||||
|
||||
bool rmt_reserved_channels[ADAFRUIT_RMT_CHANNEL_MAX];
|
||||
|
||||
static void IRAM_ATTR ws2812_rmt_adapter(const void *src, rmt_item32_t *dest, size_t src_size,
|
||||
size_t wanted_num, size_t *translated_size, size_t *item_num)
|
||||
{
|
||||
if (src == NULL || dest == NULL) {
|
||||
*translated_size = 0;
|
||||
*item_num = 0;
|
||||
return;
|
||||
}
|
||||
const rmt_item32_t bit0 = {{{ t0h_ticks, 1, t0l_ticks, 0 }}}; //Logical 0
|
||||
const rmt_item32_t bit1 = {{{ t1h_ticks, 1, t1l_ticks, 0 }}}; //Logical 1
|
||||
size_t size = 0;
|
||||
size_t num = 0;
|
||||
uint8_t *psrc = (uint8_t *)src;
|
||||
rmt_item32_t *pdest = dest;
|
||||
while (size < src_size && num < wanted_num) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
// MSB first
|
||||
if (*psrc & (1 << (7 - i))) {
|
||||
pdest->val = bit1.val;
|
||||
} else {
|
||||
pdest->val = bit0.val;
|
||||
}
|
||||
num++;
|
||||
pdest++;
|
||||
}
|
||||
size++;
|
||||
psrc++;
|
||||
}
|
||||
*translated_size = size;
|
||||
*item_num = num;
|
||||
}
|
||||
|
||||
void espShow(uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) {
|
||||
// Reserve channel
|
||||
rmt_channel_t channel = ADAFRUIT_RMT_CHANNEL_MAX;
|
||||
for (size_t i = 0; i < ADAFRUIT_RMT_CHANNEL_MAX; i++) {
|
||||
if (!rmt_reserved_channels[i]) {
|
||||
rmt_reserved_channels[i] = true;
|
||||
channel = i;
|
||||
}
|
||||
}
|
||||
if (channel == ADAFRUIT_RMT_CHANNEL_MAX) {
|
||||
// Ran out of channels!
|
||||
return;
|
||||
}
|
||||
|
||||
// Match default TX config from ESP-IDF version 3.4
|
||||
rmt_config_t config = {
|
||||
.rmt_mode = RMT_MODE_TX,
|
||||
.channel = channel,
|
||||
.gpio_num = pin,
|
||||
.clk_div = 2,
|
||||
.mem_block_num = 1,
|
||||
.tx_config = {
|
||||
.carrier_freq_hz = 38000,
|
||||
.carrier_level = RMT_CARRIER_LEVEL_HIGH,
|
||||
.idle_level = RMT_IDLE_LEVEL_LOW,
|
||||
.carrier_duty_percent = 33,
|
||||
.carrier_en = false,
|
||||
.loop_en = false,
|
||||
.idle_output_en = true,
|
||||
}
|
||||
};
|
||||
rmt_config(&config);
|
||||
rmt_driver_install(config.channel, 0, 0);
|
||||
|
||||
// Convert NS timings to ticks
|
||||
uint32_t counter_clk_hz = 0;
|
||||
|
||||
// this emulates the rmt_get_counter_clock() function from ESP-IDF 3.4
|
||||
if (RMT_LL_HW_BASE->conf_ch[config.channel].conf1.ref_always_on == RMT_BASECLK_REF) {
|
||||
uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
|
||||
uint32_t div = div_cnt == 0 ? 256 : div_cnt;
|
||||
counter_clk_hz = REF_CLK_FREQ / (div);
|
||||
} else {
|
||||
uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
|
||||
uint32_t div = div_cnt == 0 ? 256 : div_cnt;
|
||||
counter_clk_hz = APB_CLK_FREQ / (div);
|
||||
}
|
||||
|
||||
// NS to tick converter
|
||||
float ratio = (float)counter_clk_hz / 1e9;
|
||||
|
||||
if (is800KHz) {
|
||||
t0h_ticks = (uint32_t)(ratio * WS2812_T0H_NS);
|
||||
t0l_ticks = (uint32_t)(ratio * WS2812_T0L_NS);
|
||||
t1h_ticks = (uint32_t)(ratio * WS2812_T1H_NS);
|
||||
t1l_ticks = (uint32_t)(ratio * WS2812_T1L_NS);
|
||||
} else {
|
||||
t0h_ticks = (uint32_t)(ratio * WS2811_T0H_NS);
|
||||
t0l_ticks = (uint32_t)(ratio * WS2811_T0L_NS);
|
||||
t1h_ticks = (uint32_t)(ratio * WS2811_T1H_NS);
|
||||
t1l_ticks = (uint32_t)(ratio * WS2811_T1L_NS);
|
||||
}
|
||||
|
||||
// Initialize automatic timing translator
|
||||
rmt_translator_init(config.channel, ws2812_rmt_adapter);
|
||||
|
||||
// Write and wait to finish
|
||||
rmt_write_sample(config.channel, pixels, (size_t)numBytes, true);
|
||||
rmt_wait_tx_done(config.channel, pdMS_TO_TICKS(100));
|
||||
|
||||
// Free channel again
|
||||
rmt_driver_uninstall(config.channel);
|
||||
rmt_reserved_channels[channel] = false;
|
||||
|
||||
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
|
||||
}
|
||||
|
||||
#endif
|
86
ampel-firmware/src/lib/Adafruit_NeoPixel/esp8266.c
Normal file
86
ampel-firmware/src/lib/Adafruit_NeoPixel/esp8266.c
Normal file
|
@ -0,0 +1,86 @@
|
|||
// 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)
|
||||
|
||||
#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;
|
||||
|
||||
#ifdef ESP8266
|
||||
uint32_t pinMask;
|
||||
pinMask = _BV(pin);
|
||||
#endif
|
||||
|
||||
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
|
74
ampel-firmware/src/lib/Adafruit_NeoPixel/kendyte_k210.c
Normal file
74
ampel-firmware/src/lib/Adafruit_NeoPixel/kendyte_k210.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
// 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(K210)
|
||||
#define KENDRYTE_K210 1
|
||||
#endif
|
||||
|
||||
#if defined(KENDRYTE_K210)
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "sysctl.h"
|
||||
|
||||
void k210Show(
|
||||
uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz)
|
||||
{
|
||||
|
||||
#define CYCLES_800_T0H (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 2500000) // 0.4us
|
||||
#define CYCLES_800_T1H (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 1250000) // 0.8us
|
||||
#define CYCLES_800 (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 800000) // 1.25us per bit
|
||||
#define CYCLES_400_T0H (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 2000000) // 0.5uS
|
||||
#define CYCLES_400_T1H (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 833333) // 1.2us
|
||||
#define CYCLES_400 (sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 400000) // 2.5us per bit
|
||||
|
||||
uint8_t *p, *end, pix, mask;
|
||||
uint32_t t, time0, time1, period, c, startTime;
|
||||
|
||||
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 = read_cycle()) - startTime) < period)
|
||||
; // Wait for bit start
|
||||
digitalWrite(pin, HIGH);
|
||||
startTime = c; // Save start time
|
||||
while (((c = read_cycle()) - startTime) < t)
|
||||
; // Wait high duration
|
||||
digitalWrite(pin, LOW);
|
||||
|
||||
if (!(mask >>= 1))
|
||||
{ // Next bit/byte
|
||||
if (p >= end)
|
||||
break;
|
||||
pix = *p++;
|
||||
mask = 0x80;
|
||||
}
|
||||
}
|
||||
while ((read_cycle() - startTime) < period)
|
||||
; // Wait for last bit
|
||||
}
|
||||
|
||||
#endif // KENDRYTE_K210
|
72
ampel-firmware/src/lib/Adafruit_NeoPixel/keywords.txt
Normal file
72
ampel-firmware/src/lib/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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
name=Adafruit NeoPixel
|
||||
version=1.7.0
|
||||
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=*
|
10
ampel-firmware/src/lib/NTPClient-master/.travis.yml
Normal file
10
ampel-firmware/src/lib/NTPClient-master/.travis.yml
Normal file
|
@ -0,0 +1,10 @@
|
|||
language: c
|
||||
sudo: false
|
||||
before_install:
|
||||
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
|
||||
script:
|
||||
- build_platform esp8266
|
||||
notifications:
|
||||
email:
|
||||
on_success: change
|
||||
on_failure: change
|
15
ampel-firmware/src/lib/NTPClient-master/CHANGELOG
Normal file
15
ampel-firmware/src/lib/NTPClient-master/CHANGELOG
Normal file
|
@ -0,0 +1,15 @@
|
|||
NTPClient 3.1.0 - 2016.05.31
|
||||
|
||||
* Added functions for changing the timeOffset and updateInterval later. Thanks @SirUli
|
||||
|
||||
NTPClient 3.0.0 - 2016.04.19
|
||||
|
||||
* Constructors now require UDP instance argument, to add support for non-ESP8266 boards
|
||||
* Added optional begin API to override default local port
|
||||
* Added end API to close UDP socket
|
||||
* Changed return type of update and forceUpdate APIs to bool, and return success or failure
|
||||
* Change return type of getDay, getHours, getMinutes, and getSeconds to int
|
||||
|
||||
Older
|
||||
|
||||
* Changes not recorded
|
235
ampel-firmware/src/lib/NTPClient-master/NTPClient.cpp
Normal file
235
ampel-firmware/src/lib/NTPClient-master/NTPClient.cpp
Normal file
|
@ -0,0 +1,235 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
* Copyright (c) 2015 by Fabrice Weinberg
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "NTPClient.h"
|
||||
|
||||
NTPClient::NTPClient(UDP& udp) {
|
||||
this->_udp = &udp;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, int timeOffset) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
|
||||
this->_udp = &udp;
|
||||
this->_poolServerName = poolServerName;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
this->_poolServerName = poolServerName;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, unsigned long updateInterval) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
this->_poolServerName = poolServerName;
|
||||
this->_updateInterval = updateInterval;
|
||||
}
|
||||
|
||||
void NTPClient::begin() {
|
||||
this->begin(NTP_DEFAULT_LOCAL_PORT);
|
||||
}
|
||||
|
||||
void NTPClient::begin(int port) {
|
||||
this->_port = port;
|
||||
|
||||
this->_udp->begin(this->_port);
|
||||
|
||||
this->_udpSetup = true;
|
||||
}
|
||||
|
||||
bool NTPClient::isValid(byte * ntpPacket)
|
||||
{
|
||||
//Perform a few validity checks on the packet
|
||||
if((ntpPacket[0] & 0b11000000) == 0b11000000) //Check for LI=UNSYNC
|
||||
return false;
|
||||
|
||||
if((ntpPacket[0] & 0b00111000) >> 3 < 0b100) //Check for Version >= 4
|
||||
return false;
|
||||
|
||||
if((ntpPacket[0] & 0b00000111) != 0b100) //Check for Mode == Server
|
||||
return false;
|
||||
|
||||
if((ntpPacket[1] < 1) || (ntpPacket[1] > 15)) //Check for valid Stratum
|
||||
return false;
|
||||
|
||||
if( ntpPacket[16] == 0 && ntpPacket[17] == 0 &&
|
||||
ntpPacket[18] == 0 && ntpPacket[19] == 0 &&
|
||||
ntpPacket[20] == 0 && ntpPacket[21] == 0 &&
|
||||
ntpPacket[22] == 0 && ntpPacket[22] == 0) //Check for ReferenceTimestamp != 0
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NTPClient::forceUpdate() {
|
||||
#ifdef DEBUG_NTPClient
|
||||
Serial.println("Update from NTP Server");
|
||||
#endif
|
||||
// flush any existing packets
|
||||
while(this->_udp->parsePacket() != 0)
|
||||
this->_udp->flush();
|
||||
this->sendNTPPacket();
|
||||
|
||||
// Wait till data is there or timeout...
|
||||
byte timeout = 0;
|
||||
int cb = 0;
|
||||
do {
|
||||
delay ( 10 );
|
||||
cb = this->_udp->parsePacket();
|
||||
|
||||
if(cb > 0)
|
||||
{
|
||||
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||
if(!this->isValid(this->_packetBuffer))
|
||||
cb = 0;
|
||||
}
|
||||
|
||||
if (timeout > 100) return false; // timeout after 1000 ms
|
||||
timeout++;
|
||||
} while (cb == 0);
|
||||
|
||||
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
|
||||
|
||||
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
|
||||
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
|
||||
// combine the four bytes (two words) into a long integer
|
||||
// this is NTP time (seconds since Jan 1 1900):
|
||||
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
||||
|
||||
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NTPClient::update() {
|
||||
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|
||||
|| this->_lastUpdate == 0) { // Update if there was no update yet.
|
||||
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
|
||||
return this->forceUpdate();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned long NTPClient::getEpochTime() {
|
||||
return this->_timeOffset + // User offset
|
||||
this->_currentEpoc + // Epoc returned by the NTP server
|
||||
((millis() - this->_lastUpdate) / 1000); // Time since last update
|
||||
}
|
||||
|
||||
int NTPClient::getDay() {
|
||||
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
|
||||
}
|
||||
int NTPClient::getHours() {
|
||||
return ((this->getEpochTime() % 86400L) / 3600);
|
||||
}
|
||||
int NTPClient::getMinutes() {
|
||||
return ((this->getEpochTime() % 3600) / 60);
|
||||
}
|
||||
int NTPClient::getSeconds() {
|
||||
return (this->getEpochTime() % 60);
|
||||
}
|
||||
|
||||
String NTPClient::getFormattedTime(unsigned long secs) {
|
||||
unsigned long rawTime = secs ? secs : this->getEpochTime();
|
||||
unsigned int hours = (rawTime % 86400L) / 3600;
|
||||
unsigned int minutes = (rawTime % 3600) / 60;
|
||||
unsigned int seconds = rawTime % 60;
|
||||
|
||||
char formatted_time[9];
|
||||
snprintf(formatted_time, sizeof(formatted_time), "%02d:%02d:%02d", hours, minutes, seconds);
|
||||
return String(formatted_time);
|
||||
}
|
||||
|
||||
// Based on https://github.com/PaulStoffregen/Time/blob/master/Time.cpp
|
||||
String NTPClient::getFormattedDate(unsigned long secs) {
|
||||
unsigned long rawTime = (secs ? secs : this->getEpochTime()) / 86400L; // in days
|
||||
unsigned long days = 0, year = 1970;
|
||||
uint8_t month;
|
||||
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
|
||||
|
||||
while((days += (LEAP_YEAR(year) ? 366 : 365)) <= rawTime)
|
||||
year++;
|
||||
rawTime -= days - (LEAP_YEAR(year) ? 366 : 365); // now it is days in this year, starting at 0
|
||||
days=0;
|
||||
for (month=0; month<12; month++) {
|
||||
uint8_t monthLength;
|
||||
if (month==1) { // february
|
||||
monthLength = LEAP_YEAR(year) ? 29 : 28;
|
||||
} else {
|
||||
monthLength = monthDays[month];
|
||||
}
|
||||
if (rawTime < monthLength) break;
|
||||
rawTime -= monthLength;
|
||||
}
|
||||
month++; // jan is month 1
|
||||
rawTime++; // first day is day 1
|
||||
|
||||
char formatted_date[23];
|
||||
snprintf(formatted_date, sizeof(formatted_date), "%4lu-%02d-%02lu %s%+03d",
|
||||
year, month, rawTime, this->getFormattedTime(secs).c_str(), this->_timeOffset / 3600);
|
||||
|
||||
return String(formatted_date);
|
||||
}
|
||||
|
||||
void NTPClient::end() {
|
||||
this->_udp->stop();
|
||||
|
||||
this->_udpSetup = false;
|
||||
}
|
||||
|
||||
void NTPClient::setTimeOffset(int timeOffset) {
|
||||
this->_timeOffset = timeOffset;
|
||||
}
|
||||
|
||||
void NTPClient::setUpdateInterval(unsigned long updateInterval) {
|
||||
this->_updateInterval = updateInterval;
|
||||
}
|
||||
|
||||
void NTPClient::sendNTPPacket() {
|
||||
// set all bytes in the buffer to 0
|
||||
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
|
||||
// Initialize values needed to form NTP request
|
||||
// (see URL above for details on the packets)
|
||||
this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode
|
||||
this->_packetBuffer[1] = 0; // Stratum, or type of clock
|
||||
this->_packetBuffer[2] = 6; // Polling Interval
|
||||
this->_packetBuffer[3] = 0xEC; // Peer Clock Precision
|
||||
// 8 bytes of zero for Root Delay & Root Dispersion
|
||||
this->_packetBuffer[12] = 0x49;
|
||||
this->_packetBuffer[13] = 0x4E;
|
||||
this->_packetBuffer[14] = 0x49;
|
||||
this->_packetBuffer[15] = 0x52;
|
||||
|
||||
// all NTP fields have been given values, now
|
||||
// you can send a packet requesting a timestamp:
|
||||
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
|
||||
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||
this->_udp->endPacket();
|
||||
}
|
||||
|
||||
void NTPClient::setEpochTime(unsigned long secs) {
|
||||
this->_currentEpoc = secs;
|
||||
}
|
105
ampel-firmware/src/lib/NTPClient-master/NTPClient.h
Normal file
105
ampel-firmware/src/lib/NTPClient-master/NTPClient.h
Normal file
|
@ -0,0 +1,105 @@
|
|||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#include <Udp.h>
|
||||
|
||||
#define SEVENZYYEARS 2208988800UL
|
||||
#define NTP_PACKET_SIZE 48
|
||||
#define NTP_DEFAULT_LOCAL_PORT 1337
|
||||
#define LEAP_YEAR(Y) ( (Y>0) && !(Y%4) && ( (Y%100) || !(Y%400) ) )
|
||||
|
||||
|
||||
class NTPClient {
|
||||
private:
|
||||
UDP* _udp;
|
||||
bool _udpSetup = false;
|
||||
|
||||
const char* _poolServerName = "pool.ntp.org"; // Default time server
|
||||
int _port = NTP_DEFAULT_LOCAL_PORT;
|
||||
int _timeOffset = 0;
|
||||
|
||||
unsigned long _updateInterval = 60000; // In ms
|
||||
|
||||
unsigned long _currentEpoc = 0; // In s
|
||||
unsigned long _lastUpdate = 0; // In ms
|
||||
|
||||
byte _packetBuffer[NTP_PACKET_SIZE];
|
||||
|
||||
void sendNTPPacket();
|
||||
bool isValid(byte * ntpPacket);
|
||||
|
||||
public:
|
||||
NTPClient(UDP& udp);
|
||||
NTPClient(UDP& udp, int timeOffset);
|
||||
NTPClient(UDP& udp, const char* poolServerName);
|
||||
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
|
||||
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, unsigned long updateInterval);
|
||||
|
||||
/**
|
||||
* Starts the underlying UDP client with the default local port
|
||||
*/
|
||||
void begin();
|
||||
|
||||
/**
|
||||
* Starts the underlying UDP client with the specified local port
|
||||
*/
|
||||
void begin(int port);
|
||||
|
||||
/**
|
||||
* This should be called in the main loop of your application. By default an update from the NTP Server is only
|
||||
* made every 60 seconds. This can be configured in the NTPClient constructor.
|
||||
*
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool update();
|
||||
|
||||
/**
|
||||
* This will force the update from the NTP Server.
|
||||
*
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool forceUpdate();
|
||||
|
||||
int getDay();
|
||||
int getHours();
|
||||
int getMinutes();
|
||||
int getSeconds();
|
||||
|
||||
/**
|
||||
* Changes the time offset. Useful for changing timezones dynamically
|
||||
*/
|
||||
void setTimeOffset(int timeOffset);
|
||||
|
||||
/**
|
||||
* Set the update interval to another frequency. E.g. useful when the
|
||||
* timeOffset should not be set in the constructor
|
||||
*/
|
||||
void setUpdateInterval(unsigned long updateInterval);
|
||||
|
||||
/**
|
||||
* @return secs argument (or 0 for current time) formatted like `hh:mm:ss`
|
||||
*/
|
||||
String getFormattedTime(unsigned long secs = 0);
|
||||
|
||||
/**
|
||||
* @return time in seconds since Jan. 1, 1970
|
||||
*/
|
||||
unsigned long getEpochTime();
|
||||
|
||||
/**
|
||||
* @return secs argument (or 0 for current date) formatted to ISO 8601
|
||||
* like `2004-02-12T15:19:21+00:00`
|
||||
*/
|
||||
String getFormattedDate(unsigned long secs = 0);
|
||||
|
||||
/**
|
||||
* Stops the underlying UDP client
|
||||
*/
|
||||
void end();
|
||||
|
||||
/**
|
||||
* Replace the NTP-fetched time with seconds since Jan. 1, 1970
|
||||
*/
|
||||
void setEpochTime(unsigned long secs);
|
||||
};
|
47
ampel-firmware/src/lib/NTPClient-master/README.md
Normal file
47
ampel-firmware/src/lib/NTPClient-master/README.md
Normal file
|
@ -0,0 +1,47 @@
|
|||
# NTPClient
|
||||
|
||||
[](https://travis-ci.org/arduino-libraries/NTPClient)
|
||||
|
||||
Connect to a NTP server, here is how:
|
||||
|
||||
```cpp
|
||||
#include <NTPClient.h>
|
||||
// change next line to use with another board/shield
|
||||
#include <ESP8266WiFi.h>
|
||||
//#include <WiFi.h> // for WiFi shield
|
||||
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
const char *ssid = "<SSID>";
|
||||
const char *password = "<PASSWORD>";
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
|
||||
// By default 'pool.ntp.org' is used with 60 seconds update interval and
|
||||
// no offset
|
||||
NTPClient timeClient(ntpUDP);
|
||||
|
||||
// You can specify the time server pool and the offset, (in seconds)
|
||||
// additionaly you can specify the update interval (in milliseconds).
|
||||
// NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
|
||||
|
||||
void setup(){
|
||||
Serial.begin(115200);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while ( WiFi.status() != WL_CONNECTED ) {
|
||||
delay ( 500 );
|
||||
Serial.print ( "." );
|
||||
}
|
||||
|
||||
timeClient.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
timeClient.update();
|
||||
|
||||
Serial.println(timeClient.getFormattedTime());
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
```
|
20
ampel-firmware/src/lib/NTPClient-master/keywords.txt
Normal file
20
ampel-firmware/src/lib/NTPClient-master/keywords.txt
Normal file
|
@ -0,0 +1,20 @@
|
|||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
NTPClient KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
end KEYWORD2
|
||||
update KEYWORD2
|
||||
forceUpdate KEYWORD2
|
||||
getDay KEYWORD2
|
||||
getHours KEYWORD2
|
||||
getMinutes KEYWORD2
|
||||
getSeconds KEYWORD2
|
||||
getFormattedTime KEYWORD2
|
||||
getEpochTime KEYWORD2
|
24
ampel-firmware/src/lib/NTPClient-master/library.json
Normal file
24
ampel-firmware/src/lib/NTPClient-master/library.json
Normal file
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"name": "NTPClient",
|
||||
"keywords": "ntp, client, time",
|
||||
"description": "A NTPClient to connect to a time server",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Fabrice Weinberg",
|
||||
"email": "fabrice@weinberg.me"
|
||||
},
|
||||
{
|
||||
"name": "Sandeep Mistry",
|
||||
"email": "s.mistry@arduino.cc"
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/arduino-libraries/NTPClient.git"
|
||||
},
|
||||
"version": "3.1.0",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "espressif"
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
name=NTPClient
|
||||
version=3.1.0
|
||||
author=Fabrice Weinberg
|
||||
maintainer=Fabrice Weinberg <fabrice@weinberg.me>
|
||||
sentence=An NTPClient to connect to a time server
|
||||
paragraph=Get time from a NTP server and keep it in sync.
|
||||
category=Timing
|
||||
url=https://github.com/arduino-libraries/NTPClient
|
||||
architectures=*
|
85
ampel-firmware/src/lib/PubSubClient/CHANGES.txt
Normal file
85
ampel-firmware/src/lib/PubSubClient/CHANGES.txt
Normal file
|
@ -0,0 +1,85 @@
|
|||
2.8
|
||||
* Add setBufferSize() to override MQTT_MAX_PACKET_SIZE
|
||||
* Add setKeepAlive() to override MQTT_KEEPALIVE
|
||||
* Add setSocketTimeout() to overide MQTT_SOCKET_TIMEOUT
|
||||
* Added check to prevent subscribe/unsubscribe to empty topics
|
||||
* Declare wifi mode prior to connect in ESP example
|
||||
* Use `strnlen` to avoid overruns
|
||||
* Support pre-connected Client objects
|
||||
|
||||
2.7
|
||||
* Fix remaining-length handling to prevent buffer overrun
|
||||
* Add large-payload API - beginPublish/write/publish/endPublish
|
||||
* Add yield call to improve reliability on ESP
|
||||
* Add Clean Session flag to connect options
|
||||
* Add ESP32 support for functional callback signature
|
||||
* Various other fixes
|
||||
|
||||
2.4
|
||||
* Add MQTT_SOCKET_TIMEOUT to prevent it blocking indefinitely
|
||||
whilst waiting for inbound data
|
||||
* Fixed return code when publishing >256 bytes
|
||||
|
||||
2.3
|
||||
* Add publish(topic,payload,retained) function
|
||||
|
||||
2.2
|
||||
* Change code layout to match Arduino Library reqs
|
||||
|
||||
2.1
|
||||
* Add MAX_TRANSFER_SIZE def to chunk messages if needed
|
||||
* Reject topic/payloads that exceed MQTT_MAX_PACKET_SIZE
|
||||
|
||||
2.0
|
||||
* Add (and default to) MQTT 3.1.1 support
|
||||
* Fix PROGMEM handling for Intel Galileo/ESP8266
|
||||
* Add overloaded constructors for convenience
|
||||
* Add chainable setters for server/callback/client/stream
|
||||
* Add state function to return connack return code
|
||||
|
||||
1.9
|
||||
* Do not split MQTT packets over multiple calls to _client->write()
|
||||
* API change: All constructors now require an instance of Client
|
||||
to be passed in.
|
||||
* Fixed example to match 1.8 api changes - dpslwk
|
||||
* Added username/password support - WilHall
|
||||
* Added publish_P - publishes messages from PROGMEM - jobytaffey
|
||||
|
||||
1.8
|
||||
* KeepAlive interval is configurable in PubSubClient.h
|
||||
* Maximum packet size is configurable in PubSubClient.h
|
||||
* API change: Return boolean rather than int from various functions
|
||||
* API change: Length parameter in message callback changed
|
||||
from int to unsigned int
|
||||
* Various internal tidy-ups around types
|
||||
1.7
|
||||
* Improved keepalive handling
|
||||
* Updated to the Arduino-1.0 API
|
||||
1.6
|
||||
* Added the ability to publish a retained message
|
||||
|
||||
1.5
|
||||
* Added default constructor
|
||||
* Fixed compile error when used with arduino-0021 or later
|
||||
|
||||
1.4
|
||||
* Fixed connection lost handling
|
||||
|
||||
1.3
|
||||
* Fixed packet reading bug in PubSubClient.readPacket
|
||||
|
||||
1.2
|
||||
* Fixed compile error when used with arduino-0016 or later
|
||||
|
||||
|
||||
1.1
|
||||
* Reduced size of library
|
||||
* Added support for Will messages
|
||||
* Clarified licensing - see LICENSE.txt
|
||||
|
||||
|
||||
1.0
|
||||
* Only Quality of Service (QOS) 0 messaging is supported
|
||||
* The maximum message size, including header, is 128 bytes
|
||||
* The keepalive interval is set to 30 seconds
|
||||
* No support for Will messages
|
20
ampel-firmware/src/lib/PubSubClient/LICENSE.txt
Normal file
20
ampel-firmware/src/lib/PubSubClient/LICENSE.txt
Normal file
|
@ -0,0 +1,20 @@
|
|||
Copyright (c) 2008-2020 Nicholas O'Leary
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
50
ampel-firmware/src/lib/PubSubClient/README.md
Normal file
50
ampel-firmware/src/lib/PubSubClient/README.md
Normal file
|
@ -0,0 +1,50 @@
|
|||
# Arduino Client for MQTT
|
||||
|
||||
This library provides a client for doing simple publish/subscribe messaging with
|
||||
a server that supports MQTT.
|
||||
|
||||
## Examples
|
||||
|
||||
The library comes with a number of example sketches. See File > Examples > PubSubClient
|
||||
within the Arduino application.
|
||||
|
||||
Full API documentation is available here: https://pubsubclient.knolleary.net
|
||||
|
||||
## Limitations
|
||||
|
||||
- It can only publish QoS 0 messages. It can subscribe at QoS 0 or QoS 1.
|
||||
- The maximum message size, including header, is **256 bytes** by default. This
|
||||
is configurable via `MQTT_MAX_PACKET_SIZE` in `PubSubClient.h` or can be changed
|
||||
by calling `PubSubClient::setBufferSize(size)`.
|
||||
- The keepalive interval is set to 15 seconds by default. This is configurable
|
||||
via `MQTT_KEEPALIVE` in `PubSubClient.h` or can be changed by calling
|
||||
`PubSubClient::setKeepAlive(keepAlive)`.
|
||||
- The client uses MQTT 3.1.1 by default. It can be changed to use MQTT 3.1 by
|
||||
changing value of `MQTT_VERSION` in `PubSubClient.h`.
|
||||
|
||||
|
||||
## Compatible Hardware
|
||||
|
||||
The library uses the Arduino Ethernet Client api for interacting with the
|
||||
underlying network hardware. This means it Just Works with a growing number of
|
||||
boards and shields, including:
|
||||
|
||||
- Arduino Ethernet
|
||||
- Arduino Ethernet Shield
|
||||
- Arduino YUN – use the included `YunClient` in place of `EthernetClient`, and
|
||||
be sure to do a `Bridge.begin()` first
|
||||
- Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield,
|
||||
enable the `MQTT_MAX_TRANSFER_SIZE` define in `PubSubClient.h`.
|
||||
- Sparkfun WiFly Shield – [library](https://github.com/dpslwk/WiFly)
|
||||
- TI CC3000 WiFi - [library](https://github.com/sparkfun/SFE_CC3000_Library)
|
||||
- Intel Galileo/Edison
|
||||
- ESP8266
|
||||
- ESP32
|
||||
|
||||
The library cannot currently be used with hardware based on the ENC28J60 chip –
|
||||
such as the Nanode or the Nuelectronics Ethernet Shield. For those, there is an
|
||||
[alternative library](https://github.com/njh/NanodeMQTT) available.
|
||||
|
||||
## License
|
||||
|
||||
This code is released under the MIT License.
|
36
ampel-firmware/src/lib/PubSubClient/keywords.txt
Normal file
36
ampel-firmware/src/lib/PubSubClient/keywords.txt
Normal file
|
@ -0,0 +1,36 @@
|
|||
#######################################
|
||||
# Syntax Coloring Map For PubSubClient
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
PubSubClient KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
connect KEYWORD2
|
||||
disconnect KEYWORD2
|
||||
publish KEYWORD2
|
||||
publish_P KEYWORD2
|
||||
beginPublish KEYWORD2
|
||||
endPublish KEYWORD2
|
||||
write KEYWORD2
|
||||
subscribe KEYWORD2
|
||||
unsubscribe KEYWORD2
|
||||
loop KEYWORD2
|
||||
connected KEYWORD2
|
||||
setServer KEYWORD2
|
||||
setCallback KEYWORD2
|
||||
setClient KEYWORD2
|
||||
setStream KEYWORD2
|
||||
setKeepAlive KEYWORD2
|
||||
setBufferSize KEYWORD2
|
||||
setSocketTimeout KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
18
ampel-firmware/src/lib/PubSubClient/library.json
Normal file
18
ampel-firmware/src/lib/PubSubClient/library.json
Normal file
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"name": "PubSubClient",
|
||||
"keywords": "ethernet, mqtt, m2m, iot",
|
||||
"description": "A client library for MQTT messaging. MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/knolleary/pubsubclient.git"
|
||||
},
|
||||
"version": "2.8",
|
||||
"exclude": "tests",
|
||||
"examples": "examples/*/*.ino",
|
||||
"frameworks": "arduino",
|
||||
"platforms": [
|
||||
"atmelavr",
|
||||
"espressif8266",
|
||||
"espressif32"
|
||||
]
|
||||
}
|
9
ampel-firmware/src/lib/PubSubClient/library.properties
Normal file
9
ampel-firmware/src/lib/PubSubClient/library.properties
Normal file
|
@ -0,0 +1,9 @@
|
|||
name=PubSubClient
|
||||
version=2.8
|
||||
author=Nick O'Leary <nick.oleary@gmail.com>
|
||||
maintainer=Nick O'Leary <nick.oleary@gmail.com>
|
||||
sentence=A client library for MQTT messaging.
|
||||
paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.
|
||||
category=Communication
|
||||
url=http://pubsubclient.knolleary.net
|
||||
architectures=*
|
769
ampel-firmware/src/lib/PubSubClient/src/PubSubClient.cpp
Normal file
769
ampel-firmware/src/lib/PubSubClient/src/PubSubClient.cpp
Normal file
|
@ -0,0 +1,769 @@
|
|||
/*
|
||||
|
||||
PubSubClient.cpp - A simple client for MQTT.
|
||||
Nick O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#include "PubSubClient.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
PubSubClient::PubSubClient() {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
this->_client = NULL;
|
||||
this->stream = NULL;
|
||||
setCallback(NULL);
|
||||
this->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr, port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(addr,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
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->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
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->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip, port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(ip,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
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->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
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->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setClient(client);
|
||||
this->stream = NULL;
|
||||
this->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
PubSubClient::PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream) {
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
setServer(domain,port);
|
||||
setClient(client);
|
||||
setStream(stream);
|
||||
this->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
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->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
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->bufferSize = 0;
|
||||
setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
setKeepAlive(MQTT_KEEPALIVE);
|
||||
setSocketTimeout(MQTT_SOCKET_TIMEOUT);
|
||||
}
|
||||
|
||||
PubSubClient::~PubSubClient() {
|
||||
free(this->buffer);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id) {
|
||||
return connect(id,NULL,NULL,0,0,0,0,1);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char *user, const char *pass) {
|
||||
return connect(id,user,pass,0,0,0,0,1);
|
||||
}
|
||||
|
||||
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,1);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage) {
|
||||
return connect(id,user,pass,willTopic,willQos,willRetain,willMessage,1);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage, boolean cleanSession) {
|
||||
if (!connected()) {
|
||||
int result = 0;
|
||||
|
||||
|
||||
if(_client->connected()) {
|
||||
result = 1;
|
||||
} else {
|
||||
if (domain != NULL) {
|
||||
result = _client->connect(this->domain, this->port);
|
||||
} else {
|
||||
result = _client->connect(this->ip, this->port);
|
||||
}
|
||||
}
|
||||
|
||||
if (result == 1) {
|
||||
nextMsgId = 1;
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = MQTT_MAX_HEADER_SIZE;
|
||||
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++) {
|
||||
this->buffer[length++] = d[j];
|
||||
}
|
||||
|
||||
uint8_t v;
|
||||
if (willTopic) {
|
||||
v = 0x04|(willQos<<3)|(willRetain<<5);
|
||||
} else {
|
||||
v = 0x00;
|
||||
}
|
||||
if (cleanSession) {
|
||||
v = v|0x02;
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
v = v|0x80;
|
||||
|
||||
if(pass != NULL) {
|
||||
v = v|(0x80>>1);
|
||||
}
|
||||
}
|
||||
this->buffer[length++] = v;
|
||||
|
||||
this->buffer[length++] = ((this->keepAlive) >> 8);
|
||||
this->buffer[length++] = ((this->keepAlive) & 0xFF);
|
||||
|
||||
CHECK_STRING_LENGTH(length,id)
|
||||
length = writeString(id,this->buffer,length);
|
||||
if (willTopic) {
|
||||
CHECK_STRING_LENGTH(length,willTopic)
|
||||
length = writeString(willTopic,this->buffer,length);
|
||||
CHECK_STRING_LENGTH(length,willMessage)
|
||||
length = writeString(willMessage,this->buffer,length);
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
CHECK_STRING_LENGTH(length,user)
|
||||
length = writeString(user,this->buffer,length);
|
||||
if(pass != NULL) {
|
||||
CHECK_STRING_LENGTH(length,pass)
|
||||
length = writeString(pass,this->buffer,length);
|
||||
}
|
||||
}
|
||||
|
||||
write(MQTTCONNECT,this->buffer,length-MQTT_MAX_HEADER_SIZE);
|
||||
|
||||
lastInActivity = lastOutActivity = millis();
|
||||
|
||||
while (!_client->available()) {
|
||||
unsigned long t = millis();
|
||||
if (t-lastInActivity >= ((int32_t) this->socketTimeout*1000UL)) {
|
||||
_state = MQTT_CONNECTION_TIMEOUT;
|
||||
_client->stop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
uint8_t llen;
|
||||
uint32_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;
|
||||
}
|
||||
|
||||
// reads a byte into result
|
||||
boolean PubSubClient::readByte(uint8_t * result) {
|
||||
uint32_t previousMillis = millis();
|
||||
while(!_client->available()) {
|
||||
yield();
|
||||
uint32_t currentMillis = millis();
|
||||
if(currentMillis - previousMillis >= ((int32_t) this->socketTimeout * 1000)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
*result = _client->read();
|
||||
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;
|
||||
}
|
||||
|
||||
uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
||||
uint16_t len = 0;
|
||||
if(!readByte(this->buffer, &len)) return 0;
|
||||
bool isPublish = (this->buffer[0]&0xF0) == MQTTPUBLISH;
|
||||
uint32_t multiplier = 1;
|
||||
uint32_t length = 0;
|
||||
uint8_t digit = 0;
|
||||
uint16_t skip = 0;
|
||||
uint32_t start = 0;
|
||||
|
||||
do {
|
||||
if (len == 5) {
|
||||
// Invalid remaining length encoding - kill the connection
|
||||
_state = MQTT_DISCONNECTED;
|
||||
_client->stop();
|
||||
return 0;
|
||||
}
|
||||
if(!readByte(&digit)) return 0;
|
||||
this->buffer[len++] = digit;
|
||||
length += (digit & 127) * multiplier;
|
||||
multiplier <<=7; //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(this->buffer, &len)) return 0;
|
||||
if(!readByte(this->buffer, &len)) return 0;
|
||||
skip = (this->buffer[*lengthLength+1]<<8)+this->buffer[*lengthLength+2];
|
||||
start = 2;
|
||||
if (this->buffer[0]&MQTTQOS1) {
|
||||
// skip message id
|
||||
skip += 2;
|
||||
}
|
||||
}
|
||||
uint32_t idx = len;
|
||||
|
||||
for (uint32_t i = start;i<length;i++) {
|
||||
if(!readByte(&digit)) return 0;
|
||||
if (this->stream) {
|
||||
if (isPublish && idx-*lengthLength-2>skip) {
|
||||
this->stream->write(digit);
|
||||
}
|
||||
}
|
||||
|
||||
if (len < this->bufferSize) {
|
||||
this->buffer[len] = digit;
|
||||
len++;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
|
||||
if (!this->stream && idx > this->bufferSize) {
|
||||
len = 0; // This will cause the packet to be ignored.
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
boolean PubSubClient::loop() {
|
||||
if (connected()) {
|
||||
unsigned long t = millis();
|
||||
if ((t - lastInActivity > this->keepAlive*1000UL) || (t - lastOutActivity > this->keepAlive*1000UL)) {
|
||||
if (pingOutstanding) {
|
||||
this->_state = MQTT_CONNECTION_TIMEOUT;
|
||||
_client->stop();
|
||||
return false;
|
||||
} else {
|
||||
this->buffer[0] = MQTTPINGREQ;
|
||||
this->buffer[1] = 0;
|
||||
_client->write(this->buffer,2);
|
||||
lastOutActivity = t;
|
||||
lastInActivity = t;
|
||||
pingOutstanding = true;
|
||||
}
|
||||
}
|
||||
if (_client->available()) {
|
||||
uint8_t llen;
|
||||
uint16_t len = readPacket(&llen);
|
||||
uint16_t msgId = 0;
|
||||
uint8_t *payload;
|
||||
if (len > 0) {
|
||||
lastInActivity = t;
|
||||
uint8_t type = this->buffer[0]&0xF0;
|
||||
if (type == MQTTPUBLISH) {
|
||||
if (callback) {
|
||||
uint16_t tl = (this->buffer[llen+1]<<8)+this->buffer[llen+2]; /* topic length in bytes */
|
||||
memmove(this->buffer+llen+2,this->buffer+llen+3,tl); /* move topic inside buffer 1 byte to front */
|
||||
this->buffer[llen+2+tl] = 0; /* end the topic as a 'C' string with \x00 */
|
||||
char *topic = (char*) this->buffer+llen+2;
|
||||
// msgId only present for QOS>0
|
||||
if ((this->buffer[0]&0x06) == MQTTQOS1) {
|
||||
msgId = (this->buffer[llen+3+tl]<<8)+this->buffer[llen+3+tl+1];
|
||||
payload = this->buffer+llen+3+tl+2;
|
||||
callback(topic,payload,len-llen-3-tl-2);
|
||||
|
||||
this->buffer[0] = MQTTPUBACK;
|
||||
this->buffer[1] = 2;
|
||||
this->buffer[2] = (msgId >> 8);
|
||||
this->buffer[3] = (msgId & 0xFF);
|
||||
_client->write(this->buffer,4);
|
||||
lastOutActivity = t;
|
||||
|
||||
} else {
|
||||
payload = this->buffer+llen+3+tl;
|
||||
callback(topic,payload,len-llen-3-tl);
|
||||
}
|
||||
}
|
||||
} else if (type == MQTTPINGREQ) {
|
||||
this->buffer[0] = MQTTPINGRESP;
|
||||
this->buffer[1] = 0;
|
||||
_client->write(this->buffer,2);
|
||||
} else if (type == MQTTPINGRESP) {
|
||||
pingOutstanding = false;
|
||||
}
|
||||
} else if (!connected()) {
|
||||
// readPacket has closed the connection
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const char* payload) {
|
||||
return publish(topic,(const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0,false);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) {
|
||||
return publish(topic,(const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0,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 (this->bufferSize < MQTT_MAX_HEADER_SIZE + 2+strnlen(topic, this->bufferSize) + plength) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = MQTT_MAX_HEADER_SIZE;
|
||||
length = writeString(topic,this->buffer,length);
|
||||
|
||||
// Add payload
|
||||
uint16_t i;
|
||||
for (i=0;i<plength;i++) {
|
||||
this->buffer[length++] = payload[i];
|
||||
}
|
||||
|
||||
// Write the header
|
||||
uint8_t header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
return write(header,this->buffer,length-MQTT_MAX_HEADER_SIZE);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish_P(const char* topic, const char* payload, boolean retained) {
|
||||
return publish_P(topic, (const uint8_t*)payload, payload ? strnlen(payload, this->bufferSize) : 0, retained);
|
||||
}
|
||||
|
||||
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;
|
||||
int expectedLength;
|
||||
|
||||
if (!connected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
tlen = strnlen(topic, this->bufferSize);
|
||||
|
||||
header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
this->buffer[pos++] = header;
|
||||
len = plength + 2 + tlen;
|
||||
do {
|
||||
digit = len & 127; //digit = len %128
|
||||
len >>= 7; //len = len / 128
|
||||
if (len > 0) {
|
||||
digit |= 0x80;
|
||||
}
|
||||
this->buffer[pos++] = digit;
|
||||
llen++;
|
||||
} while(len>0);
|
||||
|
||||
pos = writeString(topic,this->buffer,pos);
|
||||
|
||||
rc += _client->write(this->buffer,pos);
|
||||
|
||||
for (i=0;i<plength;i++) {
|
||||
rc += _client->write((char)pgm_read_byte_near(payload + i));
|
||||
}
|
||||
|
||||
lastOutActivity = millis();
|
||||
|
||||
expectedLength = 1 + llen + 2 + tlen + plength;
|
||||
|
||||
return (rc == expectedLength);
|
||||
}
|
||||
|
||||
boolean PubSubClient::beginPublish(const char* topic, unsigned int plength, boolean retained) {
|
||||
if (connected()) {
|
||||
// Send the header and variable length field
|
||||
uint16_t length = MQTT_MAX_HEADER_SIZE;
|
||||
length = writeString(topic,this->buffer,length);
|
||||
uint8_t header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
size_t hlen = buildHeader(header, this->buffer, plength+length-MQTT_MAX_HEADER_SIZE);
|
||||
uint16_t rc = _client->write(this->buffer+(MQTT_MAX_HEADER_SIZE-hlen),length-(MQTT_MAX_HEADER_SIZE-hlen));
|
||||
lastOutActivity = millis();
|
||||
return (rc == (length-(MQTT_MAX_HEADER_SIZE-hlen)));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int PubSubClient::endPublish() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t PubSubClient::write(uint8_t data) {
|
||||
lastOutActivity = millis();
|
||||
return _client->write(data);
|
||||
}
|
||||
|
||||
size_t PubSubClient::write(const uint8_t *buffer, size_t size) {
|
||||
lastOutActivity = millis();
|
||||
return _client->write(buffer,size);
|
||||
}
|
||||
|
||||
size_t PubSubClient::buildHeader(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 len = length;
|
||||
do {
|
||||
|
||||
digit = len & 127; //digit = len %128
|
||||
len >>= 7; //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[MQTT_MAX_HEADER_SIZE-llen+i] = lenBuf[i];
|
||||
}
|
||||
return llen+1; // Full header size is variable length bit plus the 1-byte fixed header
|
||||
}
|
||||
|
||||
boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
||||
uint16_t rc;
|
||||
uint8_t hlen = buildHeader(header, buf, length);
|
||||
|
||||
#ifdef MQTT_MAX_TRANSFER_SIZE
|
||||
uint8_t* writeBuf = buf+(MQTT_MAX_HEADER_SIZE-hlen);
|
||||
uint16_t bytesRemaining = length+hlen; //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+(MQTT_MAX_HEADER_SIZE-hlen),length+hlen);
|
||||
lastOutActivity = millis();
|
||||
return (rc == hlen+length);
|
||||
#endif
|
||||
}
|
||||
|
||||
boolean PubSubClient::subscribe(const char* topic) {
|
||||
return subscribe(topic, 0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
|
||||
size_t topicLength = strnlen(topic, this->bufferSize);
|
||||
if (topic == 0) {
|
||||
return false;
|
||||
}
|
||||
if (qos > 1) {
|
||||
return false;
|
||||
}
|
||||
if (this->bufferSize < 9 + topicLength) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
if (connected()) {
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = MQTT_MAX_HEADER_SIZE;
|
||||
nextMsgId++;
|
||||
if (nextMsgId == 0) {
|
||||
nextMsgId = 1;
|
||||
}
|
||||
this->buffer[length++] = (nextMsgId >> 8);
|
||||
this->buffer[length++] = (nextMsgId & 0xFF);
|
||||
length = writeString((char*)topic, this->buffer,length);
|
||||
this->buffer[length++] = qos;
|
||||
return write(MQTTSUBSCRIBE|MQTTQOS1,this->buffer,length-MQTT_MAX_HEADER_SIZE);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::unsubscribe(const char* topic) {
|
||||
size_t topicLength = strnlen(topic, this->bufferSize);
|
||||
if (topic == 0) {
|
||||
return false;
|
||||
}
|
||||
if (this->bufferSize < 9 + topicLength) {
|
||||
// Too long
|
||||
return false;
|
||||
}
|
||||
if (connected()) {
|
||||
uint16_t length = MQTT_MAX_HEADER_SIZE;
|
||||
nextMsgId++;
|
||||
if (nextMsgId == 0) {
|
||||
nextMsgId = 1;
|
||||
}
|
||||
this->buffer[length++] = (nextMsgId >> 8);
|
||||
this->buffer[length++] = (nextMsgId & 0xFF);
|
||||
length = writeString(topic, this->buffer,length);
|
||||
return write(MQTTUNSUBSCRIBE|MQTTQOS1,this->buffer,length-MQTT_MAX_HEADER_SIZE);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void PubSubClient::disconnect() {
|
||||
this->buffer[0] = MQTTDISCONNECT;
|
||||
this->buffer[1] = 0;
|
||||
_client->write(this->buffer,2);
|
||||
_state = MQTT_DISCONNECTED;
|
||||
_client->flush();
|
||||
_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();
|
||||
}
|
||||
} else {
|
||||
return this->_state == MQTT_CONNECTED;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
boolean PubSubClient::setBufferSize(uint16_t size) {
|
||||
if (size == 0) {
|
||||
// Cannot set it back to 0
|
||||
return false;
|
||||
}
|
||||
if (this->bufferSize == 0) {
|
||||
this->buffer = (uint8_t*)malloc(size);
|
||||
} else {
|
||||
uint8_t* newBuffer = (uint8_t*)realloc(this->buffer, size);
|
||||
if (newBuffer != NULL) {
|
||||
this->buffer = newBuffer;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
this->bufferSize = size;
|
||||
return (this->buffer != NULL);
|
||||
}
|
||||
|
||||
uint16_t PubSubClient::getBufferSize() {
|
||||
return this->bufferSize;
|
||||
}
|
||||
PubSubClient& PubSubClient::setKeepAlive(uint16_t keepAlive) {
|
||||
this->keepAlive = keepAlive;
|
||||
return *this;
|
||||
}
|
||||
PubSubClient& PubSubClient::setSocketTimeout(uint16_t timeout) {
|
||||
this->socketTimeout = timeout;
|
||||
return *this;
|
||||
}
|
184
ampel-firmware/src/lib/PubSubClient/src/PubSubClient.h
Normal file
184
ampel-firmware/src/lib/PubSubClient/src/PubSubClient.h
Normal file
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
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"
|
||||
|
||||
#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. Override with setBufferSize().
|
||||
#ifndef MQTT_MAX_PACKET_SIZE
|
||||
#define MQTT_MAX_PACKET_SIZE 256
|
||||
#endif
|
||||
|
||||
// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()
|
||||
#ifndef MQTT_KEEPALIVE
|
||||
#define MQTT_KEEPALIVE 15
|
||||
#endif
|
||||
|
||||
// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds. Override with setSocketTimeout()
|
||||
#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_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)
|
||||
|
||||
// Maximum size of fixed header and variable length size header
|
||||
#define MQTT_MAX_HEADER_SIZE 5
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
#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
|
||||
|
||||
#define CHECK_STRING_LENGTH(l,s) if (l+2+strnlen(s, this->bufferSize) > this->bufferSize) {_client->stop();return false;}
|
||||
|
||||
class PubSubClient : public Print {
|
||||
private:
|
||||
Client* _client;
|
||||
uint8_t* buffer;
|
||||
uint16_t bufferSize;
|
||||
uint16_t keepAlive;
|
||||
uint16_t socketTimeout;
|
||||
uint16_t nextMsgId;
|
||||
unsigned long lastOutActivity;
|
||||
unsigned long lastInActivity;
|
||||
bool pingOutstanding;
|
||||
MQTT_CALLBACK_SIGNATURE;
|
||||
uint32_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);
|
||||
// Build up the header ready to send
|
||||
// Returns the size of the header
|
||||
// Note: the header is built at the end of the first MQTT_MAX_HEADER_SIZE bytes, so will start
|
||||
// (MQTT_MAX_HEADER_SIZE - <returned size>) bytes into the buffer
|
||||
size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
|
||||
IPAddress ip;
|
||||
const char* domain;
|
||||
uint16_t port;
|
||||
Stream* stream;
|
||||
int _state;
|
||||
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&);
|
||||
|
||||
~PubSubClient();
|
||||
|
||||
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);
|
||||
PubSubClient& setKeepAlive(uint16_t keepAlive);
|
||||
PubSubClient& setSocketTimeout(uint16_t timeout);
|
||||
|
||||
boolean setBufferSize(uint16_t size);
|
||||
uint16_t getBufferSize();
|
||||
|
||||
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);
|
||||
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage, boolean cleanSession);
|
||||
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 char* payload, boolean retained);
|
||||
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
||||
// Start to publish a message.
|
||||
// This API:
|
||||
// beginPublish(...)
|
||||
// one or more calls to write(...)
|
||||
// endPublish()
|
||||
// Allows for arbitrarily large payloads to be sent without them having to be copied into
|
||||
// a new buffer and held in memory at one time
|
||||
// Returns 1 if the message was started successfully, 0 if there was an error
|
||||
boolean beginPublish(const char* topic, unsigned int plength, boolean retained);
|
||||
// Finish off this publish message (started with beginPublish)
|
||||
// Returns 1 if the packet was sent successfully, 0 if there was an error
|
||||
int endPublish();
|
||||
// Write a single byte of payload (only to be used with beginPublish/endPublish)
|
||||
virtual size_t write(uint8_t);
|
||||
// Write size bytes from buffer into the payload (only to be used with beginPublish/endPublish)
|
||||
// Returns the number of bytes written
|
||||
virtual size_t write(const uint8_t *buffer, size_t size);
|
||||
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
|
|
@ -0,0 +1,55 @@
|
|||
SparkFun License Information
|
||||
============================
|
||||
|
||||
SparkFun uses two different licenses for our files — one for hardware and one for code.
|
||||
|
||||
Hardware
|
||||
---------
|
||||
|
||||
**SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).**
|
||||
|
||||
Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode).
|
||||
|
||||
You are free to:
|
||||
|
||||
Share — copy and redistribute the material in any medium or format
|
||||
Adapt — remix, transform, and build upon the material
|
||||
for any purpose, even commercially.
|
||||
The licensor cannot revoke these freedoms as long as you follow the license terms.
|
||||
Under the following terms:
|
||||
|
||||
Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
|
||||
ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
|
||||
No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
|
||||
Notices:
|
||||
|
||||
You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
|
||||
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
|
||||
|
||||
|
||||
Code
|
||||
--------
|
||||
|
||||
**SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).**
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 SparkFun Electronics
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,52 @@
|
|||
SparkFun SCD30 CO₂ Sensor Library
|
||||
===========================================================
|
||||
|
||||

|
||||
|
||||
[*SparkX CO₂ Humidity and Temperature Sensor - SCD30 (SPX-14751)*](https://www.sparkfun.com/products/14751)
|
||||
|
||||
The SCD30 from Sensirion is a high quality [NDIR](https://en.wikipedia.org/wiki/Nondispersive_infrared_sensor) based CO₂ sensor capable of detecting 400 to 10000ppm with an accuracy of ±(30ppm+3%). In order to improve accuracy the SCD30 has temperature and humidity sensing built-in, as well as commands to compensate for altitude.
|
||||
|
||||
We've written an Arduino library to make reading the CO₂, humidity, and temperature very easy. It can be downloaded through the Arduino Library manager: search for 'SparkFun SCD30'. We recommend using a [Qwiic Breadboard Cable](https://www.sparkfun.com/products/14425) to connect the SCD30 to a Qwiic compatible board. The Ye*LL*ow wire goes in the SC*L* pin. The SCD30 also supports a serial interface but we haven't worked with it.
|
||||
|
||||
The CO₂ sensor works very well and for additional accuracy the SCD30 accepts ambient pressure readings. We recommend using the SCD30 in conjunction with the [Qwiic Pressure Sensor - MS5637](https://www.sparkfun.com/products/14688) or the [Qwiic Environmental Sensor - BME680](https://www.sparkfun.com/products/14570) to obtain the current barometric pressure.
|
||||
|
||||
Note: The SCD30 has an automatic self-calibration routine. Sensirion recommends 7 days of continuous readings with at least 1 hour a day of 'fresh air' for self-calibration to complete.
|
||||
|
||||
Library written by Nathan Seidle ([SparkFun](http://www.sparkfun.com)).
|
||||
|
||||
Thanks to!
|
||||
|
||||
* [jobr97](https://github.com/jobr97) for adding the getTemperatureOffset() method
|
||||
* [bobobo1618](https://github.com/bobobo1618) for writing a CRC check and improving the return values of the library
|
||||
* [labeneator](https://github.com/labeneator) for adding method to disable calibrate at begin
|
||||
* [AndreasExner](https://github.com/AndreasExner) for adding [reset and getAutoSelfCalibration methods](https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library/pull/17)
|
||||
* [awatterott](https://github.com/awatterott) for adding [getAltitudeCompensation()](https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library/pull/18)
|
||||
* [jogi-k](https://github.com/jogi-k) for adding [teensy i2clib](https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library/pull/19) support
|
||||
* [paulvha](https://github.com/paulvha) for the suggestions and corrections in [his version of the library](https://github.com/paulvha/scd30)
|
||||
|
||||
Repository Contents
|
||||
-------------------
|
||||
|
||||
* **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE.
|
||||
* **/src** - Source files for the library (.cpp, .h).
|
||||
* **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE.
|
||||
* **library.properties** - General library properties for the Arduino package manager.
|
||||
|
||||
Documentation
|
||||
--------------
|
||||
|
||||
* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
|
||||
|
||||
License Information
|
||||
-------------------
|
||||
|
||||
This product is _**open source**_!
|
||||
|
||||
Various bits of the code have different licenses applied. Anything SparkFun wrote is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round!
|
||||
|
||||
Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license.
|
||||
|
||||
Distributed as-is; no warranty is given.
|
||||
|
||||
- Your friends at SparkFun.
|
Binary file not shown.
|
@ -0,0 +1,58 @@
|
|||
#######################################
|
||||
# Syntax Coloring Map
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
SCD30 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
SCD30 KEYWORD2
|
||||
begin KEYWORD2
|
||||
enableDebugging KEYWORD2
|
||||
beginMeasuring KEYWORD2
|
||||
StopMeasurement KEYWORD2
|
||||
getSettingValue KEYWORD2
|
||||
getForcedRecalibration KEYWORD2
|
||||
getMeasurementInterval KEYWORD2
|
||||
getTemperatureOffset KEYWORD2
|
||||
getAltitudeCompensation KEYWORD2
|
||||
getFirmwareVersion KEYWORD2
|
||||
getCO2 KEYWORD2
|
||||
getHumidity KEYWORD2
|
||||
getTemperature KEYWORD2
|
||||
setMeasurementInterval KEYWORD2
|
||||
setAmbientPressure KEYWORD2
|
||||
setAltitudeCompensation KEYWORD2
|
||||
setAutoSelfCalibration KEYWORD2
|
||||
setForcedRecalibrationFactor KEYWORD2
|
||||
setTemperatureOffset KEYWORD2
|
||||
getAutoSelfCalibration KEYWORD2
|
||||
dataAvailable KEYWORD2
|
||||
readMeasurement KEYWORD2
|
||||
reset KEYWORD2
|
||||
sendCommand KEYWORD2
|
||||
readRegister KEYWORD2
|
||||
computeCRC8 KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
SCD30_ADDRESS LITERAL1
|
||||
COMMAND_CONTINUOUS_MEASUREMENT LITERAL1
|
||||
COMMAND_SET_MEASUREMENT_INTERVAL LITERAL1
|
||||
COMMAND_GET_DATA_READY LITERAL1
|
||||
COMMAND_READ_MEASUREMENT LITERAL1
|
||||
COMMAND_AUTOMATIC_SELF_CALIBRATION LITERAL1
|
||||
COMMAND_SET_FORCED_RECALIBRATION_FACTOR LITERAL1
|
||||
COMMAND_SET_TEMPERATURE_OFFSET LITERAL1
|
||||
COMMAND_SET_ALTITUDE_COMPENSATION LITERAL1
|
||||
COMMAND_RESET LITERAL1
|
||||
COMMAND_STOP_MEAS LITERAL1
|
||||
COMMAND_READ_FW_VER LITERAL1
|
|
@ -0,0 +1,9 @@
|
|||
name=SparkFun SCD30 Arduino Library
|
||||
version=1.0.11
|
||||
author=SparkFun Electronics
|
||||
maintainer=SparkFun Electronics <sparkfun.com>
|
||||
sentence=Library for the Sensirion SCD30 CO2 Sensor
|
||||
paragraph=An Arduinolibrary for the SCD30 CO2 sensor from Sensirion. The SCD30 is a high quality <a href="https://en.wikipedia.org/wiki/Nondispersive_infrared_sensor">NDIR</a> based CO₂ sensor capable of detecting 400 to 10000ppm with an accuracy of ±(30ppm+3%). In order to improve accuracy the SCD30 has temperature and humidity sensing built-in, as well as commands to set the current altitude.<br><br>Get the SCD30 <a href="https://www.sparkfun.com/products/14751">here</a>.
|
||||
category=Sensors
|
||||
url=https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library
|
||||
architectures=*
|
|
@ -0,0 +1,457 @@
|
|||
/*
|
||||
This is a library written for the SCD30
|
||||
SparkFun sells these at its website: www.sparkfun.com
|
||||
Do you like this library? Help support SparkFun. Buy a board!
|
||||
https://www.sparkfun.com/products/14751
|
||||
|
||||
Written by Nathan Seidle @ SparkFun Electronics, May 22nd, 2018
|
||||
|
||||
Updated February 1st 2021 to include some of the features of paulvha's version of the library
|
||||
(while maintaining backward-compatibility):
|
||||
https://github.com/paulvha/scd30
|
||||
Thank you Paul!
|
||||
|
||||
The SCD30 measures CO2 with accuracy of +/- 30ppm.
|
||||
|
||||
This library handles the initialization of the SCD30 and outputs
|
||||
CO2 levels, relative humidty, and temperature.
|
||||
|
||||
https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library
|
||||
|
||||
Development environment specifics:
|
||||
Arduino IDE 1.8.13
|
||||
|
||||
SparkFun code, firmware, and software is released under the MIT License.
|
||||
Please see LICENSE.md for more details.
|
||||
*/
|
||||
|
||||
#include "SparkFun_SCD30_Arduino_Library.h"
|
||||
|
||||
SCD30::SCD30(void)
|
||||
{
|
||||
// Constructor
|
||||
}
|
||||
|
||||
//Initialize the Serial port
|
||||
#ifdef USE_TEENSY3_I2C_LIB
|
||||
bool SCD30::begin(i2c_t3 &wirePort, bool autoCalibrate, bool measBegin)
|
||||
#else
|
||||
bool SCD30::begin(TwoWire &wirePort, bool autoCalibrate, bool measBegin)
|
||||
#endif
|
||||
{
|
||||
_i2cPort = &wirePort; //Grab which port the user wants us to use
|
||||
|
||||
/* Especially during obtaining the ACK BIT after a byte sent the SCD30 is using clock stretching (but NOT only there)!
|
||||
* The need for clock stretching is described in the Sensirion_CO2_Sensors_SCD30_Interface_Description.pdf
|
||||
*
|
||||
* The default clock stretch (maximum wait time) on the ESP8266-library (2.4.2) is 230us which is set during _i2cPort->begin();
|
||||
* In the current implementation of the ESP8266 I2C driver there is NO error message when this time expired, while
|
||||
* the clock stretch is still happening, causing uncontrolled behaviour of the hardware combination.
|
||||
*
|
||||
* To set ClockStretchlimit() a check for ESP8266 boards has been added in the driver.
|
||||
*
|
||||
* With setting to 20000, we set a max timeout of 20mS (> 20x the maximum measured) basically disabling the time-out
|
||||
* and now wait for clock stretch to be controlled by the client.
|
||||
*/
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
_i2cPort->setClockStretchLimit(200000);
|
||||
#endif
|
||||
|
||||
uint16_t fwVer;
|
||||
if (getFirmwareVersion(&fwVer) == false) // Read the firmware version. Return false if the CRC check fails.
|
||||
return (false);
|
||||
|
||||
if (_printDebug == true)
|
||||
{
|
||||
_debugPort->print(F("SCD30 begin: got firmware version 0x"));
|
||||
_debugPort->println(fwVer, HEX);
|
||||
}
|
||||
|
||||
if (measBegin == false) // Exit now if measBegin is false
|
||||
return (true);
|
||||
|
||||
//Check for device to respond correctly
|
||||
if (beginMeasuring() == true) //Start continuous measurements
|
||||
{
|
||||
setMeasurementInterval(2); //2 seconds between measurements
|
||||
setAutoSelfCalibration(autoCalibrate); //Enable auto-self-calibration
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
||||
return (false); //Something went wrong
|
||||
}
|
||||
|
||||
//Calling this function with nothing sets the debug port to Serial
|
||||
//You can also call it with other streams like Serial1, SerialUSB, etc.
|
||||
void SCD30::enableDebugging(Stream &debugPort)
|
||||
{
|
||||
_debugPort = &debugPort;
|
||||
_printDebug = true;
|
||||
}
|
||||
|
||||
//Returns the latest available CO2 level
|
||||
//If the current level has already been reported, trigger a new read
|
||||
uint16_t SCD30::getCO2(void)
|
||||
{
|
||||
if (co2HasBeenReported == true) //Trigger a new read
|
||||
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
|
||||
|
||||
co2HasBeenReported = true;
|
||||
|
||||
return (uint16_t)co2; //Cut off decimal as co2 is 0 to 10,000
|
||||
}
|
||||
|
||||
//Returns the latest available humidity
|
||||
//If the current level has already been reported, trigger a new read
|
||||
float SCD30::getHumidity(void)
|
||||
{
|
||||
if (humidityHasBeenReported == true) //Trigger a new read
|
||||
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
|
||||
|
||||
humidityHasBeenReported = true;
|
||||
|
||||
return humidity;
|
||||
}
|
||||
|
||||
//Returns the latest available temperature
|
||||
//If the current level has already been reported, trigger a new read
|
||||
float SCD30::getTemperature(void)
|
||||
{
|
||||
if (temperatureHasBeenReported == true) //Trigger a new read
|
||||
readMeasurement(); //Pull in new co2, humidity, and temp into global vars
|
||||
|
||||
temperatureHasBeenReported = true;
|
||||
|
||||
return temperature;
|
||||
}
|
||||
|
||||
//Enables or disables the ASC
|
||||
bool SCD30::setAutoSelfCalibration(bool enable)
|
||||
{
|
||||
if (enable)
|
||||
return sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 1); //Activate continuous ASC
|
||||
else
|
||||
return sendCommand(COMMAND_AUTOMATIC_SELF_CALIBRATION, 0); //Deactivate continuous ASC
|
||||
}
|
||||
|
||||
//Set the forced recalibration factor. See 1.3.7.
|
||||
//The reference CO2 concentration has to be within the range 400 ppm ≤ cref(CO2) ≤ 2000 ppm.
|
||||
bool SCD30::setForcedRecalibrationFactor(uint16_t concentration)
|
||||
{
|
||||
if (concentration < 400 || concentration > 2000)
|
||||
{
|
||||
return false; //Error check.
|
||||
}
|
||||
return sendCommand(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, concentration);
|
||||
}
|
||||
|
||||
//Get the temperature offset. See 1.3.8.
|
||||
float SCD30::getTemperatureOffset(void)
|
||||
{
|
||||
uint16_t response = readRegister(COMMAND_SET_TEMPERATURE_OFFSET);
|
||||
return (((float)response) / 100.0);
|
||||
}
|
||||
|
||||
//Set the temperature offset. See 1.3.8.
|
||||
bool SCD30::setTemperatureOffset(float tempOffset)
|
||||
{
|
||||
union
|
||||
{
|
||||
int16_t signed16;
|
||||
uint16_t unsigned16;
|
||||
} signedUnsigned; // Avoid any ambiguity casting int16_t to uint16_t
|
||||
signedUnsigned.signed16 = tempOffset * 100;
|
||||
return sendCommand(COMMAND_SET_TEMPERATURE_OFFSET, signedUnsigned.unsigned16);
|
||||
}
|
||||
|
||||
//Get the altitude compenstation. See 1.3.9.
|
||||
uint16_t SCD30::getAltitudeCompensation(void)
|
||||
{
|
||||
return readRegister(COMMAND_SET_ALTITUDE_COMPENSATION);
|
||||
}
|
||||
|
||||
//Set the altitude compenstation. See 1.3.9.
|
||||
bool SCD30::setAltitudeCompensation(uint16_t altitude)
|
||||
{
|
||||
return sendCommand(COMMAND_SET_ALTITUDE_COMPENSATION, altitude);
|
||||
}
|
||||
|
||||
//Set the pressure compenstation. This is passed during measurement startup.
|
||||
//mbar can be 700 to 1200
|
||||
bool SCD30::setAmbientPressure(uint16_t pressure_mbar)
|
||||
{
|
||||
if (pressure_mbar < 700 || pressure_mbar > 1200)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return sendCommand(COMMAND_CONTINUOUS_MEASUREMENT, pressure_mbar);
|
||||
}
|
||||
|
||||
// SCD30 soft reset
|
||||
void SCD30::reset()
|
||||
{
|
||||
sendCommand(COMMAND_RESET);
|
||||
|
||||
}
|
||||
|
||||
// Get the current ASC setting
|
||||
bool SCD30::getAutoSelfCalibration()
|
||||
{
|
||||
uint16_t response = readRegister(COMMAND_AUTOMATIC_SELF_CALIBRATION);
|
||||
if (response == 1) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Begins continuous measurements
|
||||
//Continuous measurement status is saved in non-volatile memory. When the sensor
|
||||
//is powered down while continuous measurement mode is active SCD30 will measure
|
||||
//continuously after repowering without sending the measurement command.
|
||||
//Returns true if successful
|
||||
bool SCD30::beginMeasuring(uint16_t pressureOffset)
|
||||
{
|
||||
return (sendCommand(COMMAND_CONTINUOUS_MEASUREMENT, pressureOffset));
|
||||
}
|
||||
|
||||
//Overload - no pressureOffset
|
||||
bool SCD30::beginMeasuring(void)
|
||||
{
|
||||
return (beginMeasuring(0));
|
||||
}
|
||||
|
||||
// Stop continuous measurement
|
||||
bool SCD30::StopMeasurement(void)
|
||||
{
|
||||
return(sendCommand(COMMAND_STOP_MEAS));
|
||||
}
|
||||
|
||||
//Sets interval between measurements
|
||||
//2 seconds to 1800 seconds (30 minutes)
|
||||
bool SCD30::setMeasurementInterval(uint16_t interval)
|
||||
{
|
||||
return sendCommand(COMMAND_SET_MEASUREMENT_INTERVAL, interval);
|
||||
}
|
||||
|
||||
//Returns true when data is available
|
||||
bool SCD30::dataAvailable()
|
||||
{
|
||||
uint16_t response = readRegister(COMMAND_GET_DATA_READY);
|
||||
|
||||
if (response == 1)
|
||||
return (true);
|
||||
return (false);
|
||||
}
|
||||
|
||||
//Get 18 bytes from SCD30
|
||||
//Updates global variables with floats
|
||||
//Returns true if success
|
||||
bool SCD30::readMeasurement()
|
||||
{
|
||||
//Verify we have data from the sensor
|
||||
if (dataAvailable() == false)
|
||||
return (false);
|
||||
|
||||
ByteToFl tempCO2; tempCO2.value = 0;
|
||||
ByteToFl tempHumidity; tempHumidity.value = 0;
|
||||
ByteToFl tempTemperature; tempTemperature.value = 0;
|
||||
|
||||
_i2cPort->beginTransmission(SCD30_ADDRESS);
|
||||
_i2cPort->write(COMMAND_READ_MEASUREMENT >> 8); //MSB
|
||||
_i2cPort->write(COMMAND_READ_MEASUREMENT & 0xFF); //LSB
|
||||
if (_i2cPort->endTransmission() != 0)
|
||||
return (0); //Sensor did not ACK
|
||||
|
||||
const uint8_t receivedBytes = _i2cPort->requestFrom((uint8_t)SCD30_ADDRESS, (uint8_t)18);
|
||||
bool error = false;
|
||||
if (_i2cPort->available())
|
||||
{
|
||||
byte bytesToCrc[2];
|
||||
for (byte x = 0; x < 18; x++)
|
||||
{
|
||||
byte incoming = _i2cPort->read();
|
||||
|
||||
switch (x)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
case 3:
|
||||
case 4:
|
||||
tempCO2.array[x < 3 ? 3-x : 4-x] = incoming;
|
||||
bytesToCrc[x % 3] = incoming;
|
||||
break;
|
||||
case 6:
|
||||
case 7:
|
||||
case 9:
|
||||
case 10:
|
||||
tempTemperature.array[x < 9 ? 9-x : 10-x] = incoming;
|
||||
bytesToCrc[x % 3] = incoming;
|
||||
break;
|
||||
case 12:
|
||||
case 13:
|
||||
case 15:
|
||||
case 16:
|
||||
tempHumidity.array[x < 15 ? 15-x : 16-x] = incoming;
|
||||
bytesToCrc[x % 3] = incoming;
|
||||
break;
|
||||
default:
|
||||
//Validate CRC
|
||||
uint8_t foundCrc = computeCRC8(bytesToCrc, 2);
|
||||
if (foundCrc != incoming)
|
||||
{
|
||||
if (_printDebug == true)
|
||||
{
|
||||
_debugPort->print(F("readMeasurement: found CRC in byte "));
|
||||
_debugPort->print(x);
|
||||
_debugPort->print(F(", expected 0x"));
|
||||
_debugPort->print(foundCrc, HEX);
|
||||
_debugPort->print(F(", got 0x"));
|
||||
_debugPort->println(incoming, HEX);
|
||||
}
|
||||
error = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_printDebug == true)
|
||||
{
|
||||
_debugPort->print(F("readMeasurement: no SCD30 data found from I2C, i2c claims we should receive "));
|
||||
_debugPort->print(receivedBytes);
|
||||
_debugPort->println(F(" bytes"));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (error)
|
||||
{
|
||||
if (_printDebug == true)
|
||||
_debugPort->println(F("readMeasurement: encountered error reading SCD30 data."));
|
||||
return false;
|
||||
}
|
||||
//Now copy the uint32s into their associated floats
|
||||
co2 = tempCO2.value;
|
||||
temperature = tempTemperature.value;
|
||||
humidity = tempHumidity.value;
|
||||
|
||||
//Mark our global variables as fresh
|
||||
co2HasBeenReported = false;
|
||||
humidityHasBeenReported = false;
|
||||
temperatureHasBeenReported = false;
|
||||
|
||||
return (true); //Success! New data available in globals.
|
||||
}
|
||||
|
||||
//Gets a setting by reading the appropriate register.
|
||||
//Returns true if the CRC is valid.
|
||||
bool SCD30::getSettingValue(uint16_t registerAddress, uint16_t *val)
|
||||
{
|
||||
_i2cPort->beginTransmission(SCD30_ADDRESS);
|
||||
_i2cPort->write(registerAddress >> 8); //MSB
|
||||
_i2cPort->write(registerAddress & 0xFF); //LSB
|
||||
if (_i2cPort->endTransmission() != 0)
|
||||
return (false); //Sensor did not ACK
|
||||
|
||||
_i2cPort->requestFrom((uint8_t)SCD30_ADDRESS, (uint8_t)3); // Request data and CRC
|
||||
if (_i2cPort->available())
|
||||
{
|
||||
uint8_t data[2];
|
||||
data[0] = _i2cPort->read();
|
||||
data[1] = _i2cPort->read();
|
||||
uint8_t crc = _i2cPort->read();
|
||||
*val = (uint16_t)data[0] << 8 | data[1];
|
||||
uint8_t expectedCRC = computeCRC8(data, 2);
|
||||
if (crc == expectedCRC) // Return true if CRC check is OK
|
||||
return (true);
|
||||
if (_printDebug == true)
|
||||
{
|
||||
_debugPort->print(F("getSettingValue: CRC fail: expected 0x"));
|
||||
_debugPort->print(expectedCRC, HEX);
|
||||
_debugPort->print(F(", got 0x"));
|
||||
_debugPort->println(crc, HEX);
|
||||
}
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
//Gets two bytes from SCD30
|
||||
uint16_t SCD30::readRegister(uint16_t registerAddress)
|
||||
{
|
||||
_i2cPort->beginTransmission(SCD30_ADDRESS);
|
||||
_i2cPort->write(registerAddress >> 8); //MSB
|
||||
_i2cPort->write(registerAddress & 0xFF); //LSB
|
||||
if (_i2cPort->endTransmission() != 0)
|
||||
return (0); //Sensor did not ACK
|
||||
|
||||
_i2cPort->requestFrom((uint8_t)SCD30_ADDRESS, (uint8_t)2);
|
||||
if (_i2cPort->available())
|
||||
{
|
||||
uint8_t msb = _i2cPort->read();
|
||||
uint8_t lsb = _i2cPort->read();
|
||||
return ((uint16_t)msb << 8 | lsb);
|
||||
}
|
||||
return (0); //Sensor did not respond
|
||||
}
|
||||
|
||||
//Sends a command along with arguments and CRC
|
||||
bool SCD30::sendCommand(uint16_t command, uint16_t arguments)
|
||||
{
|
||||
uint8_t data[2];
|
||||
data[0] = arguments >> 8;
|
||||
data[1] = arguments & 0xFF;
|
||||
uint8_t crc = computeCRC8(data, 2); //Calc CRC on the arguments only, not the command
|
||||
|
||||
_i2cPort->beginTransmission(SCD30_ADDRESS);
|
||||
_i2cPort->write(command >> 8); //MSB
|
||||
_i2cPort->write(command & 0xFF); //LSB
|
||||
_i2cPort->write(arguments >> 8); //MSB
|
||||
_i2cPort->write(arguments & 0xFF); //LSB
|
||||
_i2cPort->write(crc);
|
||||
if (_i2cPort->endTransmission() != 0)
|
||||
return (false); //Sensor did not ACK
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
||||
//Sends just a command, no arguments, no CRC
|
||||
bool SCD30::sendCommand(uint16_t command)
|
||||
{
|
||||
_i2cPort->beginTransmission(SCD30_ADDRESS);
|
||||
_i2cPort->write(command >> 8); //MSB
|
||||
_i2cPort->write(command & 0xFF); //LSB
|
||||
if (_i2cPort->endTransmission() != 0)
|
||||
return (false); //Sensor did not ACK
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
||||
//Given an array and a number of bytes, this calculate CRC8 for those bytes
|
||||
//CRC is only calc'd on the data portion (two bytes) of the four bytes being sent
|
||||
//From: http://www.sunshine2k.de/articles/coding/crc/understanding_crc.html
|
||||
//Tested with: http://www.sunshine2k.de/coding/javascript/crc/crc_js.html
|
||||
//x^8+x^5+x^4+1 = 0x31
|
||||
uint8_t SCD30::computeCRC8(uint8_t data[], uint8_t len)
|
||||
{
|
||||
uint8_t crc = 0xFF; //Init with 0xFF
|
||||
|
||||
for (uint8_t x = 0; x < len; x++)
|
||||
{
|
||||
crc ^= data[x]; // XOR-in the next input byte
|
||||
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
if ((crc & 0x80) != 0)
|
||||
crc = (uint8_t)((crc << 1) ^ 0x31);
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return crc; //No output reflection
|
||||
}
|
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
This is a library written for the SCD30
|
||||
SparkFun sells these at its website: www.sparkfun.com
|
||||
Do you like this library? Help support SparkFun. Buy a board!
|
||||
https://www.sparkfun.com/products/14751
|
||||
|
||||
Written by Nathan Seidle @ SparkFun Electronics, May 22nd, 2018
|
||||
|
||||
Updated February 1st 2021 to include some of the features of paulvha's version of the library
|
||||
(while maintaining backward-compatibility):
|
||||
https://github.com/paulvha/scd30
|
||||
Thank you Paul!
|
||||
|
||||
The SCD30 measures CO2 with accuracy of +/- 30ppm.
|
||||
|
||||
This library handles the initialization of the SCD30 and outputs
|
||||
CO2 levels, relative humidty, and temperature.
|
||||
|
||||
https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library
|
||||
|
||||
Development environment specifics:
|
||||
Arduino IDE 1.8.13
|
||||
|
||||
SparkFun code, firmware, and software is released under the MIT License.
|
||||
Please see LICENSE.md for more details.
|
||||
*/
|
||||
|
||||
#ifndef __SparkFun_SCD30_ARDUINO_LIBARARY_H__
|
||||
#define __SparkFun_SCD30_ARDUINO_LIBARARY_H__
|
||||
|
||||
// Uncomment the next #define if using an Teensy >= 3 or Teensy LC and want to use the dedicated I2C-Library for it
|
||||
// Then you also have to include <i2c_t3.h> on your application instead of <Wire.h>
|
||||
|
||||
// #define USE_TEENSY3_I2C_LIB
|
||||
|
||||
#include "Arduino.h"
|
||||
#ifdef USE_TEENSY3_I2C_LIB
|
||||
#include <i2c_t3.h>
|
||||
#else
|
||||
#include <Wire.h>
|
||||
#endif
|
||||
|
||||
//The default I2C address for the SCD30 is 0x61.
|
||||
#define SCD30_ADDRESS 0x61
|
||||
|
||||
//Available commands
|
||||
|
||||
#define COMMAND_CONTINUOUS_MEASUREMENT 0x0010
|
||||
#define COMMAND_SET_MEASUREMENT_INTERVAL 0x4600
|
||||
#define COMMAND_GET_DATA_READY 0x0202
|
||||
#define COMMAND_READ_MEASUREMENT 0x0300
|
||||
#define COMMAND_AUTOMATIC_SELF_CALIBRATION 0x5306
|
||||
#define COMMAND_SET_FORCED_RECALIBRATION_FACTOR 0x5204
|
||||
#define COMMAND_SET_TEMPERATURE_OFFSET 0x5403
|
||||
#define COMMAND_SET_ALTITUDE_COMPENSATION 0x5102
|
||||
#define COMMAND_RESET 0xD304 // Soft reset
|
||||
#define COMMAND_STOP_MEAS 0x0104
|
||||
#define COMMAND_READ_FW_VER 0xD100
|
||||
|
||||
typedef union {
|
||||
byte array[4];
|
||||
float value;
|
||||
} ByteToFl; // paulvha
|
||||
|
||||
class SCD30
|
||||
{
|
||||
public:
|
||||
SCD30(void);
|
||||
|
||||
bool begin(bool autoCalibrate) { return begin(Wire, autoCalibrate); }
|
||||
#ifdef USE_TEENSY3_I2C_LIB
|
||||
bool begin(i2c_t3 &wirePort = Wire, bool autoCalibrate=true, bool measBegin=true); //By default use Wire port
|
||||
#else
|
||||
bool begin(TwoWire &wirePort = Wire, bool autoCalibrate=true, bool measBegin=true); //By default use Wire port
|
||||
#endif
|
||||
|
||||
void enableDebugging(Stream &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used.
|
||||
|
||||
bool beginMeasuring(uint16_t pressureOffset);
|
||||
bool beginMeasuring(void);
|
||||
bool StopMeasurement(void); // paulvha
|
||||
|
||||
// based on paulvha
|
||||
bool getSettingValue(uint16_t registerAddress, uint16_t *val);
|
||||
bool getForcedRecalibration(uint16_t *val) {return(getSettingValue(COMMAND_SET_FORCED_RECALIBRATION_FACTOR, val));}
|
||||
bool getMeasurementInterval(uint16_t *val) {return(getSettingValue(COMMAND_SET_MEASUREMENT_INTERVAL, val));}
|
||||
bool getTemperatureOffset(uint16_t *val) {return(getSettingValue(COMMAND_SET_TEMPERATURE_OFFSET, val));}
|
||||
bool getAltitudeCompensation(uint16_t *val) {return(getSettingValue(COMMAND_SET_ALTITUDE_COMPENSATION, val));}
|
||||
bool getFirmwareVersion(uint16_t *val) {return(getSettingValue(COMMAND_READ_FW_VER, val));}
|
||||
|
||||
uint16_t getCO2(void);
|
||||
float getHumidity(void);
|
||||
float getTemperature(void);
|
||||
float getTemperatureOffset(void);
|
||||
uint16_t getAltitudeCompensation(void);
|
||||
|
||||
bool setMeasurementInterval(uint16_t interval);
|
||||
bool setAmbientPressure(uint16_t pressure_mbar);
|
||||
bool setAltitudeCompensation(uint16_t altitude);
|
||||
bool setAutoSelfCalibration(bool enable);
|
||||
bool setForcedRecalibrationFactor(uint16_t concentration);
|
||||
bool setTemperatureOffset(float tempOffset);
|
||||
bool getAutoSelfCalibration(void);
|
||||
|
||||
bool dataAvailable();
|
||||
bool readMeasurement();
|
||||
|
||||
void reset();
|
||||
|
||||
bool sendCommand(uint16_t command, uint16_t arguments);
|
||||
bool sendCommand(uint16_t command);
|
||||
|
||||
uint16_t readRegister(uint16_t registerAddress);
|
||||
|
||||
uint8_t computeCRC8(uint8_t data[], uint8_t len);
|
||||
|
||||
private:
|
||||
|
||||
//Variables
|
||||
#ifdef USE_TEENSY3_I2C_LIB
|
||||
i2c_t3 *_i2cPort; //The generic connection to user's chosen I2C hardware
|
||||
#else
|
||||
TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware
|
||||
#endif
|
||||
//Global main datums
|
||||
float co2 = 0;
|
||||
float temperature = 0;
|
||||
float humidity = 0;
|
||||
|
||||
//These track the staleness of the current data
|
||||
//This allows us to avoid calling readMeasurement() every time individual datums are requested
|
||||
bool co2HasBeenReported = true;
|
||||
bool humidityHasBeenReported = true;
|
||||
bool temperatureHasBeenReported = true;
|
||||
|
||||
//Debug
|
||||
Stream *_debugPort; //The stream to send debug messages to if enabled. Usually Serial.
|
||||
boolean _printDebug = false; //Flag to print debugging variables
|
||||
|
||||
};
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue