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.
main.cpp
00001 #include "mbed.h" 00002 #include "pt.h" 00003 00004 #define NBUFF 10 00005 00006 typedef enum { 00007 e_ValidInput, 00008 e_Init, 00009 } Event_t; 00010 00011 DigitalOut myled_1(LED1); 00012 DigitalOut myled_2(LED2); 00013 AnalogIn input_1(p17); 00014 AnalogIn input_2(p20); 00015 DigitalIn charger_1(p16); 00016 DigitalIn charger_2(p19); 00017 DigitalOut ground_1(p15); 00018 DigitalOut ground_2(p18); 00019 Serial pc(USBTX, USBRX); // tx, rx 00020 00021 int buff_1, buff_2; 00022 int last_1, last_2; 00023 char trigger[128]; 00024 int tr_len, tr_pos; 00025 char key; 00026 00027 int touchSense_1(void); 00028 int touchSense_2(void); 00029 int keyPressed(); 00030 int buttonTouched(); 00031 char readTouchInput(); 00032 00033 00034 // int flag; 00035 Event_t ev; 00036 00037 static 00038 PT_THREAD(pt_serial(struct pt *pt)) { 00039 PT_BEGIN(pt); 00040 while (1) { 00041 PT_YIELD_UNTIL(pt, ev == e_Init); 00042 trigger[0] = '\0'; 00043 tr_len = 0; 00044 pc.printf("Please input the trigger string in the format of Sbbb...bbbE\n\r"); 00045 PT_YIELD_UNTIL(pt, keyPressed()); 00046 if (key != 'S') { 00047 pc.printf("HOST ERROR\n\r"); 00048 PT_EXIT(pt); 00049 } 00050 while (1) { 00051 PT_YIELD_UNTIL(pt, keyPressed()); 00052 if (key == 'E') { 00053 pc.printf("\n\r"); 00054 if (tr_len == 0) { 00055 pc.printf("HOST ERROR\n\r"); 00056 PT_EXIT(pt); 00057 } else { 00058 trigger[tr_len] = '\0'; 00059 break; 00060 } 00061 } else if (key == '0' || key == '1') { 00062 trigger[tr_len++] = key; 00063 pc.putc(key); 00064 } else if (key == ' ') { 00065 pc.putc(key); 00066 } else { 00067 pc.printf("\n\rHOST ERROR\n\r"); 00068 PT_EXIT(pt); 00069 } 00070 } 00071 // Now we can process to Touch Input 00072 ev = e_ValidInput; 00073 } 00074 PT_END(pt); 00075 } 00076 00077 static 00078 PT_THREAD(pt_touch(struct pt *pt)) { 00079 PT_BEGIN(pt); 00080 while (1) { 00081 PT_YIELD_UNTIL(pt, ev == e_ValidInput); 00082 pc.printf("The trigger string is %s\n\r", trigger); 00083 pc.printf("Please start to touch the button\n\r"); 00084 tr_pos = 0; 00085 while(tr_pos < tr_len) { 00086 PT_YIELD_UNTIL(pt, buttonTouched()); 00087 pc.putc(key); 00088 if (key != trigger[tr_pos++]) { 00089 pc.printf("\n\rTOUCH ERROR\n\r"); 00090 ev = e_Init; 00091 PT_EXIT(pt); 00092 } 00093 } 00094 pc.printf("\n\r"); 00095 pc.printf("MATCH\n\r"); 00096 ev = e_Init; 00097 PT_EXIT(pt); 00098 } 00099 PT_END(pt); 00100 } 00101 00102 static struct pt pt1, pt2; 00103 int main() { 00104 PT_INIT(&pt1); 00105 PT_INIT(&pt2); 00106 // flag = S_SERIAL; 00107 ev = e_Init; 00108 while (1) { 00109 pt_serial(&pt1); 00110 pt_touch(&pt2); 00111 } 00112 } 00113 00114 int keyPressed() { 00115 key = pc.getc(); 00116 return 1; 00117 } 00118 00119 int buttonTouched() { 00120 key = readTouchInput(); 00121 return 1; 00122 } 00123 00124 int touchSense_1(void) { 00125 float sample; 00126 ground_1 = 0; 00127 charger_1.mode(PullUp); 00128 charger_1.mode(PullNone); 00129 sample=input_1.read(); 00130 if (sample < 0.3) { 00131 return 1; 00132 } else { 00133 return 0; 00134 } 00135 } 00136 00137 int touchSense_2(void) { 00138 float sample; 00139 ground_2 = 0; 00140 charger_2.mode(PullUp); 00141 charger_2.mode(PullNone); 00142 sample=input_2.read(); 00143 if (sample < 0.3) { 00144 return 1; 00145 } else { 00146 return 0; 00147 } 00148 } 00149 00150 char readTouchInput() { 00151 while (1) { 00152 if (touchSense_1()) { 00153 myled_1 = 1; 00154 if (!last_1) { 00155 last_1 = 1; 00156 buff_1 = NBUFF; 00157 } 00158 } else { 00159 if (last_1) { 00160 last_1 = 0; 00161 } else { 00162 if (buff_1 == 0) { 00163 if (myled_1) { 00164 myled_1 = 0; 00165 return '0'; 00166 } 00167 } else { 00168 buff_1--; 00169 } 00170 } 00171 } 00172 if (touchSense_2()) { 00173 myled_2 = 1; 00174 if (!last_2) { 00175 last_2 = 1; 00176 buff_2 = NBUFF; 00177 } 00178 } else { 00179 if (last_2) { 00180 last_2 = 0; 00181 } else { 00182 if (buff_2 == 0) { 00183 if (myled_2) { 00184 myled_2 = 0; 00185 return '1'; 00186 } 00187 } else { 00188 buff_2--; 00189 } 00190 } 00191 } 00192 wait(0.005); 00193 } 00194 }
Generated on Wed Jul 13 2022 11:59:11 by
1.7.2