mouse_task.c

Go to the documentation of this file.
00001 /*This file is prepared for Doxygen automatic documentation generation.*/
00013 
00014 /* Copyright (c) 2009 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  * 4. This software may only be redistributed and used in connection with an Atmel
00030  * AVR product.
00031  *
00032  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00034  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00042  */
00043 
00044 //_____  I N C L U D E S ___________________________________________________
00045 
00046 #include "config.h"
00047 #include "conf_usb.h"
00048 
00049 #include "mouse_task.h"
00050 #include "usb_descriptors.h"
00051 
00052 #include "lib_mcu/usb/usb_drv.h"
00053 #include "lib_mcu/wdt/wdt_drv.h"
00054 #include "lib_mcu/power/power_drv.h"
00055 #include "lib_mcu/pll/pll_drv.h"
00056 
00057 #include "modules/usb/device_chap9/usb_standard_request.h"
00058 
00059 
00060 //_____ M A C R O S ________________________________________________________
00061 
00064 #define  Hid_mouse_report_up()            (g_hid_mouse_report[2]-=MOUSE_SPEED)
00065 #define  Hid_mouse_report_down()          (g_hid_mouse_report[2]+=MOUSE_SPEED)
00066 #define  Hid_mouse_report_left()          (g_hid_mouse_report[1]-=MOUSE_SPEED)
00067 #define  Hid_mouse_report_right()         (g_hid_mouse_report[1]+=MOUSE_SPEED)
00068 
00069 #define  Hid_mouse_report_scroll_up()     (g_hid_mouse_report[3]+=MOUSE_SPEED)
00070 #define  Hid_mouse_report_scroll_down()   (g_hid_mouse_report[3]-=MOUSE_SPEED)
00071         
00072 #define  Hid_mouse_report_click_left()    (g_hid_mouse_report[0] |= 0x01)
00073 #define  Hid_mouse_report_click_right()   (g_hid_mouse_report[0] |= 0x02)
00074 #define  Hid_mouse_report_click_middle()  (g_hid_mouse_report[0] |= 0x04)
00075         
00076         
00077 #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)
00079 
00082 #define Is_mouse_left_event()             Is_joy_left()
00083 #define Is_mouse_right_event()            Is_joy_right()
00084 #define Is_mouse_up_event()               Is_joy_up()
00085 #define Is_mouse_down_event()             Is_joy_down()
00086 
00087 #define Is_mouse_scroll_up_event()        FALSE
00088 #define Is_mouse_scroll_down_event()      FALSE
00089                                          
00090 #define Is_mouse_click_left_event()       Is_btn_left()
00091 #define Is_mouse_click_middle_event()     FALSE
00092 #define Is_mouse_click_right_event()      Is_btn_middle()
00094 
00095 
00096 //_____ D E F I N I T I O N S ______________________________________________
00097 
00098 
00099 
00100 //_____ D E C L A R A T I O N S ____________________________________________
00101 
00102 bit   is_mouse_event(void);
00103 
00105 Bool  g_b_send_report;
00107 Bool  g_b_send_ack_report;
00109 U8    g_hid_mouse_report[4];
00111 volatile U8 g_u8_cpt_sof;
00112 
00113 
00116 void mouse_task_init(void)
00117 {
00118    // Init SOF
00119    g_u8_cpt_sof=0;
00120    Usb_enable_sof_interrupt();
00121 
00122    // Init interface board
00123    Joy_init();
00124    Leds_init();
00125    Hwb_button_init();
00126    Led1_on();
00127    Led0_off();
00128    
00129    // Send a ack report at startup
00130    g_b_send_report      = FALSE;
00131    g_b_send_ack_report  = TRUE;
00132 }
00133 
00134 
00137 void mouse_task(void)
00138 {
00139 }
00140 
00141 
00142 
00150 void mouse_task_by_IT(void)
00151 {
00152    if(!Is_device_enumerated())
00153       return;  // Device not ready
00154   
00155    if(!g_b_send_report)
00156    {
00157       // No report sending on going then check mouse event to eventualy fill a new report
00158       if(is_mouse_event())
00159       {
00160          // Enable sending of report
00161          g_b_send_report      = TRUE;
00162       }
00163    }
00164    
00165    if((!g_b_send_report)&&(!g_b_send_ack_report))
00166       return;  // No report and ack to send
00167    
00168    //** A report or ack must be send
00169    Usb_select_endpoint(EP_MOUSE_IN);
00170    if(!Is_usb_write_enabled())
00171       return;  // Endpoint no free
00172    
00173    Led0_on();      
00174    if( g_b_send_report )
00175    {
00176       g_b_send_report      = FALSE;
00177       // Send an ack after a "clic" report only
00178       g_b_send_ack_report = (0!=g_hid_mouse_report[0]);
00179    }
00180    else
00181    {
00182       Hid_mouse_report_reset();     // Reset report to have a ack report
00183       g_b_send_ack_report  = FALSE;
00184    }
00185    // Send report
00186    Usb_write_byte(g_hid_mouse_report[0]);
00187    Usb_write_byte(g_hid_mouse_report[1]);
00188    Usb_write_byte(g_hid_mouse_report[2]);
00189    Usb_write_byte(g_hid_mouse_report[3]);
00190    Usb_ack_in_ready();
00191    Led0_off();      
00192 }
00193 
00194 
00200 bit is_mouse_event(void)
00201 {
00202    Hid_mouse_report_reset();
00203    
00204    // Check Click events
00205    if(Is_mouse_click_left_event()) {
00206       Hid_mouse_report_click_left();
00207    }
00208    if(Is_mouse_click_middle_event()) {
00209       Hid_mouse_report_click_middle();
00210    }
00211    if(Is_mouse_click_right_event()) {
00212       Hid_mouse_report_click_right();
00213    }
00214 
00215    // Check Mouve events
00216    if (Is_mouse_left_event()) {
00217       Hid_mouse_report_left();
00218    }
00219    if (Is_mouse_right_event()) {
00220       Hid_mouse_report_right();
00221    }
00222    if (Is_mouse_up_event()) {
00223       Hid_mouse_report_up();
00224    }
00225    if (Is_mouse_down_event()) {
00226       Hid_mouse_report_down();
00227    }
00228    if (Is_mouse_scroll_up_event()) {
00229       Hid_mouse_report_scroll_up();
00230    }
00231    if (Is_mouse_scroll_down_event()) {
00232       Hid_mouse_report_scroll_down();
00233    }
00234 
00235    // Check report
00236    if((0==g_hid_mouse_report[0])
00237    && (0==g_hid_mouse_report[1])
00238    && (0==g_hid_mouse_report[2])
00239    && (0==g_hid_mouse_report[3]))
00240    {
00241       return FALSE;  // No event
00242    }
00243    return TRUE;      // Event(s) occurs
00244 }
00245 
00246 
00256 void sof_action()
00257 {
00258    U8 ep_selected;
00259    g_u8_cpt_sof++;
00260    if (g_u8_cpt_sof == NB_IDLE_POLLING_SOF)
00261    {
00262      ep_selected = Usb_get_selected_endpoint();   // the currently selected endpoint must be saved to avoid
00263                                                   // problems when interrupt happens during mass storage endpoint reading/writing
00264      mouse_task_by_IT();
00265      g_u8_cpt_sof = 0;
00266      Usb_select_endpoint(ep_selected);            // restore current selected endpoint
00267    }
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 #if (USB_REMOTE_WAKEUP_FEATURE == ENABLED)
00298 #ifdef __GNUC__
00299  ISR(PCINT0_vect)
00300 #else
00301 #pragma vector = PCINT0_vect
00302 __interrupt void switch_event_int()
00303 #endif
00304 {
00305    Switches_disable_it();
00306    usb_generate_remote_wakeup();
00307 }
00308 #endif 
00309 

Generated on Fri Sep 11 14:39:50 2009 for ATMEL by  doxygen 1.5.3