vail-adapter

Firmware for USB morse code key adapter
git clone https://git.woozle.org/neale/vail-adapter.git

Neale Pickett  ·  2020-05-16

bounce2.h

  1/*
  2  The MIT License (MIT)
  3
  4  Copyright (c) 2013 thomasfredericks
  5
  6  Permission is hereby granted, free of charge, to any person obtaining a copy of
  7  this software and associated documentation files (the "Software"), to deal in
  8  the Software without restriction, including without limitation the rights to
  9  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 10  the Software, and to permit persons to whom the Software is furnished to do so,
 11  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, FITNESS
 18  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 19  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 20  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 21  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 22*/
 23
 24/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 25  Main code by Thomas O Fredericks (tof@t-o-f.info)
 26  Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
 27  * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 28
 29/**
 30 * @todo Make Bounce2 more abstract. Split it from the hardware layer.
 31 * @body Remove deboucing code from Bounce2 and make a new Debounce class from that code. 
 32 * Bounce2 should extend Debounce. 
 33 */
 34
 35
 36#ifndef Bounce2_h
 37#define Bounce2_h
 38
 39#if defined(ARDUINO) && ARDUINO >= 100
 40#include "Arduino.h"
 41#else
 42#include "WProgram.h"
 43#endif
 44
 45// Uncomment the following line for "LOCK-OUT" debounce method
 46//#define BOUNCE_LOCK_OUT
 47
 48// Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method
 49//#define BOUNCE_WITH_PROMPT_DETECTION
 50
 51#include <inttypes.h>
 52
 53/**
 54    @example bounce.ino
 55    Simple example of the Bounce library that switches the debug LED when a button is pressed.
 56*/
 57
 58/**
 59    @example change.ino
 60    This example toggles the debug LED (pin 13) on or off when a button on pin 2 is pressed.
 61*/
 62
 63/**
 64    @example bounce_multiple.ino
 65    Detect the falling edge of multiple buttons. Eight buttons with internal pullups. Toggles a 
 66    LED when any button is pressed. Buttons on pins 2,3,4,5,6,7,8,9
 67*/
 68
 69/**
 70    @example bounce2buttons.ino
 71     Example of two instances of the Bounce class that switches the debug LED when either one of 
 72     the two buttons is pressed.
 73 */
 74
 75static const uint8_t DEBOUNCED_STATE = 0b00000001;
 76static const uint8_t UNSTABLE_STATE  = 0b00000010;
 77static const uint8_t CHANGED_STATE   = 0b00000100;
 78
 79/**
 80     The Bounce class.
 81     */
 82class Bounce
 83{
 84 public:
 85
 86/*!
 87    @brief  Create an instance of the Bounce class.
 88
 89    @code
 90
 91    // Create an instance of the Bounce class.
 92    Bounce() button;
 93
 94    @endcode
 95*/
 96    Bounce();
 97
 98
 99/*!
100    @brief  Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT).
101            
102    @param    pin
103              The pin that is to be debounced.
104    @param    mode
105              A valid Arduino pin mode (INPUT, INPUT_PULLUP or OUTPUT).
106*/
107    void attach(int pin, int mode);
108
109    /**
110    Attach to a pin for advanced users. Only attach the pin this way once you have previously 
111    set it up. Otherwise use attach(int pin, int mode).
112    */
113    void attach(int pin);
114    
115
116    /**
117    @brief  Sets the debounce interval in milliseconds.
118            
119    @param    interval_millis
120    		The interval time in milliseconds.
121     
122     */
123    void interval(uint16_t interval_millis);
124
125
126/*!
127    @brief   Updates the pin's state. 
128
129    Because Bounce does not use interrupts, you have to "update" the object before reading its 
130    value and it has to be done as often as possible (that means to include it in your loop()).
131    Only call update() once per loop().
132
133    @return True if the pin changed state.
134*/
135
136    bool update();
137
138    /**
139     @brief Returns the pin's state (HIGH or LOW).
140
141     @return HIGH or LOW.
142     */
143    bool read();
144
145    /**
146    @brief Returns true if pin signal transitions from high to low.
147    */
148    bool fell();
149
150    /**
151    @brief Returns true if pin signal transitions from low to high.
152    */
153    bool rose();
154
155     
156     /**
157    @brief Deprecated (i.e. do not use). Included for partial compatibility for programs written 
158    with Bounce version 1
159    */
160    bool risingEdge() { return rose(); }
161     /**
162    @brief Deprecated (i.e. do not use). Included for partial compatibility for programs written 
163    with Bounce version 1
164    */
165    bool fallingEdge() { return fell(); }
166     /**
167    @brief Deprecated (i.e. do not use). Included for partial compatibility for programs written 
168    with Bounce version 1
169    */
170     Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
171        attach(pin);
172        interval(interval_millis);
173    }
174    
175    /**
176     @brief Returns the duration in milliseconds of the current state. 
177
178     Is reset to 0 once the pin rises ( rose() ) or falls ( fell() ).
179    
180      @return The duration in milliseconds (unsigned long) of the current state.
181     */
182
183    unsigned long duration();
184
185  /**
186     @brief Returns the duration in milliseconds of the previous state. 
187
188     Takes the values of duration() once the pin changes state.
189    
190      @return The duration in milliseconds (unsigned long) of the previous state. 
191     */
192    unsigned long previousDuration();     
193
194 protected:
195    unsigned long previous_millis;
196    uint16_t interval_millis;
197    uint8_t state;
198    uint8_t pin;
199    unsigned long stateChangeLastTime;
200    unsigned long durationOfPreviousState;
201    virtual bool readCurrentState() { return digitalRead(pin); }
202    virtual void setPinMode(int pin, int mode) {
203#if defined(ARDUINO_ARCH_STM32F1)
204        pinMode(pin, (WiringPinMode)mode);
205#else
206        pinMode(pin, mode);
207#endif
208    }
209
210  private:
211    inline void changeState();
212    inline void setStateFlag(const uint8_t flag)    {state |= flag;}
213    inline void unsetStateFlag(const uint8_t flag)  {state &= ~flag;}
214    inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
215    inline bool getStateFlag(const uint8_t flag)    {return((state & flag) != 0);}
216 
217  public:
218    bool changed( ) { return getStateFlag(CHANGED_STATE); }
219
220};
221
222#endif