Created
January 12, 2011 22:31
-
-
Save jimsynz/777032 to your computer and use it in GitHub Desktop.
Main Arduino sketch.
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 "Clock.h" | |
#include "WProgram.h" | |
void Clock::low_tick() { | |
if (finite) { | |
if (ticks_to_go >= 0) { | |
set_aiko_callback(0, interval); | |
ticks_to_go--; | |
tick(); | |
} | |
} | |
else { | |
tick(); | |
} | |
} | |
void Clock::set_aiko_callback(unsigned int period, unsigned int delay) { | |
// Ugh. Coupling. | |
// Create a method callback | |
Aiko::EventHandler* handler = static_cast<Aiko::EventHandler*>(malloc(sizeof(Aiko::EventHandler))); | |
handler->callback_ = Aiko::methodCallback(*this, &Clock::low_tick); | |
handler->period_ = period; | |
handler->countdown_ = delay; | |
events->addHandler(handler); | |
} | |
void Clock::set_callback_timer(Aiko::EventManager* timer) { | |
ticks_to_go = 0; | |
finite = false; | |
events = timer; | |
} | |
void Clock::update_frequency(Aiko::EventManager* timer, unsigned int hz) { | |
update_interval(timer, (unsigned int)(1000.0 / (float)hz)); | |
} | |
void Clock::update_interval(Aiko::EventManager* timer, unsigned int ms) { | |
set_callback_timer(timer); | |
set_aiko_callback(ms, 0); | |
} | |
void Clock::update_frequency(Aiko::EventManager* timer, unsigned int hz, unsigned int times) { | |
update_interval(timer, (unsigned int)(1000.0 / (float)hz), times); | |
} | |
void Clock::update_interval(Aiko::EventManager* timer, unsigned int ms, unsigned int times) { | |
set_callback_timer(timer); | |
finite = true; | |
ticks_to_go = times; | |
set_aiko_callback(0, ms); | |
} |
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_Clock_h | |
#define dCouple_Clock_h | |
#include <AikoEvents.h> | |
#include <AikoCallback.h> | |
class Clock { | |
Aiko::EventManager* events; | |
unsigned int ticks_to_go; | |
unsigned int interval; | |
bool finite; | |
void low_tick(); | |
void set_aiko_callback(unsigned int period, unsigned int delay); | |
public: | |
void set_callback_timer(Aiko::EventManager* _events); | |
void update_frequency(Aiko::EventManager* timer, unsigned int hz); | |
void update_interval(Aiko::EventManager* timer, unsigned int ms); | |
void update_frequency(Aiko::EventManager* timer, unsigned int hz, unsigned int times); | |
void update_interval(Aiko::EventManager* timer, unsigned int ms, unsigned int times); | |
virtual void tick(); | |
}; | |
#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::RGB rgb (0, 255); | |
dCouple::Analog::OutputPin* pins[3]; | |
void setup() { | |
for (int i = 0; i < 3; i++) { | |
pins[i] = &dCouple::Analog::OutputPin (i + 9); | |
pins[i]->is_connected_to(rgb.led(i)); | |
pins[i]->update_frequency(&Aiko::Events, 20); | |
} | |
} | |
void loop() { | |
Aiko::Events.loop(); | |
} | |
// ERROR: | |
// dCouple_RGB.cpp.o: In function `setup': | |
// dCouple_RGB.cpp:26: undefined reference to `dCouple::Clock::update_frequency(Aiko::EventManager*, unsigned int)' |
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::tick() { | |
dCouple::State* state = is_connected_to(); | |
if ((_output_pin > 0) && (state->state_dirty())) { | |
unsigned int value = state->state_value(); | |
if (state->state_resolution() != 255) { | |
// This is messy, not to mention inaccurate. | |
value = value / state->state_resolution() * 255; | |
} | |
analogWrite(_output_pin, value); | |
//analogWrite(9,30); | |
} | |
} |
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 | |
#include "WProgram.h" | |
class OutputPin: public dCouple::Source, public dCouple::Clock { | |
unsigned int _output_pin; | |
public: | |
OutputPin (unsigned int pin); | |
void tick(); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment