There is nothing quite like solving a problem to help develop new skills. In this case I have found a library solution to what I want to achieve but it is not RP2040 compatible and of course it does not use PIO, which is the skill I am trying to acquire.
Having seen other members deliver some great PIO related projects, I thought this would be a great opportunity to build a solution together while helping me, and hopefully others, grasp how to develop PIO code from scratch.
Basically I want to create a capacitance touch or proximity sensor using a Raspberry Pi Pico (RP2040) board.
There is an Arduino library that can do this: https://github.com/PaulStoffregen/CapacitiveSensor
But it is not RP2040 compatible: https://github.com/PaulStoffregen/CapacitiveSensor/issues/42
Having reviewed this library, it appears on face value to be well suited to use PIO. Only, I don't know how to start.
As far as I can make out, there is only one function to apply PIO... but maybe others see things differently. I'm keen to learn more.
// Private Methods /////////////////////////////////////////////////////////////
// Functions only available to other functions in this library
int CapacitiveSensor::SenseOneCycle(void)
{
noInterrupts();
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 ( !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);
if (total > CS_Timeout_Millis) {
return -2; // total variable over timeout
}
// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
noInterrupts();
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();
#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++;
}
#endif
//Serial.print("SenseOneCycle(2): ");
//Serial.println(total);
if (total >= CS_Timeout_Millis) {
return -2; // total variable over timeout
} else {
return 1;
}
}




