TCounter (Arduino Library)

When programming you will find some common functionalities you need over and over again. Well, this is all about reuseablility, software packaging,  Object Orientation etc. As this story has been discussed by many for years I stop now and dive into the actual subject of this post.

I have prepared a few Arduino C++/processing classes, packaged them as libraries and released them on github. In this and the following posts I will explore one after the other.

The first one is TCounter

A simple counter that can count up-down (e.g. 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3…) or in cycles (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, …). The counter has to be initialized with start value, counter step, lower boundary, higher boundary and counter type like follows:

TCounter counter = TCounter(start, step, lower, higher, TCounter:UPDOWN);

Then use the counter as follows:

counter.decrement(); // reduces the counter by step while maintaining the boundaries
counter.increment(); // adds the counter by step while maintaining the boundaries
counter.getSignal(); // returns >0 if the boundary was reached with the last step
counter.getValue() ; // returns the actual counter value

Here the source code which also available here

TCounter.h:


#ifndef _TCOUNTER_H_
#define _TCOUNTER_H_

class TCounter {

 public:
 // Counter types
 enum Mode_t {FIXED, UPDOWN, CYCLER};

 private:
 char _signal;
 int _value;
 int _step;
 int _min;
 int _max;
 Mode_t _mode;

 // Internal helper functions
 void doUpdate();
 void doUpDown();
 void doCycler();

 public:

 // Constructor with setup
 TCounter(int value, int step, int min, int max, Mode_t mode);

 // Setup the counter
 void init(int value, int step, int min, int max, Mode_t mode);

 // Get actual counter value
 int getValue();

 // Read signal - counter reached one limit
 char getSignal();

 // Override current counter value
 void setValue(int value);

 // Adjust counter steps
 void setStep(int value);

 // Adjust counter min
 void setMin(int value);

 // Adjust counter max
 void setMax(int value);

 // Increment counter
 char increment();

 // Decrement counter
 char decrement();
};

#endif

TCounter.cpp


#include "TCounter.h"

// ---------------------------------------------------------------------------
TCounter::TCounter(int value, int step, int minVal, int maxVal, Mode_t mode) {
 init(value, step, minVal, maxVal, mode);
}

// ---------------------------------------------------------------------------
void TCounter::init(int value, int step, int minVal, int maxVal, Mode_t mode) {
 _value = value;
 _step = step;
 _min = minVal;
 _max = maxVal;
 _mode = mode;
 _signal = 0;
};

// ---------------------------------------------------------------------------
int TCounter::getValue() {
 return _value;
};

// ---------------------------------------------------------------------------
char TCounter::getSignal() {
 return _signal;
};
// ---------------------------------------------------------------------------
void TCounter::setValue(int value) {
 _value = value;
};

// ---------------------------------------------------------------------------
void TCounter::setStep(int value) {
 _step = value;
}

// ---------------------------------------------------------------------------
void TCounter::setMin(int value) {
 _min = value;
}

// ---------------------------------------------------------------------------
void TCounter::setMax(int value) {
 _max = value;
}

// ---------------------------------------------------------------------------
char TCounter::increment() {
 doUpdate();
 return _signal;
}

// ---------------------------------------------------------------------------
char TCounter::decrement() {
 _step = -_step;
 doUpdate();
 _step = -_step;
 return _signal;
};

// ---------------------------------------------------------------------------
void TCounter::doUpdate() {

_signal = 0;

switch(_mode) {
 case TCounter::UPDOWN:
 doUpDown();
 break;

case TCounter::CYCLER:
 doCycler();
 break;

 default:
 break;
 }
};

// ---------------------------------------------------------------------------
void TCounter::doUpDown() {

 _value += _step;

 if ( _value > _max ) {
 _value = _max;
 _step = -_step;
 _signal = 1;
 }
 else if ( _value < _min ) {
 _value = _min;
 _step = -_step;
 _signal = 1;
 }
};

// ---------------------------------------------------------------------------
void TCounter::doCycler() {

 _value += _step;

if ( _value > _max ) {
 _value = _min;
 _signal = 1;
 }
 else if ( _value < _min ) {
 _value = _max;
 _signal = 1;
 }
};

Thats it.  The library is quite simple but I found it also quite handy for various purposes. If you grap the source from here you can put it into your library folder and use the bundled example to play around with it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s