From 7c0665c13024eaf7bec1f82f79e08ed463c1060c Mon Sep 17 00:00:00 2001 From: chiararuggeri Date: Thu, 27 Apr 2017 10:14:13 +0200 Subject: [PATCH] Fixed issue that occurred when no duration in tone function was indicate --- cores/arduino/Tone.cpp | 50 ++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index fa3c266..cb8dd29 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -39,6 +39,7 @@ Version Modified By Date Comments unsigned long int count_duration=0; +volatile bool no_stop = false; uint8_t pin_sound=0; @@ -52,11 +53,16 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) float per=float(1)/frequency; time_per=per/0.000008; unsigned int duty=time_per/2; - float mil=float(duration)/1000; - if(per>mil) - count_duration=1; + if(duration > 0){ + no_stop = false; + float mil=float(duration)/1000; + if(per>mil) + count_duration=1; + else + count_duration= mil/per; + } else - count_duration= mil/per; + no_stop = true; // Configure PWM static uint16_t seq_values[]={0}; @@ -129,29 +135,41 @@ extern "C"{ void PWM0_IRQHandler(void){ nrf_pwm_event_clear(NRF_PWM0, NRF_PWM_EVENT_PWMPERIODEND); - count_duration--; - if(count_duration == 0) - noTone(pin_sound); + if(!no_stop){ + count_duration--; + if(count_duration == 0) + noTone(pin_sound); + else + nrf_pwm_task_trigger(NRF_PWM0, NRF_PWM_TASK_SEQSTART0); + } else - nrf_pwm_task_trigger(NRF_PWM0, NRF_PWM_TASK_SEQSTART0); + nrf_pwm_task_trigger(NRF_PWM0, NRF_PWM_TASK_SEQSTART0); } void PWM1_IRQHandler(void){ nrf_pwm_event_clear(NRF_PWM1, NRF_PWM_EVENT_PWMPERIODEND); - count_duration--; - if(count_duration == 0) - noTone(pin_sound); + if(!no_stop){ + count_duration--; + if(count_duration == 0) + noTone(pin_sound); + else + nrf_pwm_task_trigger(NRF_PWM1, NRF_PWM_TASK_SEQSTART0); + } else - nrf_pwm_task_trigger(NRF_PWM1, NRF_PWM_TASK_SEQSTART0); + nrf_pwm_task_trigger(NRF_PWM1, NRF_PWM_TASK_SEQSTART0); } void PWM2_IRQHandler(void){ nrf_pwm_event_clear(NRF_PWM2, NRF_PWM_EVENT_PWMPERIODEND); - count_duration--; - if(count_duration == 0) - noTone(pin_sound); + if(!no_stop){ + count_duration--; + if(count_duration == 0) + noTone(pin_sound); + else + nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_SEQSTART0); + } else - nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_SEQSTART0); + nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_SEQSTART0); } #ifdef __cplusplus