From 6e1fdc42aea2bb8818217561be555f8b062dc0c2 Mon Sep 17 00:00:00 2001 From: PapaKarlo787 <33232815+PapaKarlo787@users.noreply.github.com> Date: Fri, 24 May 2024 11:13:49 +0300 Subject: [PATCH 1/6] Update Tone.cpp --- cores/arduino/Tone.cpp | 63 ++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index 1bfb3e379..d57df0d87 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -50,7 +50,6 @@ Version Modified By Date Comments #define TIMER2_COMPA_vect TIMER2_COMP_vect #define TIMSK1 TIMSK #endif - // timerx_toggle_count: // > 0 - duration specified // = 0 - stopped @@ -68,6 +67,8 @@ volatile uint8_t timer1_pin_mask; volatile long timer2_toggle_count; volatile uint8_t *timer2_pin_port; volatile uint8_t timer2_pin_mask; +volatile uint8_t tonePin; +volatile uint8_t nonzerofreq; #if defined(TIMSK3) volatile long timer3_toggle_count; @@ -123,8 +124,6 @@ static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ }; #endif - - static int8_t toneBegin(uint8_t _pin) { int8_t _timer = -1; @@ -242,6 +241,7 @@ static int8_t toneBegin(uint8_t _pin) void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { + nonzerofreq = frequency > 20; uint8_t prescalarbits = 0b001; long toggle_count = 0; uint32_t ocr = 0; @@ -477,10 +477,51 @@ void disableTimer(uint8_t _timer) } +void _next_tone(){ + // need to call noTone() so that the tone_pins[] entry is reset, so the + // timer gets initialized next time we call tone(). + // XXX: this assumes timer 2 is always the first one used. + disableTimer(2); + *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop +} + +const int16_t* auto_next_tone_pointer; +uint8_t auto_next_tone_pin; +uint16_t auto_next_tone_index; + +void auto_next_tone() { + switch (pgm_read_word(auto_next_tone_pointer + auto_next_tone_index)) { + case -1: noTone(auto_next_tone_pin); return; + case -2: auto_next_tone_index = 0; break; + } + 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)); + auto_next_tone_index += 2; +} + + + +void (*next_tone)(void) = *_next_tone; + + +void tone(void (*f)(void)) { + next_tone = *f; + next_tone(); +} + +void auto_tone(uint8_t pin, const int16_t* ntp){ + next_tone = *auto_next_tone; + auto_next_tone_pointer = ntp; + auto_next_tone_pin = pin; + auto_next_tone_index = 0; + next_tone(); +} + + void noTone(uint8_t _pin) { + digitalWrite(_pin, 0); + next_tone = *_next_tone; int8_t _timer = -1; - for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { if (tone_pins[i] == _pin) { _timer = pgm_read_byte(tone_pin_to_timer_PGM + i); @@ -490,10 +531,9 @@ void noTone(uint8_t _pin) } disableTimer(_timer); - - digitalWrite(_pin, 0); } + #ifdef USE_TIMER0 ISR(TIMER0_COMPA_vect) { @@ -537,23 +577,18 @@ ISR(TIMER1_COMPA_vect) #ifdef USE_TIMER2 ISR(TIMER2_COMPA_vect) { - if (timer2_toggle_count != 0) { // toggle the pin - *timer2_pin_port ^= timer2_pin_mask; + if (nonzerofreq) + *timer2_pin_port ^= timer2_pin_mask; if (timer2_toggle_count > 0) timer2_toggle_count--; } else { - // need to call noTone() so that the tone_pins[] entry is reset, so the - // timer gets initialized next time we call tone(). - // XXX: this assumes timer 2 is always the first one used. - noTone(tone_pins[0]); -// disableTimer(2); -// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop + next_tone(); } } #endif From a56cde90b988e809fda43f91ff6a65cb2b32c89f Mon Sep 17 00:00:00 2001 From: PapaKarlo787 <33232815+PapaKarlo787@users.noreply.github.com> Date: Fri, 24 May 2024 11:14:11 +0300 Subject: [PATCH 2/6] Update Arduino.h --- cores/arduino/Arduino.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 91eeb16bc..cb193e5f8 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -111,8 +111,7 @@ void yield(void); #define bitRead(value, bit) (((value) >> (bit)) & 0x01) #define bitSet(value, bit) ((value) |= (1UL << (bit))) #define bitClear(value, bit) ((value) &= ~(1UL << (bit))) -#define bitToggle(value, bit) ((value) ^= (1UL << (bit))) -#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit)) +#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) // avr-libc defines _NOP() since 1.6.2 #ifndef _NOP @@ -131,16 +130,16 @@ void initVariant(void); int atexit(void (*func)()) __attribute__((weak)); -void pinMode(uint8_t pin, uint8_t mode); -void digitalWrite(uint8_t pin, uint8_t val); -int digitalRead(uint8_t pin); -int analogRead(uint8_t pin); +void pinMode(uint8_t, uint8_t); +void digitalWrite(uint8_t, uint8_t); +int digitalRead(uint8_t); +int analogRead(uint8_t); void analogReference(uint8_t mode); -void analogWrite(uint8_t pin, int val); +void analogWrite(uint8_t, int); unsigned long millis(void); unsigned long micros(void); -void delay(unsigned long ms); +void delay(unsigned long); void delayMicroseconds(unsigned int us); unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); @@ -148,8 +147,8 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); -void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode); -void detachInterrupt(uint8_t interruptNum); +void attachInterrupt(uint8_t, void (*)(void), int mode); +void detachInterrupt(uint8_t); void setup(void); void loop(void); @@ -245,6 +244,8 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 100000 unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0); +void tone(void (*f)(void)); +void auto_tone(uint8_t pin, const int16_t* pointer); void noTone(uint8_t _pin); // WMath prototypes From dfec47268637b607e3d698191b728ca2f222329d Mon Sep 17 00:00:00 2001 From: PapaKarlo787 <33232815+PapaKarlo787@users.noreply.github.com> Date: Fri, 24 May 2024 11:17:10 +0300 Subject: [PATCH 3/6] Update Arduino.h --- cores/arduino/Arduino.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index cb193e5f8..b4d1e9763 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -111,7 +111,8 @@ void yield(void); #define bitRead(value, bit) (((value) >> (bit)) & 0x01) #define bitSet(value, bit) ((value) |= (1UL << (bit))) #define bitClear(value, bit) ((value) &= ~(1UL << (bit))) -#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit)) +#define bitToggle(value, bit) ((value) ^= (1UL << (bit))) +#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit)) // avr-libc defines _NOP() since 1.6.2 #ifndef _NOP @@ -130,16 +131,16 @@ void initVariant(void); int atexit(void (*func)()) __attribute__((weak)); -void pinMode(uint8_t, uint8_t); -void digitalWrite(uint8_t, uint8_t); -int digitalRead(uint8_t); -int analogRead(uint8_t); +void pinMode(uint8_t pin, uint8_t mode); +void digitalWrite(uint8_t pin, uint8_t val); +int digitalRead(uint8_t pin); +int analogRead(uint8_t pin); void analogReference(uint8_t mode); -void analogWrite(uint8_t, int); +void analogWrite(uint8_t pin, int val); unsigned long millis(void); unsigned long micros(void); -void delay(unsigned long); +void delay(unsigned long ms); void delayMicroseconds(unsigned int us); unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); @@ -147,8 +148,8 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); -void attachInterrupt(uint8_t, void (*)(void), int mode); -void detachInterrupt(uint8_t); +void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode); +void detachInterrupt(uint8_t interruptNum); void setup(void); void loop(void); From 76e1beca330454175886e9476f784815b158e3b4 Mon Sep 17 00:00:00 2001 From: PapaKarlo787 <33232815+PapaKarlo787@users.noreply.github.com> Date: Fri, 24 May 2024 11:52:07 +0300 Subject: [PATCH 4/6] Update Arduino.h --- cores/arduino/Arduino.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index b4d1e9763..28f80ab21 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -246,7 +246,7 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 10 void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0); void tone(void (*f)(void)); -void auto_tone(uint8_t pin, const int16_t* pointer); +void autoTone(uint8_t pin, const int16_t* pointer); void noTone(uint8_t _pin); // WMath prototypes From ee9c46306220a498dcd72e22716016d6e3ca4958 Mon Sep 17 00:00:00 2001 From: PapaKarlo787 <33232815+PapaKarlo787@users.noreply.github.com> Date: Fri, 24 May 2024 11:52:42 +0300 Subject: [PATCH 5/6] Update Tone.cpp --- cores/arduino/Tone.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index d57df0d87..517e6e519 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -508,7 +508,7 @@ void tone(void (*f)(void)) { next_tone(); } -void auto_tone(uint8_t pin, const int16_t* ntp){ +void autoTone(uint8_t pin, const int16_t* ntp){ next_tone = *auto_next_tone; auto_next_tone_pointer = ntp; auto_next_tone_pin = pin; From d06c945de98a3f8b93d54ae7a35e0fa5da9f2ab5 Mon Sep 17 00:00:00 2001 From: PapaKarlo787 <33232815+PapaKarlo787@users.noreply.github.com> Date: Sat, 25 May 2024 09:36:27 +0300 Subject: [PATCH 6/6] Update Arduino.h --- cores/arduino/Arduino.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 28f80ab21..1e76dd84d 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -146,7 +146,7 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); -uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); +uint8_t shifting(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode); void detachInterrupt(uint8_t interruptNum);