X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=libraries%2Fcommon%2Fcommon.cpp;h=1d9153bb0547037ad3d955b723fa5c8befff9a46;hb=2d20ec97a60179f5dc3073c77cead5706b711c56;hp=01f07cfffe1cfac49bde493fc59b02cb3d3100b1;hpb=0c04d0b910d1566ae3fa4c20f8cb6e2c1c0cbb79;p=sketchbook_andrea diff --git a/libraries/common/common.cpp b/libraries/common/common.cpp index 01f07cf..1d9153b 100644 --- a/libraries/common/common.cpp +++ b/libraries/common/common.cpp @@ -15,78 +15,78 @@ // RGB LED // Common anode / cat -RGBLed::RGBLed(byte pinR, byte pinG, byte pinB) { +RGBLed::RGBLed(byte pinR, byte pinG, byte pinB) { // Per un common catodo, valore max / min invertiti - redPin = pinR ; - greenPin = pinG ; - bluePin = pinB ; - common = 0 ; - - // Equvalente del Setup() per inizializzare i PIN - pinMode(redPin, OUTPUT); - pinMode(greenPin, OUTPUT); - pinMode(greenPin, OUTPUT); + redPin = pinR ; + greenPin = pinG ; + bluePin = pinB ; + common = 0 ; + + // Equvalente del Setup() per inizializzare i PIN + pinMode(redPin, OUTPUT); + pinMode(greenPin, OUTPUT); + pinMode(greenPin, OUTPUT); }; RGBLed::RGBLed(byte pinR, byte pinG, byte pinB, byte com) { // Per un common anode, valore max / min normali - redPin = pinR ; - greenPin = pinG ; - bluePin = pinB ; - common = com ; - - // Equvalente del Setup() per inizializzare i PIN - pinMode(redPin, OUTPUT); - pinMode(greenPin, OUTPUT); - pinMode(greenPin, OUTPUT); + redPin = pinR ; + greenPin = pinG ; + bluePin = pinB ; + common = com ; + + // Equvalente del Setup() per inizializzare i PIN + pinMode(redPin, OUTPUT); + pinMode(greenPin, OUTPUT); + pinMode(greenPin, OUTPUT); }; void RGBLed::SetColor (byte r, byte g, byte b) { // Imposta il colore di un LED RGB - analogWrite(redPin, common - r); - analogWrite(greenPin, common - g); - analogWrite(bluePin, common - b); - }; + analogWrite(redPin, common - r); + analogWrite(greenPin, common - g); + analogWrite(bluePin, common - b); +}; void RGBLed::Red () { // Accende il LED di rosso - SetColor(0,255,255); - }; + SetColor(0,255,255); +}; void RGBLed::Green () { // Accende il LED di verde - SetColor(255,0,255); - }; + SetColor(255,0,255); +}; void RGBLed::Blue () { // Accende il LED di blu - SetColor(255,255,0); - }; + SetColor(255,255,0); +}; void RGBLed::Magenta () { // Accende il LED di magenta - SetColor(0,255,0); - }; + SetColor(0,255,0); +}; void RGBLed::Cyano () { // Accende il LED di Cyano - SetColor(255,0,0); - }; + SetColor(255,0,0); +}; void RGBLed::Yellow () { // Accende il LED di giallo - SetColor(0,0,255); - }; + SetColor(0,0,255); +}; void RGBLed::White () { -// Accende il LED - SetColor(0,0,0); - }; +// Accende il LED + SetColor(0,0,0); +}; void RGBLed::Off () { -// Spegne il LED - SetColor(255,255,255); - }; +// Spegne il LED + SetColor(255,255,255); +}; @@ -112,42 +112,41 @@ void Lampeggiatore::Blink() { // Illumina il led a 500ms if(millis() + shift - previousMillis > interval) { - // save the last time you blinked the LED - previousMillis = millis(); // if the LED is off turn it on and vice-versa: ledState = !ledState ; // Inverti il LED + // set the LED with the ledState of the variable: + digitalWrite(ledPin, ledState); + // save the last time you blinked the LED + previousMillis += interval; } - // set the LED with the ledState of the variable: - digitalWrite(ledPin, ledState); }; void Lampeggiatore::Blink(long time, long drift ) { // Illumina il led secondo un intervallo passato come argomento - shift = drift; + shift = drift; if(millis() + shift - previousMillis > time) { - // save the last time you blinked the LED - previousMillis = millis(); - // if the LED is off turn it on and vice-versa: ledState = !ledState ; // Inverti il LED - } // set the LED with the ledState of the variable: digitalWrite(ledPin, ledState); + // save the last time you blinked the LED + previousMillis += time; + } }; void Lampeggiatore::Blink(long up, long down, long drift ) { // Illumina il ledB precisando ontime e downtime - shift = drift; + shift = drift; if((ledState == HIGH)&& (millis() + shift - previousMillis > up)) { - // save the last time you blinked the LED - previousMillis = millis(); + // save the last time you blinked the LED + previousMillis += up; ledState = LOW ; } else if((ledState == LOW)&& (millis() + shift - previousMillis > down)) { - previousMillis = millis(); + previousMillis += down; ledState = HIGH ; } @@ -170,16 +169,16 @@ void Lampeggiatore::Low() { void Lampeggiatore::Swap() { // Inverte lo stato del LED - digitalWrite(ledPin, !digitalRead(ledPin)); + digitalWrite(ledPin, !digitalRead(ledPin)); } ///////////////////////////////////// // Pwm // Constructor Pwm::Pwm(int pin) - // Gestione del PWM utilizzando millis - // per non bloccare il processore con delay - // Warning: serialWrite puo' interferire con i tempi. +// Gestione del PWM utilizzando millis +// per non bloccare il processore con delay +// Warning: serialWrite puo' interferire con i tempi. { ledPin = pin; pinMode(ledPin, OUTPUT); @@ -195,8 +194,8 @@ void Pwm::Up(long speed, long drift) { if (millis() != previousMillis) { // si potrebbe togliere shift = drift; - brightness = 255.0 /(float)speed * (millis() + shift); - analogWrite(ledPin, brightness); + brightness = 255.0 /(float)speed * (millis() + shift); + analogWrite(ledPin, brightness); previousMillis = millis(); }; @@ -209,8 +208,8 @@ void Pwm::lUp(long speed, long drift) { if (millis() != previousMillis) { // si potrebbe togliere shift = drift; - brightness = 255.0 /(float)speed * (millis() + shift); - analogWrite(ledPin, lum(brightness)); + brightness = 255.0 /(float)speed * (millis() + shift); + analogWrite(ledPin, lum(brightness)); previousMillis = millis(); }; @@ -222,8 +221,8 @@ void Pwm::Down(long speed, long drift) { if (millis() != previousMillis) { shift = drift; - brightness = 255 - 255.0 /(float)speed * (millis() + shift) ; - analogWrite(ledPin, brightness); + brightness = 255 - 255.0 /(float)speed * (millis() + shift) ; + analogWrite(ledPin, brightness); previousMillis = millis(); }; @@ -235,8 +234,8 @@ void Pwm::lDown(long speed, long drift) { if (millis() != previousMillis) { shift = drift; - brightness = 255 - 255.0 /(float)speed * (millis() + shift) ; - analogWrite(ledPin, lum(brightness)); + brightness = 255 - 255.0 /(float)speed * (millis() + shift) ; + analogWrite(ledPin, lum(brightness)); previousMillis = millis(); }; @@ -244,20 +243,20 @@ void Pwm::lDown(long speed, long drift) { void Pwm::UD(long speed, long drift ) { // Aumenta e riduce in sequenza la luminosita' usanndo millis() - shift = drift; + shift = drift; brightness = 128 + 127 * cos(2 * PI / speed * (millis() + shift)); - analogWrite(ledPin, brightness); + analogWrite(ledPin, brightness); } void Pwm::Set(byte brightness) { - // Imposta il valore del PWM - analogWrite(ledPin, brightness); + // Imposta il valore del PWM + analogWrite(ledPin, brightness); } void Pwm::lSet(byte brightness) { - // Imposta il valore del PWM - analogWrite(ledPin, lum(brightness)); + // Imposta il valore del PWM con correzione luminosita' LED + analogWrite(ledPin, lum(brightness)); } @@ -338,20 +337,61 @@ void Sequenza::UD(long value) { void brilla(byte pin, int velocita ) { // Defalt value di velocita' solo nell'Header - // Accende e spegne il LED accetando un argomento - // per impostare la velocita'. - -pinMode(pin, OUTPUT); - // sequenze di istruzione: accendere e spegnere il LED - digitalWrite(pin, HIGH); // turn the LED on (HIGH is the voltage level) - delay(velocita); // wait for a second - digitalWrite(pin, LOW); // turn the LED off by making the voltage LOW - delay(velocita); // wait for a second + // Accende e spegne il LED accetando un argomento + // per impostare la velocita'. + + pinMode(pin, OUTPUT); + // sequenze di istruzione: accendere e spegnere il LED + digitalWrite(pin, HIGH); // turn the LED on (HIGH is the voltage level) + delay(velocita); // wait for a second + digitalWrite(pin, LOW); // turn the LED off by making the voltage LOW + delay(velocita); // wait for a second }; -byte lum(byte val) { +byte lum(byte val) { // Mappatura dell'intervallo 0-255 con correzione di luminosita. // storata in SRAM -return pgm_read_byte_near(BCORRECT + val); + return pgm_read_byte_near(BCORRECT + val); }; + + +int calibraTrim(int pin, byte ledPin) { + /* START Calibrazione TRIM canale: + Lettura di 10 smaple + calcolo del valore medio esclusi gli 0 + + I canali come alettoni / elevatore possono avere un TRIM + (generalmente il throttle non ha un TRIM impostato), + questa funzione nel setup serve per trovare il punto medio + all'avvio dello sketch. + */ + byte a = 0; + int ail = 0; + int ailIn = 0; + Serial.println(">> Calibrazione: "); + while (a < 10) { + if (millis() > 10000) { + Serial.print(">> Calibrazione annullata: segnale assente."); + ail = 1500; + break; + }; + ailIn = pulseIn(pin, HIGH, 25000); + if (ailIn != 0 ) { + ail = ail + ailIn ; + a++ ; + Serial.print(a); + Serial.print(": "); + Serial.println(ail); + digitalWrite(ledPin, !digitalRead(ledPin)); + delay(10); + } + } + Serial.println(">> Fine Calibrazione: "); + Serial.print(ail / 10); + Serial.println("--"); + Serial.flush() ; + return(ail / 10) ; +// END calibrazione +} +