Will Porter's Hw2 Code - Glove based chording keyboard.
Dependencies: PinDetect_KL25Z mbed
main.cpp
00001 #include "mbed.h" 00002 #include "PinDetect.h" 00003 00004 Serial pc(USBTX,USBRX); 00005 00006 char alphabet[] = {'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','\0'}; 00007 int indices[] = {128,64,32,8,4,2,136,132,130,129,72,68,66,65,40,36,34,33,24,20,18,17,96,6,12,3}; 00008 00009 DigitalOut highPin(D15); // Pin Used to bring left side buttons high may not be needed 00010 bool boolArray[8]; 00011 00012 // Left side pins 00013 PinDetect L1(PTD3); 00014 PinDetect L2(PTD2); 00015 PinDetect L3(PTD0); 00016 PinDetect L4(PTC8); 00017 00018 // Right side pins 00019 PinDetect R1(PTA16); 00020 PinDetect R2(PTC17); 00021 PinDetect R3(PTC16); 00022 PinDetect R4(PTC11); 00023 00024 // Left Button ISRs 00025 void L1ButtonHeld(void){ 00026 boolArray[0] = 0; 00027 } 00028 void L1ButtonRelease(void){ 00029 boolArray[0] = 1; 00030 } 00031 void L2ButtonHeld(void){ 00032 boolArray[1] = 0; 00033 } 00034 void L2ButtonRelease(void){ 00035 boolArray[1] = 1; 00036 } 00037 void L3ButtonHeld(void){ 00038 boolArray[2] = 0; 00039 } 00040 void L3ButtonRelease(void){ 00041 boolArray[2] = 1; 00042 } 00043 void L4ButtonHeld(void){ 00044 boolArray[3] = 0; 00045 } 00046 void L4ButtonRelease(void){ 00047 boolArray[3] = 1; 00048 } 00049 00050 // Right Side ISRs 00051 void R1ButtonHeld(void){ 00052 boolArray[4] = 0; 00053 } 00054 void R1ButtonRelease(void){ 00055 boolArray[4] = 1; 00056 } 00057 void R2ButtonHeld(void){ 00058 boolArray[5] = 0; 00059 } 00060 void R2ButtonRelease(void){ 00061 boolArray[5] = 1; 00062 } 00063 void R3ButtonHeld(void){ 00064 boolArray[6] = 0; 00065 } 00066 void R3ButtonRelease(void){ 00067 boolArray[6] = 1; 00068 } 00069 void R4ButtonHeld(void){ 00070 boolArray[7] = 0; 00071 } 00072 void R4ButtonRelease(void){ 00073 boolArray[7] = 1; 00074 } 00075 00076 int main() { 00077 pc.printf("Starting\n\r"); 00078 highPin = 1; 00079 00080 L1.attach_asserted(&L1ButtonHeld); 00081 L1.attach_deasserted(&L1ButtonRelease); 00082 L1.setSampleFrequency(); 00083 L1.mode(PullUp); 00084 00085 L2.attach_asserted(&L2ButtonHeld); 00086 L2.attach_deasserted(&L2ButtonRelease); 00087 L2.setSampleFrequency(); 00088 L2.mode(PullUp); 00089 00090 L3.attach_asserted(&L3ButtonHeld); 00091 L3.attach_deasserted(&L3ButtonRelease); 00092 L3.setSampleFrequency(); 00093 L3.mode(PullUp); 00094 00095 L4.attach_asserted(&L4ButtonHeld); 00096 L4.attach_deasserted(&L4ButtonRelease); 00097 L4.setSampleFrequency(); 00098 L4.mode(PullUp); 00099 00100 R1.attach_asserted(&R1ButtonHeld); 00101 R1.attach_deasserted(&R1ButtonRelease); 00102 R1.setSampleFrequency(); 00103 R1.mode(PullUp); 00104 00105 R2.attach_asserted(&R2ButtonHeld); 00106 R2.attach_deasserted(&R2ButtonRelease); 00107 R2.setSampleFrequency(); 00108 R2.mode(PullUp); 00109 00110 R3.attach_asserted(&R3ButtonHeld); 00111 R3.attach_deasserted(&R3ButtonRelease); 00112 R3.setSampleFrequency(); 00113 R3.mode(PullUp); 00114 00115 R4.attach_asserted(&R4ButtonHeld); 00116 R4.attach_deasserted(&R4ButtonRelease); 00117 R4.setSampleFrequency(); 00118 R4.mode(PullUp); 00119 00120 while(1) { 00121 int num = 0; 00122 int pow = 1; 00123 00124 for (int i = 7; i>-1; i--){ 00125 num = num + boolArray[i] * pow; 00126 pow = pow * 2; 00127 } 00128 00129 for (int i = 0; i < 27; i++){ 00130 if (num == indices[i]){ 00131 pc.printf("%c",alphabet[i]); 00132 } 00133 } 00134 if (num == 16){ 00135 pc.printf("\b \b"); 00136 } 00137 00138 if (num == 1){ 00139 pc.printf(" "); 00140 } 00141 00142 wait(0.2); 00143 }
Generated on Wed Jul 20 2022 02:26:32 by
1.7.2