Hello all!
I am trying to write my own library (my first!) for using WS2812 LEDs with a IR remote.
I followed the Arduino tutorial on how to write an Arduino library, but their example is so simple!
I would like to utilize two other Arduino libraries, inside my own. Is this possible? I tried to do that, but I totally fudged it and it, of course, doesn't compile.
Any ideas?
Thanks in advance!
WS2812IR.h:
/*
WS2812IR.h - Library for using WS2812 with IR.
Created by K Gray, December 18, 2022.
Released into the public domain.
*/
#ifndef WS2812IR_h
#define WS2812IR_h
#include "Arduino.h"
class WS2812IR
{
public:
WS2812IR(int NUMPIXELS, int ledpin, int irpin);
void begin();
void strip(int R, int G, int B, int speed);
void striphue(int hue, int speed);
void rainbow(int loopnumber, int speed);
void lightning(int ledoffdelay, int ledondelay);
void chase(int R, int G, int B, int speed);
void pulse(int R, int G, int B, int speed);
void mess(int speed);
private:
int _ledpin;
int _irpin;
int _NUMPIXELS;
int results;
//IRrecv irrecv(int irpin);
//Adafruit_NeoPixel pixels(NUMPIXELS, ledpin, NEO_GRB + NEO_KHZ800);
};
#endif
WS2812IR.cpp:
/*
WS2812IR.cpp - Library for using WS2812 with IR.
Created by K Gray, December 18, 2022.
Released into the public domain.
*/
#include "Arduino.h"
#include "WS2812IR.h"
#include <IRremote.h>
#include <Adafruit_NeoPixel.h>
WS2812IR::WS2812IR(int NUMPIXELS, int ledpin, int irpin)
{
IRrecv irrecv(irpin);
decode_results results;
Adafruit_NeoPixel pixels(NUMPIXELS, ledpin, NEO_GRB + NEO_KHZ800);
_NUMPIXELS = NUMPIXELS;
}
void WS2812IR::begin()
{
irrecv.enableIRIn();
irrecv.blink13(true);
pixels.begin();
WS2812IR::strip(0,0,0,0);
}
void WS2812IR::strip(int R, int G, int B, int speed)
{
for(int i=0; i<_NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(R, G, B));
pixels.show();
delay(speed);
}
}
void WS2812IR::striphue(int hue, int speed)
{
for(int i=0; i<_NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(hue)));
pixels.show();
delay(speed);
}
}
void WS2812IR::rainbow(int loopnumber,int speed)
{
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
for(long firstPixelHue = 0; firstPixelHue < loopnumber*65536; firstPixelHue += 256) {
for(int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
// Offset pixel hue by an amount to make one full revolution of the
// color wheel (range of 65536) along the length of the strip
// (strip.numPixels() steps):
int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
// optionally add saturation and value (brightness) (each 0 to 255).
// Here we're using just the single-argument hue variant. The result
// is passed through strip.gamma32() to provide 'truer' colors
// before assigning to each pixel:
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
if (irrecv.decode(&results)){return;} //IR interrupt
}
pixels.show(); // Update strip with new contents
delay(speed); // Pause for a moment
}
}
void WS2812IR::lightning(int ledoffdelay,int ledondelay){//recommended lower delays for a longer strip
while (!irrecv.decode(&results)){ //IR interrupt
int pixelN = random(1,_NUMPIXELS);
pixels.setPixelColor(pixelN, pixels.Color(255, 255, 255)); //white lightning
pixels.show();
delay(random(4,ledondelay));
pixels.setPixelColor(pixelN, pixels.Color(0, 0, 0));
pixels.show();
delay(random(10,ledoffdelay));
}
}
void WS2812IR::chase(int R,int G,int B,int speed){
while (!irrecv.decode(&results)){//IR interrupt
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
pixels.clear(); // Set all pixels in RAM to 0 (off)
// 'c' counts up from 'b' to end of strip in steps of 3...
for(int c=b; c<_NUMPIXELS; c += 3) {
pixels.setPixelColor(c, pixels.Color(R,G,B)); // Set pixel 'c' to value 'color'
}
pixels.show(); // Update strip with new contents
delay(speed); // Pause for a moment
}
}
}
void WS2812IR::pulse(int R,int G,int B,int speed){
int pixel=0;
while (!irrecv.decode(&results)){//IR interrupt
for (int p=1;p<5;p++){
pixel++;
if (pixel > _NUMPIXELS){
pixel = 0;
}
int m = map(p,2,5,1,135);
pixels.setPixelColor(pixel, pixels.Color(0,m,0));//m
pixels.show();
}
pixel++;
pixels.setPixelColor(pixel, pixels.Color(0,255,0));
for (int p=1;p<5;p++){
pixel++;
if (pixel > _NUMPIXELS){
pixel = 0;
}
int m = map(p,2,5,135,1);
pixels.setPixelColor(pixel, pixels.Color(0,m-20,0));
pixels.show();
}
for (int p=0;p<10;p++){
pixel++;
if (pixel > _NUMPIXELS){
pixel = 0;
}
pixels.setPixelColor(pixel, pixels.Color(0,0,0));
pixels.show();
}
pixel++;
if (pixel > _NUMPIXELS){
pixel = 0;
}
delay(speed);
}
}
void WS2812IR::mess(int speed){
while (!irrecv.decode(&results)){//IR interrupt
int R = random(1,255);
int G = random(1,255);
int B = random(1,255);
int p = random(1,_NUMPIXELS);
int t = random(1,speed);
pixels.setPixelColor(p, pixels.Color(R,G,B));
pixels.show();
delay(t);
}
}