C language Embedded System Programming Practice 5: keyboard operations

Source: Internet
Author: User
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 in a different display status. For example, Master Screen 1:


Figure 1 Main Screen

After the user presses enter on set XX, the screen is switched to the set XX interface, 2:


Figure 2 switch to set xx

It is worth thinking about how the program judges which screen the user is on, and calls the corresponding function key processing function in the program state of the screen, and ensures a good structure.

Let's take a look at the "window" concept used in Win32 programming. When a message is sent to different windows, the message processing function of the window (a callback function) finally called, and in the message processing function of the window, the corresponding processing function in the window is called according to the Message type. In this way, Win32 effectively organizes different windows and processes messages in different windows.

What we learned from this is:

(1) compare different pictures to different windows in Win32, and include various elements (menus, buttons, etc.) in the window;

(2) provide a function key "message" processing function for each screen. The function receives the key information as a parameter;

(3) In the function key "message" processing function of each screen, judge the key type and current focus element, and call the key processing function of the corresponding element.

/* Encapsulate the window elements and message processing functions 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;
/* Obtain the focus element */
While (element [I]. ID! = Currentfocus) & (I <element_num ))
{
I ++;
}
/* "Message ing "*/
If (I <element_num)
{
Switch (keyValue)
{
Case OK:
Element [I]. onok ();
Break;
...
}
}
}

The process of calling the corresponding element key function in the message processing function in the window is similar to "message ing", which we learned from Win32 programming. Programming has reached a realm where many things are connected. The ideas from other places can be taken for my use, and they are for the "tailism" in programming ".

In this example, if we want to do more, we can use the message_map Processing Method in MFC. We can also learn how to define several exquisite Macros in MFC to implement "message ing ".
Processing numeric keys

When a user inputs a number, it is input by one digit. Each digit corresponds to a display position (X and Y coordinates) on the screen ). In addition, the program also needs to record the input value at this position. Therefore, the best way to effectively organize user digital input is to define a struct and bind coordinates and values together:

/* User number input struct */
Typedef struct taginputnum
{
Byte Bynum;/* receive user input values */
Byte xpos;/* The position X coordinate of the number input on the screen */
Byte ypos;/* The Y coordinate of the displayed position of the number input on the screen */
} Inputnum, * lpinputnum;

Then a struct array can be defined for receiving user input, and a complete number can be formed using all the members in the array:

Inputnum inputelement [num_length];/* array for receiving user numbers */
/* Numeric buttons */
Extern void onnumkey (byte num)
{
If (num = 0 | num = 1)/* only receives binary input */
{
/* Display User input on the screen */
Drawtext (inputelement [currentelementinputplace]. xpos, inputelement [currentelementinputplace]. ypos, "% 1D", num );
/* Assign the input value to the array element */
Inputelement [currentelementinputplace]. Bynum = num;
/* Shift the focus and cursor right */
Movetoright ();
}
}

After the coordinates of each digit input and the input value are bound, the program can be more structured in the number key processing function, making the program very compact.

  Sort user input

For example in section 2nd, the onnumkey function in section 2nd only obtains each digit of a number. Therefore, we need to convert it into valid data, for example, to convert to valid XXX data, the method is:

/* Convert the binary data bit 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 );
}
}

On the other hand, we may also need to display the valid data bits on the screen, because we also need to be able to reverse convert:

/* Convert valid data to binary data bit: 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 in binary format, it is not a good choice to use power functions. It is more efficient to directly use "<>" shift operations, we just want to illustrate the convenience of the problem. If the user input is in decimal format, the power function may be the only choice.

  Summary

This article provides a complete set of key processing solutions for keyboard operations: function key processing, digital key processing, and user input sorting. For the function key processing method, compare the LCD screen with the Windows window, and propose a novel solution to the complex interaction problem between the screen and keyboard.

Many of the knowledge of computer science is similar. Therefore, it is futile to catch up with fashionable technologies and ignore basic skills. We need at most three languages of "proficient" (proficient, a word that is prevalent in today's job resumes). The best partner is compilation, C, C ++ (or Java ), obviously, if you are "proficient" in these three languages, you should be "familiar" with other languages quickly; otherwise, you will not be "proficient.

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.