00001 /* ---------------------------------------------------------------------------- 00002 * ATMEL Microcontroller Software Support 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2008, 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 /** \file 00031 * Implementation of the HIDDKeyboardInputReport class. 00032 */ 00033 00034 /** \addtogroup usbd_hid_key 00035 *@{ 00036 */ 00037 00038 /*------------------------------------------------------------------------------ 00039 * Headers 00040 *------------------------------------------------------------------------------*/ 00041 00042 #include <HIDDKeyboard.h> 00043 #include <USBLib_Trace.h> 00044 00045 /*------------------------------------------------------------------------------ 00046 * Exported functions 00047 *------------------------------------------------------------------------------*/ 00048 00049 /** 00050 * Initializes a keyboard input report instance. 00051 * \param report Pointer to a HIDDKeyboardInputReport instance. 00052 */ 00053 void HIDDKeyboardInputReport_Initialize(HIDDKeyboardInputReport *report) 00054 { 00055 uint32_t i; 00056 00057 report->bmModifierKeys = 0; 00058 for (i = 0; i < HIDDKeyboardInputReport_MAXKEYPRESSES; i++) { 00059 00060 report->pressedKeys[i] = 0; 00061 } 00062 } 00063 00064 /** 00065 * Reports a standard key as being pressed. 00066 * \param report Pointer to a HIDDKeyboardInputReport instance. 00067 * \param key Key code of the standard key. 00068 */ 00069 void HIDDKeyboardInputReport_PressStandardKey(HIDDKeyboardInputReport *report, 00070 uint8_t key) 00071 { 00072 /* Find first available slot */ 00073 uint32_t i = 0; 00074 uint8_t found = 0; 00075 while ((i < HIDDKeyboardInputReport_MAXKEYPRESSES) && !found) { 00076 00077 /* Free slot: no key referenced (code = 0) or ErrorRollOver */ 00078 if ((report->pressedKeys[i] == 0) 00079 || (report->pressedKeys[i] == HIDKeypad_ERRORROLLOVER)) { 00080 00081 found = 1; 00082 report->pressedKeys[i] = key; 00083 } 00084 00085 i++; 00086 } 00087 00088 /* Report ErrorRollOver in all fields if too many keys are pressed */ 00089 if (!found) { 00090 00091 for (i=0; i < HIDDKeyboardInputReport_MAXKEYPRESSES; i++) { 00092 00093 report->pressedKeys[i] = HIDKeypad_ERRORROLLOVER; 00094 } 00095 } 00096 } 00097 00098 /** 00099 * Reports a standard key as not being pressed anymore. 00100 * \param report Pointer to a HIDDKeyboardInputReport instance. 00101 * \param key Key code of the standard key 00102 */ 00103 void HIDDKeyboardInputReport_ReleaseStandardKey(HIDDKeyboardInputReport *report, 00104 uint8_t key) 00105 { 00106 /* Look for key in array */ 00107 uint32_t i = 0; 00108 uint8_t found = 0; 00109 while ((i < HIDDKeyboardInputReport_MAXKEYPRESSES) && !found) { 00110 00111 if (report->pressedKeys[i] == key) { 00112 00113 found = 1; 00114 report->pressedKeys[i] = 0; 00115 } 00116 00117 i++; 00118 } 00119 } 00120 00121 /** 00122 * Reports a modifier key as being currently pressed. 00123 * \param report Pointer to a HIDDKeyboardInputReport instance. 00124 * \param key Key code of the modifier key. 00125 */ 00126 void HIDDKeyboardInputReport_PressModifierKey(HIDDKeyboardInputReport *report, 00127 uint8_t key) 00128 { 00129 /* Set corresponding bit */ 00130 uint8_t bit = key - HIDDKeyboardDescriptors_FIRSTMODIFIERKEY; 00131 report->bmModifierKeys |= 1 << bit; 00132 } 00133 00134 /** 00135 * Reports a modifier key as not being pressed anymore. 00136 * \param report Pointer to a HIDDKeyboardInputReport instance. 00137 * \param key Key code of the modifier key. 00138 */ 00139 void HIDDKeyboardInputReport_ReleaseModifierKey(HIDDKeyboardInputReport *report, 00140 uint8_t key) 00141 { 00142 /* Clear corresponding bit */ 00143 uint8_t bit = key - HIDDKeyboardDescriptors_FIRSTMODIFIERKEY; 00144 report->bmModifierKeys &= ~(1 << bit); 00145 } 00146 00147 /**@}*/