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.
Dependencies: PinDetect_KL25Z mbed
main.cpp
00001 #include "mbed.h" 00002 #include "PinDetect.h" 00003 00004 /*CS294 HW2 - Text Entry code. */ 00005 00006 Serial pc(USBTX, USBRX); 00007 00008 PinDetect button1(D2); 00009 PinDetect button2(D3); 00010 PinDetect button3(D4); 00011 PinDetect button4(D5); 00012 PinDetect button5(D6); 00013 PinDetect button6(D7); 00014 PinDetect button7(D8); 00015 PinDetect button8(D9); 00016 PinDetect button9(D10); 00017 /*Reserved for Space bar*/ 00018 PinDetect button10(D11); 00019 00020 int state[10]; 00021 00022 int alphabet_map[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',' '}; 00023 00024 void button1_pressed() { 00025 state[0] = 1; 00026 } 00027 void button2_pressed() { 00028 state[1] = 1; 00029 } 00030 void button3_pressed() { 00031 state[2] = 1; 00032 } 00033 void button4_pressed() { 00034 state[3] = 1; 00035 } 00036 void button5_pressed() { 00037 state[4] = 1; 00038 } 00039 void button6_pressed() { 00040 state[5] = 1; 00041 } 00042 void button7_pressed() { 00043 state[6] = 1; 00044 } 00045 void button8_pressed() { 00046 state[7] = 1; 00047 } 00048 void button9_pressed() { 00049 state[8] = 1; 00050 } 00051 void button10_pressed() { 00052 state[9] = 1; 00053 } 00054 00055 void button1_released() { 00056 state[0] = 0; 00057 } 00058 void button2_released() { 00059 state[1] = 0; 00060 } 00061 void button3_released() { 00062 state[2] = 0; 00063 } 00064 void button4_released() { 00065 state[3] = 0; 00066 } 00067 void button5_released() { 00068 state[4] = 0; 00069 } 00070 void button6_released() { 00071 state[5] = 0; 00072 } 00073 void button7_released() { 00074 state[6] = 0; 00075 } 00076 void button8_released() { 00077 state[7] = 0; 00078 } 00079 void button9_released() { 00080 state[8] = 0; 00081 } 00082 void button10_released() { 00083 state[9] = 0; 00084 } 00085 00086 /* Function that performs Text-Entry algorithm. 00087 Input: Button state array. 00088 Output: Next character to display. */ 00089 char get_next_char(int state[]) { 00090 char next_char; 00091 if (state[9]) { 00092 /*Space bar*/ 00093 next_char = alphabet_map[26]; 00094 } else if (state[0]) { 00095 /*A - H*/ 00096 for (int j = 1; j < 9; j++) { 00097 if (state[j]) { 00098 next_char = alphabet_map[j - 1]; 00099 break; 00100 } 00101 } 00102 } else if (state[1]) { 00103 /*I - O*/ 00104 for (int j = 2; j < 9; j++) { 00105 if (state[j]) { 00106 next_char = alphabet_map[j + 6]; 00107 break; 00108 } 00109 } 00110 } else if (state[2]) { 00111 /*P - U*/ 00112 for (int j = 3; j < 9; j++) { 00113 if (state[j]) { 00114 next_char = alphabet_map[j + 12]; 00115 break; 00116 } 00117 } 00118 } else if (state[3]) { 00119 /*V - Z*/ 00120 for (int j = 4; j < 9; j++) { 00121 if (state[j]) { 00122 next_char = alphabet_map[j + 17]; 00123 break; 00124 } 00125 } 00126 } else { 00127 next_char = NULL; 00128 } 00129 return next_char; 00130 } 00131 00132 int main() { 00133 00134 /* Setup buttons. */ 00135 button1.mode(PullDown); 00136 button2.mode(PullDown); 00137 button3.mode(PullDown); 00138 button4.mode(PullDown); 00139 button5.mode(PullDown); 00140 button6.mode(PullDown); 00141 button7.mode(PullDown); 00142 button8.mode(PullDown); 00143 button9.mode(PullDown); 00144 button10.mode(PullDown); 00145 00146 /*Add callback functions for events when buttons are pressed. */ 00147 button1.attach_asserted(&button1_pressed); 00148 button2.attach_asserted(&button2_pressed); 00149 button3.attach_asserted(&button3_pressed); 00150 button4.attach_asserted(&button4_pressed); 00151 button5.attach_asserted(&button5_pressed); 00152 button6.attach_asserted(&button6_pressed); 00153 button7.attach_asserted(&button7_pressed); 00154 button8.attach_asserted(&button8_pressed); 00155 button9.attach_asserted(&button9_pressed); 00156 button10.attach_asserted(&button10_pressed); 00157 00158 /*Add callback functions for events when buttons are released. */ 00159 button1.attach_deasserted(&button1_released); 00160 button2.attach_deasserted(&button2_released); 00161 button3.attach_deasserted(&button3_released); 00162 button4.attach_deasserted(&button4_released); 00163 button5.attach_deasserted(&button5_released); 00164 button6.attach_deasserted(&button6_released); 00165 button7.attach_deasserted(&button7_released); 00166 button8.attach_deasserted(&button8_released); 00167 button9.attach_deasserted(&button9_released); 00168 button10.attach_deasserted(&button10_released); 00169 00170 button1.setAssertValue(0); 00171 button2.setAssertValue(0); 00172 button3.setAssertValue(0); 00173 button4.setAssertValue(0); 00174 button5.setAssertValue(0); 00175 button6.setAssertValue(0); 00176 button7.setAssertValue(0); 00177 button8.setAssertValue(0); 00178 button9.setAssertValue(0); 00179 button10.setAssertValue(0); 00180 00181 button1.setSampleFrequency(); 00182 button2.setSampleFrequency(); 00183 button3.setSampleFrequency(); 00184 button4.setSampleFrequency(); 00185 button5.setSampleFrequency(); 00186 button6.setSampleFrequency(); 00187 button7.setSampleFrequency(); 00188 button8.setSampleFrequency(); 00189 button9.setSampleFrequency(); 00190 button10.setSampleFrequency(); 00191 00192 char curr_char = NULL; 00193 char next_char; 00194 00195 /*Loop forever. */ 00196 while(1) { 00197 next_char = get_next_char(state); 00198 if (!next_char) { 00199 curr_char = next_char; 00200 } 00201 00202 if (next_char != curr_char) { 00203 pc.printf("%c", next_char); 00204 curr_char = next_char; 00205 } 00206 wait(.2); 00207 } 00208 00209 }
Generated on Sat Jul 16 2022 17:13:07 by
1.7.2