//-------------------------------------------------------------------------- // // This module includes support routines for the three buttons on the mouse, // the bind button and motion sensor. It also provides logic for debouncing // the three mouse buttons. // //-------------------------------------------------------------------------- // $Archive: /WirelessUSB/WUSB Kits/CY4632 LS KBM RDK/DocSrc/CD_Root/Firmware/Source Code/RDK Mouse/buttons.c $ // $Modtime: 6/16/04 4:38p10/01/04 1:18p $ // $Revision: 910 $ //-------------------------------------------------------------------------- // // Copyright 2003-2004, Cypress Semiconductor Corporation. // // This software is owned by Cypress Semiconductor Corporation (Cypress) // and is protected by and subject to worldwide patent protection (United // States and foreign), United States copyright laws and international // treaty provisions. Cypress hereby grants to licensee a personal, // non-exclusive, non-transferable license to copy, use, modify, create // derivative works of, and compile the Cypress Source Code and derivative // works for the sole purpose of creating custom software in support of // licensee product to be used only in conjunction with a Cypress integrated // circuit as specified in the applicable agreement. Any reproduction, // modification, translation, compilation, or representation of this // software except as specified above is prohibited without the express // written permission of Cypress. // // Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, // WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // Cypress reserves the right to make changes without further notice to the // materials described herein. Cypress does not assume any liability arising // out of the application or use of any product or circuit described herein. // Cypress does not authorize its products for use as critical components in // life-support systems where a malfunction or failure may reasonably be // expected to result in significant injury to the user. The inclusion of // Cypress’ product in a life-support systems application implies that the // manufacturer assumes all risk of such use and in doing so indemnifies // Cypress against all charges. // // Use may be limited by and subject to the applicable Cypress software // license agreement. // //-------------------------------------------------------------------------- //-------------------------------------- // Included files //-------------------------------------- #include "buttons.h" #include "isr.h" #include "mouse.h" #include "port.h" //************************************** // Button Debounce //************************************** //-------------------------------------- // Local Function Declarations //-------------------------------------- static void age_debounce_queue(void); static BOOL debounce(UINT8 index); //************************************************************************** // Mouse Buttons - Left, Middle, Right //************************************************************************** //-------------------------------------- // Local Definitions and Types //-------------------------------------- #define MOUSE_BUTTON_ALL ( MOUSE_BUTTON_RIGHT | \ MOUSE_BUTTON_LEFT | \ MOUSE_BUTTON_MIDDLE ) //-------------------------------------- // Local Function Declarations //-------------------------------------- static UINT8 get_button_state(void); //-------------------------------------- // Local Definitions //-------------------------------------- static UINT8 last_button_state; //-------------------------------------------------------------------------- // buttons_isr //-------------------------------------------------------------------------- void buttons_isr(void) { if( (MOUSE_BUTTON_PORT & MOUSE_BUTTON_ALL) != MOUSE_BUTTON_ALL ) { // A button is pressed, do wakeup mouse_wakeupMOUSE_WAKEUP(); } } //-------------------------------------------------------------------------- // buttons_init //-------------------------------------------------------------------------- void buttons_init(void) { last_button_state = 0; port_drive_on( MOUSE_BUTTON_DRV_PORT, MOUSE_BUTTON_ALL ); isr_disable(INT_BUTTON); ISR_DISABLE(GPIO_ISR_BUTTON_IE_PORT, GPIO_ISR_BUTTON_INT); } //-------------------------------------------------------------------------- // buttons_power_up //-------------------------------------------------------------------------- void buttons_power_up(void) { buttons_init(); } //-------------------------------------------------------------------------- // buttons_power_down //-------------------------------------------------------------------------- void buttons_power_down(void) { isr_enable(INT_BUTTON); ISR_ENABLE(GPIO_ISR_BUTTON_IE_PORT, GPIO_ISR_BUTTON_INT); } //-------------------------------------------------------------------------- // buttons_get_report //-------------------------------------------------------------------------- BOOL buttons_get_report(BUTTON_REPORT *report) { UINT8 button_state; UINT8 last_state; UINT8 key_state; UINT8 key = 0x20; UINT8 i; BOOL report_changed = TRUE; button_state = get_button_state(); last_state = last_button_state; key_state = button_state ^ last_state; // Debounce key for(i=0; i<NUMBER_OF_BUTTONS; ++i) { if( (key_state & key) && debounce(i) ) // button changed state { if( button_state & key ) { last_state |= key; // button on } else { last_state &= ~key; // button off } } key <<= 1; } age_debounce_queue(); if( last_state == last_button_state && last_state == 0 ) { report_changed = FALSE; } else { last_button_state = last_state; } report->buttons = last_state; return report_changed; } //-------------------------------------------------------------------------- // buttons_up //-------------------------------------------------------------------------- BOOL buttons_up(void) { if( get_button_state() == 0 ) { return TRUE; } return FALSE; } //-------------------------------------------------------------------------- // get_button_state //-------------------------------------------------------------------------- static UINT8 get_button_state(void) { return ~MOUSE_BUTTON_PORT << 5; } //************************************************************************** // Bind Button & LED //************************************************************************** //-------------------------------------- // Local Definitions and Types //-------------------------------------- //-------------------------------------- // Local Function Declarations //-------------------------------------- //-------------------------------------- // Local Definitions //-------------------------------------- //-------------------------------------------------------------------------- // bind_button_isr //-------------------------------------------------------------------------- void bind_button_isr(void) { if( (MOUSE_BIND_PORT & MOUSE_BUTTON_BIND) == 0 ) { ISR_DISABLE(GPIO_ISR_BIND_IE_PORT, GPIO_ISR_BIND_INT); mouse_wakeupMOUSE_WAKEUP(); mouse_bind(); MOUSE_BIND(); } } //-------------------------------------------------------------------------- // bind_button_init //-------------------------------------------------------------------------- void bind_button_init(void) { UINT8 reg; port_drive_on( MOUSE_BIND_DRV_PORT, MOUSE_BUTTON_BIND ); // Read so ISR will function correctly reg = MOUSE_BIND_PORT; isr_enable(INT_BIND); ISR_ENABLE(GPIO_ISR_BIND_IE_PORT, GPIO_ISR_BIND_INT); } //-------------------------------------------------------------------------- // bind_button_power_up //-------------------------------------------------------------------------- void bind_button_power_up(void) { } //-------------------------------------------------------------------------- // bind_button_power_down //-------------------------------------------------------------------------- void bind_button_power_down(void) { } #ifdef MOUSE_MOTION_NOT_TIME_SLEEP //************************************************************************** // Motion Sensor //************************************************************************** //-------------------------------------- // Local Definitions and Types //-------------------------------------- //-------------------------------------- // Local Function Declarations //-------------------------------------- //-------------------------------------- // Local Definitions //-------------------------------------- //-------------------------------------------------------------------------- // motion_isr //-------------------------------------------------------------------------- void motion_isr(void) { // Assumption: any GPIO ISR during sleep does a wakeup. // So, we don't check to see if motion actually generated // this interrupt. This ISR is only enabled when asleep. mouse_wakeupMOUSE_WAKEUP(); } //-------------------------------------------------------------------------- // motion_init //-------------------------------------------------------------------------- void motion_init(void) { isr_disable(INT_MOTION); ISR_DISABLE(GPIO_ISR_MOTION_IE_PORT, GPIO_ISR_MOTION_INT); } //-------------------------------------------------------------------------- // motion_power_up //-------------------------------------------------------------------------- void motion_power_up(void) { isr_disable(INT_MOTION); ISR_DISABLE(GPIO_ISR_MOTION_IE_PORT, GPIO_ISR_MOTION_INT); } //-------------------------------------------------------------------------- // motion_power_down //-------------------------------------------------------------------------- void motion_power_down(void) { // read to latch current state for ISR change detection UINT8 reg = MOUSE_MOTION_PORT; isr_enable(INT_MOTION); ISR_ENABLE(GPIO_ISR_MOTION_IE_PORT, GPIO_ISR_MOTION_INT); } #endif // MOUSE_MOTION_NOT_TIME_SLEEP //************************************************************************** // Button Debounce //************************************************************************** //-------------------------------------- // Local Definitions and Types //-------------------------------------- //-------------------------------------- // Local Definitions //-------------------------------------- static UINT8 debounce_queue[NUMBER_OF_BUTTONS]; //-------------------------------------------------------------------------- // debounce_init //-------------------------------------------------------------------------- void debounce_init(void) { UINT8 i; for(i=0; i<NUMBER_OF_BUTTONS; ++i) { debounce_queue[i] = 0; } } //-------------------------------------------------------------------------- // age_debounce_queue //-------------------------------------------------------------------------- static void age_debounce_queue(void) { UINT8 i; for(i=0; i<NUMBER_OF_BUTTONS; ++i) { if( debounce_queue[i] ) { --debounce_queue[i]; } } } //-------------------------------------------------------------------------- // debounce //-------------------------------------------------------------------------- static BOOL debounce(UINT8 index) { if( debounce_queue[index] ) { return FALSE; // key is still being debounced } debounce_queue[index] = MOUSE_DEBOUNCE_COUNT; return TRUE; }