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/adc/adc_drv.h"
00054 #include "lib_mcu/wdt/wdt_drv.h"
00055 #include "lib_mcu/power/power_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 SCROLL_STEP      10
00088 #define Is_mouse_scroll_up_event()        ((g_u16_pos_scroll+SCROLL_STEP) < Get_adc_pot_val())
00089 #define Is_mouse_scroll_down_event()      ((g_u16_pos_scroll-SCROLL_STEP) > Get_adc_pot_val())
00090 
00091 #define Is_mouse_click_left_event()       Is_btn_left()
00092 #define Is_mouse_click_middle_event()     FALSE
00093 #define Is_mouse_click_right_event()      Is_btn_middle()
00095 
00096 
00097 //_____ D E F I N I T I O N S ______________________________________________
00098 
00099 
00100 
00101 //_____ D E C L A R A T I O N S ____________________________________________
00102 
00103 bit   is_mouse_event(void);
00104 
00106 Bool  g_b_send_report;
00108 Bool  g_b_send_ack_report;
00110 U8    g_hid_mouse_report[4];
00111 #if (TARGET_BOARD==STK525)
00113 U16 g_u16_pos_scroll;
00114 #endif
00116 volatile U8 g_u8_cpt_sof;
00117 
00118 
00121 void mouse_task_init(void)
00122 {
00123    // Init SOF
00124    g_u8_cpt_sof=0;
00125    Usb_enable_sof_interrupt();
00126 
00127    // Init interface board
00128    Joy_init();
00129    Hwb_button_init();
00130    Leds_init();
00131    Led1_on();
00132    Led0_off();
00133 #if (TARGET_BOARD==STK525)
00134    init_adc();
00135    g_u16_pos_scroll=Get_adc_pot_val();
00136 #endif
00137    
00138    // Send a ack report at startup
00139    g_b_send_report      = FALSE;
00140    g_b_send_ack_report  = TRUE;
00141 }
00142 
00143 
00146 void mouse_task(void)
00147 {
00148    if(Is_usb_vbus_low())
00149    {
00150       // Before power down the USB Stack must be call to manage the VBus low state
00151       usb_task();
00152       Usb_enable_vbus_interrupt();  // To exit of power down mode when VBus is plugged
00153       Setup_power_down_mode();
00154       Sleep_instruction();
00155       Usb_disable_vbus_interrupt();
00156    }
00157 
00158    if(!Is_device_enumerated())
00159       return;  // Device not ready
00160    
00161 #if (USB_LOW_SPEED_DEVICE==DISABLE)
00162    // The SOF is used to schedule the task at the same frequency that Endpoint Interrupt frequency
00163    // This check allow to win a CPU time
00164    if(g_u8_cpt_sof<NB_IDLE_POLLING_SOF)
00165       return;  // Wait a delay
00166    g_u8_cpt_sof=0;
00167 #endif
00168 
00169    if(!g_b_send_report)
00170    {
00171       // No report sending on going then check mouse event to eventualy fill a new report
00172       if(is_mouse_event())
00173       {
00174          // Enable sending of report
00175          g_b_send_report      = TRUE;
00176       }
00177    }
00178    
00179    if((!g_b_send_report)&&(!g_b_send_ack_report))
00180       return;  // No report and ack to send
00181    
00182    //** A report or ack must be send
00183    Usb_select_endpoint(EP_MOUSE_IN);
00184    if(!Is_usb_write_enabled())
00185       return;  // Endpoint no free
00186    
00187    Led0_on();      
00188    if( g_b_send_report )
00189    {
00190       g_b_send_report      = FALSE;
00191       // Send an ack after a "clic" report only
00192       g_b_send_ack_report = (0!=g_hid_mouse_report[0]);
00193    }
00194    else
00195    {
00196       Hid_mouse_report_reset();     // Reset report to have a ack report
00197       g_b_send_ack_report  = FALSE;
00198    }
00199    // Send report
00200    Usb_write_byte(g_hid_mouse_report[0]);
00201    Usb_write_byte(g_hid_mouse_report[1]);
00202    Usb_write_byte(g_hid_mouse_report[2]);
00203    Usb_write_byte(g_hid_mouse_report[3]);
00204    Usb_ack_in_ready();
00205    Led0_off();      
00206 }
00207 
00208 
00214 bit is_mouse_event(void)
00215 {
00216    Hid_mouse_report_reset();
00217    
00218    // Check Click events
00219    if(Is_mouse_click_left_event()) {
00220       Hid_mouse_report_click_left();
00221    }
00222    if(Is_mouse_click_middle_event()) {
00223       Hid_mouse_report_click_middle();
00224    }
00225    if(Is_mouse_click_right_event()) {
00226       Hid_mouse_report_click_right();
00227    }
00228 
00229    // Check Mouve events
00230    if (Is_mouse_left_event()) {
00231       Hid_mouse_report_left();
00232    }
00233    if (Is_mouse_right_event()) {
00234       Hid_mouse_report_right();
00235    }
00236    if (Is_mouse_up_event()) {
00237       Hid_mouse_report_up();
00238    }
00239    if (Is_mouse_down_event()) {
00240       Hid_mouse_report_down();
00241    }
00242 #if (TARGET_BOARD==STK525)
00243    if (Is_mouse_scroll_up_event()) {
00244       g_u16_pos_scroll+=SCROLL_STEP;
00245       Hid_mouse_report_scroll_up();
00246    }
00247    if (Is_mouse_scroll_down_event()) {
00248       g_u16_pos_scroll-=SCROLL_STEP;
00249       Hid_mouse_report_scroll_down();
00250    }
00251 #endif
00252 
00253    // Check report
00254    if((0==g_hid_mouse_report[0])
00255    && (0==g_hid_mouse_report[1])
00256    && (0==g_hid_mouse_report[2])
00257    && (0==g_hid_mouse_report[3]))
00258    {
00259       return FALSE;  // No event
00260    }
00261    return TRUE;      // Event(s) occurs
00262 }
00263 
00264 
00272 void sof_action()
00273 {
00274    g_u8_cpt_sof++;
00275 }
00276 
00277 
00284 void suspend_action(void)
00285 {
00286 #if (USB_REMOTE_WAKEUP_FEATURE == ENABLED)
00287    if (remote_wakeup_feature == ENABLED)
00288    {
00289       Switches_enable_it();
00290    }
00291    Led1_off();
00292    Enable_interrupt();
00293    Enter_power_down_mode();
00294    Led1_on();
00295 #endif 
00296 } 
00297 
00298 
00299 #if (USB_REMOTE_WAKEUP_FEATURE == ENABLED)
00306 #ifdef __GNUC__
00307  ISR(PCINT0_vect)
00308 #else
00309 #pragma vector = PCINT0_vect
00310 __interrupt void switch_event_int()
00311 #endif
00312 {
00313    Switches_disable_it();
00314    usb_generate_remote_wakeup();
00315 }
00316 #endif 
00317 

Generated on Mon Sep 14 13:21:26 2009 for ATMEL by  doxygen 1.5.3