]> git.piffa.net Git - sketchbook_andrea/blobdiff - libraries/common/common.cpp
rtos
[sketchbook_andrea] / libraries / common / common.cpp
index 5ddcaeece0f5b9df9d6910fc6542843f47afbc37..d9c0233b0027a3b30844d26137b56d1b4767cfb4 100644 (file)
@@ -71,6 +71,120 @@ void RGBLed::Off () {
 
 
 
+/////////////////////////////////////
+// Lampeggiatore
+// Constructor
+Lampeggiatore::Lampeggiatore(int pin)
+{
+    ledPin = pin;
+    pinMode(ledPin, OUTPUT);
+    ledState = LOW;
+    previousMillis = 0;
+    interval = 500;
+};
+
+
+
+
+// Una funzione facente parte di una classe prende il nome di "metodo" della stessa:
+void Lampeggiatore::Invert() {
+    // Inverte il lampeggio
+    ledState = HIGH ;
+}
+
+void Lampeggiatore::Blink() {
+    // Illumina il led a 500ms
+
+    if(millis() - 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);
+};
+
+void Lampeggiatore::Blink(long time) {
+    // Illumina il led secondo un intervallo passato come argomento
+
+    if(millis() - 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);
+};
+
+void Lampeggiatore::Blink(long up, long down) {
+    // Illumina il ledB precisando ontime e downtime
+
+    if((ledState == HIGH)&& (millis() - previousMillis > up)) {
+    // save the last time you blinked the LED
+       previousMillis = millis();
+        ledState = LOW  ;
+    }
+    else if((ledState == LOW)&& (millis() - previousMillis > down)) {
+       previousMillis = millis();
+        ledState = HIGH  ;
+    }
+
+    // set the LED with the ledState of the variable:
+    digitalWrite(ledPin, ledState);
+};
+
+/////////////////////////////////////
+// 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.
+{
+    ledPin = pin;
+    pinMode(ledPin, OUTPUT);
+    previousMillis = 0;
+    byte brightness = 0 ;
+    increment = 1;
+};
+
+void Pwm::Up(long speed) {
+    // Aumenta progressivamente la luminosita' usanndo millis()
+    // quindi senza bloccare il processore
+    // Viene usato un float, in alternativa un coseno
+
+    if (millis() != previousMillis)  { // si potrebbe togliere
+            brightness = 255.0 /(float)speed * millis() ;
+            analogWrite(ledPin, brightness);
+
+        previousMillis = millis();
+    };
+}
+
+void Pwm::Down(long speed ) {
+    // Riduce progressivamente la luminosita' usanndo millis()
+    // quindi senza bloccare il processore
+
+    if (millis() != previousMillis)  {
+            brightness = 255 - 255.0 /(float)speed * millis() ;
+            analogWrite(ledPin, brightness);
+
+        previousMillis = millis();
+    };
+}
+
+void Pwm::UD(long speed ) {
+    // Aumenta e riduce in sequenza la luminosita' usanndo millis()
+    brightness = 128 + 127 * cos(2 * PI / speed * millis());
+    analogWrite(ledPin, brightness);  
+}
+
+
+
 //////////////////
 // Funzioni
 
@@ -86,3 +200,4 @@ pinMode(pin, OUTPUT);
   digitalWrite(pin, LOW);    // turn the LED off by making the voltage LOW
   delay(velocita);               // wait for a second
 };
+