A capacitive touch sensor using a analog input port.
Dependents: TouchSenseTestProgram
TouchSense.h@0:5a0704efe081, 2013-08-14 (annotated)
- Committer:
- shintamainjp
- Date:
- Wed Aug 14 09:08:59 2013 +0000
- Revision:
- 0:5a0704efe081
- Child:
- 1:08658b9787d8
This is the initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shintamainjp | 0:5a0704efe081 | 1 | /* mbed Microcontroller Library |
shintamainjp | 0:5a0704efe081 | 2 | * Copyright (c) 2013 Shinichiro Nakamura |
shintamainjp | 0:5a0704efe081 | 3 | * |
shintamainjp | 0:5a0704efe081 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
shintamainjp | 0:5a0704efe081 | 5 | * you may not use this file except in compliance with the License. |
shintamainjp | 0:5a0704efe081 | 6 | * You may obtain a copy of the License at |
shintamainjp | 0:5a0704efe081 | 7 | * |
shintamainjp | 0:5a0704efe081 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
shintamainjp | 0:5a0704efe081 | 9 | * |
shintamainjp | 0:5a0704efe081 | 10 | * Unless required by applicable law or agreed to in writing, software |
shintamainjp | 0:5a0704efe081 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
shintamainjp | 0:5a0704efe081 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
shintamainjp | 0:5a0704efe081 | 13 | * See the License for the specific language governing permissions and |
shintamainjp | 0:5a0704efe081 | 14 | * limitations under the License. |
shintamainjp | 0:5a0704efe081 | 15 | */ |
shintamainjp | 0:5a0704efe081 | 16 | |
shintamainjp | 0:5a0704efe081 | 17 | #ifndef TOUCHSENSE_H |
shintamainjp | 0:5a0704efe081 | 18 | #define TOUCHSENSE_H |
shintamainjp | 0:5a0704efe081 | 19 | |
shintamainjp | 0:5a0704efe081 | 20 | #include "platform.h" |
shintamainjp | 0:5a0704efe081 | 21 | #include "gpio_api.h" |
shintamainjp | 0:5a0704efe081 | 22 | #include "analogin_api.h" |
shintamainjp | 0:5a0704efe081 | 23 | |
shintamainjp | 0:5a0704efe081 | 24 | namespace mbed { |
shintamainjp | 0:5a0704efe081 | 25 | |
shintamainjp | 0:5a0704efe081 | 26 | /** |
shintamainjp | 0:5a0704efe081 | 27 | * A capacitive touch sensor using analog input port. |
shintamainjp | 0:5a0704efe081 | 28 | * |
shintamainjp | 0:5a0704efe081 | 29 | * Example: |
shintamainjp | 0:5a0704efe081 | 30 | * @code |
shintamainjp | 0:5a0704efe081 | 31 | * #include "mbed.h" |
shintamainjp | 0:5a0704efe081 | 32 | * |
shintamainjp | 0:5a0704efe081 | 33 | * TouchSense tp(p18); |
shintamainjp | 0:5a0704efe081 | 34 | * |
shintamainjp | 0:5a0704efe081 | 35 | * int main() { |
shintamainjp | 0:5a0704efe081 | 36 | * tp.calibration(); |
shintamainjp | 0:5a0704efe081 | 37 | * while(1) { |
shintamainjp | 0:5a0704efe081 | 38 | * if (tp.sense()) { |
shintamainjp | 0:5a0704efe081 | 39 | * printf("ON\r\n"); |
shintamainjp | 0:5a0704efe081 | 40 | * } else { |
shintamainjp | 0:5a0704efe081 | 41 | * printf("OFF\r\n"); |
shintamainjp | 0:5a0704efe081 | 42 | * } |
shintamainjp | 0:5a0704efe081 | 43 | * wait(0.1); |
shintamainjp | 0:5a0704efe081 | 44 | * } |
shintamainjp | 0:5a0704efe081 | 45 | * } |
shintamainjp | 0:5a0704efe081 | 46 | * @endcode |
shintamainjp | 0:5a0704efe081 | 47 | */ |
shintamainjp | 0:5a0704efe081 | 48 | class TouchSense { |
shintamainjp | 0:5a0704efe081 | 49 | |
shintamainjp | 0:5a0704efe081 | 50 | public: |
shintamainjp | 0:5a0704efe081 | 51 | /** |
shintamainjp | 0:5a0704efe081 | 52 | * Create a TouchSense connected to the specified pin |
shintamainjp | 0:5a0704efe081 | 53 | * |
shintamainjp | 0:5a0704efe081 | 54 | * @param pin TouchSense pin to connect to |
shintamainjp | 0:5a0704efe081 | 55 | */ |
shintamainjp | 0:5a0704efe081 | 56 | TouchSense(PinName pin) : mypin(pin), calval(0) { |
shintamainjp | 0:5a0704efe081 | 57 | } |
shintamainjp | 0:5a0704efe081 | 58 | |
shintamainjp | 0:5a0704efe081 | 59 | void calibration() { |
shintamainjp | 0:5a0704efe081 | 60 | uint16_t f1, f2, f3, f4; |
shintamainjp | 0:5a0704efe081 | 61 | f1 = execute(); |
shintamainjp | 0:5a0704efe081 | 62 | f2 = execute(); |
shintamainjp | 0:5a0704efe081 | 63 | f3 = execute(); |
shintamainjp | 0:5a0704efe081 | 64 | f4 = execute(); |
shintamainjp | 0:5a0704efe081 | 65 | calval = (f1 + f2 + f3 + f4) / 4.0; |
shintamainjp | 0:5a0704efe081 | 66 | } |
shintamainjp | 0:5a0704efe081 | 67 | |
shintamainjp | 0:5a0704efe081 | 68 | bool sense() { |
shintamainjp | 0:5a0704efe081 | 69 | uint16_t f = execute(); |
shintamainjp | 0:5a0704efe081 | 70 | return (calval * 3.00) < f; |
shintamainjp | 0:5a0704efe081 | 71 | } |
shintamainjp | 0:5a0704efe081 | 72 | |
shintamainjp | 0:5a0704efe081 | 73 | private: |
shintamainjp | 0:5a0704efe081 | 74 | |
shintamainjp | 0:5a0704efe081 | 75 | uint16_t execute() { |
shintamainjp | 0:5a0704efe081 | 76 | gpio_init(&gpio, mypin, PIN_OUTPUT); |
shintamainjp | 0:5a0704efe081 | 77 | gpio_write(&gpio, 1); |
shintamainjp | 0:5a0704efe081 | 78 | __nop(); |
shintamainjp | 0:5a0704efe081 | 79 | __nop(); |
shintamainjp | 0:5a0704efe081 | 80 | __nop(); |
shintamainjp | 0:5a0704efe081 | 81 | __nop(); |
shintamainjp | 0:5a0704efe081 | 82 | analogin_init(&adc, mypin); |
shintamainjp | 0:5a0704efe081 | 83 | return analogin_read_u16(&adc); |
shintamainjp | 0:5a0704efe081 | 84 | } |
shintamainjp | 0:5a0704efe081 | 85 | |
shintamainjp | 0:5a0704efe081 | 86 | protected: |
shintamainjp | 0:5a0704efe081 | 87 | PinName mypin; |
shintamainjp | 0:5a0704efe081 | 88 | uint16_t calval; |
shintamainjp | 0:5a0704efe081 | 89 | gpio_t gpio; |
shintamainjp | 0:5a0704efe081 | 90 | analogin_t adc; |
shintamainjp | 0:5a0704efe081 | 91 | }; |
shintamainjp | 0:5a0704efe081 | 92 | |
shintamainjp | 0:5a0704efe081 | 93 | } // namespace mbed |
shintamainjp | 0:5a0704efe081 | 94 | |
shintamainjp | 0:5a0704efe081 | 95 | #endif |