#define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskScheduler.h>
#include <Adafruit_NeoPixel.h>
//============
// Task luci
Scheduler runner;
#define tsk(x) void x##_fun(); Task x##_t(TASK_INTERVAL,0, &x##_fun); int x##_now = 0; int x##_step = 0;
tsk(lb) // lb: luce bianca mattina
/*
tsk(lg) // lg: luce gialla giorno
tsk(lr) // lr: luce rossa sera
tsk(uv) // uv: led UV notte
tsk(tuoni) // tuoni: luci tuoni nelle nuvole
tsk(lc) // lc: luci case e strutture
tsk(wt) // wt: controllo cascata e acqua
tsk(fire) // fire: pwm fuoco
*/
//============
// Regia
#define LB_PIN 10
#define LG_PIN 11
#define TASK_INTERVAL 100
#define DAY_ITERATIONS 1000
int day_count = 0;
#define DEBUG_OUT 1
#define dbg(x) if(DEBUG_OUT)Serial.print(x);
#define dbgln(x) if(DEBUG_OUT)Serial.println(x);
struct regia_task {
int inizio;
int fine;
int valinizio;
int valfine;
int durata;
int pausa;
int step;
int now;
int pin;
Task *task;
};
regia_task lb_regia = {0,100, 0,255, 30,30,0, 0, LB_PIN, &lb_t};
regia_task *lista[] = {
&lb_regia,
};
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN 7
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 2
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
//===============
// Callback task
#define analog_def(x) analog_function(x##_t.getIterations(), &x##_regia)
void analog_function(int iter, regia_task* r){
iter = (r->durata*2 + r->pausa) - iter - 1;
if(iter == r->inizio){
r->step = (r->valfine - r->valinizio) / (r->durata) + 1;
dbgln("Prima iterazione");
dbgln(r->step);
}
if(iter == r->inizio + r->durata + r->pausa){
r->step = (r->valinizio - r->valfine) / (r->durata) - 1;
dbgln("Cambio");
dbgln(r->step);
}
r->now = r->now + r->step;
r->now = r->now > r->valfine ? r->valfine : r->now;
r->now = r->now < r->valinizio ? r->valinizio : r->now;
if(r->now == r->valfine || r->now == r->valinizio){
r->step = 0;
dbgln("Pausa");
}
analogWrite(r->pin, r->now);
dbg("Valore impostato: ");
dbgln(r->now);
}
void lb_fun(){
analog_def(lb);
}
//====================
// Definizioni tempi
#define MINCYC 20 //ms scansione di tempo minimo
#define PER_T 10000 //ms cicli maggiori
//====================
// Sintesi strutture:
/*
LB e LR potrebbero essere implementati dagli stessi neopixel, sono sempre alternativi.
TUONI potrebbero essere implementati tramite on/off randomici molto veloci.
UV, LG e LC controllati via pwm.
WT: è probabilmente un controllo on/off di una o più strutture.
*/
//====================
// Funzioni gestione
/*
// CODICE ESEMPIO
void t1Callback() {
Serial.print("t1: ");
Serial.println(millis());
if (t1.isFirstIteration()) {
runner.addTask(t3);
t3.enable();
Serial.println("t1: enabled t3 and added to the chain");
}
if (t1.isLastIteration()) {
t3.disable();
runner.deleteTask(t3);
t2.setInterval(500);
Serial.println("t1: disable t3 and delete it from the chain. t2 interval set to 500");
}
}
void t2Callback() {
Serial.print("t2: ");
Serial.println(millis());
}
void t3Callback() {
Serial.print("t3: ");
Serial.println(millis());
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
uint32_t t_wait = 0;
void rainbow(int wait) {
if(t_wait < millis()){
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
// Offset pixel hue by an amount to make one full revolution of the
// color wheel (range of 65536) along the length of the strip
// (strip.numPixels() steps):
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
// optionally add saturation and value (brightness) (each 0 to 255).
// Here we're using just the single-argument hue variant. The result
// is passed through strip.gamma32() to provide 'truer' colors
// before assigning to each pixel:
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show(); // Update strip with new contents
//delay(wait); // Pause for a moment
}
t_wait = millis() + wait;
}
}
*/
//=====================
// Funzioni principali
#define taskset(x, regia) x##_t.setIterations(regia.durata*2 + regia.pausa)
void setup () {
Serial.begin(115200);
Serial.println("Presepe TEST");
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
runner.startNow(); // set point-in-time for scheduling start
runner.addTask(lb_t);
taskset(lb, lb_regia);
//lb_t.enable();
}
void loop () {
/*
Valutare se far partire i task solo per il periodo di funzionamento o
lasciarli sempre attivi che fanno qualcosa solo quando impostato in
regia.
*/
for(int k = 0; k < sizeof(lista)/sizeof(regia_task); k++){
if(day_count == lista[k]->inizio)lista[k]->task->enable();
}
runner.execute();
day_count++;
if(day_count == DAY_ITERATIONS) day_count = 0;
}