/* Behavior - construct arduino programs as a bunch of behaviors. Copyright (c) 2007, giuliano carlini and Susan Rendina. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ extern "C" { // #include #include }; #include "Behavior.h" #define NULL 0 /* * TODO: Need to add a "scheduler" that will keep separate * run, sleep, and disabled queue's, and execute only from * run queue using a round robin algorithm. */ Behavior::~Behavior() { } Behavior::Behavior(const char* i_name) : id( ++next_id ), name(i_name), enabled(true), wake_up_time(0) { if ( NULL == all_first ) { all_first = this; all_last = this; run_first = this; run_last = this; prev = NULL; next = NULL; } else { all_last->next = this; this->all_prev = all_last; this->all_next = NULL; all_last = this; run_last->next = this; this->prev = run_last; this->next = NULL; run_last = this; } } void Behavior::RunAllReady() { for ( Behavior* next = run_first; NULL != next; next = next->next ) { next->RunIfReady(); } } void Behavior::RunIfReady() { if ( ReadyToRun() ) { //Serial.print("Running: "); //Serial.println( const_cast(GetName()) ); Run(); //Serial.println(); } } void Behavior::Run() { } bool Behavior::IsEnabled() const { return enabled; } bool Behavior::IsDisabled() const { return !IsEnabled(); } void Behavior::SetEnabled( bool value ) { enabled = value; } void Behavior::SetDisabled( bool value ) { SetEnabled(!value); } unsigned long Behavior::Delay() const { unsigned long now = millis(); if ( wake_up_time < now ) { return 0; } else { return wake_up_time - now; } } void Behavior::Delay( unsigned long value ) { wake_up_time = millis() + value; } void Behavior::DelayMicroseconds( unsigned long value ) { delayMicroseconds(value); } bool Behavior::ReadyToRun() const { return IsEnabled() && wake_up_time <= millis(); } unsigned long Behavior::GetId() const { return id; } const char* Behavior::GetName() const { return name; } unsigned Behavior::next_id; Behavior* Behavior::all_first; Behavior* Behavior::all_last; Behavior* Behavior::run_first; Behavior* Behavior::run_last; Behavior* Behavior::sleeping_first; Behavior* Behavior::sleeping_last; Behavior* Behavior::disabled_first; Behavior* Behavior::disabled_last; /* void loop() { Behavior::RunAllReady(); } */