Ich versuche eine Lib zu schreiben, deren Klasse mit mehreren Instanzen benötigt wird. Da die Instanzen keine statischen variablen besitzen, sollten die Werte unabhängig voneinander persistent sein. Leider ändern sich meine Werte nicht.
Diese Lib ist mein ertser Versuch. Libs in Arduino selbst zu basteln:
.*h File
Code:
#ifndef SystemTools_h
#define SystemTools_h
#include <Arduino.h>
class SystemTools{
private:
unsigned long _onOffCountB;
unsigned long _onOffCountB_Tmp;
unsigned long _ET;
bool _startTmp;
unsigned long _onOffCountP;
unsigned long _onOffCountP_Tmp;
public:
SystemTools(){;};
// ++++++++++++++++++ raising edge ++++++++++++++++++++++++++++
bool R_Trigg(bool start);
// +++++++++++++++++++++++++ Timer On Delay +++++++++++++++++++++++
bool TimerOnDelay (bool start, unsigned long PT,unsigned long TimeStamp);
// +++++++++++++++++++++++++ Blink +++++++++++++++++++++++
bool Blink (unsigned long PtHalf,unsigned long TimeStamp);
// +++++++++++++++++++++++++ Blink Pulse+++++++++++++++++++++++
bool BlinkPulse (unsigned long PtHalf,unsigned long TimeStamp);
};
#endif
.*cpp File
Code:
#include <Arduino.h>
#include <SystemTools.h>
// ++++++++++++++++++ Constructor ++++++++++++++++++++++++++++
//SystemTools::SystemTools();
// ++++++++++++++++++ raising edge ++++++++++++++++++++++++++++
bool SystemTools::R_Trigg(bool start)
{
if (!start){
_startTmp = false;
return false;
}
if (start &! _startTmp){
_startTmp = true;
return true;
}
else
{
return false;
}
}
// +++++++++++++++++++++++++ Timer On Delay +++++++++++++++++++++++
bool SystemTools::TimerOnDelay (bool start, unsigned long PT,unsigned long TimeStamp){
if (R_Trigg(start)){
_ET = TimeStamp;
}
if(!start) {return false;}
return ( (_ET+PT-5) < (TimeStamp) ) ? true : false;
// -number for cycle delay
}
// +++++++++++++++++++++++++ Blink +++++++++++++++++++++++
bool SystemTools::Blink (unsigned long PtHalf,unsigned long TimeStamp){
if (TimerOnDelay(_onOffCountB == _onOffCountB_Tmp, PtHalf/2, TimeStamp ) )
{
_onOffCountB++;
}
else{
_onOffCountB_Tmp = _onOffCountB;
}
if(_onOffCountB%2){
return true;
}
return false;
}
// +++++++++++++++++++++++++ Blink Pulse+++++++++++++++++++++++
bool SystemTools::BlinkPulse (unsigned long PtHalf,unsigned long TimeStamp){
if (TimerOnDelay(_onOffCountP == _onOffCountP_Tmp, PtHalf, TimeStamp ) )
{
_onOffCountP++;
Serial.println("RE:" + String(TimeStamp));
return true;
}
else{
_onOffCountP_Tmp = _onOffCountP;
//Serial.println("NOT:" + String(TimeStamp));
}
return false;
}
_________________________________________________________________
.*h File
Code:
#ifndef WS2801_M_h
#define WS2801_M_h
#include <Arduino.h>
#define pixel 31
class WS2801_M{
public:
struct color {
uint8_t red;
uint8_t green;
uint8_t blue;
};
WS2801_M(){;};
// ++++++++++++++++++ raising edge ++++++++++++++++++++++++++++
void Off(color* buf);
// ++++++++++++++++++++++ GLOW PIXEL +++++++++++++++++++++++++
void Glow(color* buf, bool res, bool trig);
private:
uint8_t _red;
uint8_t _green;
uint8_t _blue;
};
#endif
.*cpp File[/b
Code:
#include "Arduino.h"
#include "WS2801_M.h"
// +++++++++++++++++++++++++ clear all +++++++++++++++++++
void WS2801_M::Off(color* buf) {
color colTmp ={0,0,0};
for (int i = 0; i < pixel; i+=3) {
buf[i] = colTmp;
}
}
// ++++++++++++++++++++++ GLOW PIXEL +++++++++++++++++++++++++
void WS2801_M::Glow(color* buf, bool res, bool trig) {
if(res) {
_red = 0;
_green = 0;
_blue = 0;
}
if (trig)
{
if(_red < 254)
{_red++; Serial.println("INC RED , VAL: " +String(_red));}
else
{_red=0;}
for (int i=0; i<pixel; i++){
buf[i].red = _red; // red
buf[i].green = _green; // green
buf[i].blue = _blue; // blue
}
}
}
_________________________________________________________________
[b].*ino File[/b
Code:
#include <WS2801_M.h>
#include <SystemTools.h>
#include <SPI.h>
// class instances
WS2801_M ws1;
SystemTools systemTools1;
WS2801_M::color buf[pixel] = {0};
void setup()
{
SPI.begin(); // initialize SPI hardware
Serial.begin(9600);
}
//================================================================
//=========================== LOOP ===============================
//================================================================
void loop()
{
unsigned long TimeStamp = millis();
ws1.Glow(&buf[0], false, (systemTools1.BlinkPulse(20, TimeStamp))); //trigg
SPI.transfer( buf, (pixel + 1) * 3 );
}
[b]Interessant ist, dass die lib SystemTools korrekt funktioniert, dh dass es da keine Probleme mit dem inkrementieren der Variablenwerte gibt.
Natürlich freut mich neben Lösungsvorschlägen auch, wie man den Code weiter verbessern sollte.