Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit e63d450

Browse files
chiararuggerisergiotomasello
authored andcommitted
Fixed issue that occurred when no duration in tone function was indicate
1 parent da24aaa commit e63d450

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

cores/arduino/Tone.cpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Version Modified By Date Comments
3939

4040

4141
unsigned long int count_duration=0;
42+
volatile bool no_stop = false;
4243
uint8_t pin_sound=0;
4344

4445

@@ -52,11 +53,16 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
5253
float per=float(1)/frequency;
5354
time_per=per/0.000008;
5455
unsigned int duty=time_per/2;
55-
float mil=float(duration)/1000;
56-
if(per>mil)
57-
count_duration=1;
56+
if(duration > 0){
57+
no_stop = false;
58+
float mil=float(duration)/1000;
59+
if(per>mil)
60+
count_duration=1;
61+
else
62+
count_duration= mil/per;
63+
}
5864
else
59-
count_duration= mil/per;
65+
no_stop = true;
6066

6167
// Configure PWM
6268
static uint16_t seq_values[]={0};
@@ -129,29 +135,41 @@ extern "C"{
129135

130136
void PWM0_IRQHandler(void){
131137
nrf_pwm_event_clear(NRF_PWM0, NRF_PWM_EVENT_PWMPERIODEND);
132-
count_duration--;
133-
if(count_duration == 0)
134-
noTone(pin_sound);
138+
if(!no_stop){
139+
count_duration--;
140+
if(count_duration == 0)
141+
noTone(pin_sound);
142+
else
143+
nrf_pwm_task_trigger(NRF_PWM0, NRF_PWM_TASK_SEQSTART0);
144+
}
135145
else
136-
nrf_pwm_task_trigger(NRF_PWM0, NRF_PWM_TASK_SEQSTART0);
146+
nrf_pwm_task_trigger(NRF_PWM0, NRF_PWM_TASK_SEQSTART0);
137147
}
138148

139149
void PWM1_IRQHandler(void){
140150
nrf_pwm_event_clear(NRF_PWM1, NRF_PWM_EVENT_PWMPERIODEND);
141-
count_duration--;
142-
if(count_duration == 0)
143-
noTone(pin_sound);
151+
if(!no_stop){
152+
count_duration--;
153+
if(count_duration == 0)
154+
noTone(pin_sound);
155+
else
156+
nrf_pwm_task_trigger(NRF_PWM1, NRF_PWM_TASK_SEQSTART0);
157+
}
144158
else
145-
nrf_pwm_task_trigger(NRF_PWM1, NRF_PWM_TASK_SEQSTART0);
159+
nrf_pwm_task_trigger(NRF_PWM1, NRF_PWM_TASK_SEQSTART0);
146160
}
147161

148162
void PWM2_IRQHandler(void){
149163
nrf_pwm_event_clear(NRF_PWM2, NRF_PWM_EVENT_PWMPERIODEND);
150-
count_duration--;
151-
if(count_duration == 0)
152-
noTone(pin_sound);
164+
if(!no_stop){
165+
count_duration--;
166+
if(count_duration == 0)
167+
noTone(pin_sound);
168+
else
169+
nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_SEQSTART0);
170+
}
153171
else
154-
nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_SEQSTART0);
172+
nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_SEQSTART0);
155173
}
156174

157175
#ifdef __cplusplus

0 commit comments

Comments
 (0)