MCPcopy Create free account
hub / github.com/SmingHub/Sming / SenseOneCycle

Method SenseOneCycle

Sming/Libraries/CapacitiveSensor/CapacitiveSensor.cpp:137–187  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

135// Functions only available to other functions in this library
136
137int CapacitiveSensor::SenseOneCycle(void)
138{
139 noInterrupts();
140
141 DIRECT_WRITE_LOW(sReg, sBit); // sendPin Register low
142 DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
143 DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT
144 DIRECT_WRITE_LOW(rReg, rBit); // pin is now LOW AND OUTPUT
145 delayMicroseconds(10);
146 DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
147 DIRECT_WRITE_HIGH(sReg, sBit); // sendPin High
148 interrupts();
149
150 while ( !DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value
151 total++;
152 }
153 //Serial.print("SenseOneCycle(1): ");
154 //Serial.println(total);
155
156 if (total > CS_Timeout_Millis) {
157 return -2; // total variable over timeout
158 }
159
160 // set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
161 noInterrupts();
162 DIRECT_WRITE_HIGH(rReg, rBit);
163 DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
164 DIRECT_WRITE_HIGH(rReg, rBit);
165 DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
166 DIRECT_WRITE_LOW(sReg, sBit); // sendPin LOW
167 interrupts();
168
169#ifdef FIVE_VOLT_TOLERANCE_WORKAROUND
170 DIRECT_MODE_OUTPUT(rReg, rBit);
171 DIRECT_WRITE_LOW(rReg, rBit);
172 delayMicroseconds(10);
173 DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
174#else
175 while ( DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout
176 total++;
177 }
178#endif
179 //Serial.print("SenseOneCycle(2): ");
180 //Serial.println(total);
181
182 if (total >= CS_Timeout_Millis) {
183 return -2; // total variable over timeout
184 } else {
185 return 1;
186 }
187}

Callers

nothing calls this directly

Calls 3

delayMicrosecondsFunction · 0.85
noInterruptsFunction · 0.50
interruptsFunction · 0.50

Tested by

no test coverage detected