• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 #include "lib_mcu/util/start_boot.h"
00057 #include "lib_mcu/flash/flash_drv.h"
00058 #include "lib_mcu/cal_inter_rc/cal_inter_rc.h"
00059 #include "modules/usb/device_chap9/usb_standard_request.h"
00060 
00061 
00062 //_____ M A C R O S ________________________________________________________
00063 
00066 #define  Hid_mouse_report_up()            (g_hid_mouse_report[2]-=MOUSE_SPEED)
00067 #define  Hid_mouse_report_down()          (g_hid_mouse_report[2]+=MOUSE_SPEED)
00068 #define  Hid_mouse_report_left()          (g_hid_mouse_report[1]-=MOUSE_SPEED)
00069 #define  Hid_mouse_report_right()         (g_hid_mouse_report[1]+=MOUSE_SPEED)
00070 
00071 #define  Hid_mouse_report_scroll_up()     (g_hid_mouse_report[3]+=MOUSE_SPEED)
00072 #define  Hid_mouse_report_scroll_down()   (g_hid_mouse_report[3]-=MOUSE_SPEED)
00073 
00074 #define  Hid_mouse_report_click_left()    (g_hid_mouse_report[0] |= 0x01)
00075 #define  Hid_mouse_report_click_right()   (g_hid_mouse_report[0] |= 0x02)
00076 #define  Hid_mouse_report_click_middle()  (g_hid_mouse_report[0] |= 0x04)
00077 
00078 
00079 #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)
00080 
00081 
00084 #define Is_mouse_left_event()             Is_switch_3()
00085 #define Is_mouse_right_event()            Is_switch_2()
00086 #define Is_mouse_up_event()               Is_switch_1()
00087 #define Is_mouse_down_event()             Is_switch_0()
00088 
00089 #define Is_mouse_scroll_up_event()        FALSE
00090 #define Is_mouse_scroll_down_event()      FALSE
00091 
00092 #define Is_mouse_click_left_event()       Is_switch_6()
00093 #define Is_mouse_click_middle_event()     Is_switch_5()
00094 #define Is_mouse_click_right_event()      Is_switch_4()
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];
00109 volatile U8 g_u8_cpt_sof;
00110 volatile int g_int_cpt_cal_update;
00111 
00112 
00115 void mouse_task_init(void)
00116 {
00117    // Init SOF
00118    g_u8_cpt_sof=0;
00119    Usb_enable_sof_interrupt();
00120 
00121    // Init interface board
00122    Switches_init();
00123    Leds_init();
00124    Led7_on();
00125    Led6_off();
00126 
00127    // Send a ack report at startup
00128    g_b_send_report      = FALSE;
00129    g_b_send_ack_report  = TRUE;
00130 
00131    // Init the internal OSC calibration counter
00132    g_int_cpt_cal_update = 0;
00133 }
00134 
00135 
00138 void mouse_task(void)
00139 {
00140    if(!Is_device_enumerated())
00141       return;  // Device not ready
00142 
00143 #if (USB_LOW_SPEED_DEVICE==DISABLE)
00144    // The SOF is used to schedule the task at the same frequency that Endpoint Interrupt frequency
00145    // This check allow to win a CPU time
00146    if(g_u8_cpt_sof<NB_IDLE_POLLING_SOF)
00147       return;  // Wait a delay
00148    g_u8_cpt_sof=0;
00149 #endif
00150 
00151    if(!g_b_send_report)
00152    {
00153       // No report sending on going then check mouse event to eventualy fill a new report
00154       if(is_mouse_event())
00155       {
00156          // Enable sending of report
00157          g_b_send_report      = TRUE;
00158       }
00159    }
00160 
00161    if((!g_b_send_report)&&(!g_b_send_ack_report))
00162       return;  // No report and ack to send
00163 
00164    //** A report or ack must be send
00165    Usb_select_endpoint(EP_MOUSE_IN);
00166    if(!Is_usb_write_enabled())
00167       return;  // Endpoint no free
00168 
00169    Led6_on();
00170    if( g_b_send_report )
00171    {
00172       g_b_send_report      = FALSE;
00173       // Send an ack after a "clic" report only
00174       g_b_send_ack_report = (0!=g_hid_mouse_report[0]);
00175    }
00176    else
00177    {
00178       Hid_mouse_report_reset();     // Reset report to have a ack report
00179       g_b_send_ack_report  = FALSE;
00180    }
00181    // Send report
00182    Usb_write_byte(g_hid_mouse_report[0]);
00183    Usb_write_byte(g_hid_mouse_report[1]);
00184    Usb_write_byte(g_hid_mouse_report[2]);
00185    Usb_write_byte(g_hid_mouse_report[3]);
00186    Usb_ack_in_ready();
00187    Led6_off();
00188 }
00189 
00190 
00196 bit is_mouse_event(void)
00197 {
00198    Hid_mouse_report_reset();
00199 
00200    // Check Click events
00201    if(Is_mouse_click_left_event()) {
00202       Hid_mouse_report_click_left();
00203    }
00204    if(Is_mouse_click_middle_event()) {
00205       Hid_mouse_report_click_middle();
00206    }
00207    if(Is_mouse_click_right_event()) {
00208       Hid_mouse_report_click_right();
00209    }
00210 
00211    // Check Mouve events
00212    if (Is_mouse_left_event()) {
00213       Hid_mouse_report_left();
00214    }
00215    if (Is_mouse_right_event()) {
00216       Hid_mouse_report_right();
00217    }
00218    if (Is_mouse_up_event()) {
00219       Hid_mouse_report_up();
00220    }
00221    if (Is_mouse_down_event()) {
00222       Hid_mouse_report_down();
00223    }
00224    if (Is_mouse_scroll_up_event()) {
00225       Hid_mouse_report_scroll_up();
00226    }
00227    if (Is_mouse_scroll_down_event()) {
00228       Hid_mouse_report_scroll_down();
00229    }
00230 
00231    // Check report
00232    if((0==g_hid_mouse_report[0])
00233    && (0==g_hid_mouse_report[1])
00234    && (0==g_hid_mouse_report[2])
00235    && (0==g_hid_mouse_report[3]))
00236    {
00237       return FALSE;  // No event
00238    }
00239    return TRUE;      // Event(s) occurs
00240 }
00241 
00242 
00250 void sof_action()
00251 {
00252    g_u8_cpt_sof++;
00253 
00254 }
00261 void update_osc_cal_task()
00262 {
00263   g_int_cpt_cal_update++;
00264   if(g_int_cpt_cal_update > 3000)
00265  {
00266    g_int_cpt_cal_update = 0;
00267    calibrate_inter_osc(vcc_5);
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    Led7_off();
00285    Enable_interrupt();
00286    Enter_power_down_mode();
00287    Led7_on();
00288 #endif
00289 }
00290 
00291 
00292 #if (USB_REMOTE_WAKEUP_FEATURE == ENABLED)
00293 
00294 
00295 
00296 
00297 
00298 
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 Tue Nov 8 2011 16:29:45 for ATMEL by  doxygen 1.7.2