|
|
@@ -1,6 +1,7 @@
|
|
|
/*
|
|
|
CapacitiveSense.h v.04 - Capacitive Sensing Library for 'duino / Wiring
|
|
|
Copyright (c) 2009 Paul Bagder All right reserved.
|
|
|
+ Version 05 by Paul Stoffregen - Support Teensy 3.0, 3.1
|
|
|
Version 04 by Paul Stoffregen - Arduino 1.0 compatibility, issue 146 fix
|
|
|
vim: set ts=4:
|
|
|
*/
|
|
|
@@ -20,8 +21,6 @@
|
|
|
|
|
|
CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
|
|
|
{
|
|
|
- uint8_t sPort, rPort;
|
|
|
-
|
|
|
// initialize this instance's variables
|
|
|
// Serial.begin(9600); // for debugging
|
|
|
error = 1;
|
|
|
@@ -42,17 +41,13 @@ CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
|
|
|
|
|
|
pinMode(sendPin, OUTPUT); // sendpin to OUTPUT
|
|
|
pinMode(receivePin, INPUT); // receivePin to INPUT
|
|
|
+ digitalWrite(sendPin, LOW);
|
|
|
|
|
|
sBit = digitalPinToBitMask(sendPin); // get send pin's ports and bitmask
|
|
|
- sPort = digitalPinToPort(sendPin);
|
|
|
- sReg = portModeRegister(sPort);
|
|
|
- sOut = portOutputRegister(sPort); // get pointer to output register
|
|
|
+ sReg = PIN_TO_BASEREG(sendPin); // get pointer to output register
|
|
|
|
|
|
rBit = digitalPinToBitMask(receivePin); // get receive pin's ports and bitmask
|
|
|
- rPort = digitalPinToPort(receivePin);
|
|
|
- rReg = portModeRegister(rPort);
|
|
|
- rIn = portInputRegister(rPort);
|
|
|
- rOut = portOutputRegister(rPort);
|
|
|
+ rReg = PIN_TO_BASEREG(receivePin);
|
|
|
|
|
|
// get pin mapping and port for receive Pin - from digital pin functions in Wiring.c
|
|
|
leastTotal = 0x0FFFFFFFL; // input large value for autocalibrate begin
|
|
|
@@ -139,22 +134,20 @@ void CapacitiveSensor::set_CS_Timeout_Millis(unsigned long timeout_millis){
|
|
|
int CapacitiveSensor::SenseOneCycle(void)
|
|
|
{
|
|
|
noInterrupts();
|
|
|
- *sOut &= ~sBit; // sendPin Register low
|
|
|
-
|
|
|
- *rReg &= ~rBit; // receivePin to input
|
|
|
- *rOut &= ~rBit; // receivePin Register low to make sure pullups are off
|
|
|
-
|
|
|
- *rReg |= rBit; // receivePin to OUTPUT - pin is now LOW AND OUTPUT
|
|
|
- *rReg &= ~rBit; // receivePin to INPUT
|
|
|
-
|
|
|
- *sOut |= sBit; // sendPin High
|
|
|
+ DIRECT_WRITE_LOW(sReg, sBit); // sendPin Register low
|
|
|
+ DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
|
|
|
+ DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT
|
|
|
+ DIRECT_WRITE_LOW(rReg, rBit); // pin is now LOW AND OUTPUT
|
|
|
+ delayMicroseconds(10);
|
|
|
+ DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
|
|
|
+ DIRECT_WRITE_HIGH(sReg, sBit); // sendPin High
|
|
|
interrupts();
|
|
|
|
|
|
- while ( !(*rIn & rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value
|
|
|
+ while ( !DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value
|
|
|
total++;
|
|
|
}
|
|
|
- Serial.print("SenseOneCycle(1): ");
|
|
|
- Serial.println(total);
|
|
|
+ //Serial.print("SenseOneCycle(1): ");
|
|
|
+ //Serial.println(total);
|
|
|
|
|
|
if (total > CS_Timeout_Millis) {
|
|
|
return -2; // total variable over timeout
|
|
|
@@ -162,19 +155,25 @@ int CapacitiveSensor::SenseOneCycle(void)
|
|
|
|
|
|
// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
|
|
|
noInterrupts();
|
|
|
- *rOut |= rBit; // receivePin - turns on pullup
|
|
|
- *rReg |= rBit; // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
|
|
|
- *rReg &= ~rBit; // receivePin to INPUT
|
|
|
- *rOut &= ~rBit; // receivePin turn off pullup
|
|
|
-
|
|
|
- *sOut &= ~sBit; // sendPin LOW
|
|
|
+ DIRECT_WRITE_HIGH(rReg, rBit);
|
|
|
+ DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
|
|
|
+ DIRECT_WRITE_HIGH(rReg, rBit);
|
|
|
+ DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
|
|
|
+ DIRECT_WRITE_LOW(sReg, sBit); // sendPin LOW
|
|
|
interrupts();
|
|
|
|
|
|
- while ( (*rIn & rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout
|
|
|
+#ifdef FIVE_VOLT_TOLERANCE_WORKAROUND
|
|
|
+ DIRECT_MODE_OUTPUT(rReg, rBit);
|
|
|
+ DIRECT_WRITE_LOW(rReg, rBit);
|
|
|
+ delayMicroseconds(10);
|
|
|
+ DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
|
|
|
+#else
|
|
|
+ while ( DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout
|
|
|
total++;
|
|
|
}
|
|
|
- Serial.print("SenseOneCycle(2): ");
|
|
|
- Serial.println(total);
|
|
|
+#endif
|
|
|
+ //Serial.print("SenseOneCycle(2): ");
|
|
|
+ //Serial.println(total);
|
|
|
|
|
|
if (total >= CS_Timeout_Millis) {
|
|
|
return -2; // total variable over timeout
|