TDuino
Convenient functionality for Arduino
Public Member Functions
TTimelineT< DATATYPE > Class Template Reference

Used to track actions on a virtual time line and map them to a value. More...

#include <TTimelineT.h>

Public Member Functions

 TTimelineT (void(*callback)(byte, DATATYPE), byte numSlots=1)
 The constructor for a TTimelineT. More...
 
void setMinMax (DATATYPE minValue, DATATYPE maxValue)
 Set the minimum and maximum values for progress. More...
 
virtual void loop ()
 The TTimelineT's loop phase. More...
 
int firstActive ()
 Find index of the first active slot. More...
 
int firstInactive ()
 Find index of the first inactive slot. More...
 
byte getSize ()
 Get number of timeline slots. More...
 
char hasOverlap (byte index1, byte index2, bool remove=false)
 Check if two slots has an overlap. More...
 
bool isActive (byte index)
 Check if a slot is active. More...
 
bool isStarted (byte index)
 Check if a slot is started. More...
 
void restart (byte index)
 Used to restart a slot. More...
 
void restartAll ()
 Used to restart all slots. More...
 
void set (byte index, unsigned long duration, unsigned long startAfter=0)
 Set a slot in the time line. More...
 
void stop (byte index)
 Stop a slot. More...
 
void stopAll ()
 Stop all slots. More...
 
- Public Member Functions inherited from TTimeline
 TTimeline (void(*callback)(byte, float), byte numSlots=1)
 The constructor for a TTimeline. More...
 
 ~TTimeline ()
 The destuctor for a TTimeline. More...
 
int firstActive ()
 Find index of the first active slot. More...
 
int firstInactive ()
 Find index of the first inactive slot. More...
 
byte getSize ()
 Get number of timeline slots. More...
 
char hasOverlap (byte index1, byte index2, bool remove=false)
 Check if two slots has an overlap. More...
 
bool isActive (byte index)
 Check if a slot is active. More...
 
bool isStarted (byte index)
 Check if a slot is started. More...
 
void restart (byte index)
 Used to restart a slot. More...
 
void restartAll ()
 Used to restart all slots. More...
 
void set (byte index, unsigned long duration, unsigned long startAfter=0)
 Set a slot in the time line. More...
 
void stop (byte index)
 Stop a slot. More...
 
void stopAll ()
 Stop all slots. More...
 
- Public Member Functions inherited from TBase
 TBase ()
 Constructor for class TBase. More...
 
virtual ~TBase ()
 Destructor for class TBase. More...
 

Additional Inherited Members

- Protected Member Functions inherited from TTimeline
bool badIndex (byte i, const char *token)
 
void checkMemError (const char *token)
 
- Protected Member Functions inherited from TBase
virtual void defaults ()
 Method used to reset all class internal variables. More...
 
- Protected Attributes inherited from TTimeline
const byte numSlots = TDUINO_TIMELINE_SIZE
 
TTIMELINE_SLOT slots [TDUINO_TIMELINE_SIZE]
 
byte numSlots
 
TTIMELINE_SLOT * slots
 
TTIMELINE_SLOT * current
 
byte dummy
 
byte memError
 
- Protected Attributes inherited from TBase
unsigned long loopMillis
 The value of millis() to be used within loop()
 

Detailed Description

template<class DATATYPE>
class TTimelineT< DATATYPE >

Used to track actions on a virtual time line and map them to a value.

TTimelineT is usefull when you need to have sort of a virtual time line where different things are happening gradually in a period of time which can be postponed.

The difference between TTimeline and TTimelineT is that TTimelineT is a template class which allows you to map any progress to a certain data type. Since no floating point operations are used in TTimelineT it may in most cases be more efficient that the TTimeline class.

void TimelineCallback(byte slot, <DATATYPE> value)
{
// "slot" is the index of the slot in the time line.
// value is mapped progress in the wanted data type.
}
TTimelineT<byte> tline(TimelineCallback, 2); //Two slots
//Set the mapped value to be between 0 and 255
tline.setMinMax(0, 255);
//Set first slot to start after 5 seconds with a duration of 1 second
tline.set(0, 1000, 5000);
//Set the second slot to start after 10 seconds with a duration of 5 seconds
tline.set(1, 5000, 10000);
See also
TTimeline

Constructor & Destructor Documentation

◆ TTimelineT()

template<class DATATYPE >
TTimelineT< DATATYPE >::TTimelineT ( void(*)(byte, DATATYPE)  callback,
byte  numSlots = 1 
)

The constructor for a TTimelineT.

See TTimeline::TTimeline for more information.

Member Function Documentation

◆ firstActive()

template<class DATATYPE>
int TTimeline::firstActive

Find index of the first active slot.

Returns
The index of the first active slot.

If no slots are active, -1 will be returned.

◆ firstInactive()

template<class DATATYPE>
int TTimeline::firstInactive

Find index of the first inactive slot.

Returns
The index of the first inactive slot.

If no slots are inactive, -1 will be returned.

◆ getSize()

template<class DATATYPE>
byte TTimeline::getSize

Get number of timeline slots.

Returns
The number of timeline slots.

Used to get the number of timeline slots used with this instance of TTimeline.

See also
TTimeline()

◆ hasOverlap()

template<class DATATYPE>
char TTimeline::hasOverlap

Check if two slots has an overlap.

Parameters
index1The index of the first slot to check.
index2The index of the second slot to check.
removeIf true, the overlapping slot will be postponet until the overlap is removed.
Returns
0 if there is no overlap, 1 if index1 overlaps index2 and 2 if index2 overlaps index1. If the return value is negative, the operation could not be performed due to a 32 bit rollover in the slot. Eg. if the return value is -2 the slot at index2 causes a rollover.

This method is used to examine wheter or not the actual transition of two slots are overlapping in a way that they may interfere with eachother. The overlapping slot is the slot that starts later and if both slots starts at the same time, the first slot will be returned.

TTimeline tl(tlHandler, 2);
tl.set(0, 1000, 500);
tl.set(1, 1000, 1000);
byte ol = tl.hasOverlap(0, 1, true);

In the example above, "ol" will be 2 because the second slot will start when the first slot is half done. Since "remove" is set to true, the second slot will be postponet with an additional 500 ms and this means that the code above will do exactely the same as the next example:

TTimeline tl(tlHandler, 2);
tl.set(0, 1000, 500);
tl.set(1, 1000, 1500);

NOTE: By default hasOverlap() uses 32 bit integers for calculation and this will only work if the time line does not exceed ~24 days (or ~35 seconds if using micros() for timing) from start to end. This behaviour may be tweaked, read more about it in Tweaking TDuino.

◆ isActive()

template<class DATATYPE>
bool TTimeline::isActive

Check if a slot is active.

Parameters
indexThe index of the slot to check.
Returns
true if the slot is active.

Used to check if a slot is active or postponed (waiting to become active).

◆ isStarted()

template<class DATATYPE>
bool TTimeline::isStarted

Check if a slot is started.

Parameters
indexThe index of the slot to check.
Returns
true if the slot is started.

Used to check if a slot is started (transition in progress).

◆ loop()

template<class DATATYPE >
void TTimelineT< DATATYPE >::loop ( )
virtual

The TTimelineT's loop phase.

Must be called for each loop in the sketch.

See also
TTimeline::loop()

Reimplemented from TTimeline.

◆ restart()

template<class DATATYPE>
void TTimeline::restart

Used to restart a slot.

Parameters
indexThe index of the slot to restart.

This method will restart the slot at index with the values previously set with set().

◆ restartAll()

template<class DATATYPE>
void TTimeline::restartAll

Used to restart all slots.

This method will restart all slots in the time line.

◆ set()

template<class DATATYPE>
void TTimeline::set

Set a slot in the time line.

Parameters
indexIndex of the slot to set.
durationThe duration of the slot.
startAfterThe amount of time (milliseconds) to wait before starting the slot.

Sets the slot at index to start after startAfter milliseconds with duration milliseconds. By default startAfter is set to 0, which means that the slot will start emmediately.

If duration is zero, the slot will act as a one-shot timer.

◆ setMinMax()

template<class DATATYPE >
void TTimelineT< DATATYPE >::setMinMax ( DATATYPE  minValue,
DATATYPE  maxValue 
)

Set the minimum and maximum values for progress.

Parameters
minValueThe minimum progress value.
maxValueThe maximum progress.

Must be called to set the range in which the progress should be mapped. The arguments must be within the template data types's boundaries, eg. a "byte" must use values between 0 and 255, a "int" must be between −32767 and 32767 and so on.

The default values for the mapping is 0..255 for single byte data types and 0..1023 for everything else.

◆ stop()

template<class DATATYPE>
void TTimeline::stop

Stop a slot.

Parameters
indexIndex of the slot to stop.

This will deactivate the slot at the specified index.

◆ stopAll()

template<class DATATYPE>
void TTimeline::stopAll

Stop all slots.

This will deactivate all slots in the time line.