PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CapacitiveSensor.cpp 7.0KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. CapacitiveSense.h - Capacitive Sensing Library for 'duino / Wiring
  3. https://github.com/PaulStoffregen/CapacitiveSensor
  4. http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html
  5. http://playground.arduino.cc/Main/CapacitiveSensor
  6. Copyright (c) 2009 Paul Bagder
  7. Updates for other hardare by Paul Stoffregen, 2010-2016
  8. vim: set ts=4:
  9. Permission is hereby granted, free of charge, to any person obtaining a
  10. copy of this software and associated documentation files (the "Software"),
  11. to deal in the Software without restriction, including without limitation
  12. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. and/or sell copies of the Software, and to permit persons to whom the
  14. Software is furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #if ARDUINO >= 100
  26. #include "Arduino.h"
  27. #else
  28. #include "WProgram.h"
  29. #include "pins_arduino.h"
  30. #include "WConstants.h"
  31. #endif
  32. #include "CapacitiveSensor.h"
  33. // Constructor /////////////////////////////////////////////////////////////////
  34. // Function that handles the creation and setup of instances
  35. CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
  36. {
  37. // initialize this instance's variables
  38. // Serial.begin(9600); // for debugging
  39. error = 1;
  40. loopTimingFactor = 310; // determined empirically - a hack
  41. CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000;
  42. CS_AutocaL_Millis = 20000;
  43. // Serial.print("timwOut = ");
  44. // Serial.println(CS_Timeout_Millis);
  45. // get pin mapping and port for send Pin - from PinMode function in core
  46. #ifdef NUM_DIGITAL_PINS
  47. if (sendPin >= NUM_DIGITAL_PINS) error = -1;
  48. if (receivePin >= NUM_DIGITAL_PINS) error = -1;
  49. #endif
  50. pinMode(sendPin, OUTPUT); // sendpin to OUTPUT
  51. pinMode(receivePin, INPUT); // receivePin to INPUT
  52. digitalWrite(sendPin, LOW);
  53. sBit = PIN_TO_BITMASK(sendPin); // get send pin's ports and bitmask
  54. sReg = PIN_TO_BASEREG(sendPin); // get pointer to output register
  55. rBit = PIN_TO_BITMASK(receivePin); // get receive pin's ports and bitmask
  56. rReg = PIN_TO_BASEREG(receivePin);
  57. // get pin mapping and port for receive Pin - from digital pin functions in Wiring.c
  58. leastTotal = 0x0FFFFFFFL; // input large value for autocalibrate begin
  59. lastCal = millis(); // set millis for start
  60. }
  61. // Public Methods //////////////////////////////////////////////////////////////
  62. // Functions available in Wiring sketches, this library, and other libraries
  63. long CapacitiveSensor::capacitiveSensor(uint8_t samples)
  64. {
  65. total = 0;
  66. if (samples == 0) return 0;
  67. if (error < 0) return -1; // bad pin
  68. for (uint8_t i = 0; i < samples; i++) { // loop for samples parameter - simple lowpass filter
  69. if (SenseOneCycle() < 0) return -2; // variable over timeout
  70. }
  71. // only calibrate if time is greater than CS_AutocaL_Millis and total is less than 10% of baseline
  72. // this is an attempt to keep from calibrating when the sensor is seeing a "touched" signal
  73. if ( (millis() - lastCal > CS_AutocaL_Millis) && abs(total - leastTotal) < (int)(.10 * (float)leastTotal) ) {
  74. // Serial.println(); // debugging
  75. // Serial.println("auto-calibrate");
  76. // Serial.println();
  77. // delay(2000); */
  78. leastTotal = 0x0FFFFFFFL; // reset for "autocalibrate"
  79. lastCal = millis();
  80. }
  81. /*else{ // debugging
  82. Serial.print(" total = ");
  83. Serial.print(total);
  84. Serial.print(" leastTotal = ");
  85. Serial.println(leastTotal);
  86. Serial.print("total - leastTotal = ");
  87. x = total - leastTotal ;
  88. Serial.print(x);
  89. Serial.print(" .1 * leastTotal = ");
  90. x = (int)(.1 * (float)leastTotal);
  91. Serial.println(x);
  92. } */
  93. // routine to subtract baseline (non-sensed capacitance) from sensor return
  94. if (total < leastTotal) leastTotal = total; // set floor value to subtract from sensed value
  95. return(total - leastTotal);
  96. }
  97. long CapacitiveSensor::capacitiveSensorRaw(uint8_t samples)
  98. {
  99. total = 0;
  100. if (samples == 0) return 0;
  101. if (error < 0) return -1; // bad pin - this appears not to work
  102. for (uint8_t i = 0; i < samples; i++) { // loop for samples parameter - simple lowpass filter
  103. if (SenseOneCycle() < 0) return -2; // variable over timeout
  104. }
  105. return total;
  106. }
  107. void CapacitiveSensor::reset_CS_AutoCal(void){
  108. leastTotal = 0x0FFFFFFFL;
  109. }
  110. void CapacitiveSensor::set_CS_AutocaL_Millis(unsigned long autoCal_millis){
  111. CS_AutocaL_Millis = autoCal_millis;
  112. }
  113. void CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){
  114. CS_Timeout_Millis = (timeout_millis * (float)loopTimingFactor * (float)F_CPU) / 16000000; // floats to deal with large numbers
  115. }
  116. // Private Methods /////////////////////////////////////////////////////////////
  117. // Functions only available to other functions in this library
  118. int CapacitiveSensor::SenseOneCycle(void)
  119. {
  120. noInterrupts();
  121. DIRECT_WRITE_LOW(sReg, sBit); // sendPin Register low
  122. DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
  123. DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT
  124. DIRECT_WRITE_LOW(rReg, rBit); // pin is now LOW AND OUTPUT
  125. delayMicroseconds(10);
  126. DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
  127. DIRECT_WRITE_HIGH(sReg, sBit); // sendPin High
  128. interrupts();
  129. while ( !DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value
  130. total++;
  131. }
  132. //Serial.print("SenseOneCycle(1): ");
  133. //Serial.println(total);
  134. if (total > CS_Timeout_Millis) {
  135. return -2; // total variable over timeout
  136. }
  137. // set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
  138. noInterrupts();
  139. DIRECT_WRITE_HIGH(rReg, rBit);
  140. DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
  141. DIRECT_WRITE_HIGH(rReg, rBit);
  142. DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
  143. DIRECT_WRITE_LOW(sReg, sBit); // sendPin LOW
  144. interrupts();
  145. #ifdef FIVE_VOLT_TOLERANCE_WORKAROUND
  146. DIRECT_MODE_OUTPUT(rReg, rBit);
  147. DIRECT_WRITE_LOW(rReg, rBit);
  148. delayMicroseconds(10);
  149. DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
  150. #else
  151. while ( DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout
  152. total++;
  153. }
  154. #endif
  155. //Serial.print("SenseOneCycle(2): ");
  156. //Serial.println(total);
  157. if (total >= CS_Timeout_Millis) {
  158. return -2; // total variable over timeout
  159. } else {
  160. return 1;
  161. }
  162. }