AceButton  1.0.0
An Adjustable Compact Event-driven (ACE) button library for Arduino.
EventTracker.h
1 /*
2  Copyright 2018 Brian T. Park
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef EVENT_TRACKER_H
18 #define EVENT_TRACKER_H
19 
20 namespace ace_button {
21 namespace testing {
22 
26 class EventRecord {
27  public:
28  EventRecord():
29  mEventType(0),
30  mButtonState(LOW) {}
31 
32  EventRecord(uint8_t eventType, uint8_t buttonState):
33  mEventType(eventType),
34  mButtonState(buttonState) {}
35 
36  uint8_t getEventType() {
37  return mEventType;
38  }
39 
40  uint8_t getButtonState() {
41  return mButtonState;
42  }
43 
44  private:
45  // Accept the default copy-constructor and assignment operator.
46  //EventRecord(const EventRecord&) = delete;
47  //EventRecord& operator=(const EventRecord&) = delete;
48 
49  uint8_t mEventType;
50  uint8_t mButtonState;
51 };
52 
57 class EventTracker {
58  public:
59 
60  EventTracker():
61  mNumEvents(0) {}
62 
64  void addEvent(uint8_t eventType, uint8_t buttonState) {
65  mRecords[mNumEvents] = EventRecord(eventType, buttonState);
66  mNumEvents++;
67  if (mNumEvents >= kMaxEvents) {
68  mNumEvents = 0;
69  }
70  }
71 
72  void clear() { mNumEvents = 0; }
73 
74  int getNumEvents() { return mNumEvents; }
75 
76  EventRecord& getRecord(int i) { return mRecords[i]; }
77 
78  private:
79  // Disable copy-constructor and assignment operator
80  EventTracker(const EventTracker&) = delete;
81  EventTracker& operator=(const EventTracker&) = delete;
82 
83  // Don't expect more than about 3. Set to 5 just in case.
84  // Use a circular buffer to prevent corrupting memory.
85  static const int kMaxEvents = 5;
86 
87  EventRecord mRecords[kMaxEvents];
88  int mNumEvents;
89 };
90 
91 }
92 }
93 #endif
Class that can receive and remember multiple calls to the eventHandler from AceButton.
Definition: EventTracker.h:57
A record of an AceButton event, for testing purposes.
Definition: EventTracker.h:26
Definition: AceButton.h:23
void addEvent(uint8_t eventType, uint8_t buttonState)
Add event to a circular buffer of records.
Definition: EventTracker.h:64