Robert Murrell / Sketchlet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Controller.cpp Source File

Controller.cpp

00001 /*
00002  * Copyright (c) 2017, Aerialspecs, Inc.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in all
00012  * copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 
00023 #include "Controller.h"
00024 
00025 namespace com {
00026 namespace aerialspecs {
00027 namespace mbed {
00028 namespace sketchlet {
00029 
00030 Controller::Controller(uint8_t maxSketchlets) {
00031     this->sketchlets = new ArrayList<Sketchlet*>(maxSketchlets);
00032     this->iterator   = NULL;
00033     this->active     = NULL;
00034 }
00035 
00036 void Controller::append(Sketchlet* sketchlet) {
00037     this->sketchlets->append(sketchlet);
00038     sketchlet->setController(this);
00039 }
00040 
00041 void Controller::setup() {
00042     this->iterator = this->sketchlets->iterate();
00043     this->active   = this->sketchlets->get(0);
00044 }
00045 
00046 bool Controller::loop() {
00047     if(!this->active->loop())
00048         return this->nextSketchlet();
00049     else
00050         return true;
00051 }
00052 
00053 void Controller::gotoSketchlet(uint8_t at) {
00054     this->active->terminate();
00055     this->active = this->sketchlets->get(at);
00056     this->active->initialize();
00057 }
00058 
00059 bool Controller::nextSketchlet() {
00060     this->active->terminate();
00061     if(this->iterator->hasNext()) {
00062         this->active = this->iterator->next();
00063         this->active->initialize();
00064         return true;
00065     }
00066     else
00067         return false;
00068 }
00069 
00070 void Controller::reset() {
00071     this->iterator->reset();
00072     this->nextSketchlet();
00073 }
00074 
00075 Controller::~Controller() {
00076     delete this->sketchlets;
00077     delete this->iterator;
00078 }
00079 
00080 }}}}