@@ -41,7 +41,7 @@ uint32_t end_time; // start time of measured pulse
4141 * Use of SysTick hardware timer and micros( ) function makes the function
4242 * more portable across various micros and CPU clock frequencies.
4343 *
44- * Assumptions]
44+ * Assumptions:
4545 * As per AVR Arduino PulseIn pin is expected to be in input mode already
4646 * No major interrupt activity while measurement is taken (including Serial)
4747 *
@@ -52,8 +52,9 @@ uint32_t end_time; // start time of measured pulse
5252unsigned long pulseIn (pin_size_t pin, uint8_t state, unsigned long timeout) {
5353// check pin is valid by NUM_DIGITAL_PINS, return 0 if invalid
5454#ifdef NUM_DIGITAL_PINS
55- if (pin >= NUM_DIGITAL_PINS)
55+ if (pin >= NUM_DIGITAL_PINS){
5656 return 0 ;
57+ }
5758#endif
5859
5960 // Set up pin details for faster port read in loops
@@ -63,32 +64,38 @@ unsigned long pulseIn(pin_size_t pin, uint8_t state, unsigned long timeout) {
6364
6465 // Check timeout is NOT too small or > Maximum
6566 // if so use default 1 second or MAX respectively
66- if (timeout < 6 )
67+ if (timeout < 6 ){
6768 timeout = DEF_PULSE_TIMEOUT;
68- else if (timeout > MAX_PULSE_TIMEOUT)
69+ }
70+ else if (timeout > MAX_PULSE_TIMEOUT){
6971 timeout = MAX_PULSE_TIMEOUT;
70-
72+ }
7173 // Initialise conditions
7274 pulse_state = (state) ? mask : 0 ; // level to measure
7375
7476 timeout = micros () + timeout; // Set timeout for whole process
7577
7678 // If already at measurement level we have a problem
7779 // So wait for pulse to go to OPPOSITE level required (idle) first
78- while (((pulsePort->IN & mask) == pulse_state))
79- if (micros () > timeout)
80+ while (((pulsePort->IN & mask) == pulse_state)){
81+ if (micros () > timeout){
8082 return 0 ;
81-
83+ }
84+ }
8285 // Wait for pulse to go to level required
83- while (((pulsePort->IN & mask) != pulse_state))
84- if (micros () > timeout)
86+ while (((pulsePort->IN & mask) != pulse_state)){
87+ if (micros () > timeout){
8588 return 0 ;
89+ }
90+ }
8691
8792 // measure pulse length of required level
8893 start_time = micros ();
89- while (((pulsePort->IN & mask) == pulse_state))
90- if (micros () > timeout)
94+ while (((pulsePort->IN & mask) == pulse_state)){
95+ if (micros () > timeout){
9196 return 0 ;
97+ }
98+ }
9299 end_time = micros ();
93100
94101 // Return time difference
0 commit comments