added Pixel::setColorMap()

This commit is contained in:
Gregg 2024-02-24 18:15:52 -06:00
parent d10f73e3c7
commit 218e1f6611
3 changed files with 50 additions and 43 deletions

View File

@ -42,11 +42,12 @@ Pixel::Pixel(int pin, boolean isRGBW){
return; return;
if(isRGBW) if(isRGBW)
this->lastBit=0; this->bytesPerPixel=4;
else else
this->lastBit=8; this->bytesPerPixel=3;
setTiming(0.32, 0.88, 0.64, 0.56, 80.0); // set default timing parameters (suitable for most SK68 and WS28 RGB pixels) setTiming(0.32, 0.88, 0.64, 0.56, 80.0); // set default timing parameters (suitable for most SK68 and WS28 RGB pixels)
setColorMap(ColorMap::GRB); // set default color mapping
rmt_isr_register(loadData,NULL,0,NULL); // set custom interrupt handler rmt_isr_register(loadData,NULL,0,NULL); // set custom interrupt handler
@ -74,6 +75,13 @@ void Pixel::setTiming(float high0, float low0, float high1, float low1, uint32_t
/////////////////// ///////////////////
void Pixel::setColorMap(const uint8_t *map){
this->map=map;
}
///////////////////
void Pixel::set(Color *c, int nPixels, boolean multiColor){ void Pixel::set(Color *c, int nPixels, boolean multiColor){
if(!*rf || nPixels==0) if(!*rf || nPixels==0)
@ -82,10 +90,10 @@ void Pixel::set(Color *c, int nPixels, boolean multiColor){
status.nPixels=nPixels; status.nPixels=nPixels;
status.color=c; status.color=c;
status.iMem=0; status.iMem=0;
status.iBit=32;
status.started=true; status.started=true;
status.px=this; status.px=this;
status.multiColor=multiColor; status.multiColor=multiColor;
status.iByte=0;
loadData(this); // load first two bytes of data to get started loadData(this); // load first two bytes of data to get started
loadData(this); loadData(this);
@ -113,11 +121,14 @@ void IRAM_ATTR Pixel::loadData(void *arg){
return; return;
} }
for(int i=0;i<8;i++) int startBit=status.px->map[status.iByte];
RMTMEM.chan[status.px->rf->getChannel()].data32[status.iMem++].val=status.px->pattern[(status.color->val>>(--status.iBit))&1]; int endBit=startBit-8;
if(status.iBit==status.px->lastBit){ for(int iBit=startBit;iBit>endBit;iBit--)
status.iBit=32; RMTMEM.chan[status.px->rf->getChannel()].data32[status.iMem++].val=status.px->pattern[(status.color->val>>iBit)&1];
if(++status.iByte==status.px->bytesPerPixel){
status.iByte=0;
status.color+=status.multiColor; status.color+=status.multiColor;
status.nPixels--; status.nPixels--;
} }

View File

@ -37,6 +37,16 @@
[[maybe_unused]] static const char* PIXEL_TAG = "Pixel"; [[maybe_unused]] static const char* PIXEL_TAG = "Pixel";
namespace ColorMap {
static const uint8_t RGB[4]={32,24,16,8};
static const uint8_t RBG[4]={32,16,24,8};
static const uint8_t BRG[4]={24,16,32,8};
static const uint8_t BGR[4]={16,24,32,8};
static const uint8_t GBR[4]={16,32,24,8};
static const uint8_t GRB[4]={24,32,16,8};
};
//////////////////////////////////////////// ////////////////////////////////////////////
// Single-Wire RGB/RGBW NeoPixels // // Single-Wire RGB/RGBW NeoPixels //
//////////////////////////////////////////// ////////////////////////////////////////////
@ -49,8 +59,8 @@ class Pixel : public Blinkable {
struct { struct {
uint8_t white:8; uint8_t white:8;
uint8_t blue:8; uint8_t blue:8;
uint8_t red:8;
uint8_t green:8; uint8_t green:8;
uint8_t red:8;
}; };
uint32_t val; uint32_t val;
}; };
@ -121,11 +131,11 @@ class Pixel : public Blinkable {
struct pixel_status_t { struct pixel_status_t {
int nPixels; int nPixels;
Color *color; Color *color;
int iBit;
int iMem; int iMem;
boolean started; boolean started;
Pixel *px; Pixel *px;
boolean multiColor; boolean multiColor;
int iByte;
}; };
RFControl *rf; // Pixel utilizes RFControl RFControl *rf; // Pixel utilizes RFControl
@ -133,7 +143,8 @@ class Pixel : public Blinkable {
uint32_t resetTime; // minimum time (in usec) between pulse trains uint32_t resetTime; // minimum time (in usec) between pulse trains
uint32_t txEndMask; // mask for end-of-transmission interrupt uint32_t txEndMask; // mask for end-of-transmission interrupt
uint32_t txThrMask; // mask for threshold interrupt uint32_t txThrMask; // mask for threshold interrupt
uint32_t lastBit; // 0=RGBW; 8=RGB uint8_t bytesPerPixel; // RGBW=4; RGB=3
const uint8_t *map; // color map representing order in which color bytes are transmitted
Color onColor; // color used for on() command Color onColor; // color used for on() command
const int memSize=sizeof(RMTMEM.chan[0].data32)/4; // determine size (in pulses) of one channel const int memSize=sizeof(RMTMEM.chan[0].data32)/4; // determine size (in pulses) of one channel
@ -151,6 +162,7 @@ class Pixel : public Blinkable {
int getPin(){return(rf->getPin());} // returns pixel pin if valid, else returns -1 int getPin(){return(rf->getPin());} // returns pixel pin if valid, else returns -1
void setTiming(float high0, float low0, float high1, float low1, uint32_t lowReset); // changes default timings for bit pulse - note parameters are in MICROSECONDS void setTiming(float high0, float low0, float high1, float low1, uint32_t lowReset); // changes default timings for bit pulse - note parameters are in MICROSECONDS
void setColorMap(const uint8_t *map); // changes the default color map from GRBW to an alternative mapping - controls order in which color bytes are transmitted
operator bool(){ // override boolean operator to return true/false if creation succeeded/failed operator bool(){ // override boolean operator to return true/false if creation succeeded/failed
return(*rf); return(*rf);

View File

@ -25,48 +25,32 @@
* *
********************************************************************************/ ********************************************************************************/
// This is a placeholder .ino file that allows you to easily edit the contents of this files using the Arduino IDE, #include "Pixel.h"
// as well as compile and test from this point. This file is ignored when the library is included in other sketches.
#include "StepperControl.h" #define PIXEL_PIN 26 // set this to whatever pin you are using - note pin cannot be "input only"
StepperControl *bigMotor; Pixel testPixel(PIXEL_PIN);
StepperControl *smallMotor;
#define BIG_MOTOR_POSITION 1600
#define SMALL_MOTOR_POSITION 2064
///////////////////
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(1000); delay(1000);
Serial.printf("\nHomeSpan Stepper Control\n\n");
bigMotor=(new Stepper_TB6612(23,32,22,14,33,27))->setStepType(StepperControl::HALF_STEP)->setAccel(10,20); Serial.printf("Pixel Test on pin %d\n",PIXEL_PIN);
smallMotor=new Stepper_A3967(18,21,5,4,19);
// smallMotor->setStepType(StepperControl::EIGHTH_STEP);
// bigMotor->setStepType(StepperControl::HALF_STEP);
// bigMotor->setAccel(10,20);
} }
/////////////////// //////////////////////////////////////
void loop(){ void loop(){
Serial.print("Red\n");
testPixel.set(Pixel::RGB(100,0,0));
delay(2000);
if(smallMotor->position()==0) Serial.print("Green\n");
smallMotor->moveTo(SMALL_MOTOR_POSITION,2); testPixel.set(Pixel::RGB(0,100,0));
else if(smallMotor->position()==SMALL_MOTOR_POSITION) delay(2000);
smallMotor->moveTo(0,2);
if(bigMotor->position()==0) Serial.print("Blue\n");
bigMotor->moveTo(BIG_MOTOR_POSITION,4); testPixel.set(Pixel::RGB(0,0,100));
else if(bigMotor->position()==BIG_MOTOR_POSITION) delay(2000);
bigMotor->moveTo(0,4);
delay(1000);
Serial.printf("Small Motor: %d Big Motor %d\n",smallMotor->stepsRemaining(),bigMotor->stepsRemaining());
} }