SAMV71 Xplained Ultra Software Package 1.4

ui.c

00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2015, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 /**
00030  * \defgroup UI User Interface
00031  *
00032  *  - Human interface on SAM V71 Xplained Ultra board.:
00033  *   - Led 0 is continuously on when a device is connected
00034  *   - Led 0 blinks when USB host has checked and enabled HID interface
00035  *   - The blink is slow (1s) with low speed device
00036  *   - The blink is normal (0.5s) with full speed device
00037  *   - The blink is fast (0.25s) with high speed device
00038  *   - Led 1 is on when the mouse move
00039  */
00040 
00041 #include "board.h"
00042 #include "ui.h"
00043 
00044 /* Wakeup pin is RIGHT CLICK (fast wakeup 14) */
00045 #define  RESUME_PMC_FSTT (PMC_FSMR_FSTT14)
00046 #define  RESUME_PIN      (GPIO_PUSH_BUTTON_2)
00047 #define  RESUME_PIO      (PIN_PUSHBUTTON_2_PIO)
00048 #define  RESUME_PIO_ID   (PIN_PUSHBUTTON_2_ID)
00049 #define  RESUME_PIO_MASK (PIN_PUSHBUTTON_2_MASK)
00050 #define  RESUME_PIO_ATTR (PIN_PUSHBUTTON_2_ATTR)
00051 
00052 /**
00053  * \name Internal routines to manage asynchronous interrupt pin change
00054  * This interrupt is connected to a switch and allows to wakeup CPU in low sleep
00055  * mode.
00056  * This wakeup the USB devices connected via a downstream resume.
00057  * @{
00058  */
00059 static void ui_enable_asynchronous_interrupt(void);
00060 static void ui_disable_asynchronous_interrupt(void);
00061 
00062 /* Interrupt on "pin change" from RIGHT CLICK to do wakeup on USB
00063  *  Note:
00064  *  This interrupt is enable when the USB host enable remotewakeup feature
00065  *  This interrupt wakeup the CPU if this one is in idle mode */
00066 static void ui_wakeup_handler(uint32_t id, uint32_t mask)
00067 {
00068     
00069 }
00070 
00071 /**
00072  * \brief Initializes and enables interrupt pin change
00073  */
00074 static void ui_enable_asynchronous_interrupt(void)
00075 {
00076     
00077 }
00078 
00079 /**
00080  * \brief Disables interrupt pin change
00081  */
00082 static void ui_disable_asynchronous_interrupt(void)
00083 {
00084     
00085 }
00086 
00087 /*! @} */
00088 
00089 /**
00090  * \name Main user interface functions
00091  * @{
00092  */
00093 void ui_init(void)
00094 {
00095    /* Initialize LEDs */
00096     LED_Configure( LED_YELLOW0 ) ;
00097     LED_Configure( LED_YELLOW1 ) ;
00098 }
00099 
00100 void ui_usb_mode_change(bool b_host_mode)
00101 {
00102     (void)b_host_mode;
00103     ui_init();
00104 }
00105 
00106 /*! @} */
00107 
00108 /**
00109  * \name Host mode user interface functions
00110  * @{
00111  */
00112 
00113 /*! Status of device enumeration */
00114 static USBH_enum_status_t ui_enum_status = UHC_ENUM_DISCONNECT;
00115 /*! Blink frequency depending on device speed */
00116 static uint16_t ui_device_speed_blink;
00117 /*! Manages device mouse moving */
00118 static int8_t ui_x, ui_y, ui_scroll;
00119 
00120 void ui_usb_vbus_change(bool b_vbus_present)
00121 {
00122     
00123 }
00124 
00125 void ui_usb_vbus_error(void)
00126 {
00127 }
00128 
00129 void ui_usb_connection_event(USBH_device_t *pDev, bool b_present)
00130 {
00131     (void)pDev;
00132     if (b_present) {
00133         LED_Set(LED_YELLOW0);
00134     } else {
00135         LED_Clear(LED_YELLOW0);
00136         ui_enum_status = UHC_ENUM_DISCONNECT;
00137     }
00138 }
00139 
00140 void ui_usb_enum_event(USBH_device_t *pDev, USBH_enum_status_t status)
00141 {
00142     (void)pDev;
00143     ui_enum_status = status;
00144     if (ui_enum_status == UHC_ENUM_SUCCESS) {
00145         ui_x = 0, ui_y = 0, ui_scroll = 0;
00146         switch (pDev->speed) {
00147         case UHD_SPEED_HIGH:
00148             ui_device_speed_blink = 250;
00149             break;
00150 
00151         case UHD_SPEED_FULL:
00152             ui_device_speed_blink = 500;
00153             break;
00154 
00155         case UHD_SPEED_LOW:
00156         default:
00157             ui_device_speed_blink = 1000;
00158             break;
00159         }
00160     }
00161 }
00162 
00163 void ui_usb_wakeup_event(void)
00164 {
00165     ui_disable_asynchronous_interrupt();
00166 }
00167 
00168 void ui_usb_sof_event(void)
00169 {
00170     bool b_btn_state;
00171     static bool btn_suspend_and_remotewakeup = false;
00172     static uint16_t counter_sof = 0;
00173 
00174     if (ui_enum_status == UHC_ENUM_SUCCESS) {
00175         /* Display device enumerated and in active mode */
00176         if (++counter_sof > ui_device_speed_blink) {
00177             counter_sof = 0;
00178             LED_Toggle(LED_YELLOW0);
00179             }
00180 
00181         /* Scan button to enter in suspend mode and remote wakeup */
00182         /*b_btn_state = (!gpio_pin_is_high(GPIO_PUSH_BUTTON_1)) ?
00183                 true : false;*/
00184         b_btn_state = true ;
00185         if (b_btn_state != btn_suspend_and_remotewakeup) {
00186             /* Button have changed */
00187             btn_suspend_and_remotewakeup = b_btn_state;
00188             if (b_btn_state) {
00189                 /* Button has been pressed */               
00190                 ui_enable_asynchronous_interrupt();
00191                 USBH_suspend(true);
00192                 return;
00193             }
00194         }
00195 
00196         /* Power on a LED when the mouse move */
00197         if (!ui_x && !ui_y && !ui_scroll) {
00198             LED_Clear(LED_YELLOW1);
00199         } else {
00200             ui_x = ui_y = ui_scroll = 0;
00201             LED_Set(LED_YELLOW1);
00202         }
00203     }
00204 }
00205 
00206 static void ui_uhi_hid_mouse_btn(bool b_state)
00207 {
00208     static uint8_t nb_down = 0;
00209     if (b_state) {
00210         nb_down++;
00211     } else {
00212         nb_down--;
00213     }
00214 
00215     if (nb_down) {
00216         // do something
00217     } else {
00218         //do something
00219     }
00220 }
00221 
00222 void ui_uhi_hid_mouse_btn_left(bool b_state)
00223 {
00224     TRACE_INFO_WP("\r\t\t\t\tLeft Button clicked              ");
00225     ui_uhi_hid_mouse_btn(b_state);
00226 }
00227 
00228 void ui_uhi_hid_mouse_btn_right(bool b_state)
00229 {
00230     TRACE_INFO_WP("\r\t\t\t\tRight Button clicked             ");
00231     ui_uhi_hid_mouse_btn(b_state);
00232 }
00233 
00234 void ui_uhi_hid_mouse_btn_middle(bool b_state)
00235 {
00236     ui_uhi_hid_mouse_btn(b_state);
00237 }
00238 
00239 void ui_uhi_hid_mouse_move(int8_t x, int8_t y, int8_t scroll)
00240 {
00241     ui_x = x;
00242     ui_y = y;
00243     TRACE_INFO_WP("\r Mouse at X:%d, Y:%d      ", ui_x, ui_y);
00244     ui_scroll = scroll;
00245 }
00246 
00247 /*! @} */
00248 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines