PServo v0.2.1
Loading...
Searching...
No Matches
PServo.h
1#pragma once
2
13namespace ps {
39enum class State : unsigned char {
40 STANDBY,
42 HALT,
43 IN_ACTION,
44 PAUSED,
48};
49
57namespace Default {
58unsigned char constexpr MIN = 0;
59unsigned char constexpr MAX = 180;
60unsigned char constexpr DELAY = 1;
61}; // namespace Default
62
87typedef struct Props {
89 unsigned long pc;
90 unsigned long *const timer;
91 unsigned char const min;
92 unsigned char const max;
93 bool const is_resetable;
94 unsigned char curr_action;
95 unsigned char active_action;
96 unsigned char actions_count;
97 unsigned char pos;
98 unsigned short delay;
100
143class PServo {
144public:
182 PServo(unsigned long *const timer) : _timer(timer) {}
183
197 PServo(unsigned long *const timer, bool const is_resetable)
198 : _timer(timer), _is_resetable(is_resetable) {}
199
217 PServo(unsigned long *const timer, unsigned char const min,
218 unsigned char const max)
219 : _timer(timer), _min(min), _max(max) {}
220
239 PServo(unsigned long *const timer, unsigned char const min,
240 unsigned char const max, bool const is_resetable)
241 : _timer(timer), _min(min), _max(max), _is_resetable(is_resetable) {}
242
278 PServo *begin(void);
279
298 PServo *move(unsigned char const next_pos);
299
325 PServo *move(unsigned char const next_pos, unsigned short const delay);
326
336 Props const props(void) const;
337
345 State const get_state(void) const;
346
354 bool is_state(State s) const;
355
364 unsigned char pos(void) const;
365
397 void reset(void);
398
399private:
400 State _state = State::STANDBY;
401
402 unsigned long _pc = 0;
403 unsigned long *const _timer = nullptr;
404
405 unsigned char _min = Default::MIN;
406 unsigned char _max = Default::MAX;
407 bool _is_resetable = false;
408
409 unsigned char _curr_action = 0;
410 unsigned char _active_action = 0;
411 unsigned char _actions_count = 0;
412 unsigned char _pos = 0;
413 unsigned short _delay = Default::DELAY;
414
415 inline void _reset_active_action_to_start_again(void);
416 inline void _reset_or_update_and_start_next_action(void);
417};
418
438char const *state_text(State s);
439}; // namespace ps
Definition PServo.h:143
PServo(unsigned long *const timer, unsigned char const min, unsigned char const max, bool const is_resetable)
Definition PServo.h:239
Props const props(void) const
Definition PServo.cpp:122
void reset(void)
Definition PServo.cpp:140
unsigned char pos(void) const
Definition PServo.cpp:153
PServo(unsigned long *const timer)
Definition PServo.h:182
PServo * move(unsigned char const next_pos)
Definition PServo.cpp:116
bool is_state(State s) const
Definition PServo.cpp:151
PServo(unsigned long *const timer, bool const is_resetable)
Definition PServo.h:197
State const get_state(void) const
Definition PServo.cpp:149
PServo * begin(void)
Definition PServo.cpp:3
PServo(unsigned long *const timer, unsigned char const min, unsigned char const max)
Definition PServo.h:217
unsigned char constexpr MIN
Minimal default degree position.
Definition PServo.h:58
unsigned char constexpr MAX
Maximum default degree position.
Definition PServo.h:59
unsigned char constexpr DELAY
Default delay between movement updates.
Definition PServo.h:60
Definition PServo.h:13
struct ps::Props Props
char const * state_text(State s)
Definition PServo.cpp:155
State
Definition PServo.h:39
@ ERROR_NOACTION
Any actions was registered since being() (NOOP).
@ ERROR_UNEXPECTED
An unexpected state appeard somewhere (NOOP).
@ ERROR_TIMERPTR
The timer pointer was not defined properly (NOOP).
@ IN_ACTION
Will keep updating the servo's position.
@ PAUSED
No operation, but keep updating the _pc var (NOOP).
@ HALT
No operation, the final action was completed (NOOP).
@ INITIALIZED
Measn that the actions was counted, so start it!
@ STANDBY
Will count each action on this state.
Definition PServo.h:87
unsigned char active_action
Which action is, actually, performing.
Definition PServo.h:95
unsigned char const max
Maximum position that this machine can be.
Definition PServo.h:92
unsigned char const min
Minimal position that this machine can be.
Definition PServo.h:91
bool const is_resetable
Will the machine reset after it's halted?
Definition PServo.h:93
unsigned char curr_action
Says wich action it's trying to perform.
Definition PServo.h:94
unsigned long pc
Last registered process counter.
Definition PServo.h:89
unsigned char pos
Current servo position, will not be written.
Definition PServo.h:97
State state
Current state of the ps::PServo machine.
Definition PServo.h:88
unsigned char actions_count
How much actions was registred.
Definition PServo.h:96
unsigned long *const timer
Pointer to the timer variable in use.
Definition PServo.h:90
unsigned short delay
Delay stored for the current action move.
Definition PServo.h:98