![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
b
touchbutton.cpp@0:4841f4169944, 2010-12-01 (annotated)
- Committer:
- henryeherman
- Date:
- Wed Dec 01 03:29:25 2010 +0000
- Revision:
- 0:4841f4169944
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henryeherman | 0:4841f4169944 | 1 | #include "mbed.h" |
henryeherman | 0:4841f4169944 | 2 | #include "touchbutton.h" |
henryeherman | 0:4841f4169944 | 3 | #include "com.h" |
henryeherman | 0:4841f4169944 | 4 | |
henryeherman | 0:4841f4169944 | 5 | #ifdef DEBUGTOUCHLED |
henryeherman | 0:4841f4169944 | 6 | DigitalOut debugled(LED1); |
henryeherman | 0:4841f4169944 | 7 | #endif |
henryeherman | 0:4841f4169944 | 8 | |
henryeherman | 0:4841f4169944 | 9 | float TouchButton::difference() { |
henryeherman | 0:4841f4169944 | 10 | return previousValue - currentValue; |
henryeherman | 0:4841f4169944 | 11 | } |
henryeherman | 0:4841f4169944 | 12 | |
henryeherman | 0:4841f4169944 | 13 | TouchButton::TouchButton(PinName ain, PinName din, PinName dout, float threshold, int debounceTime, char *nm) : sensor(ain), charger(din), ground(dout) { |
henryeherman | 0:4841f4169944 | 14 | |
henryeherman | 0:4841f4169944 | 15 | setThreshold(threshold); |
henryeherman | 0:4841f4169944 | 16 | setDebounceTime(debounceTime); |
henryeherman | 0:4841f4169944 | 17 | AnalogIn sensor(ain); |
henryeherman | 0:4841f4169944 | 18 | DigitalIn charger(din); |
henryeherman | 0:4841f4169944 | 19 | DigitalOut ground(dout); |
henryeherman | 0:4841f4169944 | 20 | ground.write(0); |
henryeherman | 0:4841f4169944 | 21 | timer.start(); |
henryeherman | 0:4841f4169944 | 22 | sample(); |
henryeherman | 0:4841f4169944 | 23 | storeValue(); |
henryeherman | 0:4841f4169944 | 24 | strcpy(name,nm); |
henryeherman | 0:4841f4169944 | 25 | #ifdef DEBUGTOUCH |
henryeherman | 0:4841f4169944 | 26 | com.printf("INIT BUTTON %s\r\n",name); |
henryeherman | 0:4841f4169944 | 27 | #endif |
henryeherman | 0:4841f4169944 | 28 | } |
henryeherman | 0:4841f4169944 | 29 | |
henryeherman | 0:4841f4169944 | 30 | void TouchButton::storeValue() { |
henryeherman | 0:4841f4169944 | 31 | previousValue=currentValue; |
henryeherman | 0:4841f4169944 | 32 | } |
henryeherman | 0:4841f4169944 | 33 | |
henryeherman | 0:4841f4169944 | 34 | void TouchButton::setThreshold(float level) { |
henryeherman | 0:4841f4169944 | 35 | analogThreshold = level; |
henryeherman | 0:4841f4169944 | 36 | } |
henryeherman | 0:4841f4169944 | 37 | |
henryeherman | 0:4841f4169944 | 38 | void TouchButton::setDebounceTime(int t) { |
henryeherman | 0:4841f4169944 | 39 | timeout_ms = t; |
henryeherman | 0:4841f4169944 | 40 | } |
henryeherman | 0:4841f4169944 | 41 | |
henryeherman | 0:4841f4169944 | 42 | bool TouchButton::checkState() { |
henryeherman | 0:4841f4169944 | 43 | float diff; |
henryeherman | 0:4841f4169944 | 44 | #ifdef DEBUGTOUCH |
henryeherman | 0:4841f4169944 | 45 | com.printf("CHECK BUTTON\r\n"); |
henryeherman | 0:4841f4169944 | 46 | #endif |
henryeherman | 0:4841f4169944 | 47 | |
henryeherman | 0:4841f4169944 | 48 | charge(); |
henryeherman | 0:4841f4169944 | 49 | #ifdef DEBUGTOUCH |
henryeherman | 0:4841f4169944 | 50 | com.printf("CHARGE BUTTON\r\n"); |
henryeherman | 0:4841f4169944 | 51 | #endif |
henryeherman | 0:4841f4169944 | 52 | sample(); |
henryeherman | 0:4841f4169944 | 53 | #ifdef DEBUGTOUCH |
henryeherman | 0:4841f4169944 | 54 | com.printf("CHARGE & SAMPLE BUTTON %s\r\n",name); |
henryeherman | 0:4841f4169944 | 55 | com.printf("Value:%3.2f\r\n", currentValue); |
henryeherman | 0:4841f4169944 | 56 | #endif |
henryeherman | 0:4841f4169944 | 57 | |
henryeherman | 0:4841f4169944 | 58 | |
henryeherman | 0:4841f4169944 | 59 | diff=previousValue - currentValue; |
henryeherman | 0:4841f4169944 | 60 | storeValue(); |
henryeherman | 0:4841f4169944 | 61 | if (diff > analogThreshold && timer.read_ms() > timeout_ms) { |
henryeherman | 0:4841f4169944 | 62 | timer.reset(); |
henryeherman | 0:4841f4169944 | 63 | #ifdef DEBUGTOUCH |
henryeherman | 0:4841f4169944 | 64 | com.printf("TOUCH %s\r\n", name); |
henryeherman | 0:4841f4169944 | 65 | #endif |
henryeherman | 0:4841f4169944 | 66 | #ifdef DEBUGTOUCHLED |
henryeherman | 0:4841f4169944 | 67 | //Toggle LED to show touch was registered |
henryeherman | 0:4841f4169944 | 68 | if (debugled.read()==1) |
henryeherman | 0:4841f4169944 | 69 | debugled=0; |
henryeherman | 0:4841f4169944 | 70 | else |
henryeherman | 0:4841f4169944 | 71 | debugled=1; |
henryeherman | 0:4841f4169944 | 72 | #endif |
henryeherman | 0:4841f4169944 | 73 | return true; |
henryeherman | 0:4841f4169944 | 74 | } else { |
henryeherman | 0:4841f4169944 | 75 | return false; |
henryeherman | 0:4841f4169944 | 76 | } |
henryeherman | 0:4841f4169944 | 77 | |
henryeherman | 0:4841f4169944 | 78 | } |
henryeherman | 0:4841f4169944 | 79 | |
henryeherman | 0:4841f4169944 | 80 | float TouchButton::sample() { |
henryeherman | 0:4841f4169944 | 81 | |
henryeherman | 0:4841f4169944 | 82 | float sum = 0; |
henryeherman | 0:4841f4169944 | 83 | for (int i=0; i<NUMSAMP;i++) { |
henryeherman | 0:4841f4169944 | 84 | float value = sensor.read(); |
henryeherman | 0:4841f4169944 | 85 | sum = sum + value; |
henryeherman | 0:4841f4169944 | 86 | } |
henryeherman | 0:4841f4169944 | 87 | currentValue = sum/NUMSAMP; |
henryeherman | 0:4841f4169944 | 88 | return currentValue; |
henryeherman | 0:4841f4169944 | 89 | |
henryeherman | 0:4841f4169944 | 90 | } |
henryeherman | 0:4841f4169944 | 91 | |
henryeherman | 0:4841f4169944 | 92 | void TouchButton::charge() { |
henryeherman | 0:4841f4169944 | 93 | charger.mode(PullUp); |
henryeherman | 0:4841f4169944 | 94 | charger.mode(PullNone); |
henryeherman | 0:4841f4169944 | 95 | wait(.005); |
henryeherman | 0:4841f4169944 | 96 | |
henryeherman | 0:4841f4169944 | 97 | } |