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!!!

Committer:
aerialspecs
Date:
Fri Jan 20 15:10:27 2017 +0000
Revision:
3:bdd07af32afe
Parent:
0:42e6dbd4dbeb
Removed default constructor from ArrayList. Embedded environment is too constrained to allow for arbitrary sizing.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aerialspecs 0:42e6dbd4dbeb 1 /*
aerialspecs 0:42e6dbd4dbeb 2 * Copyright (c) 2017, Aerialspecs, Inc.
aerialspecs 0:42e6dbd4dbeb 3 *
aerialspecs 0:42e6dbd4dbeb 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
aerialspecs 0:42e6dbd4dbeb 5 * of this software and associated documentation files (the "Software"), to deal
aerialspecs 0:42e6dbd4dbeb 6 * in the Software without restriction, including without limitation the rights
aerialspecs 0:42e6dbd4dbeb 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
aerialspecs 0:42e6dbd4dbeb 8 * copies of the Software, and to permit persons to whom the Software is
aerialspecs 0:42e6dbd4dbeb 9 * furnished to do so, subject to the following conditions:
aerialspecs 0:42e6dbd4dbeb 10 *
aerialspecs 0:42e6dbd4dbeb 11 * The above copyright notice and this permission notice shall be included in all
aerialspecs 0:42e6dbd4dbeb 12 * copies or substantial portions of the Software.
aerialspecs 0:42e6dbd4dbeb 13 *
aerialspecs 0:42e6dbd4dbeb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
aerialspecs 0:42e6dbd4dbeb 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
aerialspecs 0:42e6dbd4dbeb 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
aerialspecs 0:42e6dbd4dbeb 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
aerialspecs 0:42e6dbd4dbeb 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
aerialspecs 0:42e6dbd4dbeb 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
aerialspecs 0:42e6dbd4dbeb 20 * SOFTWARE.
aerialspecs 0:42e6dbd4dbeb 21 */
aerialspecs 0:42e6dbd4dbeb 22
aerialspecs 0:42e6dbd4dbeb 23 #ifndef ITERATOR_H_
aerialspecs 0:42e6dbd4dbeb 24 #define ITERATOR_H_
aerialspecs 0:42e6dbd4dbeb 25
aerialspecs 0:42e6dbd4dbeb 26 #include <stdint.h>
aerialspecs 0:42e6dbd4dbeb 27
aerialspecs 0:42e6dbd4dbeb 28 namespace com {
aerialspecs 0:42e6dbd4dbeb 29 namespace aerialspecs {
aerialspecs 0:42e6dbd4dbeb 30 namespace mbed {
aerialspecs 0:42e6dbd4dbeb 31 namespace sketchlet {
aerialspecs 0:42e6dbd4dbeb 32
aerialspecs 0:42e6dbd4dbeb 33 template <class I>
aerialspecs 0:42e6dbd4dbeb 34 class Iterator {
aerialspecs 0:42e6dbd4dbeb 35 public:
aerialspecs 0:42e6dbd4dbeb 36 Iterator(I* elements, uint8_t length);
aerialspecs 0:42e6dbd4dbeb 37
aerialspecs 0:42e6dbd4dbeb 38 virtual bool hasNext();
aerialspecs 0:42e6dbd4dbeb 39
aerialspecs 0:42e6dbd4dbeb 40 virtual I next();
aerialspecs 0:42e6dbd4dbeb 41
aerialspecs 0:42e6dbd4dbeb 42 virtual void reset();
aerialspecs 0:42e6dbd4dbeb 43
aerialspecs 0:42e6dbd4dbeb 44 virtual ~Iterator();
aerialspecs 0:42e6dbd4dbeb 45
aerialspecs 0:42e6dbd4dbeb 46 private:
aerialspecs 0:42e6dbd4dbeb 47 I* elements;
aerialspecs 0:42e6dbd4dbeb 48 uint8_t index;
aerialspecs 0:42e6dbd4dbeb 49 uint8_t length;
aerialspecs 0:42e6dbd4dbeb 50 };
aerialspecs 0:42e6dbd4dbeb 51
aerialspecs 0:42e6dbd4dbeb 52 }}}}
aerialspecs 0:42e6dbd4dbeb 53
aerialspecs 0:42e6dbd4dbeb 54 #endif /* ITERATOR_H_ */