From af313ea5f7f6241735724da1e915858aef623687 Mon Sep 17 00:00:00 2001 From: Jim Parziale Date: Wed, 30 Mar 2022 14:33:17 -0400 Subject: [PATCH] Added delayMs and delayUs macros for easier code comprehension. --- examples/buttons.c | 4 ++-- examples/buttons2.c | 2 +- examples/clock.c | 2 +- examples/isr-osc.c | 4 ++-- examples/isr1.c | 6 +++--- examples/q2w/button.c | 4 ++-- examples/wfi.c | 6 +++--- gpio/gpio.c | 4 ++-- wiringPi/wiringPi.c | 2 +- wiringPi/wiringPi.h | 8 ++++++-- 10 files changed, 23 insertions(+), 19 deletions(-) diff --git a/examples/buttons.c b/examples/buttons.c index 53b8be4..b1afd8a 100644 --- a/examples/buttons.c +++ b/examples/buttons.c @@ -54,7 +54,7 @@ void scanButton (int button) // Wait for release while (digitalRead (button) == LOW) { - delay (1); + delayMs(1); } int dur = micros() - start; @@ -95,7 +95,7 @@ int main (void) { scanButton (buttons[i]); } - delay (1); + delayMs(1); } return EXIT_SUCCESS; diff --git a/examples/buttons2.c b/examples/buttons2.c index b882814..05c8c37 100644 --- a/examples/buttons2.c +++ b/examples/buttons2.c @@ -89,7 +89,7 @@ int main (void) } } - delay (1); + delayMs(1); // If pushed, look for release if (buttonVal[i] == 1) diff --git a/examples/clock.c b/examples/clock.c index 5ca98fd..e748a8d 100644 --- a/examples/clock.c +++ b/examples/clock.c @@ -193,7 +193,7 @@ int main (int argc, char *argv []) now = time (NULL) ; while (time (NULL) == now) - delay (10) ; + delayMs(10) ; } diff --git a/examples/isr-osc.c b/examples/isr-osc.c index 9552378..372b61e 100644 --- a/examples/isr-osc.c +++ b/examples/isr-osc.c @@ -125,13 +125,13 @@ int main (void) while (!terminate_process && (myCounter == globalCounter)) { - delay (250); + delayMs(250); } printf ("Done. counter: %2d: %2d\n", globalCounter, globalCounter - myCounter); digitalWrite (OUT_PIN, 1); - delay (500); + delayMs(500); digitalWrite (OUT_PIN, 0); myCounter = globalCounter; diff --git a/examples/isr1.c b/examples/isr1.c index d3042c3..be275f3 100644 --- a/examples/isr1.c +++ b/examples/isr1.c @@ -42,8 +42,8 @@ //********************************************************************************************************************** // What GPIO input are we using? -// This is a wiringPi pin number -#define BUTTON_PIN 3 +// This is a wiringPi pin number +#define BUTTON_PIN 3 // LED Pin - wiringPi pin 0 is BCM_GPIO 17. #define LED 2 @@ -117,7 +117,7 @@ int main(void) { fflush(stdout); pthread_mutex_lock( &smb_mutex ); - delay(50); + delayMs(50); isr_state = 0; button_state = digitalRead(BUTTON_PIN); diff --git a/examples/q2w/button.c b/examples/q2w/button.c index 90ba6c7..2412fb7 100644 --- a/examples/q2w/button.c +++ b/examples/q2w/button.c @@ -52,11 +52,11 @@ int main (void) digitalWrite (LED1, LOW) ; digitalWrite (LED2, HIGH) ; while (digitalRead (BUTTON) == HIGH) - delay (1) ; + delayMs(1) ; digitalWrite (LED1, HIGH) ; digitalWrite (LED2, LOW) ; } - delay (1) ; + delayMs(1) ; } return 0 ; diff --git a/examples/wfi.c b/examples/wfi.c index f4b53c3..89124f6 100644 --- a/examples/wfi.c +++ b/examples/wfi.c @@ -96,7 +96,7 @@ PI_THREAD (waitForIt) // Wait for key to be released while (digitalRead (BUTTON_PIN) == LOW) - delay (1) ; + delayMs (1) ; debounceTime = millis () + DEBOUNCE_TIME ; } @@ -151,9 +151,9 @@ int main (void) while (myCounter == lastCounter) { piLock (COUNT_KEY) ; - myCounter = globalCounter ; + myCounter = globalCounter ; piUnlock (COUNT_KEY) ; - delay (500) ; + delayMs(500) ; } printf (" Done. myCounter: %5d\n", myCounter) ; diff --git a/gpio/gpio.c b/gpio/gpio.c index 57f0cfa..a590b6b 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -532,7 +532,7 @@ void doWfi (int argc, char *argv[]) } for (;;) - delay (9999); + delayMs(9999); } @@ -1020,7 +1020,7 @@ void doBlink (int argc, char *argv[]) for (;;) { digitalWrite (pin, !digitalRead (pin)); - delay (500); + delayMs(500); } } diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index 3eedf1d..31e1386 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1950,7 +1950,7 @@ int wiringPiISR (int pin, int mode, void (*function)(void)) pthread_create (&threadId, NULL, interruptHandler, NULL); while (pinPass != -1) { - delay (1); + delayMs(1); } pthread_mutex_unlock (&pinMutex); diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index ae02713..29eabef 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -281,9 +281,13 @@ extern int piHiPri (const int pri); extern void delay (unsigned int milliseconds); extern void delayMicroseconds (unsigned int microseconds); +// Please use these for code that's easier to comprehend: +#define delayMs(ms) delay(ms) +#define delayUs(us) delayMicroseconds(us) + // These report the amount of time passed since wiringPiSetup* was called. -extern unsigned int millis (void); -extern unsigned int micros (void); +extern unsigned int millis(void); +extern unsigned int micros(void); #ifdef __cplusplus }