www.pudn.com > buttonTest.rar > buttonTest2.c, change:2012-11-07,size:2558b


/* 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> 
 
#define BUTTON_1        (1 << 0)        /* PIO0 is BUTTON_A */ 
#define LED_1     (1<<1) 		/* PIO1 is LED_1 */ 
 
#define MESSAGE_LED_FLASH 0x0 
 
typedef struct 
{ 
    TaskData    task;   /* task is required for messages to be delivered */ 
} appState; 
 
appState app; 
 
bool button_flag = 0; 
int delay =500; 
 
/* Function prototypes */ 
static void app_handler(Task task, MessageId id, Message message); 
static void handle_pio1(Task task); 
static void led1_flash(Task task); 
 
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_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)  
{ 
 
 
    switch (id) 
    { 
    case MESSAGE_PIO_CHANGED: 
        printf("MESSAGE_PIO_CHANGED\n"); 
        handle_pio1(task); 
        break; 
         
    case MESSAGE_LED_FLASH: 
        printf("MESSAGE_LED_FLASH\n"); 
        led1_flash(task); 
        break; 
         
    default: 
        printf("Unhandled message 0x%x\n", id); 
    } 
} 
 
/* set the button_flag and trigger led1_flash() */ 
static void handle_pio1(Task task) 
{ 
    printf("handle_pio starting PioGet32 %lx\n", PioGet32()); 
	/* when button1 is released, set button_flag and call led1_flash() */ 
    if (PioGet32() & BUTTON_1)  
    { 
        printf("Button pressed\n"); 
        button_flag = 1; 
        led1_flash(task); 
    } 
    else 
    { 
        button_flag = 0; 
    } 
} 
 
/* led1 flashing depend on button_flag */ 
static void led1_flash(Task task) 
{ 
    printf("led1_flash flag %d\n", button_flag); 
    if(button_flag == 1) 
    { 
        PioSetDir32(LED_1, 0xFF);         /* set PIO1 to output */ 
        printf("*****debuy led_controller1-just*****\n"); 
        PioSet32( LED_1, (PioGet32() ^ LED_1) ); 
         
		/* the square wave duty cycle and the default phase is low level */ 
        if(delay == 500) delay =1000; 
        else delay =500; 
        MessageSendLater(task, MESSAGE_LED_FLASH, 0, delay); 
    } 
 
} 
 
/* End-of-File */