Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
keys.cpp
00001 #include "cisme.h" 00002 #include "keys.h" 00003 #include "buzzer.h" 00004 #include "debug.h" 00005 00006 #define KEY_RSP_LENGHT 6 00007 #define KEY_RSP_TYPE_OFFSET 2 00008 #define KEY_RSP_SYMBOL_OFFSET 3 00009 00010 static char inBuffer[KEY_RSP_LENGHT]; 00011 static unsigned char bufferIx; 00012 00013 // Get numeric key from char 00014 static Key keyNumberFromChar(char symbol) 00015 { 00016 static const char map[] = "0123456789"; 00017 00018 for (char ix = 0; ix < (sizeof(map) - 1); ix++) { 00019 if (map[ix] == symbol) { 00020 return (Key)ix; 00021 } 00022 } 00023 00024 return KEY_NONE; 00025 } 00026 00027 // Get special key from char 00028 static Key keySpecialFromChar(char symbol) 00029 { 00030 switch (symbol) { 00031 case 'A': 00032 return KEY_ASTERISK; 00033 case '3': 00034 return KEY_HASH; 00035 default: 00036 return KEY_NONE; 00037 } 00038 } 00039 00040 // Get key from response 00041 static Key keyGetFromResponse(void) 00042 { 00043 Key keyPressed = KEY_NONE; 00044 00045 switch (inBuffer[KEY_RSP_TYPE_OFFSET]) { 00046 case '2': 00047 keyPressed = keySpecialFromChar(inBuffer[KEY_RSP_SYMBOL_OFFSET]); 00048 break; 00049 case '3': 00050 keyPressed = keyNumberFromChar(inBuffer[KEY_RSP_SYMBOL_OFFSET]); 00051 break; 00052 default: 00053 keyPressed = KEY_NONE; 00054 break; 00055 } 00056 00057 // Clear buffer 00058 bufferIx = 0; 00059 memset(inBuffer, 0, KEY_RSP_LENGHT); 00060 00061 if (keyPressed != KEY_NONE) { 00062 buzzerBeep(0.1); 00063 } 00064 00065 return keyPressed; 00066 } 00067 00068 // Check that key is in range 00069 static bool keyInRange(Key key, const Key* range) 00070 { 00071 for (char keyIx = 0; range[keyIx] != KEY_END; keyIx++) { 00072 if (key == range[keyIx]) { 00073 return true; 00074 } 00075 } 00076 return false; 00077 } 00078 00079 void keyEvent(char byte) 00080 { 00081 inBuffer[bufferIx++] = byte; 00082 if (bufferIx == KEY_RSP_LENGHT) bufferIx = 0; 00083 } 00084 00085 Key keyWait(void) 00086 { 00087 // Wait for keypress response 00088 while (bufferIx != (KEY_RSP_LENGHT - 1)) { 00089 wait(0.1); 00090 } 00091 00092 return keyGetFromResponse(); 00093 } 00094 00095 Key keyWaitInRange(const Key* range) 00096 { 00097 Key key = KEY_NONE; 00098 while (!keyInRange(key, range)) { 00099 key = keyWait(); 00100 } 00101 00102 return key; 00103 } 00104 00105 Key keyGet(void) 00106 { 00107 if (bufferIx != (KEY_RSP_LENGHT - 1)) { 00108 wait(0.1); 00109 } 00110 00111 return keyGetFromResponse(); 00112 }
Generated on Tue Sep 27 2022 18:47:00 by
1.7.2