acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

EventTracker.h (2811B)

      1 /*
      2 MIT License
      3 
      4 Copyright (c) 2018 Brian T. Park
      5 
      6 Permission is hereby granted, free of charge, to any person obtaining a copy
      7 of this software and associated documentation files (the "Software"), to deal
      8 in the Software without restriction, including without limitation the rights
      9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10 copies of the Software, and to permit persons to whom the Software is
     11 furnished to do so, subject to the following conditions:
     12 
     13 The above copyright notice and this permission notice shall be included in all
     14 copies or substantial portions of the Software.
     15 
     16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22 SOFTWARE.
     23 */
     24 
     25 #ifndef ACE_BUTTON_EVENT_TRACKER_H
     26 #define ACE_BUTTON_EVENT_TRACKER_H
     27 
     28 namespace ace_button {
     29 namespace testing {
     30 
     31 /**
     32  * A record of an AceButton event, for testing purposes.
     33  */
     34 class EventRecord {
     35   public:
     36     EventRecord():
     37         mEventType(0),
     38         mButtonState(LOW) {}
     39 
     40     EventRecord(uint8_t eventType, uint8_t buttonState):
     41         mEventType(eventType),
     42         mButtonState(buttonState) {}
     43 
     44     uint8_t getEventType() {
     45       return mEventType;
     46     }
     47 
     48     uint8_t getButtonState() {
     49       return mButtonState;
     50     }
     51 
     52   private:
     53     // Accept the default copy-constructor and assignment operator.
     54     //EventRecord(const EventRecord&) = delete;
     55     //EventRecord& operator=(const EventRecord&) = delete;
     56 
     57     uint8_t mEventType;
     58     uint8_t mButtonState;
     59 };
     60 
     61 /**
     62  * Class that can receive and remember multiple calls to the eventHandler from
     63  * AceButton.
     64  */
     65 class EventTracker {
     66   public:
     67 
     68     EventTracker():
     69         mNumEvents(0) {}
     70       
     71     /** Add event to a buffer of records, stopping when the buffer fills up. */
     72     void addEvent(uint8_t eventType, uint8_t buttonState) {
     73       if (mNumEvents < kMaxEvents) {
     74         mRecords[mNumEvents] = EventRecord(eventType, buttonState);
     75         mNumEvents++;
     76       }
     77     }
     78 
     79     void clear() { mNumEvents = 0; }
     80 
     81     int getNumEvents() { return mNumEvents; }
     82 
     83     EventRecord& getRecord(int i) { return mRecords[i]; }
     84 
     85   private:
     86     // Disable copy-constructor and assignment operator
     87     EventTracker(const EventTracker&) = delete;
     88     EventTracker& operator=(const EventTracker&) = delete;
     89 
     90     // Don't expect more than about 3. Set to 5 just in case.
     91     static const int kMaxEvents = 5;
     92 
     93     EventRecord mRecords[kMaxEvents];
     94     int mNumEvents;
     95 };
     96 
     97 }
     98 }
     99 #endif