Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
02/28/06 04:19
Read: times


 
#110897 - Buttons library
Responding to: ???'s previous message
Thank you for your attention.

I don't know how to attached files in this forum so I paste the files contents here. They are a bit long.

This is a library to query button pressed, use in conjunction of my custom-made RTOS. The real situation is not so complicated. Let me explain briefly before you read on.

I wish users of this library need no touches to buttons.h and buttons.c, no matter how many buttons used. All necessary modifications are put into btnConfig.h

-You can regard osWaitTask() equivalent to delay().
-osThisTask is a gobal variable which indicate the current running task.
-scanKey() is function to record valid button pressed
-keyOff() is to check a button is pressed or not currently

Please focus on keyOff() function (in buttons.c). Currently I use #if to enclose each case statment. It works but I wonder if there are better ways to do the job.
All comments or suggestions are welcome.

Here are the files:
(copy and paste to your editor may be better for reaing)

===========================================================
// File name: btnsConfig.h
// Date: Feb 2006
// Description : buttons configuration file, see button.h for functions available
//-------------------------------------------------------------------------


#ifndef ButtonConfig
#define ButtonConfig

// change according to actual task order
#define MaxKeys 5 // total numbers of buttons managed by this module
#define KeyFirstTask 3 // the first getKey() task ID

// map sybolic key names defined in hardward.h to names (Btn1 to Btn12) used in this library
#define DecKey Btn1
#define IncKey Btn2
#define LeftKey Btn3
#define RightKey Btn4
#define ReturnKey Btn5
//#define AnyKey Btn6


// choose one or none of the following key property, affect all buttons
// none means key property is momentary
//#define KeyToggle // toggle on / off on each key pressed, user can clear or set pressed state
#define KeyLatch // latch key pressed, user to clear pressed state

// choose when to record keyPressed
#define KeyMake // record key pressed when contact make
//#define KeyBreak // record key pressed when contact break


// normally no need to change these parameter
#define KeyScan 60 // scan key interval (ticks)
#define KeyDebounce 30 // debounce time (ticks)

#endif


===========================================================
// File name: buttons.h
// Date: Feb 2006
// Description : header file for scan buttons routines
//-------------------------------------------------------------------------

// This library support separate buttons connected diretly to port pins,
// numbers and wiring of buttons to be stated - MaxKeys and key1 to keyN in hardware.h
// use osCreateTask(queryKeyState) to create one task for each button
// tasks for buttons must be declare consecutively,
// the first task ID must be stated - KeyFirstTask in btnconfig.h

// buttons managed under same module have common properties -
// - momentary, latch or toggle, debounce time etc
// copy and modify another set of files (btnsConfig.h, buttons.h, buttons.c) for keys
// with different properties

// use the simplified module set (btnConfig.h, button.h, button.c) to manage single key

#ifndef ButtonHeader
#define ButtonHeader


// ================= General includes =============
#include "hardware.h"
#include "btnConfig.h" // button options
#include "Cos51.h"

// copy these definitions to hardware.h
// interface for push buttons and leds, change according to actual wiring
// sbit key1 = P1.0;
// sbit key2 = P1.1;
// sbit key3 = P1.2;
// sbit key4 = P1.3;
// sbit key5 = P1.4;


// ================= function prototypes =============
uint08 getKey(uint08 keyID);
// get the state of a button, user should query state of buttons with this function only
// keyID = 0 to MaxKeys, no checking for values; use constants defined (Key1 to Key12)
// return state of key (0 or 1) (latched state if KeyToggle is defined)
// no impact on stack

void clearKey(uint08 keyID);
// clear the state of a button, available only if key proporty is latch or toggle
// keyID = 0 to MaxKeys, no checking for values; use constants defined (Btn1 to Btn12)
// no impact on stack

void setKey(uint08 keyID);
// set the state of a button, available only if key property is toggle
// keyID = 0 to MaxKeys, no checking for values; use constants defined (Key1 to Key12)
// no impact on stack

task scanKey(void); // task, scan and process soft key
// prototype of task to monitor buttons, use only as parameter in osCreateTask()
// create one task per button, in consecutive order and
// define the ID of first one on TaskGetKey in file btnConfig.h
// add 4 to stack


// constants for keyID, no need to change
#define Btn1 0
#define Btn2 1
#define Btn3 2
#define Btn4 3
#define Btn5 4
#define Btn6 5
#define Btn7 6
#define Btn8 7
#define Btn9 8
#define Btn10 9
#define Btn11 10
#define Btn12 11


#endif


=========================================================
// File name : buttons.c
// Date : Feb, 2006
// Description: Routines to scan buttons status for multiple buttons
// take cares of debounce
//---------------------------------------------------------------------------


#include "hardware.h" // hardware configuration
#include "COS51.h" // OS support
#include "buttons.h"
#include "btnconfig.h"


// ======== local function prototypes =============
// get the logic level of key
static uint08 keyOff(keyID); // detect key pressed / released
// keyID = 0 to MaxKeys, no checking of value; use constansts defined (Btn1 to Btn12)
// return 0 or 1

// gobal variables, access within this file only, MaxKeys defined in hardware.h
static uint08 keyState[MaxKeys]; // valid key state (deboouced)
#if ((defined KeyToggle) || (defined KeyLatch))
static uint08 keyPressed[MaxKeys]; // latched or toggled key state
#endif

// ======= task, monitor key press, run every (KeyScan) ticks ============
// no need to modify this function even number of keys changed
task scanKey(void) {
while (1) {
// read key
if(!keyOff(osThisTask-KeyFirstTask)) { // key pressed
osWaitTick(KeyDebounce); // debounce
if(!keyOff(osThisTask-KeyFirstTask)) { // valid key
keyState[osThisTask-KeyFirstTask] = 1;
#ifdef KeyMake // record key pressed at contact make
#ifdef KeyToggle // key with toggle property
keyPressed[osThisTask-KeyFirstTask] = ~keyPressed[osThisTask-KeyFirstTask];
#endif
#ifdef KeyLatch // key with latch property
keyPressed[osThisTask-KeyFirstTask] = 1;
#endif
#endif
}
}
osWaitTick(KeyScan); // rescan key at KeyScan interval
// wait key relese
while (keyState[osThisTask-KeyFirstTask]) {
osWaitTick(KeyScan); // wait for a while
if (keyOff(osThisTask-KeyFirstTask)) { // key released
osWaitTick(KeyDebounce); // debounce
if (keyOff(osThisTask-KeyFirstTask)) { // confirmed release
keyState[osThisTask-KeyFirstTask] = 0;
#ifdef KeyBreak // record key state on contact break
#ifdef KeyToggle
keyPressed[osThisTask-KeyFirstTask] = ~keyPressed[osThisTask-KeyFirstTask];
#endif
#ifdef KeyLatch
keyPressed[osThisTask-KeyFirstTask] = 1;
#endif
#endif
}
} // end if keyOff
osWaitTick(KeyScan);
} // end while keyState
} // end while 1
} // end getKey


// detect current key pressed or not, keyID is current task ID
// modify this function to accummodate more than 12 keys
static uint08 keyOff(keyID) {
uint08 state;
switch (keyID) {
case Btn1:
state = key1;
break;

#if MaxKeys>1
case Btn2:
state = key2;
break;
#endif

#if MaxKeys>2
case Btn3:
state = key3;
break;
#endif

#if MaxKeys>3
case Btn4:
state = key4;
break;
#endif

#if MaxKeys>4
case Btn5:
state = key5;
break;
#endif

#if MaxKeys>5
case Btn6:
state = key6;
break;
#endif

#if MaxKeys>6
case Btn7:
state = key57
break;
#endif

#if MaxKeys>7
case Btn8:
state = key8;
break;
#endif

#if MaxKeys>8
case Btn9:
state = key9;
break;
#endif

#if MaxKeys>9
case Btn10:
state = key10;
break;
#endif

#if MaxKeys>10
case Btn11:
state = key11;
break;
#endif

#if MaxKeys>11
case Btn12:
state = key12;
break;
#endif

// add more cases for more keys here
default:
state = 1;
break;
}
return state;
}

// access the state of buttons
uint08 getkey(uint08 keyID) {
#if ((defined KeyToggle) || (defined KeyLatch))
return keyPressed[keyID];
#else
return keyState[keyID];
#endif
}

#if ((defined KeyToggle) || (defined KeyLatch))
// clear key status
void clearKey(uint08 keyID) {
keyPressed[keyID] = 0;
}
#endif

#ifdef KeyToggle
// set key status
void setKey(uint08 keyID) {
keyPressed[keyID] = 1;
}
#endif

=========================================================

List of 12 messages in thread
TopicAuthorDate
question on keil C sbit access            01/01/70 00:00      
   What are you trying to do?            01/01/70 00:00      
      Try or doing            01/01/70 00:00      
         sbit access            01/01/70 00:00      
            The 8051 does not have indirect bits            01/01/70 00:00      
            send me keilc code for keypad            01/01/70 00:00      
               Yes sir! Right away sir!            01/01/70 00:00      
                  How about Fries?            01/01/70 00:00      
   please tell more            01/01/70 00:00      
   Buttons library            01/01/70 00:00      
      how to post code            01/01/70 00:00      
      The main question            01/01/70 00:00      

Back to Subject List