Simple framework to allow for mini-programs, or Sketchlets, to be executed on a single SoC. Distributed under the MIT license.
Arduino version of this library can be found at https://github.com/murrellrr/arduino_sketchlet.
Example Usage:
Inside your man.cpp file.
#include "main.h" using namespace com::aerialspecs::mbed::sketchlet; void setup() { controller = new Controller(MAX_SKETCHLET_SIZE); controller->setup(); } bool loop() { return controller->loop(); } void shutdown() { delete controller; } /** * */ int main() { setup(); while(loop()) {} shutdown(); }
Creating a Sketchlet:
A sample of a Sketchlet. DefaultSketchlet.h and DefaultSketchlet.cpp
// In your header file. #define LED_PIN 13 #define BLINK_TIMEOUT 1000 #include "Sketchlet.h" using namespace com::aerialspecs::mbed::sketchlet; class DefaultSketchlet : public Sketchlet { public: DefaultSketchlet(); virtual void initialize(); virtual bool loop(); virtual void terminate(); virtual ~DefaultSketchlet(); private: uint32_t lastTime; }; // Inside CPP file. #include "DefaultSketchlet.h" using namespace com::aerialspecs::mbed::sketchlet; DefaultSketchlet::DefaultSketchlet() : public Sketchlet() { this->lastTime = millis(); } void DefaultSketchlet::initialize() { // initialize your led } bool DefaultSketchlet::loop() { uint32_t currentTime = millis(); if((currentTime - this->lastTime) >= BLINK_TIMEOUT) { // toggle your led this->lastTime = currentTime; } return true; } void DefaultSketchlet::terminate() { // release the LED, if required by your platform. } DefaultSketchlet::~DefaultSketchlet() { // cleanup anything... }
Using your sketchlet:
Using your sketchlet
#include "main.h" using namespace com::aerialspecs::mbed::sketchlet; void setup() { controller = new Controller(MAX_SKETCHLET_SIZE); sketchlet = new DefaultSketchlet(); controller->append(sketchlet); controller->setup(); }
You can append multiple sketchlets up to the number defined in the Controller constructor. The controller will run the sketchlets in the order they are registered under normal conditions. Returning false from a sketchlet will cause the controller to invoke the terminate life-cycle of the current sketchlet, load the next one as current, and execute the initialize life-cycle.
If you need to execute a sketchlet out of order you may execute the Controller::gotoSketchlet(uint8_t at) function to go to a specific sketchlet. Please note, in the current version, the "GOTO" does not update the iterator so completing the sketchlet you "GOTO" will cause the controller to go to the next sketchlet from the first time "GOTO" was invoked, NOT from the current sketchlet.
ENJOY!!!
Files at revision 3:bdd07af32afe
Name | Size | Actions |
---|---|---|
[up] | ||
ArrayList.h | 3172 | Revisions Annotate |
ArrayList.hpp | 2025 | Revisions Annotate |
Controller.cpp | 2444 | Revisions Annotate |
Controller.h | 1911 | Revisions Annotate |
IController.h | 1498 | Revisions Annotate |
Iterator.h | 1616 | Revisions Annotate |
Iterator.hpp | 1851 | Revisions Annotate |
LICENSE.txt | 1094 | Revisions Annotate |
Sketchlet.cpp | 1539 | Revisions Annotate |
Sketchlet.h | 1722 | Revisions Annotate |