This library improves behaviour of push buttons. The automatic repeat is very convenient for e.g. entering time with up/down buttons. The library buffers presses, so no presses are missed.

Dependents:   Thermostat_NucleoF401

Committer:
hilgo
Date:
Sun Feb 20 18:47:18 2011 +0000
Revision:
0:8be79829ce90
Initial revision, tested with the example provided.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hilgo 0:8be79829ce90 1 /* mbed RepeatButton Library
hilgo 0:8be79829ce90 2 * Copyright (c) 2011 Jeroen Hilgers
hilgo 0:8be79829ce90 3 *
hilgo 0:8be79829ce90 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
hilgo 0:8be79829ce90 5 * of this software and associated documentation files (the "Software"), to deal
hilgo 0:8be79829ce90 6 * in the Software without restriction, including without limitation the rights
hilgo 0:8be79829ce90 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hilgo 0:8be79829ce90 8 * copies of the Software, and to permit persons to whom the Software is
hilgo 0:8be79829ce90 9 * furnished to do so, subject to the following conditions:
hilgo 0:8be79829ce90 10 *
hilgo 0:8be79829ce90 11 * The above copyright notice and this permission notice shall be included in
hilgo 0:8be79829ce90 12 * all copies or substantial portions of the Software.
hilgo 0:8be79829ce90 13 *
hilgo 0:8be79829ce90 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hilgo 0:8be79829ce90 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hilgo 0:8be79829ce90 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hilgo 0:8be79829ce90 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hilgo 0:8be79829ce90 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hilgo 0:8be79829ce90 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hilgo 0:8be79829ce90 20 * THE SOFTWARE.
hilgo 0:8be79829ce90 21 */
hilgo 0:8be79829ce90 22
hilgo 0:8be79829ce90 23 #include "mbed.h"
hilgo 0:8be79829ce90 24 #include "RepeatButton.h"
hilgo 0:8be79829ce90 25
hilgo 0:8be79829ce90 26 KeyBuffer::KeyBuffer(int size)
hilgo 0:8be79829ce90 27 {
hilgo 0:8be79829ce90 28 // One byte cannot be used; if it were
hilgo 0:8be79829ce90 29 // used, the buffer empty condition
hilgo 0:8be79829ce90 30 // mReadIndex == mWriteIndex would apply.
hilgo 0:8be79829ce90 31 // So one byte extra is allocated.
hilgo 0:8be79829ce90 32 mBuffer = new uint8_t[size+1];
hilgo 0:8be79829ce90 33 mSize = size+1;
hilgo 0:8be79829ce90 34 mReadIndex = 0;
hilgo 0:8be79829ce90 35 mWriteIndex = 0;
hilgo 0:8be79829ce90 36 }
hilgo 0:8be79829ce90 37
hilgo 0:8be79829ce90 38 KeyBuffer::~KeyBuffer()
hilgo 0:8be79829ce90 39 {
hilgo 0:8be79829ce90 40 delete mBuffer;
hilgo 0:8be79829ce90 41 }
hilgo 0:8be79829ce90 42
hilgo 0:8be79829ce90 43 void KeyBuffer::Write(uint8_t value)
hilgo 0:8be79829ce90 44 {
hilgo 0:8be79829ce90 45 int nextIndex = mWriteIndex + 1;
hilgo 0:8be79829ce90 46 if(nextIndex >= mSize)
hilgo 0:8be79829ce90 47 nextIndex = 0;
hilgo 0:8be79829ce90 48 if(nextIndex == mReadIndex)
hilgo 0:8be79829ce90 49 {
hilgo 0:8be79829ce90 50 // Buffer full!
hilgo 0:8be79829ce90 51 return;
hilgo 0:8be79829ce90 52 }
hilgo 0:8be79829ce90 53 mBuffer[mWriteIndex] = value;
hilgo 0:8be79829ce90 54 mWriteIndex = nextIndex;
hilgo 0:8be79829ce90 55 }
hilgo 0:8be79829ce90 56
hilgo 0:8be79829ce90 57 int KeyBuffer::Read()
hilgo 0:8be79829ce90 58 {
hilgo 0:8be79829ce90 59 if(mReadIndex == mWriteIndex)
hilgo 0:8be79829ce90 60 {
hilgo 0:8be79829ce90 61 // Buffer empty!
hilgo 0:8be79829ce90 62 return -1;
hilgo 0:8be79829ce90 63 }
hilgo 0:8be79829ce90 64 int value = mBuffer[mReadIndex];
hilgo 0:8be79829ce90 65 int nextIndex = mReadIndex + 1;
hilgo 0:8be79829ce90 66 if(nextIndex >= mSize)
hilgo 0:8be79829ce90 67 nextIndex = 0;
hilgo 0:8be79829ce90 68 mReadIndex = nextIndex;
hilgo 0:8be79829ce90 69 return value;
hilgo 0:8be79829ce90 70 }
hilgo 0:8be79829ce90 71
hilgo 0:8be79829ce90 72
hilgo 0:8be79829ce90 73 RepeatButton::RepeatButton(PinName pin, int delay_ms, int period_ms, KeyBuffer *buf, char value) :
hilgo 0:8be79829ce90 74 mIn(pin),
hilgo 0:8be79829ce90 75 mIrq(pin)
hilgo 0:8be79829ce90 76 {
hilgo 0:8be79829ce90 77 mIn.mode(PullUp);
hilgo 0:8be79829ce90 78 mValue = value;
hilgo 0:8be79829ce90 79 mDelay = delay_ms * 1000;
hilgo 0:8be79829ce90 80 mPeriod = period_ms * 1000;
hilgo 0:8be79829ce90 81 mIrq.rise(this, &RepeatButton::OnChange);
hilgo 0:8be79829ce90 82 mIrq.fall(this, &RepeatButton::OnChange);
hilgo 0:8be79829ce90 83 mTarget = buf;
hilgo 0:8be79829ce90 84 }
hilgo 0:8be79829ce90 85
hilgo 0:8be79829ce90 86 void RepeatButton::OnChange()
hilgo 0:8be79829ce90 87 {
hilgo 0:8be79829ce90 88 // Any change on input. Wait 5ms to get rid of bounce and get reading afterwards.
hilgo 0:8be79829ce90 89 mTimeout.attach_us(this, &RepeatButton::OnBounce, 5000);
hilgo 0:8be79829ce90 90 }
hilgo 0:8be79829ce90 91
hilgo 0:8be79829ce90 92 void RepeatButton::OnBounce()
hilgo 0:8be79829ce90 93 {
hilgo 0:8be79829ce90 94 if(mIn == 0)
hilgo 0:8be79829ce90 95 {
hilgo 0:8be79829ce90 96 // Key is still pressed. Generate first press.
hilgo 0:8be79829ce90 97 mTimeout.attach_us(this, &RepeatButton::OnRepeat, mDelay);
hilgo 0:8be79829ce90 98 mTarget->Write(mValue);
hilgo 0:8be79829ce90 99 }
hilgo 0:8be79829ce90 100 }
hilgo 0:8be79829ce90 101
hilgo 0:8be79829ce90 102 void RepeatButton::OnRepeat()
hilgo 0:8be79829ce90 103 {
hilgo 0:8be79829ce90 104 if(mIn == 0)
hilgo 0:8be79829ce90 105 {
hilgo 0:8be79829ce90 106 // Key is still pressed. Generate repeated press.
hilgo 0:8be79829ce90 107 mTimeout.attach_us(this, &RepeatButton::OnRepeat, mPeriod);
hilgo 0:8be79829ce90 108 mTarget->Write(mValue);
hilgo 0:8be79829ce90 109 }
hilgo 0:8be79829ce90 110 }