Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Mode Class Referenceabstract

#include <Mode.h>

Inheritance diagram for Mode:
Inheritance graph

Public Types

enum  RunState {
  NotStarted, Started, Running, Finished,
  Stopped
}
 

Public Member Functions

 Mode ()
 
virtual void begin ()
 
virtual bool isFinished ()
 
void start ()
 
void stop ()
 
virtual void update ()
 
void setUpdatePeriod (uint16_t periodMs)
 

Protected Member Functions

virtual void modeStart ()
 
virtual void modeStop ()
 
virtual void modeUpdate ()=0
 

Protected Attributes

RunState _state
 
uint32_t _lastUpdateMs
 
uint16_t _updatePeriodMs
 

Detailed Description

Mode base class

This is the base class. We just ensure there are implementations of important methods in derived classes so we have a nice common interface...

To implement a mode:

  1. Create a class derived from Mode
  2. Implement modeUpdate() in your class to do mode-specific things
  3. Optionally over-ride begin(), modeStart() and modeStop()
  4. If you want periodic calls to modeUpdate(), call setUpdatePeriod() from your class constructor

To use a mode in a sketch:

  1. call begin() before use
  2. call start() when starting a mode (this will in turn call modeStart())
  3. call stop() when stopping a mode (this will in turn call modeStop())
  4. call update() frequently (this will in turn call modeUpdate())

A typical sketch will switch modes by calling a function like this:

Mode* CurrentMode;
...
void switchMode(Mode* newMode)
{
CurrentMode->stop();
CurrentMode = newMode;
CurrentMode->start();
}

Definition at line 39 of file Mode.h.

Constructor & Destructor Documentation

◆ Mode()

Mode::Mode ( )
inline

Constructor.

Definition at line 52 of file Mode.h.

Member Function Documentation

◆ begin()

virtual void Mode::begin ( )
inlinevirtual

Initialization.

Over-ride in derived classes if you need to do something from setup().

Definition at line 58 of file Mode.h.

◆ isFinished()

virtual bool Mode::isFinished ( )
inlinevirtual

Report mode self-termination.

Provide a mechanism for modes to tell the main loop they're finished - over-ride if desired.

Definition at line 64 of file Mode.h.

Here is the caller graph for this function:

◆ modeStart()

virtual void Mode::modeStart ( )
inlineprotectedvirtual

Mode start (or re-start).

Over-ride in derived classes - executed when mode is started.

Definition at line 108 of file Mode.h.

Here is the caller graph for this function:

◆ modeStop()

virtual void Mode::modeStop ( )
inlineprotectedvirtual

Mode stop.

Over-ride in derived classes - executed when mode is stopped.

Definition at line 114 of file Mode.h.

Here is the caller graph for this function:

◆ modeUpdate()

virtual void Mode::modeUpdate ( )
protectedpure virtual

Mode update.

Implement in derived classes - will be called from update(), but only if _updatePeriodMs has passed since the last invocation.

Here is the caller graph for this function:

◆ setUpdatePeriod()

void Mode::setUpdatePeriod ( uint16_t  periodMs)
inline

Set update period.

Parameters
periodMssets the minimum time between calls to modeUpdate() from update() in milliseconds.

Definition at line 101 of file Mode.h.

◆ start()

void Mode::start ( )
inline

To be called when switching to this mode

Definition at line 68 of file Mode.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void Mode::stop ( )
inline

To be called when switching to another mode

Definition at line 72 of file Mode.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update()

virtual void Mode::update ( )
inlinevirtual

Allocate timeslice.

This should be called frequently (typically from loop()). It will in turn call modeUpdate(), which must be implemented in all derived classes, and should contain user-code to be executed frequently.

It is not generally implemeneted in normal derived classes - modeUpdate() should be used instead for class secific updates. If it is, it should call Mode::update().

Reimplemented in BrownoutMode.

Definition at line 85 of file Mode.h.

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: