Created
January 8, 2011 21:48
-
-
Save jimsynz/771179 to your computer and use it in GitHub Desktop.
Example Sketch to fade an LED in and out using dCouple & Aiko.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dCouple_Single_LED_Example.cpp.o: In function `setup': | |
dCouple_Single_LED_Example.cpp:14: undefined reference to `dCouple::Analog::Sink::is_connected_to(dCouple::Analog::Source*)' | |
dCouple/dCouple.cpp.o: In function `dCouple::Analog::OutputPin::sink_update()': | |
/Users/jnh/Documents/Arduino/libraries/dCouple/Analog/OutputPin.cpp:22: undefined reference to `dCouple::Analog::Sink::is_connected_to()' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "WProgram.h" | |
#include <dCouple.h> | |
namespace dCouple { | |
namespace Analog { | |
#include "Analog/Source.cpp" | |
#include "Analog/Sink.cpp" | |
#include "Analog/OutputPin.cpp" | |
} | |
namespace LED { | |
#include "LED/LED.cpp" | |
#include "LED/RGB.cpp" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef dCouple_h | |
#define dCouple_h | |
#include "WProgram.h" | |
namespace dCouple { | |
namespace Analog { | |
#include "Analog/Source.h" | |
#include "Analog/Sink.h" | |
#include "Analog/OutputPin.h" | |
} | |
namespace LED { | |
#include "LED/LED.h" | |
#include "LED/RGB.h" | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <AikoEvents.h> | |
#include <dCouple.h> | |
dCouple::LED::LED* led = &dCouple::LED::LED(); | |
dCouple::Analog::OutputPin* pin = &dCouple::Analog::OutputPin(9); | |
int ledCycleDirection = 1; | |
void setup() { | |
pin->is_connected_to(led); | |
Aiko::Events.addHandler(updateOutputPin, 70); // ~14Hz | |
Aiko::Events.addHandler(cycleLed, 10); // 100Hz | |
} | |
void updateOutputPin() { | |
pin->sink_update(); | |
} | |
void cycleLed() { | |
if (led->intensity() == 255) { | |
ledCycleDirection = -1; | |
} | |
if (led->intensity() == 0) { | |
ledCycleDirection = 1; | |
} | |
led->set_intensity(led->intensity() + ledCycleDirection); | |
} | |
void loop() { | |
Aiko::Events.loop(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "OutputPin.h" | |
OutputPin::OutputPin (unsigned int pin) { | |
// FIXME: What about the Arduino MEGA?!? | |
switch (pin) { | |
case 3: | |
case 5: | |
case 6: | |
case 9: | |
case 10: | |
case 11: | |
_output_pin = pin; | |
break; | |
default: | |
_output_pin = 0; | |
} | |
} | |
void OutputPin::sink_update() { | |
Source* source = is_connected_to(); | |
if ((_output_pin > 0) && (source->analog_source_dirty())) { | |
unsigned int value = source->analog_source_value(); | |
if (source->analog_source_resolution() != 255) { | |
// This is messy, not to mention inaccurate. | |
value = value / source->analog_source_resolution() * 255; | |
} | |
analogWrite(_output_pin, value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef dCouple_Analog_OutputPin_h | |
#define dCouple_Analog_OutputPin_h | |
class OutputPin: public dCouple::Analog::Sink { | |
unsigned int _output_pin; | |
public: | |
OutputPin (unsigned int pin); | |
void sink_update(); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Sink.h" | |
void Sink::is_connected_to(Source* source) { | |
_output_source = source; | |
} | |
Source* Sink::is_connected_to() { | |
return _output_source; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef dCouple_Analog_Sink | |
#define dCouple_Analog_Sink | |
class Sink { | |
Source* _output_source; | |
public: | |
void is_connected_to(Source* source); | |
Source* is_connected_to(); | |
virtual void sink_update() = 0; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment