标签:
一.手工编写按钮事件
使用PIO11 PIO12来分别模拟ButtonA和ButtonB,则相应的按钮事件和处理程序为:
#include <pio.h> #include <stdio.h> #include <message.h> #define BUTTON_A (1 << 11) /* PIO11 is BUTTON_A */ #define BUTTON_B (1 << 12) /* PIO12 is BUTTON_B */ typedef struct { TaskData task; /* task is required for messages to be delivered */ } appState; appState app; /* Function prototypes */ static void app_handler(Task task, MessageId id, Message message); static void handle_pio(Task task, MessagePioChanged *pio); int main(void) { /* Set app_handler() function to handle app‘s messages */ app.task.handler = app_handler; /* Set app task to receive PIO messages */ MessagePioTask(&app.task); /* Setup PIO interrupt messages */ PioDebounce32(BUTTON_A | BUTTON_B, /* PIO pins we are interested in */ 2, 20); /* 2 reads and 20ms between them */ MessageLoop(); return 0; } static void app_handler(Task task, MessageId id, Message message) { switch (id) { case MESSAGE_PIO_CHANGED: handle_pio(task, (MessagePioChanged*)message); break; default: printf("Unhandled message 0x%x\n", id); } } static void handle_pio(Task task, MessagePioChanged *pio) { if (pio->state & BUTTON_A) printf("Button A pressed\n"); if (pio->state & BUTTON_B) printf("Button B pressed\n"); }
将程序下载到板子上,按目标板上的“PLAY”和“VOL-”按钮,则会出现对应Button A Pressed和Button B Pressed。
二.自动生成按钮事件
使用CSR自带的buttonparsepro.exe程序将sample.button文件转换成对应的sample.c和sample.h文件,然后主程序引用这两个文件即可。
首先我们编写sample.button文件:
// // Setup debouncing // // The format for PIO debouncing is // "debounce" count period // where // "debounce" is text debounce as is // count is the number of times to read from the pins // period is the delay between reads // Do two reads 20 ms between them debounce 2 20 // // Define PIO pins that are used // // The format for defining PIO pins is // "pio" pin name // where // "pio" is text pio as is // pin is pin number, for example 7 for PIO7 // name is pin name, for example BUTTON1 // Use name BUTTON_A for PIO11 pio 11 BUTTON_A // Use name BUTTON_B for PIO12 pio 12 BUTTON_B // // Define messages for PIO events // // The format for defining PIO events is // "message" name // message_parameters* // where // "message" is text message as is // name is name of the message, for example BUTTON_PRESSED // message_parameters // are parameters defining when message is send // Send message A_PRESS when BUTTON_A is pressed message A_PRESS BUTTON_A enter // Send message B_RELEASE when BUTTON_B is released message B_RELEASE BUTTON_B release // Send message A_DOUBLE when BUTTON_A is double-pressed within half a sec message A_DOUBLE BUTTON_A double 500
该文件就是描述PIO11和PIO12分别对应BUTTON_A的A_PRESS事件/A_DOUBLE事件和BUTTON_B的B_RELEASE事件。
然后我们使用buttonparsepro.exe程序来解析该文件,命令为:
buttonparsepro sample.button sample
命令运行后,会提示成功与否,如果成功,则会生成sample.c和sample.h两个文件,将这两个文件拷贝到主程序中,并在主程序中将sample.h包含进去,即可使用对应的按钮事件消息。引用sample.h的程序为:
#include <pio.h> #include <stdio.h> #include <message.h> #include "sample.h" typedef struct { TaskData task; /* task is required for messages to be delivered */ PioState pio; /* PIO state used by buttonparse */ } appState; appState app; /* Function prototypes */ static void app_handler(Task task, MessageId id, Message message); int main(void) { /* Set app_handler() function to handle app‘s messages */ app.task.handler = app_handler; /* Set app task to receive button messages */ pioInit(&app.pio, &app.task); MessageLoop(); return 0; } static void app_handler(Task task, MessageId id, Message message) { switch (id) { case A_PRESS: printf("Button A pressed\n"); break; case B_RELEASE: printf("Button B released\n"); break; case A_DOUBLE: printf("Button A double-pressed\n"); break; default: printf("Unhandled message 0x%x\n", id); } } /* End-of-File */
运行程序,按目标板上的“PLAY”和“VOL-”按钮,则会出现对应Button A Pressed和Button B Released事件。
标签:
原文地址:http://www.cnblogs.com/fingertouch/p/4588173.html