Hello. I am writing a wrapper for Neopixel library. I want to write my own functions for controlling LED to save code space and make it easier for me to understand. I have leds.c and leds.h . The functions from the leds.c will be used in many other source files such as my main.c and others as well.
My leds.c:
and my leds.h:
Notice that I have all the color definitions in my leds.c and in my leds.h I use extern for each colour, that way when I include "leds.h" in any other source file I can use these colours as they are included as extern.
I am curious whether there is a better way of defining colors? Perhaps there is a way to achieve the simmilar thing just without using the extern? I have read that using extern is usually not a good idea. For example, I can create an array of RgbColor objects in my main.c but I have not figured a way how can I make it accessible from other source files without using extern.
My leds.c:
Code:
#include "leds.h"
RgbColor nice_purple(153,153,255);
RgbColor orange(255,165,0);
RgbColor bright_purple(204,0,102);
RgbColor cyan(0, colorSaturation, colorSaturation);
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor yellow(colorSaturation,colorSaturation,0);
RgbColor white(colorSaturation);
RgbColor black(0);
RgbColor purple(colorSaturation, 0, colorSaturation);
NeoPixelBus<NeoGrbFeature, NeoEsp32Rmt0800KbpsMethod> strip(PixelCount, PixelPin);
void setup_leds(){
strip.Begin();
}
void toggle_led_strip(color_t colour,int number){
for(int i=0;i<=number;i++){
Serial.print("colour is =");
Serial.println(colour);
strip.SetPixelColor(0, colour);
strip.SetPixelColor(1, colour);
strip.SetPixelColor(2, colour);
strip.SetPixelColor(3, colour);
strip.SetPixelColor(4, colour);
strip.SetPixelColor(5, colour);
strip.SetPixelColor(6, colour);
strip.SetPixelColor(7, colour);
strip.Show();
delay(100);
strip.SetPixelColor(0, black);
strip.SetPixelColor(1, black);
strip.SetPixelColor(2, black);
strip.SetPixelColor(3, black);
strip.SetPixelColor(4, black);
strip.SetPixelColor(5, black);
strip.SetPixelColor(6, black);
strip.SetPixelColor(7, black);
strip.Show();
delay(100);
}
}
void toggle_led_strip_on(RgbColor colour){
strip.SetPixelColor(0, colour);
strip.SetPixelColor(1, colour);
strip.SetPixelColor(2, colour);
strip.SetPixelColor(3, colour);
strip.SetPixelColor(4, colour);
strip.SetPixelColor(5, colour);
strip.SetPixelColor(6, colour);
strip.SetPixelColor(7, colour);
strip.Show();
}
void toggle_led_strip_off(){
strip.SetPixelColor(0, black);
strip.SetPixelColor(1, black);
strip.SetPixelColor(2, black);
strip.SetPixelColor(3, black);
strip.SetPixelColor(4, black);
strip.SetPixelColor(5, black);
strip.SetPixelColor(6, black);
strip.SetPixelColor(7, black);
strip.Show();
}
void clear_LED_strip(int number){
for (int i=0;i<number;i++)
strip.SetPixelColor(i, black);
//delay(1);
strip.Show();
}
void LED_strip_ON(RgbColor colour,int number){
for (int i=0;i<number;i++)
strip.SetPixelColor(i, colour);
strip.Show();
}
and my leds.h:
Code:
#ifndef LEDS_H
#define LEDS_H
#include "NeoPixelBus.h"
#include <Adafruit_I2CDevice.h>
#define PixelCount 8 // this example assumes 4 pixels, making it smaller will cause a failure
#define PixelPin 27 // make sure to set this to the correct pin, ignored for Esp8266
#define colorSaturation 250
extern RgbColor nice_purple;
extern RgbColor orange;
extern RgbColor bright_purple;
extern RgbColor cyan;
extern RgbColor red;
extern RgbColor green;
extern RgbColor blue;
extern RgbColor yellow;
extern RgbColor white;
extern RgbColor black;
extern RgbColor purple;
void setup_leds();
void toggle_led_strip(color_t colour,int number);
void toggle_led_strip_on(RgbColor colour);
void toggle_led_strip_off();
void clear_LED_strip(int number);
void LED_strip_ON(RgbColor colour,int number);
#endif
Notice that I have all the color definitions in my leds.c and in my leds.h I use extern for each colour, that way when I include "leds.h" in any other source file I can use these colours as they are included as extern.
I am curious whether there is a better way of defining colors? Perhaps there is a way to achieve the simmilar thing just without using the extern? I have read that using extern is usually not a good idea. For example, I can create an array of RgbColor objects in my main.c but I have not figured a way how can I make it accessible from other source files without using extern.