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.
Fork of TouchSense by
TouchSense.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2013 Shinichiro Nakamura 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef TOUCHSENSE_H 00018 #define TOUCHSENSE_H 00019 00020 #include "platform.h" 00021 #include "gpio_api.h" 00022 #include "analogin_api.h" 00023 00024 namespace mbed 00025 { 00026 00027 /** 00028 * A capacitive touch sensor using analog input port. 00029 * 00030 * Example: 00031 * @code 00032 * #include "mbed.h" 00033 * 00034 * TouchSense tp(p18); 00035 * 00036 * int main() { 00037 * tp.calibration(); 00038 * while(1) { 00039 * if (tp.sense()) { 00040 * printf("ON\r\n"); 00041 * } else { 00042 * printf("OFF\r\n"); 00043 * } 00044 * wait(0.1); 00045 * } 00046 * } 00047 * @endcode 00048 */ 00049 class TouchSense 00050 { 00051 00052 public: 00053 /** 00054 * Create a TouchSense connected to the specified pin 00055 * 00056 * @param pin TouchSense pin to connect to 00057 * @param thr Threshold (1 to 20) 00058 */ 00059 TouchSense(PinName pin, int thr = 10) : mypin(pin), calval(0) { 00060 for (int i = 0; i < DATA_COUNT; i++) { 00061 data[i] = 0; 00062 } 00063 if (thr < THRMIN) { 00064 thr = THRMIN; 00065 } 00066 if (thr > THRMAX) { 00067 thr = THRMAX; 00068 } 00069 threshold = thr; 00070 execute(); 00071 } 00072 00073 void calibration() { 00074 uint32_t r = 0; 00075 for (int i = 0; i < DATA_COUNT; i++) { 00076 r += execute(); 00077 wait_us(100); 00078 } 00079 r = r / DATA_COUNT; 00080 calval = r; 00081 } 00082 00083 bool sense() { 00084 data[index] = execute(); 00085 index = (index + 1) % DATA_COUNT; 00086 uint32_t r = 0; 00087 for (int i = 0; i < DATA_COUNT; i++) { 00088 r += data[i]; 00089 } 00090 r = r / DATA_COUNT; 00091 return (calval * threshold) < r; 00092 } 00093 00094 private: 00095 00096 uint16_t execute() { 00097 uint16_t r; 00098 analogin_init(&adc, mypin); 00099 r = analogin_read_u16(&adc) >> 4; 00100 gpio_init_out(&gpio, mypin); 00101 gpio_write(&gpio, 1); 00102 return r; 00103 } 00104 00105 protected: 00106 PinName mypin; 00107 uint16_t calval; 00108 gpio_t gpio; 00109 analogin_t adc; 00110 static const int DATA_COUNT = 8; 00111 uint32_t data[DATA_COUNT]; 00112 int index; 00113 int threshold; 00114 static const int THRMIN = 1; 00115 static const int THRMAX = 20; 00116 }; 00117 00118 } // namespace mbed 00119 00120 #endif
Generated on Tue Jul 12 2022 18:33:36 by
1.7.2
