SAMV71 Xplained Ultra Software Package 1.5

ui.c

00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
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 
00063 /**
00064  * \brief Initializes and enables interrupt pin change
00065  */
00066 static void ui_enable_asynchronous_interrupt(void)
00067 {
00068 
00069 }
00070 
00071 /**
00072  * \brief Disables interrupt pin change
00073  */
00074 static void ui_disable_asynchronous_interrupt(void)
00075 {
00076 
00077 }
00078 
00079 /*! @} */
00080 
00081 /**
00082  * \name Main user interface functions
00083  * @{
00084  */
00085 void ui_init(void)
00086 {
00087     /* Initialize LEDs */
00088     LED_Configure(LED_YELLOW0);
00089 #if 2 == LED_NUM
00090     LED_Configure(LED_YELLOW1);
00091 #endif
00092 }
00093 
00094 void ui_usb_mode_change(bool b_host_mode)
00095 {
00096     (void)b_host_mode;
00097     ui_init();
00098 }
00099 
00100 /*! @} */
00101 
00102 /**
00103  * \name Host mode user interface functions
00104  * @{
00105  */
00106 
00107 /*! Status of device enumeration */
00108 static USBH_enum_status_t ui_enum_status = UHC_ENUM_DISCONNECT;
00109 /*! Blink frequency depending on device speed */
00110 static uint16_t ui_device_speed_blink;
00111 /*! Manages device mouse moving */
00112 static int8_t ui_x, ui_y, ui_scroll;
00113 
00114 void ui_usb_vbus_change(bool b_vbus_present)
00115 {
00116     b_vbus_present = b_vbus_present;
00117 
00118 }
00119 
00120 void ui_usb_vbus_error(void)
00121 {
00122 }
00123 
00124 void ui_usb_connection_event(USBH_device_t *pDev, bool b_present)
00125 {
00126     (void)pDev;
00127 
00128     if (b_present)
00129         LED_Set(LED_YELLOW0);
00130     else {
00131         LED_Clear(LED_YELLOW0);
00132         ui_enum_status = UHC_ENUM_DISCONNECT;
00133     }
00134 }
00135 
00136 void ui_usb_enum_event(USBH_device_t *pDev, USBH_enum_status_t status)
00137 {
00138     (void)pDev;
00139     ui_enum_status = status;
00140 
00141     if (ui_enum_status == UHC_ENUM_SUCCESS) {
00142         ui_x = 0, ui_y = 0, ui_scroll = 0;
00143 
00144         switch (pDev->speed) {
00145         case UHD_SPEED_HIGH:
00146             ui_device_speed_blink = 250;
00147             break;
00148 
00149         case UHD_SPEED_FULL:
00150             ui_device_speed_blink = 500;
00151             break;
00152 
00153         case UHD_SPEED_LOW:
00154         default:
00155             ui_device_speed_blink = 1000;
00156             break;
00157         }
00158     }
00159 }
00160 
00161 void ui_usb_wakeup_event(void)
00162 {
00163     ui_disable_asynchronous_interrupt();
00164 }
00165 
00166 void ui_usb_sof_event(void)
00167 {
00168     bool b_btn_state;
00169     static bool btn_suspend_and_remotewakeup = false;
00170     static uint16_t counter_sof = 0;
00171 
00172     if (ui_enum_status == UHC_ENUM_SUCCESS) {
00173         /* Display device enumerated and in active mode */
00174         if (++counter_sof > ui_device_speed_blink) {
00175             counter_sof = 0;
00176             LED_Toggle(LED_YELLOW0);
00177         }
00178 
00179         /* Scan button to enter in suspend mode and remote wakeup */
00180         /*b_btn_state = (!gpio_pin_is_high(GPIO_PUSH_BUTTON_1)) ?
00181                 true : false;*/
00182         b_btn_state = true;
00183 
00184         if (b_btn_state != btn_suspend_and_remotewakeup) {
00185             /* Button have changed */
00186             btn_suspend_and_remotewakeup = b_btn_state;
00187 
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 #if 2 == LED_NUM
00199             LED_Clear(LED_YELLOW1);
00200 #endif
00201         } else {
00202             ui_x = ui_y = ui_scroll = 0;
00203 #if 2 == LED_NUM
00204             LED_Set(LED_YELLOW1);
00205 #endif
00206         }
00207     }
00208 }
00209 
00210 static void ui_uhi_hid_mouse_btn(bool b_state)
00211 {
00212     static uint8_t nb_down = 0;
00213 
00214     if (b_state)
00215         nb_down++;
00216     else
00217         nb_down--;
00218 
00219     if (nb_down) {
00220         // do something
00221     } else {
00222         //do something
00223     }
00224 }
00225 
00226 void ui_uhi_hid_mouse_btn_left(bool b_state)
00227 {
00228     TRACE_INFO_WP("\r\t\t\t\tLeft Button clicked              ");
00229     ui_uhi_hid_mouse_btn(b_state);
00230 }
00231 
00232 void ui_uhi_hid_mouse_btn_right(bool b_state)
00233 {
00234     TRACE_INFO_WP("\r\t\t\t\tRight Button clicked             ");
00235     ui_uhi_hid_mouse_btn(b_state);
00236 }
00237 
00238 void ui_uhi_hid_mouse_btn_middle(bool b_state)
00239 {
00240     ui_uhi_hid_mouse_btn(b_state);
00241 }
00242 
00243 void ui_uhi_hid_mouse_move(int8_t x, int8_t y, int8_t scroll)
00244 {
00245     ui_x = x;
00246     ui_y = y;
00247     TRACE_INFO_WP("\r Mouse at X:%d, Y:%d      ", ui_x, ui_y);
00248     ui_scroll = scroll;
00249 }
00250 
00251 /*! @} */
00252 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines