Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

hideakitai/SceneManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deprecated

This library has integrated to TaskManager. Please use TaskManger insted.

SceneManager

cooperative multi-scene manager for Arduino

Usage

SceneManager can handle more flexible operation than TaskManager. TaskManager can only handle functions, but making scene classes can achieve more complex and flexible event handling. To use SceneManager, first define the scene class, and add it to SceneManager (Scenes) with framerate. Overrided update() function will be automatically called if Scenes.update() is called and the target frame interval has elapsed.

#include <SceneManager.h>

// make your scene class
class Blink : public Scene::Base
{
    bool b;

public:

    virtual ~Blink() {}

    Blink(const String& name, double fps)
    : Base(name, fps)
    , b(false)
    {
        pinMode(13, OUTPUT);
        digitalWrite(13, LOW);
    }

    virtual void update() override
    {
        digitalWrite(13, b);
        b = !b;
    }
};


void setup()
{
    Scenes.add<Blink>("blink", 1); // add scene to run in 1 [fps]
    Scenes.start();                // start scenes
}

void loop()
{
    Scenes.update(); // automatically Blink::update() is called in 1 [fps]
}

Scene Class Optional Methods

You can optionally define methods which is called only when specific timing. See handle_scene example for detail.

class EmptyScene : public Scene::Base
{
public:
    EmptyScene(const String& name, double fps)
    : Base(name, fps) {}
    virtual ~EmptyScene() {}

    virtual void begin() override {}  // optional
    virtual void enter() override {}  // optional
    virtual void update() override {} // must be implemented
    virtual void exit() override {}   // optional
    virtual void reset() override {}  // optional
};

APIs

SceneManager

template <typename SceneType>
Ref<SceneType> add(const String& name, const double fps);
void start();
void update();
void stop();
void restart();
void reset();
bool empty() const;
size_t size() const;
bool start(const String& name);
bool stop(const String& name);
bool restart(const String& name);
bool reset(const String& name);
bool isRunning(const String& name) const;
bool isNext(const String& name) const;
void erase(const String& name);
void setFrameRate(const String& name, float fps);
template <typename SceneType>
Ref<SceneType> getSceneByName(const String& name) const;
template <typename SceneType>
Ref<SceneType> operator[] (const String& name) const;
template <typename SceneType>
Ref<SceneType> operator[] (const size_t i) const;

Scene

virtual void begin();      // optional
virtual void enter();      // optional
virtual void update() = 0; // must be implemented
virtual void exit();       // optional
virtual void reset();      // optional

bool hasEnter() const;
bool hasExit() const;
const String& getName() const;

// defined on FrameRateCounter (derived)
void start();
void startFrom(const String& name, const double from_sec);
void stop();
bool play(const String& name);
void pause(const String& name);
void restart();
bool isRunning() const;
bool isPausing(const String& name) const;
bool isStopping(const String& name) const;
double usec();
double msec();
double sec();
double frame();
int64_t usec64(const String& name);
double usec(const String& name);
double msec(const String& name);
double sec(const String& name);
double getDuration(const String& name) const;
double getRemainingTime(const String& name);
double getRemainingLife(const String& name);
void setOffset(const String& name, const double sec);
void addOffset(const String& name, const double sec);
void setTime(const String& name, const double sec);
void setFrameRate(const double rate);
double getFrameRate() const;

Embedded Libraries

License

MIT

About

cooperative multi-scene manager for Arduino

Resources

License

Stars

Watchers

Forks

Packages

No packages published