www.pudn.com > buttonTest.rar > buttonTest3.c, change:2012-11-09,size:3030b


/* Copyright (C) Cambridge Silicon Radio Limited 2008-2012 */ 
/* Part of BlueLab 6.2.0.64 */ 
 
/* 
 * Buttons Tutorial: Part 1 - PIO messages 
 */ 
 
#include <pio.h> 
#include <stdio.h> 
#include <message.h> 
#include <stdlib.h> 
 
#define BUTTON_1        (1 << 0)        /* PIO0 is BUTTON_A */ 
#define LED_1     (1<<1) 		/* PIO1 is LED_1 */ 
 
#define MESSAGE_LED_HIGH 0x0 
#define MESSAGE_LED_LOW 0x1 
 
#define DELAY1 1000 
#define DELAY2 200 
 
typedef struct 
{ 
    TaskData    task;   /* task is required for messages to be delivered */ 
} appState; 
 
appState app; 
 
/*bool button_flag = 0;    Added by winson, 2012/11/7: 可否不通过全局变量方式实现? */ 
/*int delay =500;          Added by winson, 2012/11/7: 可否不通过全局变量方式实现? */ 
 
/* Function prototypes */ 
static void app_handler(Task task, MessageId id, Message message); 
static void handle_pio1(Task task); 
static void led1_high(Task task); 
static void led1_low(Task task); 
 
int main(void) 
{ 
    /* Set app_handler() function to handle app's messages */ 
    app.task.handler = app_handler;     /* Added by winson, 2012/11/7: 请把初始化过程抽象成 
                                                                       函数实现 */ 
 
    /* Set app task to receive PIO messages */ 
    MessagePioTask(&app.task); 
 
    /* Setup PIO interrupt messages */ 
    PioDebounce32(BUTTON_1,  /* 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)  
{ 
    printf("app_handler\n"); 
 
    switch (id) 
    { 
    case MESSAGE_PIO_CHANGED: 
        printf("MESSAGE_PIO_CHANGED\n"); 
        handle_pio1(task); 
        break; 
         
    case MESSAGE_LED_HIGH: 
        printf("MESSAGE_LED_HIGH\n"); 
        led1_high(task); 
        break; 
    case MESSAGE_LED_LOW: 
        printf("MESSAGE_LED_LOW\n"); 
        led1_low(task); 
        break; 
         
    default: 
        printf("Unhandled message 0x%x\n", id); 
    } 
} 
 
/* when button1 is released, trigger led1_flash() */ 
static void handle_pio1(Task task) 
{ 
    printf("handle_pio starting PioGet32 %lx\n", PioGet32()); 
    /* when button1 is released, trigger led1_flash() */ 
    if (PioGet32() & BUTTON_1)  
    { 
        printf("Button pressed\n"); 
 
        led1_high(task); 
    } 
} 
 
/* led1 set high */ 
static void led1_high(Task task) 
{ 
    if(PioGet32() & BUTTON_1) 
    { 
        PioSetDir32(LED_1, 0xFF);         /* set PIO1 to output */ 
        PioSet32( LED_1, 0 );    /* led1 set high */ 
        MessageSendLater(task, MESSAGE_LED_LOW, NULL, DELAY1); 
    } 
} 
 
/* led1 set low */ 
static void led1_low(Task task) 
{ 
    if(PioGet32() & BUTTON_1) 
    { 
        PioSetDir32(LED_1, 0xFF);         /* set PIO1 to output */ 
        PioSet32( LED_1, 0xFF );     /* led1 set low */ 
        MessageSendLater(task, MESSAGE_LED_HIGH, NULL, DELAY2); 
    } 
} 
 
/* End-of-File */