I want to declare use a struct / type in my header file for a lib but I can´t find any information about it must look like.
*.file:
#ifndef WS2801_M_H
#define WS2801_M_H
#include <Arduino.h>
#define pixel 31
class WS2801_M{
public:
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
}color;
};
#endif
*.cpp file:
#include <Arduino.h>
#include <WS2801_M.h>
// +++++++++++++++++++++++++ clear all +++++++++++++++++++
bool WS2801_M::Off(color* buf) {
for (int i = 0; i < pixel; i++) {
buf[i].red = 0;
buf[i].green = 0;
buf[i].blue = 0;
}
return true;
}
*.ino file
#include <SPI.h>
#include <SystemTools.h>
#include <WS2801_M.h>
#define pixel 31
color buf[pixel] = {0};
unsigned long TimeStamp;
..
I have no idea how that syntax has to look.
Why typedef? Why is your method not in your class definition?
This worked for me:
// Sketch
#include "WS2801_M.h"
const unsigned PixelCount = 31;
WS2801Color buf[PixelCount];
WS2801_M strip;
void setup() {
// put your setup code here, to run once:
strip.Off(buf, PixelCount);
}
void loop() {
// put your main code here, to run repeatedly:
}
// WS2801_M.h
#ifndef WS2801_M_H
#define WS2801_M_H
#include <Arduino.h>
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
} WS2801Color;
class WS2801_M {
public:
bool Off(WS2801Color* buf, const unsigned count);
};
#endif
// WS2801_M.cpp
#include <Arduino.h>
#include "WS2801_M.h"
// +++++++++++++++++++++++++ clear all +++++++++++++++++++
bool WS2801_M::Off(WS2801Color* buf, const unsigned count) {
for (int i = 0; i < count; i++) {
buf[i].red = 0;
buf[i].green = 0;
buf[i].blue = 0;
}
return true;
}
johnwasser:
This worked for me:
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
} WS2801Color;
As @aarg mentioned... this is C++
structs, enums and unions behave like they are typedef'ed by default.
OK. Change it to:
struct WS2801Color {
uint8_t red;
uint8_t green;
uint8_t blue;
};