device_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 "device_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 
00055 #include "modules/usb/device_chap9/usb_standard_request.h"
00056 
00057 
00058 //_____ M A C R O S ________________________________________________________
00059 
00062 #define  Hid_mouse_report_up()            (g_hid_mouse_report[2]-=MOUSE_SPEED)
00063 #define  Hid_mouse_report_down()          (g_hid_mouse_report[2]+=MOUSE_SPEED)
00064 #define  Hid_mouse_report_left()          (g_hid_mouse_report[1]-=MOUSE_SPEED)
00065 #define  Hid_mouse_report_right()         (g_hid_mouse_report[1]+=MOUSE_SPEED)
00066 
00067 #define  Hid_mouse_report_scroll_up()     (g_hid_mouse_report[3]+=MOUSE_SPEED)
00068 #define  Hid_mouse_report_scroll_down()   (g_hid_mouse_report[3]-=MOUSE_SPEED)
00069         
00070 #define  Hid_mouse_report_click_left()    (g_hid_mouse_report[0] |= 0x01)
00071 #define  Hid_mouse_report_click_right()   (g_hid_mouse_report[0] |= 0x02)
00072 #define  Hid_mouse_report_click_middle()  (g_hid_mouse_report[0] |= 0x04)
00073         
00074         
00075 #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)
00077 
00080 #define Is_mouse_left_event()             Is_joy_left()
00081 #define Is_mouse_right_event()            Is_joy_right()
00082 #define Is_mouse_up_event()               Is_joy_up()
00083 #define Is_mouse_down_event()             Is_joy_down()
00084 
00085 #define SCROLL_STEP      10
00086 #define Is_mouse_scroll_up_event()        ((g_u16_pos_scroll+SCROLL_STEP) < Get_adc_pot_val())
00087 #define Is_mouse_scroll_down_event()      ((g_u16_pos_scroll-SCROLL_STEP) > Get_adc_pot_val())
00088 
00089 #define Is_mouse_click_left_event()       Is_btn_left()
00090 #define Is_mouse_click_middle_event()     FALSE
00091 #define Is_mouse_click_right_event()      Is_btn_middle()
00093 
00094 
00095 //_____ D E F I N I T I O N S ______________________________________________
00096 
00097 
00098 
00099 //_____ D E C L A R A T I O N S ____________________________________________
00100 
00101 bit   is_device_mouse_event(void);
00102 
00104 Bool  g_b_send_report;
00106 Bool  g_b_send_ack_report;
00108 U8    g_hid_mouse_report[4];
00110 U16 g_u16_pos_scroll;
00112 volatile U8  g_u8_cpt_sof   =0;
00113 volatile U16 g_u16_cpt2_sof =0;
00114 
00115 
00118 void device_mouse_task_init(void)
00119 {
00120    // Init SOF
00121    Usb_enable_sof_interrupt();
00122 
00123    // Init interface board
00124    Joy_init();
00125    Leds_init();
00126    Hwb_button_init();
00127    init_adc();
00128    g_u16_pos_scroll=Get_adc_pot_val();
00129    
00130    // Send a ack report at startup
00131    g_b_send_report      = FALSE;
00132    g_b_send_ack_report  = TRUE;
00133 }
00134 
00135 
00138 void device_mouse_task(void)
00139 {
00140    if(Is_usb_id_device())
00141    {
00142       Usb_enable_sof_interrupt();   // In case of interrupt disabled when USB ID pin change
00143    }
00144 }
00145 
00153 void device_mouse_task_by_IT(void)
00154 {
00155    if(!Is_device_enumerated())
00156       return;  // Device not ready
00157    
00158    if(!g_b_send_report)
00159    {
00160       // No report sending on going then check mouse event to eventualy fill a new report
00161       if(is_device_mouse_event())
00162       {
00163          // Enable sending of report
00164          g_b_send_report      = TRUE;
00165       }
00166    }
00167    
00168    if((!g_b_send_report)&&(!g_b_send_ack_report))
00169       return;  // No report and ack to send
00170    
00171    //** A report or ack must be send
00172    Usb_select_endpoint(EP_MOUSE_IN);
00173    if(!Is_usb_write_enabled())
00174       return;  // Endpoint no free
00175    
00176    if( g_b_send_report )
00177    {
00178       g_b_send_report      = FALSE;
00179       // Send an ack after a "clic" report only
00180       g_b_send_ack_report = (0!=g_hid_mouse_report[0]);
00181    }
00182    else
00183    {
00184       Hid_mouse_report_reset();     // Reset report to have a ack report
00185       g_b_send_ack_report  = FALSE;
00186    }
00187    // Send report
00188    Usb_write_byte(g_hid_mouse_report[0]);
00189    Usb_write_byte(g_hid_mouse_report[1]);
00190    Usb_write_byte(g_hid_mouse_report[2]);
00191    Usb_write_byte(g_hid_mouse_report[3]);
00192    Usb_ack_in_ready();
00193 }
00194 
00195 
00201 bit is_device_mouse_event(void)
00202 {
00203    Hid_mouse_report_reset();
00204    
00205    // Check Click events
00206    if(Is_mouse_click_left_event()) {
00207       Hid_mouse_report_click_left();
00208    }
00209    if(Is_mouse_click_middle_event()) {
00210       Hid_mouse_report_click_middle();
00211    }
00212    if(Is_mouse_click_right_event()) {
00213       Hid_mouse_report_click_right();
00214    }
00215 
00216    // Check Mouve events
00217    if (Is_mouse_left_event()) {
00218       Hid_mouse_report_left();
00219    }
00220    if (Is_mouse_right_event()) {
00221       Hid_mouse_report_right();
00222    }
00223    if (Is_mouse_up_event()) {
00224       Hid_mouse_report_up();
00225    }
00226    if (Is_mouse_down_event()) {
00227       Hid_mouse_report_down();
00228    }
00229 
00230    if (Is_mouse_scroll_up_event()) {
00231       g_u16_pos_scroll+=SCROLL_STEP;
00232       Hid_mouse_report_scroll_up();
00233    }
00234    if (Is_mouse_scroll_down_event()) {
00235       g_u16_pos_scroll-=SCROLL_STEP;
00236       Hid_mouse_report_scroll_down();
00237    }
00238 
00239    // Check report
00240    if((0==g_hid_mouse_report[0])
00241    && (0==g_hid_mouse_report[1])
00242    && (0==g_hid_mouse_report[2])
00243    && (0==g_hid_mouse_report[3]))
00244    {
00245       return FALSE;  // No event
00246    }
00247    return TRUE;      // Event(s) occurs
00248 }
00249 
00250 
00260 void sof_action()
00261 {
00262    U8 ep_selected;
00263    g_u16_cpt2_sof++;
00264    g_u8_cpt_sof++;
00265    if (g_u8_cpt_sof == NB_IDLE_POLLING_SOF)
00266    {
00267      ep_selected = Usb_get_selected_endpoint();   // the currently selected endpoint must be saved to avoid
00268                                                   // problems when interrupt happens during mass storage endpoint reading/writing
00269      device_mouse_task_by_IT();
00270      g_u8_cpt_sof = 0;
00271      Usb_select_endpoint(ep_selected);            // restore current selected endpoint
00272    }
00273 }
00274 

Generated on Wed Sep 23 09:33:17 2009 for ATMEL by  doxygen 1.5.3