Keyboard operation of C language Embedded system programming

Source: Internet
Author: User
Tags define valid win32

Processing function keys
The problem with the function key is that the user interface is not fixed, and the selection of the user function key will make the screen picture appear in a different display state. For example, the main screen is shown in Figure 1:


Figure 1 Main Screen


When the user presses the ENTER key on XX, the screen switches to the interface of setting XX, as shown in Figure 2:


Figure 2 Switch to set XX screen

How to judge which picture the user is in, and call the corresponding function key processing function under the program state of the picture, and ensure good structure, is a question worth thinking.
Let's look at the "window" concept used in WIN32 programming, when messages are sent to different windows, the message handler function (a callback function) of the window is eventually invoked, and in the message handler function of the window, The corresponding handler function in the window is also invoked based on the type of message. In this way, WIN32 effectively organizes different windows and handles messages in different windows.
What we have learned from this is:
(1) The different picture analogy is the different window in the WIN32, the various elements (menus, buttons, etc.) in the window are included in the window;
(2) provides a function key "message" processing function for each picture, the function receives the key information as the parameter;
(3) In each screen function key "message" processing function, judge the key type and the current focus element, and call the corresponding element key processing function.
/* Encapsulates the window element, message handler function in the window.
struct windows
{
BYTE Currentfocus;
ELEMENT Element[element_num];
void (*messagefun) (BYTE keyvalue);
...
};
/* Message processing function/*
void Messagefunction (BYTE keyvalue)
{
BYTE i = 0;
/* Get focus element * *
while (element [i].id!= currentfocus) && (i < element_num))
{
i++;
}
/* "Message map" * *
if (I < element_num)
{
Switch (keyvalue)
{
Case OK:
Element[i]. OnOK ();
Break
...
}
}
}


The process of calling the corresponding element key function in the message handler function of the window is similar to "message mapping", which we learned from WIN32 programming. Programming to a realm, a lot of things are interlinked. Other places of thought can be taken over for my use, is for programming "copycat".
In this example, if we want to play a little bit more, we can learn from MFC to deal with the Message_map method, we can also study MFC to define a few subtle macros to implement the "message map."

Handling number Keys
The user enters a number as a single input, and each input corresponds to a display position (x-coordinate, y-coordinate) on the screen. In addition, the program also needs to record the value entered at that location, so the best way to effectively organize the user's digital input is to define a struct and bundle the coordinates and values together:

/* User Digital input Structure * *
typedef struct TAGINPUTNUM
{
BYTE Bynum; /* Receive user input assignment * *
BYTE xpos; /* Digital input on the screen display position x-coordinate
BYTE YPos; /* Digital input on the screen display position y-coordinate *
}inputnum, *lpinputnum;

Then you can define an array of structures by receiving user input, and make a complete number with each of the members in the array:
Inputnum Inputelement[num_length]; /* Receive the user's digital input array * *
/* Digital KEY processing function * *
extern void Onnumkey (BYTE num)
{
if (num==0| | num==1)/* Receive only binary input/*
{
/* Display user input on the screen * *
DrawText (Inputelement[currentelementinputplace].xpos, Inputelement[currentelementinputplace].ypos, "%1d", num);
/* Assign input to array elements * *
Inputelement[currentelementinputplace].bynum = num;
/* Focus and cursor right/
Movetoright ();
}
}

After each digit input coordinates and the input value bundle, in the numerical key processing function may have the structure organization procedure, causes the program to appear very compact.
Organize user input
To continue with the example in section 2nd, in section 2nd of the Onnumkey function, just get each digit of the number, so we need to convert it into valid data, such as to convert to valid XXX data by:
/* Convert from 2 to valid data: XXX */
void Converttoxxx ()
{
BYTE i;
XXX = 0;
for (i = 0; i < num_length; i++)
{
XXX + = Inputelement[i].bynum*power (2, num_length-i-1);
}
}

Conversely, we may also need to display those valid data bits on the screen, because we also need to be able to reverse transform:
/* Convert from valid data to 2 data bits: XXX */
void Convertfromxxx ()
{
BYTE i;
XXX = 0;
for (i = 0; i < num_length; i++)
{
Inputelement[i].bynum = Xxx/power (2, num_length-i-1)% 2;
}
}

Of course in the above example, because the data is 2, using the Power function is not a good choice, the direct use of "<< >>" shift operation is more efficient, we are only to illustrate the convenience of the problem. Imagine that if the user input is decimal, the power function may be the only option.
Summarize
This article gives the keyboard operation involved in all aspects: Function key processing, digital key processing and user input collation, basically provides a full set of key processing scheme. For the function key processing method, the LCD screen is compared with the Windows window, and a new solution to the problem of screen and keyboard interaction is proposed.
Many of the knowledge of computer science are interlinked, so it is futile to keep chasing the fashion technology and ignore the basic skills. We need to be "proficient" in three languages (proficient, a word that is rampant in today's resume), the best partner is the assembly, C, C + + (or Java), obviously, if you are "proficient" in these three languages, other languages you should be able to quickly "familiar", otherwise you are not "proficient" them.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.