Hi all, just wondering why the pin must be known at compile time?
I was looking at the source code and it has if-else for each pin so i'm wondering why the pin needs to be know at compile when it's still doing an if-else.
With that in mind, would the function below have the same or similar effect for code that has a variable pin number?
I'm assuming it's more involved than that, otherwise the Teensy Core would just implement the digitalReadFast as the default and then check if the pin is a constant and if not default to the standard way to read pins. I'm guessing digitalReadFast is not the default because that adds an additional "if" to see if the pin is a constant.
Additionally would digitalReadFast() work the same way if the pin number is coming from a #define? like this:
This is mostly curiosity Teensy's are so fast that digitalRead is still really fast, I don't actually need to read pins extremely fast or anything like that but if i can speed it up further then might as well.
Thank you in advance.
I was looking at the source code and it has if-else for each pin so i'm wondering why the pin needs to be know at compile when it's still doing an if-else.
With that in mind, would the function below have the same or similar effect for code that has a variable pin number?
Code:
uint8_t readPin(uint8_t pin){
switch(pin){
case 0: return digitalReadFast(0);
case 1: return digitalReadFast(1);
case 2: return digitalReadFast(2);
case 3: return digitalReadFast(3);
case 4: return digitalReadFast(4);
// all pins
}
return digitalReadFast(pin);
}
I'm assuming it's more involved than that, otherwise the Teensy Core would just implement the digitalReadFast as the default and then check if the pin is a constant and if not default to the standard way to read pins. I'm guessing digitalReadFast is not the default because that adds an additional "if" to see if the pin is a constant.
Additionally would digitalReadFast() work the same way if the pin number is coming from a #define? like this:
Code:
#define MY_PIN_0 7
#define MY_PIN_1 5
#define MY_PIN_2 1
#define MY_PIN_3 6
#define MY_PIN_4 4
uint8_t readPin(uint8_t pin){
switch(pin){
case MY_PIN_0: return digitalReadFast(MY_PIN_0);
case MY_PIN_1: return digitalReadFast(MY_PIN_1);
case MY_PIN_2: return digitalReadFast(MY_PIN_2);
case MY_PIN_3: return digitalReadFast(MY_PIN_3);
case MY_PIN_4: return digitalReadFast(MY_PIN_4);
// all pins
}
return digitalReadFast(pin);
}
This is mostly curiosity Teensy's are so fast that digitalRead is still really fast, I don't actually need to read pins extremely fast or anything like that but if i can speed it up further then might as well.
Thank you in advance.