From 218e1f661158fabf3ec23a4c0a86197a77c0f7ea Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 24 Feb 2024 18:15:52 -0600 Subject: [PATCH] added Pixel::setColorMap() --- src/src/extras/Pixel.cpp | 27 +++++++++++++++------- src/src/extras/Pixel.h | 18 ++++++++++++--- src/src/extras/extras.ino | 48 +++++++++++++-------------------------- 3 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/src/extras/Pixel.cpp b/src/src/extras/Pixel.cpp index 11f837e..8c423f6 100644 --- a/src/src/extras/Pixel.cpp +++ b/src/src/extras/Pixel.cpp @@ -42,11 +42,12 @@ Pixel::Pixel(int pin, boolean isRGBW){ return; if(isRGBW) - this->lastBit=0; + this->bytesPerPixel=4; 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) + setColorMap(ColorMap::GRB); // set default color mapping 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){ if(!*rf || nPixels==0) @@ -82,10 +90,10 @@ void Pixel::set(Color *c, int nPixels, boolean multiColor){ status.nPixels=nPixels; status.color=c; status.iMem=0; - status.iBit=32; status.started=true; status.px=this; status.multiColor=multiColor; + status.iByte=0; loadData(this); // load first two bytes of data to get started loadData(this); @@ -112,12 +120,15 @@ void IRAM_ATTR Pixel::loadData(void *arg){ RMTMEM.chan[status.px->rf->getChannel()].data32[status.iMem].val=0; return; } + + int startBit=status.px->map[status.iByte]; + int endBit=startBit-8; - for(int i=0;i<8;i++) - RMTMEM.chan[status.px->rf->getChannel()].data32[status.iMem++].val=status.px->pattern[(status.color->val>>(--status.iBit))&1]; - - if(status.iBit==status.px->lastBit){ - status.iBit=32; + for(int iBit=startBit;iBit>endBit;iBit--) + 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.nPixels--; } diff --git a/src/src/extras/Pixel.h b/src/src/extras/Pixel.h index 0a8d37e..cd00e06 100644 --- a/src/src/extras/Pixel.h +++ b/src/src/extras/Pixel.h @@ -37,6 +37,16 @@ [[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 // //////////////////////////////////////////// @@ -49,8 +59,8 @@ class Pixel : public Blinkable { struct { uint8_t white:8; uint8_t blue:8; - uint8_t red:8; uint8_t green:8; + uint8_t red:8; }; uint32_t val; }; @@ -121,11 +131,11 @@ class Pixel : public Blinkable { struct pixel_status_t { int nPixels; Color *color; - int iBit; int iMem; boolean started; Pixel *px; boolean multiColor; + int iByte; }; 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 txEndMask; // mask for end-of-transmission 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 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 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 return(*rf); diff --git a/src/src/extras/extras.ino b/src/src/extras/extras.ino index c86d0cc..a47cb35 100644 --- a/src/src/extras/extras.ino +++ b/src/src/extras/extras.ino @@ -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, -// as well as compile and test from this point. This file is ignored when the library is included in other sketches. +#include "Pixel.h" -#include "StepperControl.h" +#define PIXEL_PIN 26 // set this to whatever pin you are using - note pin cannot be "input only" -StepperControl *bigMotor; -StepperControl *smallMotor; - -#define BIG_MOTOR_POSITION 1600 -#define SMALL_MOTOR_POSITION 2064 - -/////////////////// +Pixel testPixel(PIXEL_PIN); void setup() { - + Serial.begin(115200); 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); - smallMotor=new Stepper_A3967(18,21,5,4,19); - -// smallMotor->setStepType(StepperControl::EIGHTH_STEP); - -// bigMotor->setStepType(StepperControl::HALF_STEP); -// bigMotor->setAccel(10,20); + Serial.printf("Pixel Test on pin %d\n",PIXEL_PIN); } -/////////////////// +////////////////////////////////////// void loop(){ + Serial.print("Red\n"); + testPixel.set(Pixel::RGB(100,0,0)); + delay(2000); - if(smallMotor->position()==0) - smallMotor->moveTo(SMALL_MOTOR_POSITION,2); - else if(smallMotor->position()==SMALL_MOTOR_POSITION) - smallMotor->moveTo(0,2); - - if(bigMotor->position()==0) - bigMotor->moveTo(BIG_MOTOR_POSITION,4); - else if(bigMotor->position()==BIG_MOTOR_POSITION) - bigMotor->moveTo(0,4); - - delay(1000); - Serial.printf("Small Motor: %d Big Motor %d\n",smallMotor->stepsRemaining(),bigMotor->stepsRemaining()); + Serial.print("Green\n"); + testPixel.set(Pixel::RGB(0,100,0)); + delay(2000); + + Serial.print("Blue\n"); + testPixel.set(Pixel::RGB(0,0,100)); + delay(2000); }