Skip to content
Snippets Groups Projects
Behaviours.h 2.05 KiB
Newer Older
Dave Murray-Rust's avatar
Dave Murray-Rust committed
#ifndef BEHAVIOUR_h
#define BEHAVIOUR_h

class Behaviour {
protected:
  boolean _interruptable = true;
  boolean _temp = false;
  boolean _priority = false;
  boolean _running = false;
Joe Revans's avatar
Joe Revans committed
  boolean _background = false;
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  String _name = "name";

public:
  Behaviour(String name) : _name(name) {};
  ~Behaviour() {};

  //Can this behaviour be interruped
  virtual boolean is_interruptable() { return _interruptable; };
  //Can this behaviour be run quickly without stopping what's going on (e.g. comms, debug)
  virtual boolean is_temp() { return _temp; };
  //Should this behaviour override others
Joe Revans's avatar
Joe Revans committed
  virtual boolean is_priority() { return _priority; };
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  //Is the behaviour running
Joe Revans's avatar
Joe Revans committed
  virtual boolean is_running() { return _running; };
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  //What's the name of this behaviour
Joe Revans's avatar
Joe Revans committed
  virtual boolean is_background() { return _background; };
  //What's the name of this behaviour
  virtual String name() { return _name; };
  //What arguments does the behaviour take? Override this to document your behaviour
Joe Revans's avatar
Joe Revans committed
  virtual char* args() { return "null"; };
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  //Start the behaviour, with arguments (don't know why this can't be virtual?)
Joe Revans's avatar
Joe Revans committed
  virtual String start(String args) { Serial.println("Base start called <"+args+">"); };
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  //Update the behaviour periodically
Joe Revans's avatar
Joe Revans committed
  virtual void update() { };
Dave Murray-Rust's avatar
Dave Murray-Rust committed
  //Start the behaviour, with arguments (don't know why this can't be virtual?)
  virtual void stop() { _running = false; };
};

/*
 * Example way to make a simple behaviour
 */
class TestBehaviour : public Behaviour {
public:
  TestBehaviour(String n) : Behaviour(n)  {}
  String start(String args) {
    return "Test behaviour " + _name + " with (" + args + ")";
  }
};


class BehaviourTable {
  Behaviour* behaviours[40];

public:
  int num = 0;

Joe Revans's avatar
Joe Revans committed
  BehaviourTable() {}

Dave Murray-Rust's avatar
Dave Murray-Rust committed
  void add(Behaviour *b) {
    behaviours[num] = b;
    num++;
  }

  Behaviour* get(String n) {
    for( int i = 0; i < num; i++ ) {
      if( behaviours[i]->name() == n) { return behaviours[i]; }
    }
    return nullptr;
  }

  Behaviour* get_by_num(int n) {
    return behaviours[n];
  }

  int get_num_behaviours() {
    return num;
  }
Dave Murray-Rust's avatar
Dave Murray-Rust committed
};

#endif