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!!!
IController.h
- Committer:
- aerialspecs
- Date:
- 2017-01-20
- Revision:
- 3:bdd07af32afe
- Parent:
- 0:42e6dbd4dbeb
File content as of revision 3:bdd07af32afe:
/* * Copyright (c) 2017, Aerialspecs, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #ifndef ICONTROLLER_H_ #define ICONTROLLER_H_ #include "stdint.h" namespace com { namespace aerialspecs { namespace mbed { namespace sketchlet { class IController { public: virtual void gotoSketchlet(uint8_t at) = 0; virtual bool nextSketchlet() = 0; virtual void reset() = 0; }; }}}} #endif /* ICONTROLLER_H_ */