Last active
April 15, 2021 05:58
-
-
Save Fordi/aee088ded1edf502882f0d10f4f2acda to your computer and use it in GitHub Desktop.
Macro versions of pinMode and digitalWrite. Tend to be faster and smaller.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file will reduce the size of and improve the performance of certain arduino code | |
* 1. It strips away about 310 bytes of overhead by replacing the implicit main function with a minimal one (which will also be faster) | |
* 2. It replaces the library `pinMode` and `digitalWrite` code with register-based variants - saving 120 | |
* bytes - and which which perform faster than the built-ins for single-pin changes (for multi-pin changes, | |
* use the registers yourself). | |
*/ | |
#ifndef ECON_101 | |
#define ECON_101 | |
#define BITMASK(n) (1 << ((n) & 7)) | |
#define DDR(n) ((n) < 8 ? DDRD : ((n) < 14 ? DDRB : DDRC)) | |
#define DDR_ON(n) DDR(n) |= BITMASK(n) | |
#define DDR_OFF(n) DDR(n) &= ~BITMASK(n) | |
#define pinMode(n, s) (s) == OUTPUT ? DDR_ON(n) : DDR_OFF(n); (s) == INPUT_PULLUP && (PORT_ON(n)) | |
#define PORT(n) ((n) < 8 ? PORTD : ((n) < 14 ? PORTB : PORTC)) | |
#define PORT_ON(n) PORT(n) |= BITMASK(n) | |
#define PORT_OFF(n) PORT(n) &= ~BITMASK(n) | |
#define digitalWrite(n, s) ((s) ? PORT_ON(n) : PORT_OFF(n)) | |
int main() { | |
setup(); | |
while (true) loop(); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment