Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
Heartbeat.cpp
1 #include <Arduino.h>
2 #include "MutilaDebug.h"
3 #include "Heartbeat.h"
4 #include "Millis.h"
5 
6 Heartbeat::Heartbeat(const uint8_t pin, const bool invertedLogic) :
7  _mode(Heartbeat::Normal),
8  _pin(pin),
9  _invertedLogic(invertedLogic),
10  _pinState(true),
11  _lastStateFlip(0),
12  _onTime(0),
13  _offTime(0),
14  _enabled(true)
15 {
16  setMode(_mode);
17 }
18 
19 Heartbeat::~Heartbeat()
20 {
21 }
22 
24 {
25  pinMode(_pin, OUTPUT);
26 }
27 
29 {
30  return _mode;
31 }
32 
34 {
35  _mode = mode;
36  switch (_mode) {
37  case Normal:
38  _onTime = NormalOnMs;
39  _offTime = NormalOffMs;
40  break;
41  case Quick:
42  _onTime = QuickOnMs;
43  _offTime = QuickOffMs;
44  break;
45  case Quicker:
46  _onTime = QuickerOnMs;
47  _offTime = QuickerOffMs;
48  break;
49  case Slow:
50  _onTime = SlowOnMs;
51  _offTime = SlowOffMs;
52  break;
53  case Slower:
54  _onTime = SlowerOnMs;
55  _offTime = SlowerOffMs;
56  break;
57  case Off:
58  _onTime = 0;
59  _offTime = 1;
60  break;
61  case On:
62  _onTime = 1;
63  _offTime = 0;
64  break;
65  default:
66  break;
67  }
68 
69 }
70 
72  if (times.mode == Custom) {
73  setCustomMode(times.onTime, times.offTime);
74  } else {
75  setMode(times.mode);
76  }
77 }
78 
79 void Heartbeat::setCustomMode(uint16_t onTime, uint16_t offTime)
80 {
81  _onTime = onTime;
82  _offTime = offTime;
83  _mode = Custom;
84 }
85 
87 {
88  if (!_enabled) {
89  return;
90  }
91  uint32_t wait = _pinState ? _onTime : _offTime;
92  if (_onTime == 0) {
93  if (_pinState) {
94  updatePin(0);
95  }
96  return;
97  } else if (_offTime == 0) {
98  if (!_pinState) {
99  updatePin(1);
100  }
101  return;
102  } else if (MillisSince(_lastStateFlip) >= wait) {
103  updatePin(!_pinState);
104  }
105 }
106 
107 void Heartbeat::updatePin(bool state)
108 {
109  _pinState = state;
110  digitalWrite(_pin, _invertedLogic ? !_pinState : _pinState);
111  _lastStateFlip = Millis();
112 }
113 
115 {
116  Times t;
117  t.mode = _mode;
118  t.onTime = _onTime;
119  t.offTime = _offTime;
120  return t;
121 }
122 
124 {
125  _enabled = on;
126  if (!_enabled) {
127  updatePin(false);
128  }
129 }
130 
void setCustomMode(uint16_t onTime, uint16_t offTime)
Definition: Heartbeat.cpp:79
Mode mode()
Definition: Heartbeat.cpp:28
void update()
Definition: Heartbeat.cpp:86
Definition: Mode.h:39
void setEnabled(bool on=true)
Definition: Heartbeat.cpp:123
Heartbeat(const uint8_t pin, const bool invertedLogic=false)
Definition: Heartbeat.cpp:6
blinks every 1000ms
Definition: Heartbeat.h:31
blinks every 50ms
Definition: Heartbeat.h:30
LED constant on.
Definition: Heartbeat.h:34
void setMode(Mode mode)
Definition: Heartbeat.cpp:33
void begin()
Definition: Heartbeat.cpp:23
LED constant off.
Definition: Heartbeat.h:33
blinks every 500ms
Definition: Heartbeat.h:28
blinks every 2000ms
Definition: Heartbeat.h:32
blinks every 150ms
Definition: Heartbeat.h:29
Times times()
Definition: Heartbeat.cpp:114
Indicates custom on/off times (set with setCustomMode)
Definition: Heartbeat.h:35