6
6
7
7
static TaskHandle_t _tone_task = NULL ;
8
8
static QueueHandle_t _tone_queue = NULL ;
9
- static uint8_t _channel = 0 ;
9
+ static int8_t _pin = - 1 ;
10
10
11
11
typedef enum {
12
12
TONE_START,
13
- TONE_END,
14
- TONE_SET_CHANNEL
13
+ TONE_END
15
14
} tone_cmd_t ;
16
15
17
16
typedef struct {
18
17
tone_cmd_t tone_cmd;
19
18
uint8_t pin;
20
19
unsigned int frequency;
21
20
unsigned long duration;
22
- uint8_t channel;
23
21
} tone_msg_t ;
24
22
25
23
static void tone_task (void *){
@@ -28,28 +26,28 @@ static void tone_task(void*){
28
26
xQueueReceive (_tone_queue, &tone_msg, portMAX_DELAY);
29
27
switch (tone_msg.tone_cmd ){
30
28
case TONE_START:
31
- log_d (" Task received from queue TONE_START: _pin =%d, frequency=%u Hz, duration=%lu ms" , tone_msg.pin , tone_msg.frequency , tone_msg.duration );
29
+ log_d (" Task received from queue TONE_START: pin =%d, frequency=%u Hz, duration=%lu ms" , tone_msg.pin , tone_msg.frequency , tone_msg.duration );
32
30
33
- log_d (" Setup LED controll on channel %d" , _channel);
34
- ledcAttachPin (tone_msg.pin , _channel);
35
- ledcWriteTone (_channel, tone_msg.frequency );
31
+ if (_pin == -1 ) {
32
+ if (ledcAttach (tone_msg.pin , tone_msg.frequency , 10 ) == 0 ) {
33
+ log_e (" Tone start failed" );
34
+ break ;
35
+ }
36
+ _pin = tone_msg.pin ;
37
+ }
38
+ ledcWriteTone (tone_msg.pin , tone_msg.frequency );
36
39
37
40
if (tone_msg.duration ){
38
41
delay (tone_msg.duration );
39
- ledcDetachPin (tone_msg.pin );
40
- ledcWriteTone (_channel, 0 );
42
+ ledcWriteTone (tone_msg.pin , 0 );
41
43
}
42
44
break ;
43
45
44
46
case TONE_END:
45
47
log_d (" Task received from queue TONE_END: pin=%d" , tone_msg.pin );
46
- ledcDetachPin (tone_msg.pin );
47
- ledcWriteTone (_channel, 0 );
48
- break ;
49
-
50
- case TONE_SET_CHANNEL:
51
- log_d (" Task received from queue TONE_SET_CHANNEL: channel=%d" , tone_msg.channel );
52
- _channel = tone_msg.channel ;
48
+ ledcWriteTone (tone_msg.pin , 0 );
49
+ ledcDetach (tone_msg.pin );
50
+ _pin = -1 ;
53
51
break ;
54
52
55
53
default : ; // do nothing
@@ -87,49 +85,45 @@ static int tone_init(){
87
85
return 1 ; // OK
88
86
}
89
87
90
- void setToneChannel (uint8_t channel){
91
- log_d (" channel=%d" , channel);
92
- if (tone_init ()){
93
- tone_msg_t tone_msg = {
94
- .tone_cmd = TONE_SET_CHANNEL,
95
- .pin = 0 , // Ignored
96
- .frequency = 0 , // Ignored
97
- .duration = 0 , // Ignored
98
- .channel = channel
99
- };
100
- xQueueSend (_tone_queue, &tone_msg, portMAX_DELAY);
101
- }
102
- }
103
-
104
- void noTone (uint8_t _pin){
88
+ void noTone (uint8_t pin){
105
89
log_d (" noTone was called" );
106
- if (tone_init ()){
107
- tone_msg_t tone_msg = {
108
- .tone_cmd = TONE_END,
109
- .pin = _pin,
110
- .frequency = 0 , // Ignored
111
- .duration = 0 , // Ignored
112
- .channel = 0 // Ignored
113
- };
114
- xQueueSend (_tone_queue, &tone_msg, portMAX_DELAY);
90
+ if (_pin == pin) {
91
+ if (tone_init ()){
92
+ tone_msg_t tone_msg = {
93
+ .tone_cmd = TONE_END,
94
+ .pin = pin,
95
+ .frequency = 0 , // Ignored
96
+ .duration = 0 , // Ignored
97
+ };
98
+ xQueueSend (_tone_queue, &tone_msg, portMAX_DELAY);
99
+ }
100
+ }
101
+ else {
102
+ log_e (" Tone is not running on given pin %d" , pin);
115
103
}
116
104
}
117
105
118
106
// parameters:
119
- // _pin - pin number which will output the signal
107
+ // pin - pin number which will output the signal
120
108
// frequency - PWM frequency in Hz
121
109
// duration - time in ms - how long will the signal be outputted.
122
110
// If not provided, or 0 you must manually call noTone to end output
123
- void tone (uint8_t _pin, unsigned int frequency, unsigned long duration){
124
- log_d (" _pin=%d, frequency=%u Hz, duration=%lu ms" , _pin, frequency, duration);
125
- if (tone_init ()){
126
- tone_msg_t tone_msg = {
127
- .tone_cmd = TONE_START,
128
- .pin = _pin,
129
- .frequency = frequency,
130
- .duration = duration,
131
- .channel = 0 // Ignored
132
- };
133
- xQueueSend (_tone_queue, &tone_msg, portMAX_DELAY);
111
+ void tone (uint8_t pin, unsigned int frequency, unsigned long duration){
112
+ log_d (" pin=%d, frequency=%u Hz, duration=%lu ms" , pin, frequency, duration);
113
+ if (_pin == -1 || _pin == pin) {
114
+ if (tone_init ()){
115
+ tone_msg_t tone_msg = {
116
+ .tone_cmd = TONE_START,
117
+ .pin = pin,
118
+ .frequency = frequency,
119
+ .duration = duration,
120
+ };
121
+ xQueueSend (_tone_queue, &tone_msg, portMAX_DELAY);
122
+ return ;
123
+ }
124
+ }
125
+ else {
126
+ log_e (" Tone is still running on pin %d, call noTone(%d) first!" , _pin, _pin);
127
+ return ;
134
128
}
135
129
}
0 commit comments