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.
touchbutton.cpp
00001 #include "mbed.h" 00002 #include "touchbutton.h" 00003 #include "com.h" 00004 00005 #ifdef DEBUGTOUCHLED 00006 DigitalOut debugled(LED1); 00007 #endif 00008 00009 float TouchButton::difference() { 00010 return previousValue - currentValue; 00011 } 00012 00013 TouchButton::TouchButton(PinName ain, PinName din, PinName dout, float threshold, int debounceTime, char *nm) : sensor(ain), charger(din), ground(dout) { 00014 00015 setThreshold(threshold); 00016 setDebounceTime(debounceTime); 00017 AnalogIn sensor(ain); 00018 DigitalIn charger(din); 00019 DigitalOut ground(dout); 00020 ground.write(0); 00021 timer.start(); 00022 sample(); 00023 storeValue(); 00024 strcpy(name,nm); 00025 #ifdef DEBUGTOUCH 00026 com.printf("INIT BUTTON %s\r\n",name); 00027 #endif 00028 } 00029 00030 void TouchButton::storeValue() { 00031 previousValue=currentValue; 00032 } 00033 00034 void TouchButton::setThreshold(float level) { 00035 analogThreshold = level; 00036 } 00037 00038 void TouchButton::setDebounceTime(int t) { 00039 timeout_ms = t; 00040 } 00041 00042 bool TouchButton::checkState() { 00043 float diff; 00044 #ifdef DEBUGTOUCH 00045 com.printf("CHECK BUTTON\r\n"); 00046 #endif 00047 00048 charge(); 00049 #ifdef DEBUGTOUCH 00050 com.printf("CHARGE BUTTON\r\n"); 00051 #endif 00052 sample(); 00053 #ifdef DEBUGTOUCH 00054 com.printf("CHARGE & SAMPLE BUTTON %s\r\n",name); 00055 com.printf("Value:%3.2f\r\n", currentValue); 00056 #endif 00057 00058 00059 diff=previousValue - currentValue; 00060 storeValue(); 00061 if (diff > analogThreshold && timer.read_ms() > timeout_ms) { 00062 timer.reset(); 00063 #ifdef DEBUGTOUCH 00064 com.printf("TOUCH %s\r\n", name); 00065 #endif 00066 #ifdef DEBUGTOUCHLED 00067 //Toggle LED to show touch was registered 00068 if (debugled.read()==1) 00069 debugled=0; 00070 else 00071 debugled=1; 00072 #endif 00073 return true; 00074 } else { 00075 return false; 00076 } 00077 00078 } 00079 00080 float TouchButton::sample() { 00081 00082 float sum = 0; 00083 for (int i=0; i<NUMSAMP;i++) { 00084 float value = sensor.read(); 00085 sum = sum + value; 00086 } 00087 currentValue = sum/NUMSAMP; 00088 return currentValue; 00089 00090 } 00091 00092 void TouchButton::charge() { 00093 charger.mode(PullUp); 00094 charger.mode(PullNone); 00095 wait(.005); 00096 00097 }
Generated on Wed Jul 13 2022 17:38:20 by
1.7.2