diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bea433 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.DS_Store diff --git a/FancyLED.cpp b/FancyLED.cpp new file mode 100644 index 0000000..6ccf55e --- /dev/null +++ b/FancyLED.cpp @@ -0,0 +1,270 @@ +/* + FancyLED.h - FancyLED for Wiring/Arduino + (cc) 2007 Carlyn Maw . Some rights reserved. + + Created 29 Oct 2010 + Version 0.1 +*/ + + +// include core Wiring API +#include "WProgram.h" + +// include this library's description file +#include "FancyLED.h" + + + +// Constructor ///////////////////////////////////////////////////////////////// +// Function that handles the creation and setup of instances + +//------------------------------------------------------ Using Arduino Pin Num +FancyLED::FancyLED(int myPin, bool myMode) +{ + // initialize this instance's variables + this->_myPin = myPin; + + pinMode(this->_myPin, OUTPUT); + + this->_type = 0; + this->_myBit = this->_myPin; + this->_mode = myMode; + + _lastState = 0; + _currentState = 0; + _pinState= 0; + + _blinkOnPeriod = 500; + _dBrightness = 100; + _brightnessIncrement = 1; + +} + +//----------------------------------------------------------------- Using Byte +FancyLED::FancyLED(int myBit, bool myMode, unsigned char *myRegister) +{ + // initialize this instance's variables + this->_type = 1; + this->_mode = myMode; + this->_myBit = myBit; + this->_myPin = this->_myBit; + this->_myRegister = myRegister; + + this->_registerValue = 255; + + _lastState = 0; + _currentState = 0; + _pinState= 0; + + _blinkOnPeriod = 500; + _dBrightness = 70; + _brightnessIncrement = 1; +} + +// Public Methods ////////////////////////////////////////////////////////////// +// Functions available in Wiring sketches, this library, and other libraries + +//---------////////////////////MAIN LOOP / LISTENER ///////////--------------// + +void FancyLED::setCurrentTime(unsigned long newCurrentTime) { + _currentTime = newCurrentTime; +} + +bool FancyLED::getState(void){ + return _currentState; +} + +bool FancyLED::isOn(void){ + return _currentState; +} + +bool FancyLED::isOff(void){ + return !_currentState; +} + + + +void FancyLED::turnOn(void){ + _pinState = _mode; + _lastState = _currentState; + _currentState = _mode; + _lBrightness = _cBrightness; + _cBrightness = 255; + //_lastOnTime = _currentOnTime; + //_currentOnTime = _currentTime; + _blinkFlipTime = _currentTime + _blinkOnPeriod; + updatePin(_pinState); +} + +void FancyLED::turnOff(void){ + _pinState = !_mode; + _lastState = _currentState; + _currentState = !_mode; + _lBrightness = _cBrightness; + _cBrightness = 0; + //_lastOffTime = _currentOffTime; + //_currentOffTime = _currentTime; + _blinkFlipTime = _currentTime + _blinkOnPeriod; + + updatePin(_pinState); +} + +void FancyLED::toggle(void){ + _lastState = _currentState; + _currentState ? _currentState=false : _currentState=true; + _currentState ? _pinState=_mode : _pinState = !_mode; + _blinkFlipTime = _currentTime; + _lBrightness = _cBrightness; + _cBrightness = _currentState*255; + updatePin(_pinState); +} + + +void FancyLED::setState(bool newState) { + newState ? turnOn() : turnOff(); +} + +void FancyLED::blinkWithoutDelay(void) { + + if ((_blinkOnPeriod) < (_currentTime - _blinkFlipTime)) { + _currentState ? _currentState=false : _currentState=true; + _currentState ? _pinState=_mode : _pinState = !_mode; + //update the blinkFlipTime with the current time. + _blinkFlipTime = _currentTime; + } + + updatePin(_pinState); //can't use toggle b/c better to have this + //value on the outside of the if +} + +void FancyLED::blinkWithoutDelay(unsigned int myBlinkPeriod) { + + _blinkOnPeriod = myBlinkPeriod; + blinkWithoutDelay(); + +} + +void FancyLED::delayBlinkWithCount(unsigned int numberOfTimes, unsigned int blinkPeriod) { + + _blinkOnPeriod = blinkPeriod; + delayBlinkWithCount(numberOfTimes); + +} + +void FancyLED::delayBlinkWithCount(unsigned int numberOfTimes) { + for (int i=0; i < numberOfTimes; i++) { + _blinkFlipTime = _currentTime; + _currentState=true; + _lBrightness = _cBrightness; + _cBrightness = 255; + _pinState=_mode; + updatePin(_pinState); + delay(_blinkOnPeriod); + _blinkFlipTime = _currentTime; + _currentState=false; + _lBrightness = _cBrightness; + _cBrightness = 0; + _pinState= !_mode; + updatePin(_pinState); + delay(_blinkOnPeriod); + } + //updatePin(_pinState); //can't use toggle b/c better to have this + //value on the outside of the if +} + +unsigned int FancyLED::getBlinkPeriod(void) { + return _blinkOnPeriod; +} + +void FancyLED::setBlinkPeriod(unsigned int newBlinkPeriod) { + _blinkOnPeriod = newBlinkPeriod; +} + + +void FancyLED::turnOnAnalog(byte myBrightness){ + setCurrentBrightness(myBrightness); +} + +void FancyLED::turnOnAnalog(void){ + setCurrentBrightness(_dBrightness); +} + +unsigned char FancyLED::getCurrentBrightness(void) { + return _cBrightness; +} + +void FancyLED::setCurrentBrightness(unsigned char myBrightness) { + if (myBrightness) { + _pinState = _mode; + _lastState = _currentState; + _currentState = _mode; + _lBrightness = _cBrightness; + _cBrightness = myBrightness; + //_lastOnTime = _currentOnTime; + //_currentOnTime = _currentTime; + _blinkFlipTime = _currentTime + _blinkOnPeriod; + updatePinAnalog(_cBrightness); + } else { + turnOff(); + } +} + +unsigned char FancyLED::getDefaultBrightness(void) { + return _dBrightness; +} + +void FancyLED::setDefaultBrightness(unsigned char myBrightness) { + _dBrightness = myBrightness; +} + + +void FancyLED::brighten(void) { + brighten(_brightnessIncrement); +} + +void FancyLED::dim(void) { + dim(_brightnessIncrement); +} + +void FancyLED::brighten(unsigned char increment) { + updateBrightness(1, increment); +} + +void FancyLED::dim(unsigned char increment) { + updateBrightness(-1, increment); +} + +unsigned char FancyLED::getBrightnessIncrement(void) { + return _brightnessIncrement; +} + +void FancyLED::setBrightnessIncrement(unsigned char increment) { + _brightnessIncrement = increment; +} + + +// Private Methods ////////////////////////////////////////////////////////////// +// Functions available to the library only. + + +void FancyLED::updatePin(bool pinValue) { + if (pinValue) { + digitalWrite(_myPin, HIGH); + } else { + digitalWrite(_myPin, LOW); + } + +} + +void FancyLED::updateBrightness(int directionMultiplier, unsigned char increment) { + setCurrentBrightness(constrain((_cBrightness + (directionMultiplier*increment)), 0, 255)); +} + +void FancyLED::updatePinAnalog(byte brightness) { + if (_mode) { + analogWrite(_myPin, brightness); + } else { + analogWrite(_myPin, 255-brightness); + } +} + diff --git a/FancyLED.h b/FancyLED.h new file mode 100644 index 0000000..e73916f --- /dev/null +++ b/FancyLED.h @@ -0,0 +1,121 @@ +/* + FancyLED.h - - FancyLED library for Wiring/Arduino - Version 0.1 + + Original library (0.1) by Carlyn Maw. + + */ + +// ensure this library description is only included once +#ifndef FancyLED_h +#define FancyLED_h + +// include types & constants of Wiring core API +#include "WProgram.h" + +// library interface description +class FancyLED { + + // user-accessible "public" interface + public: + // constructors: + FancyLED(int myPin, bool myMode); + FancyLED(int myBit, bool myMode, unsigned char *myRegister); + + //char* version(void); // get the library version + //unsigned char getRegisterValue(void); + + void setCurrentTime(unsigned long); + + bool getState(void); + void setState(bool); + bool isOn(void); + bool isOff(void); + + void turnOn(void); + //void turnOnWithDuration(unsigned int onPeriod); + + void turnOff(void); + + void toggle(void); + + void blinkWithoutDelay(void); + void blinkWithoutDelay(unsigned int blinkPeriod); + + void delayBlinkWithCount(unsigned int numberOfTimes); + void delayBlinkWithCount(unsigned int numberOfTimes, unsigned int blinkPeriod); + + void turnOnAnalog(void); + void turnOnAnalog(byte brightness); + + void brighten(void); + void dim(void); + + void brighten(unsigned char); + void dim(unsigned char increment); + + + //void blinkWithAnalog(void); + //void blinkWithAnalog(byte brightness); + //void delayBlinkWithCountAndAnalog(unsigned int numberOfTimes, byte brightness); + //void delayBlinkWithCountAndAnalog(unsigned int numberOfTimes, unsigned int blinkPeriod, byte brightness); + + unsigned int getBlinkPeriod(void); + void setBlinkPeriod(unsigned int); + + unsigned char getCurrentBrightness(void); + unsigned char getDefaultBrightness(void); + void setCurrentBrightness(unsigned char); + void setDefaultBrightness(unsigned char); + + unsigned char getBrightnessIncrement(void); + void setBrightnessIncrement(unsigned char); + + + + + // library-accessible "private" interface + private: + int _myPin; + int _myBit; + unsigned char *_myRegister; + unsigned char _registerValue; + bool _type; //direct pin or shift register + bool _mode; //HIGH == pressed (1) or LOW == pressed (0) + + bool _lastState; + bool _currentState; + bool _pinState; + + unsigned int _blinkOnPeriod; //same as on duration + //unsigned int _blinkOffPeriod; + + byte _dBrightness; + byte _cBrightness; + byte _lBrightness; + unsigned char _brightnessIncrement; + + bool _changed; + bool _justOn; + bool _justOff; + //unsigned int _onCount; + //unsigned int _offCount; + + unsigned long int _lastOnTime; + unsigned long int _currentOnTime; + unsigned long int _lastOffTime; + unsigned long int _currentOffTime; + unsigned long int _blinkFlipTime; + + unsigned long int _currentTime; + + bool _toggleFlag; + + void updatePin(bool); + void updatePinAnalog(byte); + void updateBrightness(int, unsigned char); + + +}; + +#endif + diff --git a/examples/FancyLED_Ex01_OnAndOff/FancyLED_Ex01_OnAndOff.pde b/examples/FancyLED_Ex01_OnAndOff/FancyLED_Ex01_OnAndOff.pde new file mode 100644 index 0000000..a170ecc --- /dev/null +++ b/examples/FancyLED_Ex01_OnAndOff/FancyLED_Ex01_OnAndOff.pde @@ -0,0 +1,52 @@ +/* + * FanceyLED Library Example 1 + * ------------ + * + * Circuit: + * - Button, pulled up externally with 10k resitor on pin 2 + * - Extra LED on pin 9 + * + * Behavior: + * + * Created 4 April 2010 + * Updated 29 October 2010 + * by Carlyn Maw + * + */ + +#include +#include + + //Instantiate Button on digital pin 2 + //pressed = ground (i.e. pulled high with external resistor) + Button helloButton = Button(2, LOW); + + FancyLED externalLED = FancyLED(9, HIGH); + FancyLED boardLED = FancyLED(13, HIGH); + +void setup() +{ + + +} + +void loop() +{ + + helloButton.listen(); + + + if (helloButton.isPressed()) { + externalLED.turnOn(); + boardLED.turnOff(); + } + + + if (helloButton.isReleased()) { + externalLED.turnOff(); + boardLED.turnOn(); + + } + +} + diff --git a/examples/FancyLED_Ex02_Toggle/FancyLED_Ex02_Toggle.pde b/examples/FancyLED_Ex02_Toggle/FancyLED_Ex02_Toggle.pde new file mode 100644 index 0000000..7e6586f --- /dev/null +++ b/examples/FancyLED_Ex02_Toggle/FancyLED_Ex02_Toggle.pde @@ -0,0 +1,46 @@ +/* + * FanceyLED Library Example 2 + * ------------ + * + * Circuit: + * - Button, pulled up externally with 10k resitor on pin 2 + * - Extra LED on pin 9 + * + * Behavior: + * + * Created 4 April 2010 + * Updated 29 October 2010 + * by Carlyn Maw + * + */ + +#include +#include + + //Instantiate Button on digital pin 2 + //pressed = ground (i.e. pulled high with external resistor) + Button helloButton = Button(2, LOW); + + FancyLED externalLED = FancyLED(9, HIGH); + FancyLED boardLED = FancyLED(13, HIGH); + +void setup() +{ + externalLED.turnOn(); + boardLED.turnOff(); + +} + +void loop() +{ + + helloButton.listen(); + + + if (helloButton.onPress()) { + externalLED.toggle(); + boardLED.toggle(); + } + +} + diff --git a/examples/FancyLED_Ex03_blinkWithoutDelay/FancyLED_Ex03_blinkWithoutDelay.pde b/examples/FancyLED_Ex03_blinkWithoutDelay/FancyLED_Ex03_blinkWithoutDelay.pde new file mode 100644 index 0000000..807f12b --- /dev/null +++ b/examples/FancyLED_Ex03_blinkWithoutDelay/FancyLED_Ex03_blinkWithoutDelay.pde @@ -0,0 +1,60 @@ +/* + * FanceyLED Library Example 3 + * ------------ + * + * Circuit: + * - Button, pulled up externally with 10k resitor on pin 2 + * - Extra LED on pin 9 + * + * Behavior: + * + * Created 4 April 2010 + * Updated 29 October 2010 + * by Carlyn Maw + * + */ + +#include +#include + +unsigned long loopCurrentTime = 0; + +//Instantiate Button on digital pin 2 +//pressed = ground (i.e. pulled high with external resistor) +Button helloButton = Button(2, LOW); + +FancyLED externalLED = FancyLED(9, HIGH); +FancyLED boardLED = FancyLED(13, HIGH); + +void setup() +{ + externalLED.turnOff(); + boardLED.turnOff(); + + Serial.begin(9600); + Serial.print("Default = \t"); + Serial.println(externalLED.getBlinkPeriod()); + externalLED.setBlinkPeriod(1000); + Serial.print("New Delay = \t"); + Serial.println(externalLED.getBlinkPeriod()); + +} + +void loop() +{ + loopCurrentTime = millis(); + externalLED.setCurrentTime(loopCurrentTime); + boardLED.setCurrentTime(loopCurrentTime); + + helloButton.listen(); + + + if (helloButton.onPress()) { + externalLED.blinkWithoutDelay(); + boardLED.blinkWithoutDelay(300); + } else { + externalLED.turnOn(); + boardLED.turnOff(); + } +} + diff --git a/examples/FancyLED_Ex04_delayBlinkWithCount/FancyLED_Ex04_delayBlinkWithCount.pde b/examples/FancyLED_Ex04_delayBlinkWithCount/FancyLED_Ex04_delayBlinkWithCount.pde new file mode 100644 index 0000000..cfabf00 --- /dev/null +++ b/examples/FancyLED_Ex04_delayBlinkWithCount/FancyLED_Ex04_delayBlinkWithCount.pde @@ -0,0 +1,36 @@ +/* + * FanceyLED Library Example 4 + * ------------ + * + * Circuit: + * - Button, pulled up externally with 10k resitor on pin 2 + * - Extra LED on pin 9 + * + * Behavior: + * + * Created 4 April 2010 + * Updated 29 October 2010 + * by Carlyn Maw + * + */ +#include + +unsigned long loopCurrentTime = 0; + +//Instantiate Button on digital pin 2 +//pressed = ground (i.e. pulled high with external resistor) +Button helloButton = Button(2, LOW); + +FancyLED externalLED = FancyLED(9, HIGH); +FancyLED boardLED = FancyLED(13, HIGH); + +void setup() +{ + externalLED.delayBlinkWithCount(4); + boardLED.delayBlinkWithCount(4, 200); +} + +void loop() +{ + +} diff --git a/examples/FancyLED_Ex05_brightness/FancyLED_Ex05_brightness.pde b/examples/FancyLED_Ex05_brightness/FancyLED_Ex05_brightness.pde new file mode 100644 index 0000000..04f95fe --- /dev/null +++ b/examples/FancyLED_Ex05_brightness/FancyLED_Ex05_brightness.pde @@ -0,0 +1,60 @@ +/* + * FanceyLED Library Example 5 + * ------------ + * + * Circuit: + * - Button, pulled up externally with 10k resitor on pin 2 + * - Extra LED on pin 9 + * + * Behavior: + * + * Created 4 April 2010 + * Updated 29 October 2010 + * by Carlyn Maw + * + */ + +#include +#include + +//Instantiate Button on digital pin 2 +//pressed = ground (i.e. pulled high with external resistor) +Button helloButton = Button(2, LOW); + +FancyLED externalLED = FancyLED(9, HIGH); +FancyLED boardLED = FancyLED(13, HIGH); + +void setup() +{ + + Serial.begin(9600); + Serial.print("Default = \t"); + Serial.println(externalLED.getDefaultBrightness(), DEC); + externalLED.setDefaultBrightness(80); + Serial.print("New Brightness = \t"); + Serial.println(externalLED.getDefaultBrightness(), DEC); + +} + +void loop() +{ + + helloButton.listen(); + + + if (helloButton.isPressed()) { + //externalLED.turnOnAnalog(); + externalLED.turnOnAnalog(2); //turns on at 2, + //leaves default alone + boardLED.turnOff(); + } + + + if (helloButton.isReleased()) { + externalLED.turnOnAnalog(); //uses default brightness + boardLED.turnOnAnalog(); //won't work!!!!wrong type of pin!! + + } + +} + diff --git a/examples/FancyLED_Ex06_AdvancedBrightness/FancyLED_Ex06_AdvancedBrightness.pde b/examples/FancyLED_Ex06_AdvancedBrightness/FancyLED_Ex06_AdvancedBrightness.pde new file mode 100644 index 0000000..db77da9 --- /dev/null +++ b/examples/FancyLED_Ex06_AdvancedBrightness/FancyLED_Ex06_AdvancedBrightness.pde @@ -0,0 +1,77 @@ +/* + * FancyLED Library Example 6, combines w/ AnalogIn SerialOut example included + * in Arduino 21 + * ------------ + * + * Circuit: + * - Button, pulled up externally with 10k resitor on pin 2 + * - Extra LED on pin 9 + * + * Behavior: + * + * Created 4 April 2010 + * Updated 29 October 2010 + * by Carlyn Maw + * + */ + +#include +#include + +//Instantiate Button on digital pin 2 +//pressed = ground (i.e. pulled high with external resistor) +Button helloButton = Button(2, LOW); + +FancyLED externalLED = FancyLED(9, HIGH); +FancyLED boardLED = FancyLED(13, HIGH); + +bool runFlag = 0; +const int analogInPin = A1; // Analog input pin that the potentiometer is attached to +int sensorValue = 0; // value read from the pot +int outputValue = 0; // value output to the PWM (analog out) + +void setup() +{ + + Serial.begin(9600); + Serial.print("Default = \t"); + Serial.println(externalLED.getBrightnessIncrement(), DEC); + externalLED.setBrightnessIncrement(2); + Serial.print("New Brightness Increment = \t"); + Serial.println(externalLED.getBrightnessIncrement(), DEC); + externalLED.setDefaultBrightness(178); +} + +void loop() +{ + + helloButton.listen(); + + // read the analog in value: + sensorValue = analogRead(analogInPin); + // map it to the range of the analog out: + outputValue = map(sensorValue, 0, 1023, 0, 255); + // change the analog out value: + externalLED.setDefaultBrightness(outputValue); + + if (helloButton.onPress()) { + helloButton.listen(); + runFlag = true; + } + + if (runFlag) { + externalLED.turnOff(); + for(int i = 0; i < (externalLED.getDefaultBrightness()/externalLED.getBrightnessIncrement()); i++){ //goes through each speed from 0 to 255 + //externalLED.brighten(); //brightens by + //externalLED.brighten(20); + //externalLED.dim(); + //externalLED.dim(20); + externalLED.brighten(); + delay(20); + } + Serial.println(externalLED.getCurrentBrightness(), DEC); + runFlag = false; + } + +} + diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..eee5177 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,41 @@ +####################################### +# Syntax Coloring Map For Test +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +FancyLED KEYWORD1 + +####################################### +# Methods and Functions ( KEYWORD2) +####################################### + +setCurrentTime KEYWORD2 +turnOn KEYWORD2 +turnOnWithDuration KEYWORD2 +turnOnAnalog KEYWORD2 +turnOff KEYWORD2 +toggle KEYWORD2 +blinkWithoutDelay KEYWORD2 +delayBlinkWithCount KEYWORD2 +getBlinkPeriod KEYWORD2 +setBlinkPeriod KEYWORD2 +turnOnAnalog KEYWORD2 +brighten KEYWORD2 +dim KEYWORD2 +getBrightness KEYWORD2 +setBrightness KEYWORD2 +getBrightnessIncrement KEYWORD2 +setBrightnessIncrement KEYWORD2 + + +###################################### +# Instances ( KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +#######################################