From fc3e20e513c8f7230fdfb0f0fb4bbab85d06139a Mon Sep 17 00:00:00 2001 From: eaman Date: Tue, 18 Oct 2016 02:28:00 +0200 Subject: [PATCH] Common library (semaphore not yet committed) --- RGB_LED/rgb_5_struct/rgb_5_struct.ino | 1 - libraries/common/common.cpp | 116 ++++++++++++++++++++ libraries/common/common.h | 38 +++++++ libraries/common/examples/loader/loader.ino | 25 +++++ libraries/common/keywords.txt | 11 ++ 5 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 libraries/common/common.cpp create mode 100644 libraries/common/common.h create mode 100644 libraries/common/examples/loader/loader.ino create mode 100644 libraries/common/keywords.txt diff --git a/RGB_LED/rgb_5_struct/rgb_5_struct.ino b/RGB_LED/rgb_5_struct/rgb_5_struct.ino index 03e8f7a..fa8ef10 100644 --- a/RGB_LED/rgb_5_struct/rgb_5_struct.ino +++ b/RGB_LED/rgb_5_struct/rgb_5_struct.ino @@ -78,4 +78,3 @@ void loop() analogWrite(led.blue,led.b); } */ - */ diff --git a/libraries/common/common.cpp b/libraries/common/common.cpp new file mode 100644 index 0000000..94657ae --- /dev/null +++ b/libraries/common/common.cpp @@ -0,0 +1,116 @@ +/* Common + * + * Oggetti di uso comune + */ + +#include "Arduino.h" +#include "common.h" + + +////////////////////// +// RGB LED +// Common anode + +RGBLed::RGBLed(byte pinR, byte pinG, byte pinB) { + redPin = pinR ; + greenPin = pinG ; + bluePin = pinB ; + + // Equvalente del Setup() per inizializzare i PIN + pinMode(redPin, OUTPUT); + pinMode(greenPin, OUTPUT); + pinMode(greenPin, OUTPUT); +}; + +void RGBLed::Red () { +// Accende il LED di rosso + analogWrite(redPin, 0); + analogWrite(greenPin, 255); + analogWrite(bluePin, 255); + }; + +void RGBLed::Green () { +// Accende il LED di verde + analogWrite(redPin, 255); + analogWrite(greenPin, 0); + analogWrite(bluePin, 255); + }; + +void RGBLed::Blue () { +// Accende il LED di blu + analogWrite(redPin, 255); + analogWrite(greenPin, 255); + analogWrite(bluePin, 0); + }; + +void RGBLed::Magenta () { +// Accende il LED di magenta + analogWrite(redPin, 0); + analogWrite(greenPin, 255); + analogWrite(bluePin, 0); + }; + +void RGBLed::Cyano () { +// Accende il LED di Cyano + analogWrite(redPin, 255); + analogWrite(greenPin, 0); + analogWrite(bluePin, 0); + }; + +void RGBLed::Yellow () { +// Accende il LED di giallo + analogWrite(redPin, 0); + analogWrite(greenPin, 0); + analogWrite(bluePin, 255); + }; + +void RGBLed::White () { +// Accende il LED + analogWrite(redPin, 0); + analogWrite(greenPin, 0); + analogWrite(bluePin, 0); + }; + +void RGBLed::Off () { +// Spegne il LED + analogWrite(redPin, 255); + analogWrite(greenPin, 255); + analogWrite(bluePin, 255); + }; + +void RGBLed::SetColor (byte r, byte g, byte b) { + // Imposta il colore di un LED RGB + + analogWrite(redPin, r); + analogWrite(greenPin, g); + analogWrite(bluePin, b); + }; + + +////////////////// +// Funzioni + +void brilla(byte pin) { + // Accende e spegne il LED senza un argomento + // per impostare la velocita'. + const int velocita = 500; + +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 +}; + +void brilla(byte pin, int velocita) { + // 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 +}; diff --git a/libraries/common/common.h b/libraries/common/common.h new file mode 100644 index 0000000..71cb19a --- /dev/null +++ b/libraries/common/common.h @@ -0,0 +1,38 @@ +/* + Common Class + + Oggetti comuni + +*/ + +#include "Arduino.h" +#ifndef common_h +#define common_h + + +class RGBLed { + // Classe rappresentativa di un LED RGB + + byte redPin ; + byte greenPin ; + byte bluePin ; + byte redValue ; + byte greenValue ; + byte blueValue ; + + public: + RGBLed (byte pinR, byte pinG, byte pinB) ; + void Red (); + void Green (); + void Blue (); + void Magenta (); + void Cyano (); + void White (); + void Yellow (); + void Off (); + void SetColor (byte r, byte g, byte b) ; +}; + +void brilla(byte pin, int velocita = 500) ; + +#endif diff --git a/libraries/common/examples/loader/loader.ino b/libraries/common/examples/loader/loader.ino new file mode 100644 index 0000000..de6c7d7 --- /dev/null +++ b/libraries/common/examples/loader/loader.ino @@ -0,0 +1,25 @@ +/* Esempio + + Come caricare e usare un oggetto e una funzione + facente parte della libreria. +*/ + +#include + +void setup() { + // I PINs vengono impostati dalla dichiarazione dell'ogetto. +} + +// Instanziamo un LED +RGBLed led(11, 10, 9); //Istanziamo un oggetto led facente parte + // della classe RGBLed + +void loop() { + led.Red(); + delay(1000); + led.SetColor(255, 0, 255) ; // Mettiamo il LED in Green + delay(1000); + led.Off(); + + brilla(13); // Funzione +} diff --git a/libraries/common/keywords.txt b/libraries/common/keywords.txt new file mode 100644 index 0000000..3bbb239 --- /dev/null +++ b/libraries/common/keywords.txt @@ -0,0 +1,11 @@ +RGBLed KEYWORD1 +Red KEYWORD2 +Green KEYWORD2 +Blue KEYWORD2 +Magenta KEYWORD2 +Cyano KEYWORD2 +White KEYWORD2 +Yellow KEYWORD2 +Off KEYWORD2 +SetColor KEYWORD2 +brilla KEYWORD2 -- 2.39.2