From 8e4b319c4decc382232d5cd16097963289558945 Mon Sep 17 00:00:00 2001 From: Ton Huisman Date: Sat, 31 Dec 2022 12:33:07 +0100 Subject: [PATCH] [Libs] Update `NewPing` library, add sanity check for `triggerWidth` argument --- lib/NewPing/README.md | 2 +- ...orsTimer.pde => NewPing15SensorsTimer.ino} | 0 ...ewPing3Sensors.pde => NewPing3Sensors.ino} | 6 +-- ...ngEventTimer.pde => NewPingEventTimer.ino} | 0 ...{NewPingExample.pde => NewPingExample.ino} | 0 ...TimerMedian.pde => NewPingTimerMedian.ino} | 0 .../{TimerExample.pde => TimerExample.ino} | 0 lib/NewPing/library.properties | 6 +-- lib/NewPing/src/NewPing.cpp | 4 +- lib/NewPing/src/NewPing.h | 43 +++++++++++++------ 10 files changed, 40 insertions(+), 21 deletions(-) rename lib/NewPing/examples/NewPing15SensorsTimer/{NewPing15SensorsTimer.pde => NewPing15SensorsTimer.ino} (100%) rename lib/NewPing/examples/NewPing3Sensors/{NewPing3Sensors.pde => NewPing3Sensors.ino} (92%) rename lib/NewPing/examples/NewPingEventTimer/{NewPingEventTimer.pde => NewPingEventTimer.ino} (100%) rename lib/NewPing/examples/NewPingExample/{NewPingExample.pde => NewPingExample.ino} (100%) rename lib/NewPing/examples/NewPingTimerMedian/{NewPingTimerMedian.pde => NewPingTimerMedian.ino} (100%) rename lib/NewPing/examples/TimerExample/{TimerExample.pde => TimerExample.ino} (100%) diff --git a/lib/NewPing/README.md b/lib/NewPing/README.md index 8760c35ff..5044422a4 100644 --- a/lib/NewPing/README.md +++ b/lib/NewPing/README.md @@ -1,3 +1,3 @@ # NewPing Arduino Library for Arduino -## See the [NewPing Wiki](https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home) for documentation \ No newline at end of file +## See the [NewPing Wiki](https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home) for documentation diff --git a/lib/NewPing/examples/NewPing15SensorsTimer/NewPing15SensorsTimer.pde b/lib/NewPing/examples/NewPing15SensorsTimer/NewPing15SensorsTimer.ino similarity index 100% rename from lib/NewPing/examples/NewPing15SensorsTimer/NewPing15SensorsTimer.pde rename to lib/NewPing/examples/NewPing15SensorsTimer/NewPing15SensorsTimer.ino diff --git a/lib/NewPing/examples/NewPing3Sensors/NewPing3Sensors.pde b/lib/NewPing/examples/NewPing3Sensors/NewPing3Sensors.ino similarity index 92% rename from lib/NewPing/examples/NewPing3Sensors/NewPing3Sensors.pde rename to lib/NewPing/examples/NewPing3Sensors/NewPing3Sensors.ino index 061d7f3ee..5cc0b2402 100644 --- a/lib/NewPing/examples/NewPing3Sensors/NewPing3Sensors.pde +++ b/lib/NewPing/examples/NewPing3Sensors/NewPing3Sensors.ino @@ -8,8 +8,8 @@ #define MAX_DISTANCE 200 // Maximum distance (in cm) to ping. NewPing sonar[SONAR_NUM] = { // Sensor object array. - NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. - NewPing(6, 7, MAX_DISTANCE), + NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. + NewPing(6, 7, MAX_DISTANCE), NewPing(8, 9, MAX_DISTANCE) }; @@ -17,7 +17,7 @@ void setup() { Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. } -void loop() { +void loop() { for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results. delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. Serial.print(i); diff --git a/lib/NewPing/examples/NewPingEventTimer/NewPingEventTimer.pde b/lib/NewPing/examples/NewPingEventTimer/NewPingEventTimer.ino similarity index 100% rename from lib/NewPing/examples/NewPingEventTimer/NewPingEventTimer.pde rename to lib/NewPing/examples/NewPingEventTimer/NewPingEventTimer.ino diff --git a/lib/NewPing/examples/NewPingExample/NewPingExample.pde b/lib/NewPing/examples/NewPingExample/NewPingExample.ino similarity index 100% rename from lib/NewPing/examples/NewPingExample/NewPingExample.pde rename to lib/NewPing/examples/NewPingExample/NewPingExample.ino diff --git a/lib/NewPing/examples/NewPingTimerMedian/NewPingTimerMedian.pde b/lib/NewPing/examples/NewPingTimerMedian/NewPingTimerMedian.ino similarity index 100% rename from lib/NewPing/examples/NewPingTimerMedian/NewPingTimerMedian.pde rename to lib/NewPing/examples/NewPingTimerMedian/NewPingTimerMedian.ino diff --git a/lib/NewPing/examples/TimerExample/TimerExample.pde b/lib/NewPing/examples/TimerExample/TimerExample.ino similarity index 100% rename from lib/NewPing/examples/TimerExample/TimerExample.pde rename to lib/NewPing/examples/TimerExample/TimerExample.ino diff --git a/lib/NewPing/library.properties b/lib/NewPing/library.properties index 1231e1135..a6177b7cb 100644 --- a/lib/NewPing/library.properties +++ b/lib/NewPing/library.properties @@ -1,9 +1,9 @@ name=NewPing -version=1.9.4 +version=1.9.6 author=Tim Eckel maintainer=Tim Eckel -sentence=A library that makes working with ultrasonic sensors easy. -paragraph=When I first received an ultrasonic sensor I was not happy with how poorly it performed. I soon realized the problem was not the sensor, it was the available ping and ultrasonic libraries causing the problem. The NewPing library totally fixes these problems, adds many new features, and breathes new life into these very affordable distance sensors. +sentence=NewPing allows interfacing with ultrasonic sensors simple, fast & powerful. +paragraph=Initially, I was not happy with how poorly ultrasonic sensors performed. I soon realized the problem was not the sensor, it was the available ping and ultrasonic libraries causing the problem. The NewPing library totally fixes these problems, adds many new features, and breathes new life into these very affordable distance sensors. category=Sensors url=https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home architectures=avr,arm,megaavr,esp32,esp8266 diff --git a/lib/NewPing/src/NewPing.cpp b/lib/NewPing/src/NewPing.cpp index 49d6234b7..8eae06aa7 100644 --- a/lib/NewPing/src/NewPing.cpp +++ b/lib/NewPing/src/NewPing.cpp @@ -32,8 +32,10 @@ NewPing::NewPing(uint8_t trigger_pin, uint8_t echo_pin, unsigned int max_cm_dist #else _triggerPin = trigger_pin; _echoPin = echo_pin; - _triggerWidth = trigger_width; #endif + if ((trigger_width >= DEFAULT_TRIGGER_WIDTH) && (trigger_width <= MAX_TRIGGER_WIDTH)) { + _triggerWidth = trigger_width; + } set_max_distance(max_cm_distance); // Call function to set the max sensor distance. diff --git a/lib/NewPing/src/NewPing.h b/lib/NewPing/src/NewPing.h index c0aa291dc..8e73ce265 100644 --- a/lib/NewPing/src/NewPing.h +++ b/lib/NewPing/src/NewPing.h @@ -1,5 +1,5 @@ // --------------------------------------------------------------------------- -// NewPing Library - v1.9.4 - 01/26/2022 +// NewPing Library - v1.9.6 - 12/15/2022 // // AUTHOR/LICENSE: // Created by Tim Eckel - eckel.tim@gmail.com @@ -7,8 +7,8 @@ // permission. Permission is only granted to use as-is for private and // commercial use. If you wish to contribute, make changes, or enhancements, // please create a pull request. I get a lot of support issues from this -// library, most of which comes from old versions, buggy forks or sites without -// proper documentation, just trying to wrangle this project together. +// library, much of which comes from old versions, buggy forks or sites without +// proper documentation. Trying to wrangle this project together, thanks! // // LINKS: // Project home: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home @@ -18,12 +18,16 @@ // This software is furnished "as is", without technical support, and with no // warranty, express or implied, as to its usefulness for any purpose. // +// ATTENTION: +// Special ESPEasy adapted version, using DIRECT_Pin_IO by Paul Stoffregen +// See: https://github.com/PaulStoffregen/OneWire/blob/master/util/ +// // BACKGROUND: -// When I first received an ultrasonic sensor I was not happy with how poorly -// it worked. Quickly I realized the problem wasn't the sensor, it was the -// available ping and ultrasonic libraries causing the problem. The NewPing -// library totally fixes these problems, adds many new features, and breaths -// new life into these very affordable distance sensors. +// Initially, I wasn't happy with how poorly ultrasonic sensors performed. I +// soon realized the problem was not the sensor, it was the available ping and +// ultrasonic libraries causing the problem. The NewPing library totally fixes +// these problems, adds many new features, and breathes new life into these +// very affordable distance sensors. // // FEATURES: // * Works with many different ultrasonic sensors: SR04, SRF05, SRF06, DYP-ME007, URM37 & Parallax PING)))™. @@ -61,18 +65,29 @@ // NewPing::timer_stop() - Stop the timer. // // HISTORY: -// 29/12/2022 tonhuisman: Add triggerWidth option, defaults to 10 +// 29/12/2022 tonhuisman: Add triggerWidth option, defaults to 10usec +// +// 12/15/2022 v1.9.6 - Reverted timer overflow commit as it contained bugs. +// +// 12/13/2022 v1.9.5 - Resolved micros() timer overflow bug. Changed example +// sketch extensions so they load in latest release of the Arduino IDE. +// Changed private members to protected for easier 3rd party customization. +// // 01/26/2022 v1.9.4 - Added esp32 to the compatible architectures (note: // non-timer methods only) +// // 01/20/2022 v1.9.3 - Default to disable timer methods for non-AVR // microcontrollers and on the ATmega4809 used in the Nano Every. +// // 07/15/2018 v1.9.1 - Added support for ATtiny441 and ATtiny841 // microcontrollers. +// // 12/09/2017 v1.9.0 - Added support for the ARM-based Particle devices. If // other ARM-based microcontrollers adopt a similar timer method that the // Particle and Teensy 3.x share, support for other ARM-based microcontrollers // could be possible. Attempt to add to Arduino library manager. License // changed. +// // 07/30/2016 v1.8 - Added support for non-AVR microcontrollers. For non-AVR // microcontrollers, advanced ping_timer() timer methods are disabled due to // inconsistencies or no support at all between platforms. However, standard @@ -169,11 +184,12 @@ // Shouldn't need to change these values unless you have a specific need to do so. #define MAX_SENSOR_DISTANCE 500 // Maximum sensor distance can be as high as 500cm, no reason to wait for ping longer than sound takes to travel this distance and back. Default=500 #define US_ROUNDTRIP_CM 57 // Microseconds (uS) it takes sound to travel round-trip 1cm (2cm total), uses integer to save compiled code space. Default=57 - #define US_ROUNDTRIP_IN 146 // Microseconds (uS) it takes sound to travel round-trip 1 inch (2 inches total), uses integer to save compiled code space. Defalult=146 + #define US_ROUNDTRIP_IN 146 // Microseconds (uS) it takes sound to travel round-trip 1 inch (2 inches total), uses integer to save compiled code space. Default=146 #define ONE_PIN_ENABLED true // Set to "false" to disable one pin mode which saves around 14-26 bytes of binary size. Default=true #define ROUNDING_ENABLED false // Set to "true" to enable distance rounding which also adds 64 bytes to binary size. Default=false #define URM37_ENABLED false // Set to "true" to enable support for the URM37 sensor in PWM mode. Default=false #define DEFAULT_TRIGGER_WIDTH 10 // Length of the pulse that starts the measurement. Default=10usec. + #define MAX_TRIGGER_WIDTH 50 // Max. length of the pulse that starts the measurement. // Probably shouldn't change these values unless you really know what you're doing. #define NO_ECHO 0 // Value returned if there's no ping echo within the specified MAX_SENSOR_DISTANCE or max_cm_distance. Default=0 @@ -209,7 +225,7 @@ #endif // Disable the timer interrupts when using ATmega128, ATmega4809 and all ATtiny microcontrollers. - #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega4809__) || defined (__AVR_ATtiny24__) || defined (__AVR_ATtiny44__) || defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny84__) || defined (__AVR_ATtiny841__) || defined (__AVR_ATtiny25__) || defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__) || defined (__AVR_ATtiny261__) || defined (__AVR_ATtiny461__) || defined (__AVR_ATtiny861__) || defined (__AVR_ATtiny43U__) + #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega4809__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny441__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny841__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny261__) || defined(__AVR_ATtiny461__) || defined(__AVR_ATtiny861__) || defined(__AVR_ATtiny43U__) || defined(__AVR_ATtiny1614__) #undef TIMER_ENABLED #define TIMER_ENABLED false #endif @@ -253,7 +269,7 @@ static void timer_ms(unsigned long frequency, void (*userFunc)(void)); static void timer_stop(); #endif - private: + protected: boolean ping_trigger(); void set_max_distance(unsigned int max_cm_distance); #if TIMER_ENABLED == true @@ -284,8 +300,9 @@ #else uint8_t _triggerPin; uint8_t _echoPin; - uint8_t _triggerWidth; #endif + uint8_t _triggerWidth = DEFAULT_TRIGGER_WIDTH; + unsigned int _maxEchoTime; unsigned long _max_time;