]> git.piffa.net Git - sketchbook_andrea/blob - libraries/common/common.cpp
3fd82db3cb2c0a1a418e2469e8fa2ffaa87c40fb
[sketchbook_andrea] / libraries / common / common.cpp
1 /*  Common
2  *
3  *  Oggetti di uso comune
4  */
5
6 #include "Arduino.h"
7 #include "common.h"
8
9
10 //////////////////////
11 // RGB LED
12 // Common anode
13
14 RGBLed::RGBLed(byte pinR, byte pinG, byte pinB) {
15       redPin    = pinR ;
16       greenPin  = pinG ;
17       bluePin   = pinB ;
18
19       // Equvalente del Setup() per inizializzare i PIN
20       pinMode(redPin, OUTPUT);
21       pinMode(greenPin, OUTPUT);
22       pinMode(greenPin, OUTPUT);
23 };
24
25 void RGBLed::SetColor (byte r, byte g, byte b) {
26 // Imposta il colore di un LED RGB
27       analogWrite(redPin,   r);
28       analogWrite(greenPin, g);
29       analogWrite(bluePin,  b);
30     };
31
32 void RGBLed::Red () {
33 // Accende il LED di rosso
34       SetColor(0,255,255);
35     };
36
37 void RGBLed::Green () {
38 // Accende il LED di verde
39       SetColor(255,0,255);
40     };
41
42 void RGBLed::Blue () {
43 // Accende il LED di blu
44       SetColor(255,255,0);
45     };
46
47 void RGBLed::Magenta () {
48 // Accende il LED di magenta
49       SetColor(0,255,0);
50     };
51
52 void RGBLed::Cyano () {
53 // Accende il LED di Cyano
54       SetColor(255,0,0);
55     };
56
57 void RGBLed::Yellow () {
58 // Accende il LED di giallo
59       SetColor(0,0,255);
60     };
61
62 void RGBLed::White () {
63 // Accende il LED 
64       SetColor(0,0,0);
65     };
66
67 void RGBLed::Off () {
68 // Spegne il LED 
69       SetColor(255,255,255);
70     };
71
72
73
74 /////////////////////////////////////
75 // Lampeggiatore
76 // Constructor
77 Lampeggiatore::Lampeggiatore(int pin)
78 {
79     ledPin = pin;
80     pinMode(ledPin, OUTPUT);
81     ledState = LOW;
82     previousMillis = 0;
83     interval = 500;
84 };
85
86
87
88
89 // Una funzione facente parte di una classe prende il nome di "metodo" della stessa:
90 void Lampeggiatore::Invert() {
91     // Inverte il lampeggio
92     ledState = HIGH ;
93 }
94
95 void Lampeggiatore::Blink() {
96     // Illumina il led a 500ms
97
98     if(millis() - previousMillis > interval) {
99         // save the last time you blinked the LED
100         previousMillis = millis();
101
102         // if the LED is off turn it on and vice-versa:
103         ledState = !ledState ; // Inverti il LED
104     }
105     // set the LED with the ledState of the variable:
106     digitalWrite(ledPin, ledState);
107 };
108
109 void Lampeggiatore::Blink(long time) {
110     // Illumina il led secondo un intervallo passato come argomento
111
112     if(millis() - previousMillis > time) {
113         // save the last time you blinked the LED
114         previousMillis = millis();
115
116         // if the LED is off turn it on and vice-versa:
117         ledState = !ledState ; // Inverti il LED
118     }
119     // set the LED with the ledState of the variable:
120     digitalWrite(ledPin, ledState);
121 };
122
123 void Lampeggiatore::Blink(long up, long down) {
124     // Illumina il ledB precisando ontime e downtime
125
126     if((ledState == HIGH)&& (millis() - previousMillis > up)) {
127     // save the last time you blinked the LED
128         previousMillis = millis();
129         ledState = LOW  ;
130     }
131     else if((ledState == LOW)&& (millis() - previousMillis > down)) {
132         previousMillis = millis();
133         ledState = HIGH  ;
134     }
135
136     // set the LED with the ledState of the variable:
137     digitalWrite(ledPin, ledState);
138 };
139
140 /////////////////////////////////////
141 // Pwm
142 // Constructor
143 Pwm::Pwm(int pin)
144     // Gestione del PWM utilizzando millis
145     // per non bloccare il processore con delay
146     // Warning: serialWrite puo' interferire con i tempi.
147 {
148     ledPin = pin;
149     pinMode(ledPin, OUTPUT);
150     previousMillis = 0;
151     byte brightness = 0 ;
152     increment = 1;
153 };
154
155 void Pwm::Up(long speed) {
156     // Aumenta progressivamente la luminosita' usanndo millis()
157     // quindi senza bloccare il processore
158     // Viene usato un float, in alternativa un coseno
159
160     if (millis() != previousMillis)  { // si potrebbe togliere
161             brightness = 255.0 /(float)speed * millis() ;
162             analogWrite(ledPin, brightness);
163
164         previousMillis = millis();
165     };
166 }
167
168 void Pwm::Down(long speed ) {
169     // Riduce progressivamente la luminosita' usanndo millis()
170     // quindi senza bloccare il processore
171
172     if (millis() != previousMillis)  {
173             brightness = 255 - 255.0 /(float)speed * millis() ;
174             analogWrite(ledPin, brightness);
175
176         previousMillis = millis();
177     };
178 }
179
180 void Pwm::UD(long speed ) {
181     // Aumenta e riduce in sequenza la luminosita' usanndo millis()
182     brightness = 128 + 127 * cos(2 * PI / speed * millis());
183     analogWrite(ledPin, brightness);  
184 }
185
186
187
188 //////////////////
189 // Funzioni
190
191
192 void brilla(byte pin, int velocita ) { // Defalt value di velocita' solo nell'Header
193   // Accende e spegne il LED accetando un argomento 
194   // per impostare la velocita'.
195
196 pinMode(pin, OUTPUT); 
197   // sequenze di istruzione: accendere e spegnere il LED
198   digitalWrite(pin, HIGH);   // turn the LED on (HIGH is the voltage level)
199   delay(velocita);               // wait for a second
200   digitalWrite(pin, LOW);    // turn the LED off by making the voltage LOW
201   delay(velocita);               // wait for a second
202 };
203
204
205 byte lum(byte val) { 
206     // Mappatura dell'intervallo 0-255 con correzione di luminosita.
207     // storata in SRAM
208 return pgm_read_byte_near(BCORRECT + val);
209 };