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: DebounceIn USBDevice mbed
Fork of Pushbutton_NoBounce_Demo by
main.cpp
00001 #include "mbed.h" 00002 #include "DebounceIn.h" 00003 #include "USBKeyboard.h" 00004 00005 // Serial connection for debugging 00006 Serial pc(USBTX, USBRX); 00007 // mimic USB keyboard 00008 USBKeyboard keyboard; 00009 // LED 00010 DigitalOut red(LED_RED); 00011 DigitalOut green(LED_GREEN); 00012 DigitalOut blue(LED_BLUE); 00013 // Push Buttons with Debounce 00014 DebounceIn pb1(D2); 00015 DebounceIn pb2(D3); 00016 DebounceIn pb3(D4); 00017 DebounceIn pb4(D5); 00018 DebounceIn pb5(D6); 00019 00020 // chord character map 00021 char chord_characters[32] = { 00022 '?', // 00000 0 00023 'e', // 00001 1 00024 'd', // 00010 2 00025 'i', // 00011 3 00026 'c', // 00100 4 00027 'l', // 00101 5 00028 'h', // 00110 6 00029 'v', // 00111 7 00030 'b', // 01000 8 00031 'n', // 01001 9 00032 'k', // 01010 10 00033 'u', // 01011 11 00034 'g', // 01100 12 00035 't', // 01101 13 00036 's', // 01110 14 00037 ' ', // 01111 15 SPACE 00038 'a', // 10000 16 00039 'o', // 10001 17 00040 'm', // 10010 18 00041 'y', // 10011 19 00042 'j', // 10100 20 00043 'x', // 10101 21 00044 'r', // 10110 22 00045 '?', // 10111 23 00046 'f', // 11000 24 00047 'w', // 11001 25 00048 'q', // 11010 26 00049 'z', // 11011 27 00050 'p', // 11100 28 00051 '?', // 11101 29 00052 '\b', // 11110 30 00053 '\n', // 11111 31 00054 }; 00055 00056 // print binary representation of char for debugging 00057 void printbinchar(char c) 00058 { 00059 for (int i = 7; i >= 0; --i) 00060 { 00061 pc.printf("%c", ( (c & (1 << i)) ? '1' : '0' )); 00062 } 00063 pc.printf("\r\n"); 00064 } 00065 00066 void setup() { 00067 // use internal pullup for pushbuttons 00068 pb1.mode(PullUp); 00069 pb2.mode(PullUp); 00070 pb3.mode(PullUp); 00071 pb4.mode(PullUp); 00072 pb5.mode(PullUp); 00073 // delay for initial pullup to take effect 00074 wait(0.1); 00075 // setup LED's 00076 red = 1, green = 1, blue = 1; 00077 } 00078 00079 int main() { 00080 setup(); 00081 int keycode = 0; 00082 while(1) { 00083 // determine which push buttons are depressed 00084 if(!pb1){ keycode |= 1; } 00085 if(!pb2){ keycode |= 2; } 00086 if(!pb3){ keycode |= 4; } 00087 if(!pb4){ keycode |= 8; } 00088 if(!pb5){ keycode |= 16; } 00089 // when all buttons are released and some buttons were previously depressed 00090 if(pb1 && pb2 && pb3 && pb4 && pb5 && keycode) 00091 { 00092 int chr = chord_characters[keycode]; 00093 pc.printf("%c", chr); 00094 keyboard.printf("%c", chr); 00095 00096 //pc.printf("%d, %c\r\n", chr, chr); 00097 //printbinchar(chr); 00098 //printbinchar(keycode); 00099 keycode = 0; 00100 } 00101 } 00102 } 00103 00104 //============================= 00105 // python code to generate chord_characters array 00106 // import binascii 00107 // 00108 // chord_map = { 00109 // "10000":"a", 00110 // "01000":"b", 00111 // "00100":"c", 00112 // "00010":"d", 00113 // "00001":"e", 00114 // "11000":"f", 00115 // "01100":"g", 00116 // "00110":"h", 00117 // "00011":"i", 00118 // "10100":"j", 00119 // "01010":"k", 00120 // "00101":"l", 00121 // "10010":"m", 00122 // "01001":"n", 00123 // "10001":"o", 00124 // "11100":"p", 00125 // "11010":"q", 00126 // "10110":"r", 00127 // "01110":"s", 00128 // "01101":"t", 00129 // "01011":"u", 00130 // "00111":"v", 00131 // "11001":"w", 00132 // "10101":"x", 00133 // "10011":"y", 00134 // "11011":"z", 00135 // "11110":"?", 00136 // "10111":"?", 00137 // "11101":"?", 00138 // "01111":" ", 00139 // "11111":"?", 00140 // } 00141 // 00142 // if __name__ == '__main__': 00143 // array = [0] * 32 00144 // for k, v in chord_map.iteritems(): 00145 // array[ int(k, 2) ] = k 00146 // for i in range(len(array)): 00147 // try: 00148 // print "'" + chord_map[array[i]] + "', //", array[i], i 00149 // except KeyError: 00150 // print "'" + "?" + "', //", array[i], i 00151 //===================================================
Generated on Thu Jul 14 2022 23:05:42 by
 1.7.2 
    