Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
Mode.h
1 #pragma once
2 
3 #include <Arduino.h>
4 #include <stdint.h>
5 #include "MutilaDebug.h"
6 #include "Millis.h"
7 
39 class Mode {
40 public:
41  enum RunState {
42  NotStarted,
43  Started,
44  Running,
45  Finished,
46  Stopped
47  };
48 
49 public:
52  Mode() : _state(NotStarted), _lastUpdateMs(0), _updatePeriodMs(0) {;}
53 
58  virtual void begin() {;}
59 
64  virtual bool isFinished() { return _state == Finished; }
65 
68  void start() { _state = Started; _lastUpdateMs = 0; modeStart(); }
69 
72  void stop() { modeStop(); _state = Stopped; }
73 
85  virtual void update() {
86  uint32_t now = Millis();
87  if (MillisSince(_lastUpdateMs, now) > _updatePeriodMs || _state == Started) {
88  modeUpdate();
89  _lastUpdateMs = now;
90  if (_state == Started) {
91  _state = Running;
92  }
93  }
94  }
95 
101  void setUpdatePeriod(uint16_t periodMs) { _updatePeriodMs = periodMs; }
102 
103 protected:
108  virtual void modeStart() {;}
109 
114  virtual void modeStop() {;}
115 
121  virtual void modeUpdate() = 0;
122 
123 protected:
124  RunState _state;
125  uint32_t _lastUpdateMs;
126  uint16_t _updatePeriodMs;
127 
128 };
129 
virtual void modeUpdate()=0
virtual void update()
Definition: Mode.h:85
Definition: Mode.h:39
void stop()
Definition: Mode.h:72
virtual void begin()
Definition: Mode.h:58
Mode()
Definition: Mode.h:52
void setUpdatePeriod(uint16_t periodMs)
Definition: Mode.h:101
virtual void modeStart()
Definition: Mode.h:108
virtual bool isFinished()
Definition: Mode.h:64
void start()
Definition: Mode.h:68
virtual void modeStop()
Definition: Mode.h:114