@@ -50,7 +50,6 @@ Version Modified By Date Comments
5050#define TIMER2_COMPA_vect TIMER2_COMP_vect
5151#define TIMSK1 TIMSK
5252#endif
53-
5453// timerx_toggle_count:
5554// > 0 - duration specified
5655// = 0 - stopped
@@ -68,6 +67,8 @@ volatile uint8_t timer1_pin_mask;
6867volatile long timer2_toggle_count;
6968volatile uint8_t *timer2_pin_port;
7069volatile uint8_t timer2_pin_mask;
70+ volatile uint8_t tonePin;
71+ volatile uint8_t nonzerofreq;
7172
7273#if defined(TIMSK3)
7374volatile long timer3_toggle_count;
@@ -123,8 +124,6 @@ static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
123124
124125#endif
125126
126-
127-
128127static int8_t toneBegin (uint8_t _pin)
129128{
130129 int8_t _timer = -1 ;
@@ -242,6 +241,7 @@ static int8_t toneBegin(uint8_t _pin)
242241
243242void tone (uint8_t _pin, unsigned int frequency, unsigned long duration)
244243{
244+ nonzerofreq = frequency > 20 ;
245245 uint8_t prescalarbits = 0b001 ;
246246 long toggle_count = 0 ;
247247 uint32_t ocr = 0 ;
@@ -477,10 +477,51 @@ void disableTimer(uint8_t _timer)
477477}
478478
479479
480+ void _next_tone (){
481+ // need to call noTone() so that the tone_pins[] entry is reset, so the
482+ // timer gets initialized next time we call tone().
483+ // XXX: this assumes timer 2 is always the first one used.
484+ disableTimer (2 );
485+ *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
486+ }
487+
488+ const int16_t * auto_next_tone_pointer;
489+ uint8_t auto_next_tone_pin;
490+ uint16_t auto_next_tone_index;
491+
492+ void auto_next_tone () {
493+ switch (pgm_read_word (auto_next_tone_pointer + auto_next_tone_index)) {
494+ case -1 : noTone (auto_next_tone_pin); return ;
495+ case -2 : auto_next_tone_index = 0 ; break ;
496+ }
497+ tone (auto_next_tone_pin, pgm_read_word (auto_next_tone_pointer + auto_next_tone_index), pgm_read_word (auto_next_tone_pointer + auto_next_tone_index + 1 ));
498+ auto_next_tone_index += 2 ;
499+ }
500+
501+
502+
503+ void (*next_tone)(void ) = *_next_tone;
504+
505+
506+ void tone (void (*f)(void )) {
507+ next_tone = *f;
508+ next_tone ();
509+ }
510+
511+ void autoTone (uint8_t pin, const int16_t * ntp){
512+ next_tone = *auto_next_tone;
513+ auto_next_tone_pointer = ntp;
514+ auto_next_tone_pin = pin;
515+ auto_next_tone_index = 0 ;
516+ next_tone ();
517+ }
518+
519+
480520void noTone (uint8_t _pin)
481521{
522+ digitalWrite (_pin, 0 );
523+ next_tone = *_next_tone;
482524 int8_t _timer = -1 ;
483-
484525 for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
485526 if (tone_pins[i] == _pin) {
486527 _timer = pgm_read_byte (tone_pin_to_timer_PGM + i);
@@ -490,10 +531,9 @@ void noTone(uint8_t _pin)
490531 }
491532
492533 disableTimer (_timer);
493-
494- digitalWrite (_pin, 0 );
495534}
496535
536+
497537#ifdef USE_TIMER0
498538ISR (TIMER0_COMPA_vect)
499539{
@@ -537,23 +577,18 @@ ISR(TIMER1_COMPA_vect)
537577#ifdef USE_TIMER2
538578ISR (TIMER2_COMPA_vect)
539579{
540-
541580 if (timer2_toggle_count != 0 )
542581 {
543582 // toggle the pin
544- *timer2_pin_port ^= timer2_pin_mask;
583+ if (nonzerofreq)
584+ *timer2_pin_port ^= timer2_pin_mask;
545585
546586 if (timer2_toggle_count > 0 )
547587 timer2_toggle_count--;
548588 }
549589 else
550590 {
551- // need to call noTone() so that the tone_pins[] entry is reset, so the
552- // timer gets initialized next time we call tone().
553- // XXX: this assumes timer 2 is always the first one used.
554- noTone (tone_pins[0 ]);
555- // disableTimer(2);
556- // *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
591+ next_tone ();
557592 }
558593}
559594#endif
0 commit comments