From df6411c76b00863499d693e9a1fe2b590216d3fe Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Sat, 29 Apr 2023 10:09:20 +0300 Subject: [PATCH] manager: [#25] add Counter template for set_point --- source/shoh/src/threads/manager/Counter.h | 102 ++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 source/shoh/src/threads/manager/Counter.h diff --git a/source/shoh/src/threads/manager/Counter.h b/source/shoh/src/threads/manager/Counter.h new file mode 100644 index 0000000..8efb2fc --- /dev/null +++ b/source/shoh/src/threads/manager/Counter.h @@ -0,0 +1,102 @@ +/* + * Counter.h + * + * Created on: Sep 1, 2022 + * Author: tylen + */ + +#ifndef COUNTER_H_ +#define COUNTER_H_ + +#include "chip.h" + +template class Counter +{ + +public: + Counter (CounterDataType lower_boundary, CounterDataType upper_boundary, + CounterDataType step); + void inc (); + void dec (); + CounterDataType getCurrent (); + void setCurrent (CounterDataType num); + ~Counter () = default; + +private: + CounterDataType init; + CounterDataType up_lim; + CounterDataType down_lim; + CounterDataType _step; +}; + + +template +void +Counter::inc () +{ + if ((init + _step) <= up_lim) + { + init += _step; + } +} + +template +void +Counter::dec () +{ + if (init - _step >= down_lim) + { + init -= _step; + } +} + +template +CounterDataType +Counter::getCurrent () +{ + return this->init; +} + +template +Counter::Counter (CounterDataType lower_boundary, + CounterDataType upper_boundary, + CounterDataType step) +{ + up_lim = upper_boundary; + down_lim = lower_boundary; + _step = step; + if (down_lim > up_lim) + { + init = up_lim; + } + else if (down_lim < 0) + { + init = 0; + } + else + { + init = down_lim; + } +} + +template +void +Counter::setCurrent (CounterDataType num) +{ + if (num > up_lim) + { + init = up_lim; + } + else if (num < down_lim) + { + init = down_lim; + } + else + { + init = num; + } +} + + + +#endif /* COUNTER_H_ */ \ No newline at end of file