Author Topic: Using a PC Keyboard to simulate buttons (I/O pins) on a Target  (Read 3965 times)

Offline mhoneywill

  • Full Member
  • ***
  • Posts: 173
    • View Profile
Hi,

I'm trying to use buttons on a PC Keyboard to simulate  buttons on my target device, making it easier to drive a user interface rather than clicking individual I/O pins with the mouse. I was planning to use the PC cursor keys to emulate a keypad, with other keys emulating an ok and esc button.

My thought was to add a variable KeyBoardCode into the WM_KEYDOWN event in WndProc() in WinSimMain.cpp, I could then examine this variable in my code to check for a keypress. I was planning to wrop this test so it was only compiled if running in simulation mode.

        case WM_KEYDOWN:
           KeyBoardCode = wParam;
           if (0x10 == wParam) {                                        // shift key down {11}
                iShiftPressed = 1;
            }
#ifdef SUPPORT_KEY_SCAN
            else if (VK_ESCAPE == wParam) {                              // ESC key pressed
                iInputChange = fnCheckKeypad(-1, -1, 0);
            }
#endif
            break;

I defined KeyBoardCode in application.c

    unsigned int KeyBoardCode;

And put an extern reference to it in application.h

    extern unsigned int KeyBoardCode;

When I compile this I get the error

    Error   1   error LNK2001: unresolved external symbol "unsigned int KeyBoardCode" (?KeyBoardCode@@3IA)   WinSimMain.obj   uTasker

Even though I know application.h is included via config.h in WinSimMain.cpp.

I'm not sure whats happening here, is it because I trying to include a variable defined in a C file in a C++ file?

Any help greatly appreciated

Cheers

Martin

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3091
    • View Profile
    • uTasker
Re: Using a PC Keyboard to simulate buttons (I/O pins) on a Target
« Reply #1 on: July 22, 2009, 01:29:24 PM »
Hi Martin

C and CPP are different "worlds" and routines need to be defined accordingly if they are to be accessed in the other 'world'.

For example, extern "C" void CollectCommand(bool bRS, unsigned long ulByte) allows a routine in a CPP file to be accessed by a C file.
The project uses this only in one or two places and possibly not the other way around, and also not with variables.

The "trick" used is to pass such information via a main() call - there are many examples of this in the project. This was no being clever but because originally I had no idea how to pass such things between the two worlds - I just realised that a main() call was the only thing that was understood by both so used this with parameters as required. This worked and has been used since; it could probably be reworked to be much better but it has been quite successful so no real urgency to fix something that isn't actually broken ;-)

If you don't find a better method just copy this style.

Regards

Mark

Offline mhoneywill

  • Full Member
  • ***
  • Posts: 173
    • View Profile
Re: Using a PC Keyboard to simulate buttons (I/O pins) on a Target
« Reply #2 on: July 22, 2009, 03:40:37 PM »
Thanks Mark,

Using your pointer I added an extra external definition to WinSimMain.cpp as shown below.

    extern "C" unsigned int KeyBoardCode;

This meant I could remove the line

    extern unsigned int KeyBoardCode;

From application.h as it was no longer needed, because the only external reference to KeyBoardCode is in WinSimMain.cpp.

Now it works as I hopped, KeyBoardCode contains a code representing the last key pressed, which my application can use when it is running in the Simulator.  ;D

For others who might be reading this post see this link it provides a bit more info

http://msdn.microsoft.com/en-us/library/0603949d%28VS.80%29.aspx

Cheers

Martin

« Last Edit: July 22, 2009, 04:54:18 PM by mhoneywill »