Skip to content

Commit 60535ee

Browse files
committed
removed references to none-supported ping timer functions for the ESP8266 chip
1 parent c1e8c63 commit 60535ee

File tree

3 files changed

+3
-172
lines changed

3 files changed

+3
-172
lines changed

NewPingESP8266.cpp

-148
Original file line numberDiff line numberDiff line change
@@ -192,154 +192,6 @@ void NewPingESP8266::set_max_distance(unsigned int max_cm_distance) {
192192
}
193193

194194

195-
#if TIMER_ENABLED == true && DO_BITWISE == true
196-
197-
// ---------------------------------------------------------------------------
198-
// Timer interrupt ping methods (won't work with non-AVR, ATmega128 and all ATtiny microcontrollers)
199-
// ---------------------------------------------------------------------------
200-
201-
void NewPingESP8266::ping_timer(void (*userFunc)(void), unsigned int max_cm_distance) {
202-
if (max_cm_distance > 0) set_max_distance(max_cm_distance); // Call function to set a new max sensor distance.
203-
204-
if (!ping_trigger()) return; // Trigger a ping, if it returns false, return without starting the echo timer.
205-
timer_us(ECHO_TIMER_FREQ, userFunc); // Set ping echo timer check every ECHO_TIMER_FREQ uS.
206-
}
207-
208-
209-
boolean NewPingESP8266::check_timer() {
210-
if (micros() > _max_time) { // Outside the time-out limit.
211-
timer_stop(); // Disable timer interrupt
212-
return false; // Cancel ping timer.
213-
}
214-
215-
#if URM37_ENABLED == false
216-
if (!(*_echoInput & _echoBit)) { // Ping echo received.
217-
#else
218-
if (*_echoInput & _echoBit) { // Ping echo received.
219-
#endif
220-
timer_stop(); // Disable timer interrupt
221-
ping_result = (micros() - (_max_time - _maxEchoTime) - PING_TIMER_OVERHEAD); // Calculate ping time including overhead.
222-
return true; // Return ping echo true.
223-
}
224-
225-
return false; // Return false because there's no ping echo yet.
226-
}
227-
228-
229-
// ---------------------------------------------------------------------------
230-
// Timer2/Timer4 interrupt methods (can be used for non-ultrasonic needs)
231-
// ---------------------------------------------------------------------------
232-
233-
// Variables used for timer functions
234-
void (*intFunc)();
235-
void (*intFunc2)();
236-
unsigned long _ms_cnt_reset;
237-
volatile unsigned long _ms_cnt;
238-
#if defined(__arm__) && defined(TEENSYDUINO)
239-
IntervalTimer itimer;
240-
#endif
241-
242-
243-
void NewPingESP8266::timer_us(unsigned int frequency, void (*userFunc)(void)) {
244-
intFunc = userFunc; // User's function to call when there's a timer event.
245-
timer_setup(); // Configure the timer interrupt.
246-
247-
#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
248-
OCR4C = min((frequency>>2) - 1, 255); // Every count is 4uS, so divide by 4 (bitwise shift right 2) subtract one, then make sure we don't go over 255 limit.
249-
TIMSK4 = (1<<TOIE4); // Enable Timer4 interrupt.
250-
#elif defined (__arm__) && defined (TEENSYDUINO) // Timer for Teensy 3.x
251-
itimer.begin(userFunc, frequency); // Really simple on the Teensy 3.x, calls userFunc every 'frequency' uS.
252-
#else
253-
OCR2A = min((frequency>>2) - 1, 255); // Every count is 4uS, so divide by 4 (bitwise shift right 2) subtract one, then make sure we don't go over 255 limit.
254-
TIMSK2 |= (1<<OCIE2A); // Enable Timer2 interrupt.
255-
#endif
256-
}
257-
258-
259-
void NewPingESP8266::timer_ms(unsigned long frequency, void (*userFunc)(void)) {
260-
intFunc = NewPingESP8266::timer_ms_cntdwn; // Timer events are sent here once every ms till user's frequency is reached.
261-
intFunc2 = userFunc; // User's function to call when user's frequency is reached.
262-
_ms_cnt = _ms_cnt_reset = frequency; // Current ms counter and reset value.
263-
timer_setup(); // Configure the timer interrupt.
264-
265-
#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
266-
OCR4C = 249; // Every count is 4uS, so 1ms = 250 counts - 1.
267-
TIMSK4 = (1<<TOIE4); // Enable Timer4 interrupt.
268-
#elif defined (__arm__) && defined (TEENSYDUINO) // Timer for Teensy 3.x
269-
itimer.begin(NewPingESP8266::timer_ms_cntdwn, 1000); // Set timer to 1ms (1000 uS).
270-
#else
271-
OCR2A = 249; // Every count is 4uS, so 1ms = 250 counts - 1.
272-
TIMSK2 |= (1<<OCIE2A); // Enable Timer2 interrupt.
273-
#endif
274-
}
275-
276-
277-
void NewPingESP8266::timer_stop() { // Disable timer interrupt.
278-
#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
279-
TIMSK4 = 0;
280-
#elif defined (__arm__) && defined (TEENSYDUINO) // Timer for Teensy 3.x
281-
itimer.end();
282-
#else
283-
TIMSK2 &= ~(1<<OCIE2A);
284-
#endif
285-
}
286-
287-
288-
// ---------------------------------------------------------------------------
289-
// Timer2/Timer4 interrupt method support functions (not called directly)
290-
// ---------------------------------------------------------------------------
291-
292-
void NewPingESP8266::timer_setup() {
293-
#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
294-
timer_stop(); // Disable Timer4 interrupt.
295-
TCCR4A = TCCR4C = TCCR4D = TCCR4E = 0;
296-
TCCR4B = (1<<CS42) | (1<<CS41) | (1<<CS40) | (1<<PSR4); // Set Timer4 prescaler to 64 (4uS/count, 4uS-1020uS range).
297-
TIFR4 = (1<<TOV4);
298-
TCNT4 = 0; // Reset Timer4 counter.
299-
#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) || defined (__AVR_ATmega8535__) // Alternate timer commands for certain microcontrollers.
300-
timer_stop(); // Disable Timer2 interrupt.
301-
ASSR &= ~(1<<AS2); // Set clock, not pin.
302-
TCCR2 = (1<<WGM21 | 1<<CS22); // Set Timer2 to CTC mode, prescaler to 64 (4uS/count, 4uS-1020uS range).
303-
TCNT2 = 0; // Reset Timer2 counter.
304-
#elif defined (__arm__) && defined (TEENSYDUINO)
305-
timer_stop(); // Stop the timer.
306-
#else
307-
timer_stop(); // Disable Timer2 interrupt.
308-
ASSR &= ~(1<<AS2); // Set clock, not pin.
309-
TCCR2A = (1<<WGM21); // Set Timer2 to CTC mode.
310-
TCCR2B = (1<<CS22); // Set Timer2 prescaler to 64 (4uS/count, 4uS-1020uS range).
311-
TCNT2 = 0; // Reset Timer2 counter.
312-
#endif
313-
}
314-
315-
316-
void NewPingESP8266::timer_ms_cntdwn() {
317-
if (!_ms_cnt--) { // Count down till we reach zero.
318-
intFunc2(); // Scheduled time reached, run the main timer event function.
319-
_ms_cnt = _ms_cnt_reset; // Reset the ms timer.
320-
}
321-
}
322-
323-
#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
324-
ISR(TIMER4_OVF_vect) {
325-
intFunc(); // Call wrapped function.
326-
}
327-
#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) || defined (__AVR_ATmega8535__) // Alternate timer commands for certain microcontrollers.
328-
ISR(TIMER2_COMP_vect) {
329-
intFunc(); // Call wrapped function.
330-
}
331-
#elif defined (__arm__)
332-
// Do nothing...
333-
#else
334-
ISR(TIMER2_COMPA_vect) {
335-
intFunc(); // Call wrapped function.
336-
}
337-
#endif
338-
339-
340-
#endif
341-
342-
343195
// ---------------------------------------------------------------------------
344196
// Conversion methods (rounds result to nearest cm or inch).
345197
// ---------------------------------------------------------------------------

NewPingESP8266.h

-21
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@
150150
#define ONE_PIN_ENABLED true // Set to "false" to disable one pin mode which saves around 14-26 bytes of binary size. Default=true
151151
#define ROUNDING_ENABLED false // Set to "true" to enable distance rounding which also adds 64 bytes to binary size. Default=false
152152
#define URM37_ENABLED false // Set to "true" to enable support for the URM37 sensor in PWM mode. Default=false
153-
#define TIMER_ENABLED true // Set to "false" to disable the timer ISR (if getting "__vector_7" compile errors set this to false). Default=true
154153

155154
// Probably shouldn't change these values unless you really know what you're doing.
156155
#define NO_ECHO 0 // Value returned if there's no ping echo within the specified MAX_SENSOR_DISTANCE or max_cm_distance. Default=0
@@ -181,19 +180,11 @@
181180
#define PING_OVERHEAD 1
182181
#undef PING_TIMER_OVERHEAD
183182
#define PING_TIMER_OVERHEAD 1
184-
#undef TIMER_ENABLED
185-
#define TIMER_ENABLED false
186183
#define DO_BITWISE false
187184
#else
188185
#define DO_BITWISE true
189186
#endif
190187

191-
// Disable the timer interrupts when using ATmega128 and all ATtiny microcontrollers.
192-
#if defined (__AVR_ATmega128__) || defined (__AVR_ATtiny24__) || defined (__AVR_ATtiny44__) || defined (__AVR_ATtiny84__) || defined (__AVR_ATtiny25__) || defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__) || defined (__AVR_ATtiny261__) || defined (__AVR_ATtiny461__) || defined (__AVR_ATtiny861__) || defined (__AVR_ATtiny43U__)
193-
#undef TIMER_ENABLED
194-
#define TIMER_ENABLED false
195-
#endif
196-
197188
// Define timers when using ATmega8, ATmega16, ATmega32 and ATmega8535 microcontrollers.
198189
#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) || defined (__AVR_ATmega8535__)
199190
#define OCR2A OCR2
@@ -210,22 +201,10 @@ class NewPingESP8266 {
210201
unsigned long ping_median(uint32_t it = 5, unsigned int max_cm_distance = 0);
211202
static unsigned int convert_cm(unsigned int echoTime);
212203
static unsigned int convert_in(unsigned int echoTime);
213-
#if TIMER_ENABLED == true
214-
void ping_timer(void (*userFunc)(void), unsigned int max_cm_distance = 0);
215-
boolean check_timer();
216-
unsigned long ping_result;
217-
static void timer_us(unsigned int frequency, void (*userFunc)(void));
218-
static void timer_ms(unsigned long frequency, void (*userFunc)(void));
219-
static void timer_stop();
220204
#endif
221205
private:
222206
boolean ping_trigger();
223207
void set_max_distance(unsigned int max_cm_distance);
224-
#if TIMER_ENABLED == true
225-
boolean ping_trigger_timer(unsigned int trigger_delay);
226-
boolean ping_wait_timer();
227-
static void timer_setup();
228-
static void timer_ms_cntdwn();
229208
#endif
230209
#if DO_BITWISE == true
231210
uint32_t _triggerBit;

examples/NewPingExample/NewPingExample.pde examples/NewPingESP8266Example/NewPingESP8266Example.pde

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// ---------------------------------------------------------------------------
2-
// Example NewPing library sketch that does a ping about 20 times per second.
2+
// Example NewPingESP8266 library sketch that does a ping about 20 times per second.
33
// ---------------------------------------------------------------------------
44

5-
#include <NewPing.h>
5+
#include <NewPingESP8266.h>
66

77
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
88
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
99
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
1010

11-
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
11+
NewPingESP8266 sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPingESP8266 setup of pins and maximum distance.
1212

1313
void setup() {
1414
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

0 commit comments

Comments
 (0)