Go Down

Topic: Extending FastLED Library for WS2812B Matrix (Read 309 times) previous topic - next topic

jul124

Hi,

I'm building a 11x21 LED matrix based on the WS2812B and would like to write a C++ class that can use the FastLED library to draw circles, lines, rectangles etc.

How can I either create a FastLED object within this new class that I am writing or pass the FastLED object from the main sketch to this new class to allow me to use it's functions? I know this is more of a C++ question but this was the best place I could think of to post this.

Below was my initial idea but this gives the error "'FastLED' is not a type" in the function prototype for sendData() (in the .h file).

Main Sketch:
Code: [Select]
#include <FastLED.h>
#include "LED_Matrix.h"

#define cols        21
#define rows        11
#define LED_PIN     7
#define NUM_LEDS    231

CRGB leds[NUM_LEDS];
LED_Matrix matrix = LED_Matrix(rows, cols, 127);

void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

  setPixel(3, 5, 127, 15, 50);
  matrix.sendData(FastLED, leds);
}

void loop() {
 
}


LED_Matrix.cpp:
Code: [Select]
#include "Arduino.h"
#include "LED_Matrix.h"
#include <FastLED.h>

LED_Matrix(uint8_t rows, uint8_t cols, uint8_t brightness){
  g_rows = rows;
  g_cols = cols;

  g_brightness = brightness;

  clearBuffer();
} // LED_Matrix

void clearBuffer(){
  for (uint8_t i = 0; i < g_rows; i++){                                         // rows
    for (uint8_t j = 0; j < g_cols; j++){                                       // cols
      for (uint8_t k = 0; k < 3; k++){                                          // colour
        g_buffer[i][j][k]=0;
      } // for colour
    } // for cols
  } // for rows
} // clearBuffer

void setPixel(uint8_t row, uint8_t col, uint8_t red_value, uint8_t green_value, uint8_t blue_value){
  g_buffer[row][col][0] = red_value;
  g_buffer[row][col][1] = green_value;
  g_buffer[row][col][2] = blue_value;
} // setPixel

void sendData(FastLED &LEDs, CRGB leds){

  uint16_t counter = 0;

  for (uint8_t i = 0; i < g_rows; i++){                                         // rows
    for (uint8_t j = 0; j < g_cols; j++){                                       // cols
      if (i % 2 == 0){
        line[counter] = matrix[i][j];
        leds[counter] = CRGB(g_buffer[i][j][0],g_buffer[i][j][1],g_buffer[i][j][2]);
      } // if
      else{
        line[counter] = matrix[i][cols - j - 1];
        leds[counter] = CRGB(g_buffer[i][g_colscols - j - 1][0],g_buffer[i][g_colscols - j - 1][1],g_buffer[i][g_colscols - j - 1][2]);
      } // else
      counter++
    } // for cols
  } // for rows
  leds.show();
} // sendData


LED_Matrix.h:
Code: [Select]
#include "Arduino.h"
#include <FastLED.h>

class LED_Matrix{
  private:

    uint8_t g_rows;
    uint8_t g_cols;


    uint8_t g_brightness;

    uint8_t g_buffer[11][21][3];

    void clearBuffer();

  public:

    LED_Matrix(uint8_t rows, uint8_t cols, uint8_t brightness);

    // set a pixel to a desired colour
    void setPixel(uint8_t row, uint8_t col, uint8_t red_value, uint8_t green_value, uint8_t blue_value);
    // send all data to the matrix, execute once per frame, takes FastLED object as input
    void sendData(FastLED LEDs, CRGB leds);

}

3Dgeo

#1
Jul 30, 2020, 11:25 am Last Edit: Jul 30, 2020, 11:27 am by 3Dgeo
Take a look:
#71 How to create an Arduino Library - easy!

You can also take a look in existing libraries to understand how they work.

noiasca

#2
Jul 30, 2020, 06:53 pm Last Edit: Jul 30, 2020, 06:54 pm by noiasca
How can I either create a FastLED object within this new class that I am writing or pass the FastLED object from the main sketch to this new class to allow me to use it's functions? I know this is more of a C++ question but this was the best place I could think of to post this.
if you want an example how it could be done, feel free to see the source code of my

Neopixel 7 Segment display library

I let the user declare the strip object (in my case based on Adafruit) and the user has to handover a reference to that strip object when he calls the constructor of my library.

my class inherits from print class. Therefore all the print commands are working like you are used to from Serial, LCD and lot of other libraries.

how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

jul124

Thanks to you both for your help, it turns out I am actually able to create the object within the constructor of my new class and then use it as normal, simple really :D

Go Up