CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Multiple Keypress detection

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mikebruce



Joined: 28 Apr 2005
Posts: 3

View user's profile Send private message

Multiple Keypress detection
PostPosted: Wed Apr 12, 2006 8:50 am     Reply with quote

Hi All,

I am looking for some advice. I am requiring to control a device that can move forward/backwards as well as left/right. I am using an adapted version of the generic keyscan routine (as I have a 32 key, keypad) supplied with the CCS complier and it works fine, I now need to modify it to allow me to detect when I am pressing for example Forward and Right or Forward and Left etc and not allow for example both Forward and Backwards to be pressed at the same time.

Any advice would be greatly appreciated many thanks,

Mike Very Happy
KamPutty
Guest







PostPosted: Wed Apr 12, 2006 10:34 am     Reply with quote

Hi Mike,

When I want to detect keys, I handle it in 2 steps.

#1. The first step just is an ISR etc that just detects keys being pressed and updated a mask for each key.

#2. I have another routine (can be ISR, user controlled etc) that reads the keyboard mask that was updated by #1., and based on that I can apply rules. If the Up and Down are both pressed, do "this", else do "that". The ISR (#1) also detects the first key pressed, so I can determine if Up and Down where both pressed, which was really first.

Make sense?

By using masks, it's very easy to check for valid keys entries as all I am doing is checking against a valid key combo.

Here is what I have in my ISR...

Code:
   
// update buttons only after 'n' mseconds
    keyCounter++;
    if(keyCounter>INTS_PER_KEY_POLL)
    {
        keyCounter=0;

        // lets get the current state - remember to invert them
        buttons.physicalButtons.singleButton.button1=~input(BUTTON_1_PIN);
        buttons.physicalButtons.singleButton.button2=~input(BUTTON_2_PIN);
        buttons.physicalButtons.singleButton.button3=~input(BUTTON_3_PIN);
        buttons.physicalButtons.singleButton.button4=~input(BUTTON_4_PIN);
        buttons.physicalButtons.singleButton.button5=~input(BUTTON_5_PIN);
        buttons.physicalButtons.singleButton.button6=~input(BUTTON_6_PIN);


I check against 6 buttons.

When I wish to check for any buttons, I can do this

Code:

        // A?
        if(buttons.activeButtons.allButtons==BUTTON_A)
        {
            currentProfile=0;
            loadProfile(currentProfile);
            return YES;
        }
        // A+B?
        else if(buttons.activeButtons.allButtons==(BUTTON_A+BUTTON_B))
        {
            currentProfile=1;
            loadProfile(currentProfile);
            return YES;
        }


Here is the struct that I use...

Code:
struct Buttons
{
    // true active button state
    union
    {
        int8 allButtons;
        struct
        {
            int1    button1;        // true state of button 1
            int1    button2;        // true state of button 2
            int1    button3;        // true state of button 3
            int1    button4;        // true state of button 4
            int1    button5;        // true state of button 5
            int1    button6;        // true state of button 6
        } singleButton;
    } physicalButtons;

    // program active buttons, buttons we allow
    union
    {
        int8 allButtons;
        struct
        {
            int1    button1;        // true state of button 1
            int1    button2;        // true state of button 2
            int1    button3;        // true state of button 3
            int1    button4;        // true state of button 4
            int1    button5;        // true state of button 5
            int1    button6;        // true state of button 6
        } singleButton;
    } activeButtons;

    int8 firstButton;
};

struct Buttons buttons;


I have 2 parts to the struct, the actual values (this that are truely pressed), and then the settings of what I allow (this way I can select what to act on vs. what is physically pressed)

Hope it helps!

~Kam (^8*
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Apr 12, 2006 11:06 am     Reply with quote

I would tackle the problem differently.
Code:

123
456
789
*0#

I know this isn't your setup, But you will get the idea.
Just define another key for valid options.
I would think of it like a game controler.
1==Forward, Left
2==Forward
3==Forward, Right
6==Right
9==Backword, Right ...

This way you don't have to detect the multi-press
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Apr 12, 2006 11:53 am     Reply with quote

Since you have 4 switches that represent direction, there are a possible 16 combinations. Only 8 of these are correct, 8 are invalid. I would create an enum of all 16 values (17 if you count no keypress). Let's say the four keys were:
-1-
2-3
-4-

Therefore:
Up would be 0b00000001 - d'1
Left would be 0b00000010 - d'2
Right would be 0b00000100 - d'4
Down would be 0b00001000 - d'8

Up-Left would be 0b00000011 - d'3
Up-Right would be 0b00000101 - d'5
Down-Right would be 0b00001100 - d'12
Down-Left would be 0b00001010 - d'10

so

Code:
enum Keys
{
  KEY_NONE,
  KEY_UP,
  KEY_LEFT,
  KEY_UP_LEFT,
  KEY_RIGHT,
  KEY_UP_RIGHT,
  KEY_INVALID1,
  KEY_INVALID2,
  KEY_DOWN,
  KEY_INVALID3,
  KEY_DOWN_LEFT,
  KEY_INVALID4,
  KEY_DOWN_RIGHT,
  KEY_INVALID5,
  KEY_INVALID6,
  KEY_INVALID7,
  KEY_INVALID8,
};


Now you can just read the value of those 4 switches and process them with a switch statement.

Note that depending on how your switches are connected, your results will be different.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group