1
+ #include " esp32-hal-ledc.h"
1
2
#include " CommandHandler.h"
2
3
3
4
Servo servo6;
@@ -138,6 +139,10 @@ void CommandHandler::initTwin_F() {
138
139
if (servo9.attached ()) servo9.detach ();
139
140
if (servo10.attached ()) servo10.detach ();
140
141
142
+ ledcDetach (D6_output_pin);
143
+ ledcDetach (D9_output_pin);
144
+ ledcDetach (D10_output_pin);
145
+
141
146
if (dc_coding_at_pin_06_ptr)
142
147
{
143
148
delete dc_coding_at_pin_06_ptr;
@@ -249,14 +254,22 @@ void CommandHandler::digitalRead_F() {
249
254
250
255
void CommandHandler::analogWrite_F (){
251
256
uint8_t pin = getActualPin (bleChannel.received .message_data [0 ], DIGITAL);
252
- pinMode (pin, OUTPUT);
253
257
uint16_t value = bleChannel.received .message_data [1 ];
254
258
// Serial.println(value);
255
-
259
+ int channel;
260
+
256
261
value = map (value,0 ,255 ,0 ,4095 ) + 1 ;
257
262
if (value > 4095 ) value = 4095 ;
258
263
259
- ledcAttach (pin, freq, resolution);
264
+ switch (pin) {
265
+ case D6_output_pin: channel = PWM_CHANNEL_1; break ;
266
+ case D9_output_pin: channel = PWM_CHANNEL_2; break ;
267
+ case D10_output_pin: channel = PWM_CHANNEL_3; break ;
268
+ }
269
+
270
+ pinMode (pin, OUTPUT);
271
+ ledcDetach (pin);
272
+ ledcAttachChannel (pin, freq, resolution, channel);
260
273
ledcWrite (pin, value);
261
274
262
275
bleChannel.sent .message_length = 1 ;
@@ -300,10 +313,11 @@ void CommandHandler::playNote_F(){
300
313
uint16_t note = bleChannel.received .message_data [1 ] + (bleChannel.received .message_data [2 ] << 8 );
301
314
uint16_t duration = bleChannel.received .message_data [3 ] + (bleChannel.received .message_data [4 ] << 8 );
302
315
303
- ledcAttach (pin,2700 ,8 );
316
+ ledcAttachChannel (pin,2700 ,8 ,TONE_CHANNEL );
304
317
ledcWriteTone (pin, note);
305
318
delay (duration);
306
319
ledcWriteTone (pin, 0 );
320
+ ledcDetach (pin);
307
321
308
322
bleChannel.sent .message_length = 1 ;
309
323
uint8_t response_data[1 ] = {1 };
@@ -435,7 +449,7 @@ void CommandHandler::singleDC_F(){
435
449
if (!dc_coding_at_pin_06_ptr)
436
450
{
437
451
delete dc_coding_at_pin_06_ptr;
438
- dc_coding_at_pin_06_ptr = new TwinDCMotor (1 , D6_motor_pin );
452
+ dc_coding_at_pin_06_ptr = new TwinDCMotor (1 , D6_output_pin );
439
453
}
440
454
// Serial.println(motor_dir);
441
455
// Serial.println(motor_speed);
@@ -449,7 +463,7 @@ void CommandHandler::singleDC_F(){
449
463
if (!dc_coding_at_pin_09_ptr)
450
464
{
451
465
delete dc_coding_at_pin_09_ptr;
452
- dc_coding_at_pin_09_ptr = new TwinDCMotor (1 , D9_motor_pin );
466
+ dc_coding_at_pin_09_ptr = new TwinDCMotor (1 , D9_output_pin );
453
467
}
454
468
455
469
dc_coding_at_pin_09_ptr->setDirAndSpeedCoding (0 , motor_dir, motor_speed);
@@ -460,7 +474,7 @@ void CommandHandler::singleDC_F(){
460
474
if (!dc_coding_at_pin_0A_ptr)
461
475
{
462
476
delete dc_coding_at_pin_0A_ptr;
463
- dc_coding_at_pin_0A_ptr = new TwinDCMotor (1 , D10_motor_pin );
477
+ dc_coding_at_pin_0A_ptr = new TwinDCMotor (1 , D10_output_pin );
464
478
}
465
479
// Serial.println(motor_dir);
466
480
// Serial.println(motor_speed);
@@ -564,11 +578,11 @@ uint8_t CommandHandler::getActualPin(uint8_t logicalPin, bool signal_type) {
564
578
switch (logicalPin) {
565
579
566
580
case 6 :
567
- if (signal_type == DIGITAL) return D6_motor_pin ;
581
+ if (signal_type == DIGITAL) return D6_output_pin ;
568
582
else if (signal_type == ANALOG) return AN_IN_4;
569
583
570
- case 9 : return D9_motor_pin ;
571
- case 10 : return D10_motor_pin ;
584
+ case 9 : return D9_output_pin ;
585
+ case 10 : return D10_output_pin ;
572
586
573
587
// input pins
574
588
case 4 : return D_IN_4;
0 commit comments