Skip to content

Instantly share code, notes, and snippets.

@dothebart
Created March 26, 2018 17:11
Show Gist options
  • Save dothebart/18a14b75a3af822d0473b4dd8fc8661a to your computer and use it in GitHub Desktop.
Save dothebart/18a14b75a3af822d0473b4dd8fc8661a to your computer and use it in GitHub Desktop.
#include "Cosa/Types.h"
#include "Cosa/AnalogPin.hh"
// #include "Cosa/AnalogPins.hh"
#include "Cosa/Periodic.hh"
#include "Cosa/Event.hh"
#include "Cosa/Board.hh"
#include "Cosa/OutputPin.hh"
#include "Cosa/Watchdog.hh"
#include "Cosa/Trace.hh"
//#include "Cosa/IOStream/Driver/UART.hh"
#include "Cosa/UART.hh"
#include "Cosa/Memory.h"
static const uint16_t SAMPLE_MS = 100;
class samplePin : public AnalogPin, Periodic {
static const uint8_t nBuf = 10;
uint32_t ringBuffer[nBuf];
uint8_t pBuf;
public:
samplePin(Job::Scheduler* scheduler, Board::AnalogPin pin) :
AnalogPin(pin),
Periodic(scheduler, SAMPLE_MS),
ringBuffer({0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
pBuf(0) {
// trace << PSTR("hi samplePin \n");
}
void begin() {
trace << PSTR("run samplePin \n");
powerup();
sample_request(Event::SAMPLE_COMPLETED_TYPE);
}
virtual void on_event(uint8_t type, uint16_t value) {
UNUSED(type);
trace << PSTR("analog on_event\n");
ringBuffer[pBuf] = value * value;
pBuf ++;
if (pBuf > nBuf) {
pBuf = 0;
}
}
};
Watchdog::Scheduler scheduler;
samplePin sPin(&scheduler, Board::A0);
void setup()
{
// Start trace output stream on the serial port
uart.begin(9600);
trace.begin(&uart, PSTR("CosaTWISlave: started"));
// Check amount of free memory and size of classes
TRACE(free_memory());
// Start the watchdog ticks counter
trace << PSTR("watchdog began\n");
sPin.begin();
Watchdog::begin();
// Watchdog::job(&scheduler);
}
void loop()
{
Event event;
trace << PSTR("loop\n");
Event::queue.await(&event);
event.dispatch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment