Accessories
Arduino for motors and lights library.
Accessory Class Referenceabstract

#include "Accessory.hpp"

Inheritance diagram for Accessory:
AccessoryLight AccessoryLightMulti AccessoryMotor AccessoryServo AccessoryStepper AccessoryMotorOneWay AccessoryMotorTwoWays

Public Member Functions

 Accessory ()
 
void begin (ACC_STATE inStartingState = STATE_NONE)
 
PortGetPort () const
 
ACC_STATE GetState () const
 
ACC_STATE GetPreviousState () const
 
bool IsNone () const
 
bool IsFirst () const
 
bool IsSecond () const
 
unsigned int GetDebounceDelay () const
 
unsigned long GetLastMoveTime () const
 
unsigned long GetDuration () const
 
bool loop ()
 
virtual void Event (unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
 
virtual int EEPROMSave (int inPos, bool inSimulate = false)
 
virtual int EEPROMLoad (int inPos)
 
uint8_t AddMovingPosition (unsigned long inId, int inPosition)
 
uint8_t AddMovingPositions (const MovingPosition *inMovingPositions_P)
 
void AdjustMovingPositionsSize (uint8_t inNewSize)
 
bool IsEmpty () const
 
uint8_t IndexOfMovingPositionById (unsigned long inId) const
 
int GetMovingPositionValueById (unsigned long inId) const
 
int GetMovingPositionValueByIndex (uint8_t inIndex) const
 
unsigned long GetMovingPositionIdByIndex (uint8_t inIndex) const
 
const uint8_t GetLastMovingPosition () const
 
const uint8_t GetMovingPositionSize () const
 
void SetLastMovingPosition (uint8_t inLastPositionIndex)
 
void SetDebounceDelay (unsigned int inDebounceDelay)
 
void SetLastMoveTime ()
 
void SetDuration (unsigned long inDuration)
 
void SetNextAccessory (Accessory *inAccessory)
 
Accessory * GetNextAccessory () const
 
unsigned long GetActionStartingMillis () const
 
virtual void StartAction ()
 
virtual void StartAction (ACC_STATE inState)
 
virtual void ResetAction ()
 
virtual bool ActionEnded ()
 
bool IsActionDelayPending () const
 
virtual bool IsGroupActionPending () const
 
virtual void Move (unsigned long inMovingPositionId) = 0
 
virtual ACC_STATE Toggle () = 0
 
virtual bool CanBePositional () const
 
virtual void MovePosition (int inPosition)
 
virtual void SetState (ACC_STATE inNewState)
 
void SetStateRaw (ACC_STATE inNewState)
 
void SetStartingMillis ()
 
void ResetStartingMillis ()
 

Static Public Member Functions

static void ExecuteEvent (unsigned long inId, ACCESSORIES_EVENT_TYPE inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID, int inData = 0)
 
static uint8_t GetCount ()
 
static bool IsActionPending ()
 
static Accessory * GetFirstAccessory ()
 

Protected Attributes

PortpPort
 
ACC_STATE prevState
 

Detailed Description

This is the base class for all the accessories.

This class is a pure virtual class, so it cant be instantiated !

An accessory is a class driving a physical motor or light, receiving events to change its state. With each accessory is defined a port, giving power to the accessory. There is no accessory without port.

Each accessory have one or more MovingPosition, with each one defined by an Id. Each accessory have also a current state, a current speed and can have a current position. All these states are saved in EEPROM at each state change, with a protection to avoid more than one saving by second.

The EEPROM area starting from EEPROMStart will be filled on EEPROMsize with the current version number (a byte), and the total number of accessories and groups. The fourth byte will be the checksum of the three starting bytes... If any of these four bytes are different from the actual values, the EEPROM will be considered as free. Two bytes are then added to store the size of one complete record, in order to be able to configure the CircularBuffer.

Following these bytes, there is the CircularBuffer of accessories. For each one the current state, the current position and the current speed will be saved. After that, each group save its current item id.

+-+
Version                 |V|
Accessory number        |A|
Group number            |G|
header checksum         |C|
size byte 1             |s1]
size byte 2             |s2]
+-+
Circular buffer         | |
+-+
End of file.

And the Circular buffer is :

+-+
Acc 1 : State           |S|
Acc 1 : Position        |P|
Acc 1 : Speed           |s|
+-+
Acc 2 : State           |S|
Acc 2 : Position        |P|
Acc 2 : Speed           |s|
+-+
Acc 3 : State           |S|
Acc 3 : Position        |P|
Acc 3 : Speed           |s|
...                     +-+
Group 1 : current ID    |I|
Group 3 : current ID    |I|
Group 3 : current ID    |I|
+-+
End of record.

Definition at line 141 of file Accessory.hpp.

Constructor & Destructor Documentation

Accessory::Accessory ( )

Default constructor.

Definition at line 15 of file Accessory.cpp.

Member Function Documentation

bool Accessory::ActionEnded ( )
virtual

Ends the current action of this accessory.

Returns
true if the action is not finished.
Remarks
Only for internal usage.

Reimplemented in AccessoryMotor.

Definition at line 192 of file Accessory.cpp.

uint8_t Accessory::AddMovingPosition ( unsigned long  inId,
int  inPosition 
)

Add one moving position to the accessory.

Parameters
[in]inIdnew Id
[in]inPositionPosition for this id.
Returns
index of the new position in the positions list.
Remarks
the total number of MovingPosition must be set before with AdjustMovingPositionsSize().

Definition at line 57 of file Accessory.cpp.

uint8_t Accessory::AddMovingPositions ( const MovingPosition inMovingPositions_P)

Add a list of moving positions declared with PROGMEM to the accessory.

Parameters
[in]inMovingPositions_Plist of MovingPosition defined with PROGMEM attribute.
Returns
number of positions in the list.

Definition at line 68 of file Accessory.cpp.

void Accessory::AdjustMovingPositionsSize ( uint8_t  inNewSize)

Sets the number of MovingPosition. If some MovingPosition was previously existing, they will be preserved.

Parameters
inNewSizenew size.

Definition at line 30 of file Accessory.cpp.

void Accessory::begin ( ACC_STATE  inStartingState = STATE_NONE)
inline

Initialize the instance.

Parameters
inStartingStateStarting state of the accessory. This is NOT an order to set the accessory in this state, only a way to take account of the current physical state of it !

Definition at line 200 of file Accessory.hpp.

virtual bool Accessory::CanBePositional ( ) const
inlinevirtual

Checks if the accessory can be moved by an absolute value instead of a fix position.

Returns
true if the accessory can be moved with move() function using an absolute value.

Reimplemented in AccessoryServo, and AccessoryStepper.

Definition at line 445 of file Accessory.hpp.

virtual int Accessory::EEPROMLoad ( int  inPos)
virtual

Reload all data from the EEPROM.

Remarks
Only for internal usage.

Reimplemented in AccessoryServo, AccessoryLight, and AccessoryMotor.

virtual int Accessory::EEPROMSave ( int  inPos,
bool  inSimulate = false 
)
virtual

Save all the data in the EEPROM.

Remarks
Only for internal usage.
virtual void Accessory::Event ( unsigned long  inId,
ACCESSORIES_EVENT_TYPE  inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID,
int  inData = 0 
)
inlinevirtual

Execute a new event.

Parameters
inIdId of an accessory or an accessory item.
inEventType of the new event. Default is ACCESSORIES_EVENT_MOVEPOSITIONID.
inDataAssociated data to the event type. Default is 0.

Reimplemented in AccessoryServo, AccessoryLightMulti, AccessoryLight, AccessoryStepper, and AccessoryMotor.

Definition at line 278 of file Accessory.hpp.

void Accessory::ExecuteEvent ( unsigned long  inId,
ACCESSORIES_EVENT_TYPE  inEvent = ACCESSORIES_EVENT_MOVEPOSITIONID,
int  inData = 0 
)
static

Execute the event.

Definition at line 461 of file Accessory.cpp.

unsigned long Accessory::GetActionStartingMillis ( ) const
inline

Gets the time of the starting of the current movement.

Returns
starting time of the current movement.
Remarks
Only for internal usage.

Definition at line 392 of file Accessory.hpp.

uint8_t Accessory::GetCount ( )
static

Gets the number of accessories.

Returns
total number of accessories.

Definition at line 293 of file Accessory.cpp.

unsigned int Accessory::GetDebounceDelay ( ) const
inline

Gets the debounce delay of the accessory, when relevant.

Returns
Previous debounce delay in milliseconds.

Definition at line 255 of file Accessory.hpp.

unsigned long Accessory::GetDuration ( ) const
inline

Gets the duration. Some accessories needs a duration: blinking light, step for servo motor...

Returns
duration for this accessory. 0 if no duration.

Definition at line 266 of file Accessory.hpp.

static Accessory* Accessory::GetFirstAccessory ( )
inlinestatic

Sets the first accessory address in the list.

Returns
address of the first accessory in the list.

Definition at line 381 of file Accessory.hpp.

unsigned long Accessory::GetLastMoveTime ( ) const
inline

Gets the time of the last change of this accessory. A move can be done only if the elapsed time since last move is bigger than 'debounce' delay.

Returns
last time of move in milliseconds.

Definition at line 261 of file Accessory.hpp.

const uint8_t Accessory::GetLastMovingPosition ( ) const
inline

Gets the last position reached by this accessory.

Returns
last position reached by the accessory, or 255 if it never moved.

Definition at line 344 of file Accessory.hpp.

unsigned long Accessory::GetMovingPositionIdByIndex ( uint8_t  inIndex) const
inline

Gets the id of the MovingPosition at the given index of the list.

Parameters
inIndexindex of the MovingPosition in the list.
Returns
Id of this position.

Definition at line 339 of file Accessory.hpp.

const uint8_t Accessory::GetMovingPositionSize ( ) const
inline

Gets the number of MovingPosition.

Returns
Number of MovingPosition set by AdjustMovingPositionsSize().

Definition at line 349 of file Accessory.hpp.

int Accessory::GetMovingPositionValueById ( unsigned long  inId) const
inline

Gets the position associated with the given Id.

Parameters
inIdid of the MovingPosition to find.
Returns
position or UNDEFINED_POS if not found..

Definition at line 327 of file Accessory.hpp.

int Accessory::GetMovingPositionValueByIndex ( uint8_t  inIndex) const
inline

Gets the position at the given index of the list.

Parameters
inIndexindex of the MovingPosition in the list.
Returns
position.

Definition at line 333 of file Accessory.hpp.

Accessory* Accessory::GetNextAccessory ( ) const
inline

Gets the next accessory in the list.

Returns
address of the next accessory in the list.

Definition at line 386 of file Accessory.hpp.

Port* Accessory::GetPort ( ) const
inline

Gets the associated port.

Returns
port address or NULL.

Definition at line 205 of file Accessory.hpp.

ACC_STATE Accessory::GetPreviousState ( ) const
inline

Gets the previous state of the accessory, when relevant.

Returns
Previous state.
Remarks
Only for internal usage.

Definition at line 235 of file Accessory.hpp.

ACC_STATE Accessory::GetState ( ) const
inline

Gets the current state of the accessory.

Returns
current state.

Definition at line 229 of file Accessory.hpp.

uint8_t Accessory::IndexOfMovingPositionById ( unsigned long  inId) const

Find the index of a MovingPosition from its Id.

Parameters
inIdid of the MovingPosition to find.
Returns
index of the found MovingPosition or 255 if not found.

Definition at line 94 of file Accessory.cpp.

bool Accessory::IsActionDelayPending ( ) const
inline

Checks if an action is still pending.

Returns
true if an action is not ended.
Remarks
Only for internal usage.

Definition at line 424 of file Accessory.hpp.

bool Accessory::IsActionPending ( )
static

Checks if there is at least one accessory which needs more loop() calls.

Returns
true if one accessory needs more 'loop()'.

Definition at line 320 of file Accessory.cpp.

bool Accessory::IsEmpty ( ) const
inline

Checks if at least one MovingPosition has been defined.

Returns
true if there is at least one MovingPosition defined.

Definition at line 315 of file Accessory.hpp.

bool Accessory::IsFirst ( ) const
inline

Checks if current state is STATE_FIRST.

Returns
true if the current state is STATE_FIRST.

Definition at line 245 of file Accessory.hpp.

virtual bool Accessory::IsGroupActionPending ( ) const
inlinevirtual

Checks if an action is still pending on a group.

Returns
true if an action is not ended or if all the items of the parent group have not been actioned.
Remarks
Only for internal usage.

Definition at line 430 of file Accessory.hpp.

bool Accessory::IsNone ( ) const
inline

Checks if current state is STATE_NONE.

Returns
true if the current state is STATE_NONE.

Definition at line 240 of file Accessory.hpp.

bool Accessory::IsSecond ( ) const
inline

Checks if current state is STATE_SECOND.

Returns
true if the current state is STATE_SECOND.

Definition at line 250 of file Accessory.hpp.

bool Accessory::loop ( )
inline

Main loop function.

Returns
true if something has been done.

Definition at line 271 of file Accessory.hpp.

virtual void Accessory::Move ( unsigned long  inMovingPositionId)
pure virtual

Basic moving function.

Parameters
[in]inMovingPositionIdnew position id from MovingPosition to reach.

Implemented AccessoryServo, AccessoryStepper, and AccessoryMotor.

virtual void Accessory::MovePosition ( int  inPosition)
inlinevirtual

Basic moving function for a positional motor.

Parameters
[in]inPositionnew position to reach.
Remarks
this function can be used only if CanBePositional() returns true.

Reimplemented in AccessoryServo, and AccessoryStepper.

Definition at line 451 of file Accessory.hpp.

void Accessory::ResetAction ( )
inlinevirtual

Prepare for a future action

Remarks
Only for internal usage.

Definition at line 411 of file Accessory.hpp.

void Accessory::ResetStartingMillis ( )
inline

Resets the starting time of the current action.

Remarks
Only for internal usage.

Definition at line 472 of file Accessory.hpp.

void Accessory::SetDebounceDelay ( unsigned int  inDebounceDelay)
inline

Sets the debounce delay. This delay is used to be sure that the current move is finished. A new move will only operate if this delay is expired.

Parameters
[in]inDebounceDelaydelay needed to finish an action (movement...) of the accessory.

Definition at line 360 of file Accessory.hpp.

void Accessory::SetDuration ( unsigned long  inDuration)
inline

Sets duration. This duration is used by lights for blinking, or by servos for the interval between elementary movements.

Parameters
[in]inDurationduration of the accessory.

Definition at line 370 of file Accessory.hpp.

void Accessory::SetLastMoveTime ( )
inline

Sets the last move time from the current time.

Remarks
Only for internal usage.

Definition at line 365 of file Accessory.hpp.

void Accessory::SetLastMovingPosition ( uint8_t  inLastPositionIndex)

Sets the last position reached by this accessory.

Parameters
[in]inLastPositionIndexlast position reached by the accessory.
Remarks
Only for internal usage.

Definition at line 215 of file Accessory.cpp.

void Accessory::SetNextAccessory ( Accessory *  inAccessory)
inline

Sets the next accessory in the list.

Parameters
[in]inAccessoryaddress of the next accessory in the list.
Remarks
Only for internal usage.

Definition at line 376 of file Accessory.hpp.

void Accessory::SetStartingMillis ( )
inline

Sets the starting time of a new action.

Remarks
Only for internal usage.

Definition at line 467 of file Accessory.hpp.

virtual void Accessory::SetState ( ACC_STATE  inNewState)
inlinevirtual

Change the state of the accessory. The accessory will physically move to reach the new state.

Parameters
inNewStatenew state to reach.

Reimplemented in AccessoryMotor, and AccessoryLight.

Definition at line 456 of file Accessory.hpp.

void Accessory::SetStateRaw ( ACC_STATE  inNewState)

Change the state of the accessory. The accessory will NOT move to reach the new state.

Parameters
inNewStatenew state to reach.
Remarks
Only for internal usage.

Definition at line 223 of file Accessory.cpp.

void Accessory::StartAction ( )
virtual

Starts a new action.

Remarks
Only for internal usage.

Definition at line 135 of file Accessory.cpp.

void Accessory::StartAction ( ACC_STATE  inState)
virtual

Starts a new action and sets the current state of this accessory.

Parameters
[in]inStatenew current state of this accessory.
Remarks
Only for internal usage.

Definition at line 148 of file Accessory.cpp.

virtual ACC_STATE Accessory::Toggle ( )
pure virtual

Basic function to toggle the accessory. A turnout will change its position, a light will change its state to on or off depending of its current state...

Returns
new state of the accessory.

Implemented AccessoryMotor, and AccessoryLight.

Member Data Documentation

Port* Accessory::pPort
protected

Port to drive the accessory. An accessory should always have a port, even if this is the Arduino itself...

Definition at line 187 of file Accessory.hpp.

ACC_STATE Accessory::prevState
protected

Previous state of the accessory.

Definition at line 190 of file Accessory.hpp.


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