mouse_task.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00013 
00014 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
00015  *
00016  * Redistribution and use in source and binary forms, with or without
00017  * modification, are permitted provided that the following conditions are met:
00018  *
00019  * 1. Redistributions of source code must retain the above copyright notice,
00020  * this list of conditions and the following disclaimer.
00021  *
00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
00023  * this list of conditions and the following disclaimer in the documentation
00024  * and/or other materials provided with the distribution.
00025  *
00026  * 3. The name of ATMEL may not be used to endorse or promote products derived
00027  * from this software without specific prior written permission.
00028  *
00029  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00030  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00031  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00032  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00033  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00034  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00036  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00038  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 //_____  I N C L U D E S ___________________________________________________
00042 
00043 #include "config.h"
00044 #include "conf_usb.h"
00045 
00046 #include "mouse_task.h"
00047 #include "usb_descriptors.h"
00048 
00049 #include "lib_mcu/usb/usb_drv.h"
00050 #include "lib_mcu/adc/adc_drv.h"
00051 #include "lib_mcu/wdt/wdt_drv.h"
00052 #include "lib_mcu/power/power_drv.h"
00053 
00054 #include "modules/usb/device_chap9/usb_standard_request.h"
00055 
00056 
00057 //_____ M A C R O S ________________________________________________________
00058 
00061 #define  Hid_mouse_report_up()            (g_hid_mouse_report[2]-=MOUSE_SPEED)
00062 #define  Hid_mouse_report_down()          (g_hid_mouse_report[2]+=MOUSE_SPEED)
00063 #define  Hid_mouse_report_left()          (g_hid_mouse_report[1]-=MOUSE_SPEED)
00064 #define  Hid_mouse_report_right()         (g_hid_mouse_report[1]+=MOUSE_SPEED)
00065 
00066 #define  Hid_mouse_report_scroll_up()     (g_hid_mouse_report[3]+=MOUSE_SPEED)
00067 #define  Hid_mouse_report_scroll_down()   (g_hid_mouse_report[3]-=MOUSE_SPEED)
00068         
00069 #define  Hid_mouse_report_click_left()    (g_hid_mouse_report[0] |= 0x01)
00070 #define  Hid_mouse_report_click_right()   (g_hid_mouse_report[0] |= 0x02)
00071 #define  Hid_mouse_report_click_middle()  (g_hid_mouse_report[0] |= 0x04)
00072         
00073         
00074 #define  Hid_mouse_report_reset()         (g_hid_mouse_report[0]=0,g_hid_mouse_report[1]=0,g_hid_mouse_report[2]=0,g_hid_mouse_report[3]=0)
00076 
00079 #define Is_mouse_left_event()             Is_joy_left()
00080 #define Is_mouse_right_event()            Is_joy_right()
00081 #define Is_mouse_up_event()               Is_joy_up()
00082 #define Is_mouse_down_event()             Is_joy_down()
00083 
00084 #define SCROLL_STEP      10
00085 #define Is_mouse_scroll_up_event()        ((g_u16_pos_scroll+SCROLL_STEP) < Get_adc_pot_val())
00086 #define Is_mouse_scroll_down_event()      ((g_u16_pos_scroll-SCROLL_STEP) > Get_adc_pot_val())
00087 
00088 #define Is_mouse_click_left_event()       Is_btn_left()
00089 #define Is_mouse_click_middle_event()     FALSE
00090 #define Is_mouse_click_right_event()      Is_btn_middle()
00092 
00093 
00094 //_____ D E F I N I T I O N S ______________________________________________
00095 
00096 
00097 
00098 //_____ D E C L A R A T I O N S ____________________________________________
00099 
00100 bit   is_mouse_event(void);
00101 
00103 Bool  g_b_send_report;
00105 Bool  g_b_send_ack_report;
00107 U8    g_hid_mouse_report[4];
00108 #if (TARGET_BOARD==STK525)
00110 U16 g_u16_pos_scroll;
00111 #endif
00113 volatile U8 g_u8_cpt_sof;
00114 
00115 
00118 void mouse_task_init(void)
00119 {
00120    // Init SOF
00121    g_u8_cpt_sof=0;
00122    Usb_enable_sof_interrupt();
00123 
00124    // Init interface board
00125    Joy_init();
00126    Hwb_button_init();
00127    Leds_init();
00128    Led1_on();
00129    Led0_off();
00130 #if (TARGET_BOARD==STK525)
00131    init_adc();
00132    g_u16_pos_scroll=Get_adc_pot_val();
00133 #endif
00134    
00135    // Send a ack report at startup
00136    g_b_send_report      = FALSE;
00137    g_b_send_ack_report  = TRUE;
00138 }
00139 
00140 
00143 void mouse_task(void)
00144 {
00145    if(Is_usb_vbus_low())
00146    {
00147       Setup_power_down_mode();
00148       Sleep_instruction();
00149    }
00150 
00151    if(!Is_device_enumerated())
00152       return;  // Device not ready
00153    
00154 #if (USB_LOW_SPEED_DEVICE==DISABLE)
00155    // The SOF is used to schedule the task at the same frequency that Endpoint Interrupt frequency
00156    // This check allow to win a CPU time
00157    if(g_u8_cpt_sof<NB_IDLE_POLLING_SOF)
00158       return;  // Wait a delay
00159    g_u8_cpt_sof=0;
00160 #endif
00161 
00162    if(!g_b_send_report)
00163    {
00164       // No report sending on going then check mouse event to eventualy fill a new report
00165       if(is_mouse_event())
00166       {
00167          // Enable sending of report
00168          g_b_send_report      = TRUE;
00169       }
00170    }
00171    
00172    if((!g_b_send_report)&&(!g_b_send_ack_report))
00173       return;  // No report and ack to send
00174    
00175    //** A report or ack must be send
00176    Usb_select_endpoint(EP_MOUSE_IN);
00177    if(!Is_usb_write_enabled())
00178       return;  // Endpoint no free
00179    
00180    Led0_on();      
00181    if( g_b_send_report )
00182    {
00183       g_b_send_report      = FALSE;
00184       // Send an ack after a "clic" report only
00185       g_b_send_ack_report = (0!=g_hid_mouse_report[0]);
00186    }
00187    else
00188    {
00189       Hid_mouse_report_reset();     // Reset report to have a ack report
00190       g_b_send_ack_report  = FALSE;
00191    }
00192    // Send report
00193    Usb_write_byte(g_hid_mouse_report[0]);
00194    Usb_write_byte(g_hid_mouse_report[1]);
00195    Usb_write_byte(g_hid_mouse_report[2]);
00196    Usb_write_byte(g_hid_mouse_report[3]);
00197    Usb_ack_in_ready();
00198    Led0_off();      
00199 }
00200 
00201 
00207 bit is_mouse_event(void)
00208 {
00209    Hid_mouse_report_reset();
00210    
00211    // Check Click events
00212    if(Is_mouse_click_left_event()) {
00213       Hid_mouse_report_click_left();
00214    }
00215    if(Is_mouse_click_middle_event()) {
00216       Hid_mouse_report_click_middle();
00217    }
00218    if(Is_mouse_click_right_event()) {
00219       Hid_mouse_report_click_right();
00220    }
00221 
00222    // Check Mouve events
00223    if (Is_mouse_left_event()) {
00224       Hid_mouse_report_left();
00225    }
00226    if (Is_mouse_right_event()) {
00227       Hid_mouse_report_right();
00228    }
00229    if (Is_mouse_up_event()) {
00230       Hid_mouse_report_up();
00231    }
00232    if (Is_mouse_down_event()) {
00233       Hid_mouse_report_down();
00234    }
00235 #if (TARGET_BOARD==STK525)
00236    if (Is_mouse_scroll_up_event()) {
00237       g_u16_pos_scroll+=SCROLL_STEP;
00238       Hid_mouse_report_scroll_up();
00239    }
00240    if (Is_mouse_scroll_down_event()) {
00241       g_u16_pos_scroll-=SCROLL_STEP;
00242       Hid_mouse_report_scroll_down();
00243    }
00244 #endif
00245 
00246    // Check report
00247    if((0==g_hid_mouse_report[0])
00248    && (0==g_hid_mouse_report[1])
00249    && (0==g_hid_mouse_report[2])
00250    && (0==g_hid_mouse_report[3]))
00251    {
00252       return FALSE;  // No event
00253    }
00254    return TRUE;      // Event(s) occurs
00255 }
00256 
00257 
00265 void sof_action()
00266 {
00267    g_u8_cpt_sof++;
00268 }
00269 
00270 
00277 void suspend_action(void)
00278 {
00279 #if (USB_REMOTE_WAKEUP_FEATURE == ENABLED)
00280    if (remote_wakeup_feature == ENABLED)
00281    {
00282       Switches_enable_it();
00283    }
00284    Led1_off();
00285    Enable_interrupt();
00286    Enter_power_down_mode();
00287    Led1_on();
00288 #endif 
00289 } 
00290 
00291 
00292 #if (USB_REMOTE_WAKEUP_FEATURE == ENABLED)
00299 #ifdef __GNUC__
00300  ISR(PCINT0_vect)
00301 #else
00302 #pragma vector = PCINT0_vect
00303 __interrupt void switch_event_int()
00304 #endif
00305 {
00306    Switches_disable_it();
00307    usb_generate_remote_wakeup();
00308 }
00309 #endif 
00310 

Generated on Fri Oct 31 14:47:06 2008 for ATMEL by  doxygen 1.5.3