Jeroen Hilgers / RepeatButton

Dependents:   Thermostat_NucleoF401

Embed: (wiki syntax)

« Back to documentation index

KeyBuffer Class Reference

KeyBuffer Class Reference

KeyBuffer and RepeatButton classes. More...

#include <RepeatButton.h>

Public Member Functions

 KeyBuffer (int size)
 Create a KeyBuffer object and initizalize it.
void Write (uint8_t value)
 Add a press or repeat to the buffer.
int Read ()
 Check if there is anything waiting in the buffer.

Detailed Description

KeyBuffer and RepeatButton classes.

The RepeatButton provides removal of contact bounce and repeat functionality. The repeat functionality works similar to that of a PC keyboard. If you press a key, it will register once. If you keep the key down, it will start repeating after a small delay.

The KeyBuffer class provides a key buffer functionality. It stores presses and repeats from one or more RepeatButton instances untill the main loop requests them.

Example:

 #include "mbed.h"
 #include "RepeatButton.h"

 Serial pc(USBTX, USBRX);

 #define REPEAT_DELAY 500
 #define REPEAT_PERIOD 100
 #define KEY_LEFT 1
 #define KEY_UP 2
 #define KEY_RIGHT 3
 #define KEY_DOWN 4
 #define KEY_PUSH 5
 
 KeyBuffer TheKeyBuffer(32);
 
 RepeatButton naviLeft(p5, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_LEFT);
 RepeatButton naviUp(p6, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_UP);
 RepeatButton naviRight(p7, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_RIGHT);
 RepeatButton naviDown(p9, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_DOWN);
 RepeatButton naviPush(p8, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_PUSH);
 
 DigitalOut led1(LED1);
 
 int main() 
 {
     int x=0, y=0, value;
     pc.baud(115200);
     pc.printf("Repeat key demo.\r\n");
     while(1) 
     {
         // Read TheKeyBuffer until empty.
         for(value = TheKeyBuffer.Read(); value != -1; value = TheKeyBuffer.Read())
         {
             switch(value)
             {
                 case KEY_LEFT:
                     x--;
                 break;
                 
                 case KEY_RIGHT:
                     x++;
                 break;
                 
                 case KEY_DOWN:
                     y--;
                 break;
                 
                 case KEY_UP:
                     y++;
                 break;
                 
                 case KEY_PUSH:
                     x = 0;
                     y = 0;
                 break;
             }
         }
         pc.printf("(x, y) = (%d, %d)\r\n", x, y);
         wait(0.05);
         led1 = led1^1;
     }
 }

Definition at line 107 of file RepeatButton.h.


Constructor & Destructor Documentation

KeyBuffer ( int  size )

Create a KeyBuffer object and initizalize it.

Parameters:
sizeNumber of button events that can be stored in the buffer.

Definition at line 26 of file RepeatButton.cpp.


Member Function Documentation

int Read (  )

Check if there is anything waiting in the buffer.

Returns:
value Returns -1 if the buffer is empty. Otherwise, the next value is returned and removed from the buffer.

Definition at line 57 of file RepeatButton.cpp.

void Write ( uint8_t  value )

Add a press or repeat to the buffer.

Parameters:
valueIdentifier for the key pressed.

Definition at line 43 of file RepeatButton.cpp.