@@ -50,7 +50,6 @@ Version Modified By Date Comments
50
50
#define TIMER2_COMPA_vect TIMER2_COMP_vect
51
51
#define TIMSK1 TIMSK
52
52
#endif
53
-
54
53
// timerx_toggle_count:
55
54
// > 0 - duration specified
56
55
// = 0 - stopped
@@ -68,6 +67,8 @@ volatile uint8_t timer1_pin_mask;
68
67
volatile long timer2_toggle_count;
69
68
volatile uint8_t *timer2_pin_port;
70
69
volatile uint8_t timer2_pin_mask;
70
+ volatile uint8_t tonePin;
71
+ volatile uint8_t nonzerofreq;
71
72
72
73
#if defined(TIMSK3)
73
74
volatile long timer3_toggle_count;
@@ -123,8 +124,6 @@ static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
123
124
124
125
#endif
125
126
126
-
127
-
128
127
static int8_t toneBegin (uint8_t _pin)
129
128
{
130
129
int8_t _timer = -1 ;
@@ -242,6 +241,7 @@ static int8_t toneBegin(uint8_t _pin)
242
241
243
242
void tone (uint8_t _pin, unsigned int frequency, unsigned long duration)
244
243
{
244
+ nonzerofreq = frequency > 20 ;
245
245
uint8_t prescalarbits = 0b001 ;
246
246
long toggle_count = 0 ;
247
247
uint32_t ocr = 0 ;
@@ -477,10 +477,51 @@ void disableTimer(uint8_t _timer)
477
477
}
478
478
479
479
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
+
480
520
void noTone (uint8_t _pin)
481
521
{
522
+ digitalWrite (_pin, 0 );
523
+ next_tone = *_next_tone;
482
524
int8_t _timer = -1 ;
483
-
484
525
for (int i = 0 ; i < AVAILABLE_TONE_PINS; i++) {
485
526
if (tone_pins[i] == _pin) {
486
527
_timer = pgm_read_byte (tone_pin_to_timer_PGM + i);
@@ -490,10 +531,9 @@ void noTone(uint8_t _pin)
490
531
}
491
532
492
533
disableTimer (_timer);
493
-
494
- digitalWrite (_pin, 0 );
495
534
}
496
535
536
+
497
537
#ifdef USE_TIMER0
498
538
ISR (TIMER0_COMPA_vect)
499
539
{
@@ -537,23 +577,18 @@ ISR(TIMER1_COMPA_vect)
537
577
#ifdef USE_TIMER2
538
578
ISR (TIMER2_COMPA_vect)
539
579
{
540
-
541
580
if (timer2_toggle_count != 0 )
542
581
{
543
582
// toggle the pin
544
- *timer2_pin_port ^= timer2_pin_mask;
583
+ if (nonzerofreq)
584
+ *timer2_pin_port ^= timer2_pin_mask;
545
585
546
586
if (timer2_toggle_count > 0 )
547
587
timer2_toggle_count--;
548
588
}
549
589
else
550
590
{
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 ();
557
592
}
558
593
}
559
594
#endif
0 commit comments