| 1 | /***************************************************************************//** |
| 2 | * @file Command.c |
| 3 | * @brief Implementation of the program's main function. |
| 4 | * @author Mihai Bancisor ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2013(c) Analog Devices, Inc. |
| 7 | * |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions are met: |
| 12 | * - Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * - Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * - The use of this software may or may not infringe the patent rights |
| 22 | * of one or more patent holders. This license does not release you |
| 23 | * from the requirement that you obtain separate licenses from these |
| 24 | * patent holders to use this software. |
| 25 | * - Use of the software either in source or binary form, must be run |
| 26 | * on or directly connected to an Analog Devices Inc. component. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | *******************************************************************************/ |
| 39 | |
| 40 | /******************************************************************************/ |
| 41 | /***************************** Include Files **********************************/ |
| 42 | /******************************************************************************/ |
| 43 | #include "Command.h" |
| 44 | #include "ADP5589.h" |
| 45 | #include "Console.h" |
| 46 | |
| 47 | /******************************************************************************/ |
| 48 | /************************ Constants Definitions *******************************/ |
| 49 | /******************************************************************************/ |
| 50 | /*! List of available commands */ |
| 51 | const char* cmdList[] = {"help?", |
| 52 | "pwmOnTime=", |
| 53 | "pwmOffTime=", |
| 54 | "keyPressed?", |
| 55 | "keyReleased?" |
| 56 | }; |
| 57 | const char* cmdDescription[] = { |
| 58 | " - Displays all available commands.", |
| 59 | " - On-time of the PWM pulse. Accepted values: \r\n\ |
| 60 | \t1 .. 65535[us] - time when the signal is high.", |
| 61 | " - Off-time of the PWM pulse. Accepted values: \r\n\ |
| 62 | \t1 .. 65535[us] - time when the signal is low.", |
| 63 | " - Displays the last pressed key.", |
| 64 | " - Displays the last released key."}; |
| 65 | const char* cmdExample[] = {"", |
| 66 | "To set pe PWM on-time to 1000us, type pwmOnTime=1000.", |
| 67 | "To set pe PWM off-time to 40000us, type pwmOffTime=40000.", |
| 68 | "", |
| 69 | "", |
| 70 | }; |
| 71 | |
| 72 | const char cmdNo = (sizeof(cmdList) / sizeof(const char*)); |
| 73 | |
| 74 | /******************************************************************************/ |
| 75 | /************************ Variables Definitions *******************************/ |
| 76 | /******************************************************************************/ |
| 77 | cmdFunction cmdFunctions[5] = {GetHelp, SetPwmOnTime, SetPwmOffTime, |
| 78 | GetKeyPressed, GetKeyReleased}; |
| 79 | unsigned short pwmOnTime = 1; |
| 80 | unsigned short pwmOffTime = 1; |
| 81 | char lastKeyR = 0; |
| 82 | char lastKeyP = 0; |
| 83 | char keyR = 0; |
| 84 | char keyP = 0; |
| 85 | |
| 86 | /******************************************************************************/ |
| 87 | /************************ Functions Description *******************************/ |
| 88 | /******************************************************************************/ |
| 89 | |
| 90 | /***************************************************************************//** |
| 91 | * @brief Displays all available commands. |
| 92 | * |
| 93 | * @return None. |
| 94 | *******************************************************************************/ |
| 95 | void GetHelp(double* param, char paramNo) /*!< "help?" command */ |
| 96 | { |
| 97 | char displayCmd; |
| 98 | |
| 99 | CONSOLE_Print("Available commands:\r\n"); |
| 100 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 101 | { |
| 102 | CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd], |
| 103 | (char*)cmdDescription[displayCmd]); |
| 104 | } |
| 105 | } |
| 106 | /***************************************************************************//** |
| 107 | * @brief Initializes the device. |
| 108 | * |
| 109 | * @return - The result of the initialization. |
| 110 | * Example: ERROR - the device was not initialized or the device |
| 111 | * is not present. |
| 112 | * SUCCES - the device was initialized and the device |
| 113 | * is present. |
| 114 | *******************************************************************************/ |
| 115 | char DoDeviceInit(void) |
| 116 | { |
| 117 | if(ADP5589_Init() == 0) |
| 118 | { |
| 119 | CONSOLE_Print("ADP5589 OK\r\n"); |
| 120 | GetHelp(NULL, 0); |
| 121 | ADP5589_InitKey(PMOD_IOXP_J2); /*!< Init ADP5589 as keyboard decoder */ |
| 122 | ADP5589_InitPwm(); /*!< Init PWM on pin R3 */ |
| 123 | ADP5589_SetPwm(1,1); |
| 124 | return SUCCESS; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | CONSOLE_Print("ADP5589 Error\r\n"); |
| 129 | |
| 130 | return ERROR; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /***************************************************************************//** |
| 135 | * @brief Sets the PWM on-time; PWM is mapped on pin R3. |
| 136 | * |
| 137 | * @param param[0] - set power-on time in us; |
| 138 | * paramNo - number of parameters (must be >=1); only the first parameter is taken into account; |
| 139 | * |
| 140 | * @return None. |
| 141 | *******************************************************************************/ |
| 142 | void SetPwmOnTime(double* param, char paramNo) /*!< "pwmOnTime=" command */ |
| 143 | { |
| 144 | if(paramNo >= 1) |
| 145 | { |
| 146 | if(param[0] < 1) |
| 147 | { |
| 148 | param[0] = 1; |
| 149 | } |
| 150 | if(param[0] > 65535) |
| 151 | { |
| 152 | param[0] = 65535; |
| 153 | } |
| 154 | pwmOnTime = (unsigned short)param[0]; |
| 155 | /*!< Configure the PWM on/off registers */ |
| 156 | ADP5589_SetPwm(pwmOffTime, pwmOnTime); |
| 157 | /*!< Send feedback to user */ |
| 158 | CONSOLE_Print("%s%d \r\n",(char*)cmdList[1],pwmOnTime); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | /*!< Display error messages. */ |
| 163 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 164 | CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]); |
| 165 | CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /***************************************************************************//** |
| 170 | * @brief Sets the PWM off-time; PWM is mapped on pin R3. |
| 171 | * |
| 172 | * @param param[0] - set power-on time in us; |
| 173 | * paramNo - number of parameters (must be >=1); only the first parameter is taken into account; |
| 174 | * |
| 175 | * @return None. |
| 176 | *******************************************************************************/ |
| 177 | void SetPwmOffTime(double* param, char paramNo) /*!< "pwmOffTime=" command */ |
| 178 | { |
| 179 | if(paramNo >= 1) |
| 180 | { |
| 181 | if(param[0] < 1) |
| 182 | { |
| 183 | param[0] = 1; |
| 184 | } |
| 185 | if(param[0] > 65535) |
| 186 | { |
| 187 | param[0] = 65535; |
| 188 | } |
| 189 | pwmOffTime = (unsigned short)param[0]; |
| 190 | /*!< Configure the PWM on/off registers */ |
| 191 | ADP5589_SetPwm(pwmOffTime, pwmOnTime); |
| 192 | /*!< Send feedback to user */ |
| 193 | CONSOLE_Print("%s%d \r\n",(char*)cmdList[2],pwmOffTime); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | /*!< Display error messages. */ |
| 198 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 199 | CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]); |
| 200 | CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /***************************************************************************//** |
| 205 | * @brief Displays the last pressed key. |
| 206 | * |
| 207 | * @return None. |
| 208 | *******************************************************************************/ |
| 209 | void GetKeyPressed(double* param, char paramNo) /*!< "keyPressed?" command */ |
| 210 | { |
| 211 | unsigned char statusReg = 0; |
| 212 | unsigned char eventCount = 0; |
| 213 | unsigned char eventIndex = 0; |
| 214 | unsigned char event = 0; |
| 215 | |
| 216 | /*!< Determine the FIFO events number */ |
| 217 | statusReg = ADP5589_GetRegisterValue(ADP5589_ADR_STATUS); |
| 218 | eventCount = (statusReg & ADP5589_STATUS_EC(0x1F)); |
| 219 | /*!< Verify if a key press or key release took place */ |
| 220 | for(eventIndex = 0; eventIndex < eventCount; eventIndex++) |
| 221 | { |
| 222 | event = ADP5589_GetRegisterValue(ADP5589_ADR_FIFO1); |
| 223 | keyP = ADP5589_KeyDecode(event,ADP5589_EVENT_KEY_PRESSED,PMOD_IOXP_J2); |
| 224 | keyR = ADP5589_KeyDecode(event,ADP5589_EVENT_KEY_RELEASED,PMOD_IOXP_J2); |
| 225 | if (keyP != 'x') |
| 226 | { |
| 227 | lastKeyP = keyP; |
| 228 | } |
| 229 | if (keyR != 'x') |
| 230 | { |
| 231 | lastKeyR = keyR; |
| 232 | } |
| 233 | } |
| 234 | /*!< Send feedback to user */ |
| 235 | if (lastKeyP == 0) |
| 236 | { |
| 237 | CONSOLE_Print("Last key pressed: none. \r\n"); |
| 238 | } |
| 239 | else |
| 240 | CONSOLE_Print("Last key pressed: %c. \r\n",lastKeyP); |
| 241 | } |
| 242 | |
| 243 | /***************************************************************************//** |
| 244 | * @brief Displays the last released key. |
| 245 | * |
| 246 | * @return None. |
| 247 | *******************************************************************************/ |
| 248 | void GetKeyReleased(double* param, char paramNo) /*!< "keyReleased?" command */ |
| 249 | { |
| 250 | unsigned char statusReg = 0; |
| 251 | unsigned char eventCount = 0; |
| 252 | unsigned char eventIndex = 0; |
| 253 | unsigned char event = 0; |
| 254 | |
| 255 | /*!< Determine the FIFO events number */ |
| 256 | statusReg = ADP5589_GetRegisterValue(ADP5589_ADR_STATUS); |
| 257 | eventCount = (statusReg & ADP5589_STATUS_EC(0x1F)); |
| 258 | /*!< Verify if a key press or key release took place */ |
| 259 | for(eventIndex = 0; eventIndex < eventCount; eventIndex++) |
| 260 | { |
| 261 | event = ADP5589_GetRegisterValue(ADP5589_ADR_FIFO1); |
| 262 | keyP = ADP5589_KeyDecode(event,ADP5589_EVENT_KEY_PRESSED,PMOD_IOXP_J2); |
| 263 | keyR = ADP5589_KeyDecode(event,ADP5589_EVENT_KEY_RELEASED,PMOD_IOXP_J2); |
| 264 | if (keyP != 'x') |
| 265 | { |
| 266 | lastKeyP = keyP; |
| 267 | } |
| 268 | if (keyR != 'x') |
| 269 | { |
| 270 | lastKeyR = keyR; |
| 271 | } |
| 272 | } |
| 273 | if (lastKeyR == 0) |
| 274 | { |
| 275 | CONSOLE_Print("Last key released: none. \r\n"); |
| 276 | } |
| 277 | else |
| 278 | CONSOLE_Print("Last key released: %c. \r\n",lastKeyR); |
| 279 | } |