-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDriver.h
1582 lines (1427 loc) · 44.1 KB
/
Driver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "Driver/ac101/ac101.h"
#include "Driver/ad1938/ad1938.h"
#include "Driver/cs42448/cs42448.h"
#include "Driver/cs43l22/cs43l22.h"
#include "Driver/es7210/es7210.h"
#include "Driver/es7243/es7243.h"
#include "Driver/es7243e/es7243e.h"
#include "Driver/es8156/es8156.h"
#include "Driver/es8311/es8311.h"
#include "Driver/es8374/es8374.h"
#include "Driver/es8388/es8388.h"
#include "Driver/pcm3168/pcm3168.h"
#include "Driver/tas5805m/tas5805m.h"
#include "Driver/wm8960/mtb_wm8960.h"
#include "Driver/wm8978/WM8978.h"
#include "Driver/wm8994/wm8994.h"
#include "DriverCommon.h"
#include "DriverPins.h"
namespace audio_driver {
const int rate_num[14] = {8000, 11025, 16000, 22050, 24000, 32000, 44100,
48000, 64000, 88200, 96000, 128000, 176400, 192000};
const samplerate_t rate_code[14] = {
RATE_8K, RATE_11K, RATE_16K, RATE_22K, RATE_24K, RATE_32K, RATE_44K,
RATE_48K, RATE_64K, RATE_88K, RATE_96K, RATE_128K, RATE_176K, RATE_192K};
/**
* @brief I2S configuration and definition of input and output with default
* values
* @ingroup audio_driver
* @author Phil Schatzmann
* @copyright GPLv3
*/
class CodecConfig : public codec_config_t {
public:
/// @brief setup default values
CodecConfig() {
input_device = ADC_INPUT_LINE1;
output_device = DAC_OUTPUT_ALL;
i2s.bits = BIT_LENGTH_16BITS;
i2s.rate = RATE_44K;
i2s.channels = CHANNELS2;
i2s.fmt = I2S_NORMAL;
// codec is slave - microcontroller is master
i2s.mode = MODE_SLAVE;
}
/// Compare all attributes but ignore sample rate
bool equalsExRate(CodecConfig alt) {
return (input_device == alt.input_device &&
output_device == alt.output_device && i2s.bits == alt.i2s.bits &&
i2s.channels == alt.i2s.channels && i2s.fmt == alt.i2s.fmt &&
i2s.mode == alt.i2s.mode);
}
/// Returns bits per sample as number
int getBitsNumeric() {
switch (i2s.bits) {
case BIT_LENGTH_16BITS:
return 16;
case BIT_LENGTH_24BITS:
return 24;
case BIT_LENGTH_32BITS:
return 32;
default:
return 0;
}
return 0;
}
/// Sets the bits per sample with a numeric value
bool setBitsNumeric(int bits) {
switch (bits) {
case 16:
i2s.bits = BIT_LENGTH_16BITS;
return true;
case 18:
i2s.bits = BIT_LENGTH_18BITS;
return true;
case 20:
i2s.bits = BIT_LENGTH_20BITS;
return true;
case 24:
i2s.bits = BIT_LENGTH_24BITS;
return true;
case 32:
i2s.bits = BIT_LENGTH_32BITS;
return true;
}
AD_LOGE("bits not supported: %d", bits);
return false;
}
/// Get the sample rate as number
int getRateNumeric() {
for (int j = 0; j < 14; j++) {
if (rate_code[j] == i2s.rate) {
AD_LOGD("-> %d", rate_num[j]);
return rate_num[j];
}
}
return 0;
}
/// Returns the number of channels as number
int getChannelsNumeric() { return i2s.channels; }
/// Defines the number of channels
bool setChannelsNumeric(int channels) {
switch (2) {
case CHANNELS2:
i2s.channels = (channels_t)channels;
return true;
case CHANNELS4:
i2s.channels = (channels_t)channels;
return true;
case CHANNELS8:
i2s.channels = (channels_t)channels;
return true;
case CHANNELS16:
i2s.channels = (channels_t)channels;
return true;
default:
AD_LOGE("Channels not supported: %d - using %d", channels, 2);
i2s.channels = CHANNELS2;
return false;
}
}
/// Sets the sample rate as number: returns the effectively set rate
int setRateNumeric(int requestedRate) {
int diff = 99999;
int result = 0;
for (int j = 0; j < 14; j++) {
if (rate_num[j] == requestedRate) {
AD_LOGD("-> %d", rate_num[j]);
i2s.rate = rate_code[j];
return requestedRate;
} else {
int new_diff = abs(rate_code[j] - requestedRate);
if (new_diff < diff) {
result = j;
diff = new_diff;
}
}
}
AD_LOGE("Sample Rate not supported: %d - using %d", requestedRate,
rate_num[result]);
i2s.rate = rate_code[result];
return rate_num[result];
}
/// Determines the codec_mode_t dynamically based on the input and output
codec_mode_t get_mode() {
// codec_mode_t mode;
bool is_input = false;
bool is_output = false;
if (input_device != ADC_INPUT_NONE) is_input = true;
if (output_device != DAC_OUTPUT_NONE) is_output = true;
if (is_input && is_output) {
AD_LOGD("mode->CODEC_MODE_BOTH");
return CODEC_MODE_BOTH;
}
if (is_output) {
AD_LOGD("mode->CODEC_MODE_DECODE");
return CODEC_MODE_DECODE;
}
if (is_input) {
AD_LOGD("mode->CODEC_MODE_ENCODE");
return CODEC_MODE_ENCODE;
}
AD_LOGD("mode->CODEC_MODE_NONE");
return CODEC_MODE_NONE;
}
// if sd active we setup SPI for the SD
bool sd_active = true;
};
/**
* @brief Abstract Driver API for codec chips
* @ingroup audio_driver
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriver {
public:
/// Starts the processing
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
AD_LOGI("AudioDriver::begin");
p_pins = &pins;
// Store default i2c address to pins
setupI2CAddress();
p_pins->setSPIActiveForSD(codec_cfg.sd_active);
if (!p_pins->begin()) {
AD_LOGE("AudioBoard::pins::begin failed");
return false;
}
if (!setConfig(codecCfg)) {
AD_LOGE("setConfig has failed");
return false;
}
setPAPower(true);
// setup default volume
setVolume(DRIVER_DEFAULT_VOLUME);
return true;
}
/// changes the configuration
virtual bool setConfig(CodecConfig codecCfg) {
AD_LOGI("AudioDriver::setConfig");
codec_cfg = codecCfg;
if (!init(codec_cfg)) {
AD_LOGE("AudioDriver init failed");
return false;
}
codec_mode_t codec_mode = codec_cfg.get_mode();
if (!controlState(codec_mode)) {
AD_LOGE("AudioDriver controlState failed");
return false;
}
bool result = configInterface(codec_mode, codec_cfg.i2s);
if (!result) {
AD_LOGE("AudioDriver configInterface failed");
return false;
}
return result;
}
/// Ends the processing: shut down dac and adc
virtual bool end(void) { return deinit(); }
/// Mutes all output lines
virtual bool setMute(bool enable) = 0;
/// Mute individual lines: only supported for some rare DACs
virtual bool setMute(bool mute, int line) {
AD_LOGE("setMute not supported on line level");
return false;
}
/// Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.
virtual bool setVolume(int volume) = 0;
/// Determines the actual volume (range: 0-100)
virtual int getVolume() = 0;
/// Defines the input volume (range: 0-100) if supported
virtual bool setInputVolume(int volume) { return false; }
/// Determines if setVolume() is suppored
virtual bool isVolumeSupported() { return true; }
/// Determines if setInputVolume() is supported
virtual bool isInputVolumeSupported() { return false; }
/// Provides the pin information
virtual DriverPins &pins() { return *p_pins; }
void setPins(DriverPins &pins) { p_pins = &pins; }
/// Sets the PA Power pin to active or inactive
bool setPAPower(bool enable) {
if (p_pins == nullptr) {
AD_LOGI("pins are null");
return false;
}
GpioPin pin = pins().getPinID(PinFunction::PA);
if (pin == -1) {
AD_LOGI("PinFunction::PA not defined");
return false;
}
AD_LOGI("setPAPower pin %d -> %d", pin, enable);
digitalWrite(pin, enable ? HIGH : LOW);
return true;
}
operator bool() { return p_pins != nullptr; }
/// Defines the i2c address
virtual bool setI2CAddress(uint16_t adr) {
if (p_pins == nullptr) return false;
// look it up from the pin definition
auto i2c = pins().getI2CPins(PinFunction::CODEC);
if (i2c) {
AD_LOGI("==> Updating address: 0x%x", adr);
PinsI2C val = i2c.value();
val.address = adr;
pins().setI2C(val);
return true;
} else {
// we must have a codec defined!
assert(false);
}
return false;
}
/// Provides the i2c address
virtual int getI2CAddress() {
if (p_pins == nullptr) return i2c_default_address;
// look it up from the pin definition
auto i2c = pins().getI2CPins(PinFunction::CODEC);
if (i2c) {
auto value = i2c.value();
auto addr = value.address;
if (addr > -1) return addr;
}
return i2c_default_address;
}
/// If no address is defined in the pins we provide it here
void setupI2CAddress() {
AD_LOGI("setupI2CAddress: 0x%x", i2c_default_address);
int adr = getI2CAddress();
setI2CAddress(adr);
}
protected:
CodecConfig codec_cfg;
DriverPins *p_pins = nullptr;
int i2c_default_address = -1;
int mapVolume(int x, int in_min, int in_max, int out_min, int out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/// Determine the TwoWire object from the I2C config or use Wire
virtual i2c_bus_handle_t getI2C() {
if (p_pins == nullptr) return DEFAULT_WIRE;
auto i2c = pins().getI2CPins(PinFunction::CODEC);
if (!i2c) {
return DEFAULT_WIRE;
}
i2c_bus_handle_t *result = (i2c_bus_handle_t *)i2c.value().p_wire;
return result;
}
virtual bool init(codec_config_t codec_cfg) { return false; }
virtual bool deinit() { return false; }
virtual bool controlState(codec_mode_t mode) { return false; };
virtual bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return false;
};
/// make sure that value is in range
/// @param volume
/// @return
int limitValue(int volume, int min = 0, int max = 100) {
if (volume > max) volume = max;
if (volume < min) volume = min;
return volume;
}
};
/**
* @brief Dummy Driver which does nothing.
* @ingroup audio_driver
* @author Phil Schatzmann
* @copyright GPLv3
*/
class NoDriverClass : public AudioDriver {
public:
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
// codec_cfg = codecCfg;
// p_pins = &pins;
return true;
}
virtual bool end(void) { return true; }
virtual bool setMute(bool enable) { return false; }
virtual bool setVolume(int volume) { return false; }
virtual int getVolume() { return 100; }
virtual bool setInputVolume(int volume) { return false; }
virtual bool isVolumeSupported() {
{
return false;
}
}
virtual bool isInputVolumeSupported() { return false; }
};
/**
* @brief Driver API for AC101 codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverAC101Class : public AudioDriver {
public:
AudioDriverAC101Class() { i2c_default_address = 0x1A; }
bool setMute(bool mute) { return ac101_set_voice_mute(mute); }
bool setVolume(int volume) {
return ac101_set_voice_volume(limitValue(volume, 0, 100));
};
int getVolume() {
int vol;
ac101_get_voice_volume(&vol);
return vol;
};
protected:
bool init(codec_config_t codec_cfg) {
return ac101_init(&codec_cfg, getI2C(), getI2CAddress()) == RESULT_OK;
}
bool deinit() { return ac101_deinit() == RESULT_OK; }
bool controlState(codec_mode_t mode) {
return ac101_ctrl_state_active(mode, true) == RESULT_OK;
}
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return ac101_config_i2s(mode, &iface) == RESULT_OK;
}
};
/**
* @brief Driver API for the CS43l22 codec chip on 0x94 (0x4A<<1)
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverCS43l22Class : public AudioDriver {
public:
AudioDriverCS43l22Class(uint16_t deviceAddr = 0x4A) {
i2c_default_address = deviceAddr;
}
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
AD_LOGD("AudioDriverCS43l22Class::begin");
p_pins = &pins;
codec_cfg = codecCfg;
// manage reset pin -> acive high
setPAPower(true);
// Setup enable pin for codec
delay(100);
uint32_t freq = getFrequency(codec_cfg.i2s.rate);
uint16_t outputDevice = getOutput(codec_cfg.output_device);
AD_LOGD("cs43l22_Init");
bool result =
cs43l22_Init(deviceAddr, outputDevice, volume, freq, getI2C()) == 0;
if (!result) {
AD_LOGE("error: cs43l22_Init");
}
cs43l22_Play(deviceAddr, nullptr, 0);
return result;
}
virtual bool setConfig(CodecConfig codecCfg) {
codec_cfg = codecCfg;
uint32_t freq = getFrequency(codec_cfg.i2s.rate);
uint16_t outputDevice = getOutput(codec_cfg.output_device);
return cs43l22_Init(deviceAddr, outputDevice, this->volume, freq,
getI2C()) == 0;
}
bool setMute(bool mute) {
uint32_t rc = mute ? cs43l22_Pause(deviceAddr) : cs43l22_Resume(deviceAddr);
return rc == 0;
}
bool setVolume(int volume) {
this->volume = volume;
return cs43l22_SetVolume(deviceAddr, volume) == 0;
}
int getVolume() { return volume; }
protected:
uint16_t deviceAddr;
int volume = 100;
bool deinit() {
int cnt = cs43l22_Stop(deviceAddr, AUDIO_MUTE_ON);
cnt += cs43l22_Reset(deviceAddr);
setPAPower(false);
return cnt == 0;
}
uint32_t getFrequency(samplerate_t rateNum) {
switch (rateNum) {
case RATE_8K:
return 8000; /*!< set to 8k samples per second */
case RATE_11K:
return 11024; /*!< set to 11.025k samples per second */
case RATE_16K:
return 16000; /*!< set to 16k samples in per second */
case RATE_22K:
return 22050; /*!< set to 22.050k samples per second */
case RATE_24K:
return 24000; /*!< set to 24k samples in per second */
case RATE_32K:
return 32000; /*!< set to 32k samples in per second */
case RATE_44K:
return 44100; /*!< set to 44.1k samples per second */
case RATE_48K:
return 48000; /*!< set to 48k samples per second */
default:
break;
}
return 44100;
}
uint16_t getOutput(output_device_t output_device) {
switch (output_device) {
case DAC_OUTPUT_NONE:
return 0;
case DAC_OUTPUT_LINE1:
return OUTPUT_DEVICE_SPEAKER;
case DAC_OUTPUT_LINE2:
return OUTPUT_DEVICE_HEADPHONE;
case DAC_OUTPUT_ALL:
return OUTPUT_DEVICE_BOTH;
default:
break;
}
return OUTPUT_DEVICE_BOTH;
}
};
/**
* @brief Driver API for CS42448 TDS DAC/ADC
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverCS42448Class : public AudioDriver {
public:
AudioDriverCS42448Class() { i2c_default_address = 0x48; }
bool begin(CodecConfig codecCfg, DriverPins &pins) override {
cfg = codecCfg;
// setup pins
pins.begin();
// setup cs42448
cs42448.begin(cfg, getI2C(), getI2CAddress());
cs42448.setMute(false);
return true;
}
virtual bool setConfig(CodecConfig codecCfg) {
bool result = true;
if (codecCfg.equalsExRate(cfg)) {
// just update the rate
if (cfg.i2s.rate != codecCfg.getRateNumeric()) {
cs42448.setMute(true);
cs42448.setSampleRate(codecCfg.getRateNumeric());
cs42448.setMute(false);
}
} else {
assert(p_pins != nullptr);
result = begin(codecCfg, *p_pins);
}
return result;
}
bool end(void) override { return cs42448.end(); }
bool setMute(bool enable) override { return cs42448.setMute(enable); }
bool setMute(bool enable, int line) {
return cs42448.setMuteDAC(line, enable);
}
/// Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.
bool setVolume(int volume) override {
this->volume = volume;
return cs42448.setVolumeDAC(2.55f * volume);
}
bool setVolume(int dac, int volume) {
return cs42448.setVolumeDAC(dac, 2.55f * volume);
}
int getVolume() override { return volume; }
bool setInputVolume(int volume) override {
int vol = mapVolume(volume, 0, 100, -128, 127);
return cs42448.setVolumeADC(vol);
}
bool isVolumeSupported() override { return true; }
bool isInputVolumeSupported() override { return true; }
DriverPins &pins() { return *p_pins; }
CS42448 &driver() { return cs42448; }
protected:
CS42448 cs42448;
int volume = 100;
CodecConfig cfg;
};
/**
* @brief Driver API for ES7210 codec chip. This chip supports only input!
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverES7210Class : public AudioDriver {
public:
AudioDriverES7210Class() { i2c_default_address = ES7210_AD1_AD0_00 >> 1; }
bool setMute(bool mute) { return es7210_set_mute(mute) == RESULT_OK; }
bool setVolume(int volume) {
this->volume = volume;
return es7210_adc_set_volume(limitValue(volume, 0, 100)) == RESULT_OK;
}
int getVolume() { return volume; }
bool setInputVolume(int volume) { return setVolume(volume); }
bool isInputVolumeSupported() { return true; }
protected:
int volume;
bool init(codec_config_t codec_cfg) {
return es7210_adc_init(&codec_cfg, getI2C()) == RESULT_OK;
}
bool deinit() { return es7210_adc_deinit() == RESULT_OK; }
bool controlState(codec_mode_t mode) {
return es7210_adc_ctrl_state_active(mode, true) == RESULT_OK;
}
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return es7210_adc_config_i2s(mode, &iface) == RESULT_OK;
}
};
/**
* @brief Driver API for Lyrat ES7243 codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverES7243Class : public AudioDriver {
public:
AudioDriverES7243Class() { i2c_default_address = 0x13; }
bool setMute(bool mute) {
return es7243_adc_set_voice_mute(mute) == RESULT_OK;
}
bool setVolume(int volume) {
return es7243_adc_set_voice_volume(limitValue(volume, 0, 100)) == RESULT_OK;
}
int getVolume() {
int vol;
es7243_adc_get_voice_volume(&vol);
return vol;
}
protected:
bool init(codec_config_t codec_cfg) {
return es7243_adc_init(&codec_cfg, getI2C()) == RESULT_OK;
}
bool deinit() { return es7243_adc_deinit() == RESULT_OK; }
bool controlState(codec_mode_t mode) {
return es7243_adc_ctrl_state_active(mode, true) == RESULT_OK;
}
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return es7243_adc_config_i2s(mode, &iface) == RESULT_OK;
}
};
/**
* @brief Driver API for ES7243e codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverES7243eClass : public AudioDriver {
public:
AudioDriverES7243eClass() { i2c_default_address = 0x13; }
bool setMute(bool mute) {
return mute ? setVolume(0) == RESULT_OK : setVolume(volume) == RESULT_OK;
}
bool setVolume(int volume) {
this->volume = volume;
return es7243e_adc_set_voice_volume(limitValue(volume, 0, 100)) ==
RESULT_OK;
}
int getVolume() {
int vol;
es7243e_adc_get_voice_volume(&vol);
return vol;
}
protected:
int volume = 0;
bool init(codec_config_t codec_cfg) {
return es7243e_adc_init(&codec_cfg, getI2C()) == RESULT_OK;
}
bool deinit() { return es7243e_adc_deinit() == RESULT_OK; }
bool controlState(codec_mode_t mode) {
return es7243e_adc_ctrl_state_active(mode, true) == RESULT_OK;
}
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return es7243e_adc_config_i2s(mode, &iface) == RESULT_OK;
}
};
/**
* @brief Driver API for ES8156 codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverES8156Class : public AudioDriver {
public:
AudioDriverES8156Class() { i2c_default_address = 0x8; }
bool setMute(bool mute) {
return es8156_codec_set_voice_mute(mute) == RESULT_OK;
}
bool setVolume(int volume) {
AD_LOGD("volume %d", volume);
return es8156_codec_set_voice_volume(limitValue(volume, 0, 100)) ==
RESULT_OK;
}
int getVolume() {
int vol;
es8156_codec_get_voice_volume(&vol);
return vol;
}
protected:
bool init(codec_config_t codec_cfg) {
return es8156_codec_init(&codec_cfg, getI2C()) == RESULT_OK;
}
bool deinit() { return es8156_codec_deinit() == RESULT_OK; }
bool controlState(codec_mode_t mode) {
return es8156_codec_ctrl_state_active(mode, true) == RESULT_OK;
}
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return es8156_codec_config_i2s(mode, &iface) == RESULT_OK;
}
};
/**
* @brief Driver API for Lyrat ES8311 codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverES8311Class : public AudioDriver {
public:
AudioDriverES8311Class() { i2c_default_address = 0x18; }
bool setMute(bool mute) { return es8311_set_voice_mute(mute) == RESULT_OK; }
bool setVolume(int volume) {
return es8311_codec_set_voice_volume(limitValue(volume, 0, 100)) ==
RESULT_OK;
}
int getVolume() {
int vol;
es8311_codec_get_voice_volume(&vol);
return vol;
}
protected:
bool init(codec_config_t codec_cfg) {
int mclk_src = pins().getPinID(PinFunction::MCLK_SOURCE);
if (mclk_src == -1) {
mclk_src = 0; // = FROM_MCLK_PIN;
}
AD_LOGI("MCLK_SOURCE: %d", mclk_src);
assert(getI2C() != nullptr);
return es8311_codec_init(&codec_cfg, getI2C(), mclk_src, getI2CAddress()) ==
RESULT_OK;
}
bool deinit() { return es8311_codec_deinit() == RESULT_OK; }
bool controlState(codec_mode_t mode) {
return es8311_codec_ctrl_state_active(mode, true) == RESULT_OK;
}
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return es8311_codec_config_i2s(mode, &iface) == RESULT_OK;
}
};
/**
* @brief Driver API for ES8374 codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverES8374Class : public AudioDriver {
public:
AudioDriverES8374Class() { i2c_default_address = 0x10; }
bool setMute(bool mute) { return es8374_set_voice_mute(mute) == RESULT_OK; }
bool setVolume(int volume) {
AD_LOGD("volume %d", volume);
return es8374_codec_set_voice_volume(limitValue(volume, 0, 100)) ==
RESULT_OK;
}
int getVolume() {
int vol;
es8374_codec_get_voice_volume(&vol);
return vol;
}
protected:
bool init(codec_config_t codec_cfg) {
auto codec_mode = this->codec_cfg.get_mode();
return es8374_codec_init(&codec_cfg, codec_mode, getI2C(),
getI2CAddress()) == RESULT_OK;
}
bool deinit() { return es8374_codec_deinit() == RESULT_OK; }
bool controlState(codec_mode_t mode) {
return es8374_codec_ctrl_state_active(mode, true) == RESULT_OK;
}
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return es8374_codec_config_i2s(mode, &iface) == RESULT_OK;
}
};
/**
* @brief Driver API for ES8388 codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverES8388Class : public AudioDriver {
public:
AudioDriverES8388Class(int volumeHack) {
i2c_default_address = 0x10;
volume_hack = volumeHack;
}
AudioDriverES8388Class() { i2c_default_address = 0x10; }
bool setMute(bool mute) {
line_active[0] = !mute;
line_active[1] = !mute;
return es8388_set_voice_mute(mute) == RESULT_OK;
}
// mute individual line: lines start at 0 (valid range 0:1)
bool setMute(bool mute, int line) {
if (line > 1) {
AD_LOGD("invalid line %d", line);
return false;
}
// mute is managed on line level, so deactivate global mute
line_active[line] = !mute;
if (line_active[0] && line_active[1]) {
return es8388_config_output_device(DAC_OUTPUT_ALL) == RESULT_OK;
} else if (!line_active[0] && !line_active[1]) {
return es8388_config_output_device(DAC_OUTPUT_NONE) == RESULT_OK;
} else if (line_active[0]) {
return es8388_config_output_device(DAC_OUTPUT_LINE1) == RESULT_OK;
} else if (line_active[1]) {
return es8388_config_output_device(DAC_OUTPUT_LINE2) == RESULT_OK;
}
return false;
}
bool setVolume(int volume) {
AD_LOGD("volume %d", volume);
return es8388_set_voice_volume(limitValue(volume, 0, 100)) == RESULT_OK;
}
int getVolume() {
int vol;
es8388_get_voice_volume(&vol);
return vol;
}
bool setInputVolume(int volume) {
// mapVolume values from 0 - 100 to 0 to 8
// es_mic_gain_t: MIC_GAIN_MIN = -1, 0,3,6,9,12,15,18,21,24 MIC_GAIN_MAX =
// 25
// Vol: 0, 12.5, 25, 37.5, 50, 62.5, 75, 87.5, 100
// idx: 0, 1, 2, 3, 4, 5, 6, 7, 8
// dB/gain: 0, 3, 6, 9, 12, 15, 18, 21, 24
// factor: 1, 2, 4, 8, 16, 32, 63, 126, 252
// es8388 Register 9 – ADC Control 1
// dB MicL MicR
// 0 0000 0000
// 3 0001 0001
// 6 0010 0010
// 9 0011 0011
// 12 0100 0100
// 15 0101 0101
// 18 0110 0110
// 21 0111 0111
// 24 1000 1000
es_mic_gain_t gains[] = {MIC_GAIN_0DB, MIC_GAIN_3DB, MIC_GAIN_6DB,
MIC_GAIN_9DB, MIC_GAIN_12DB, MIC_GAIN_15DB,
MIC_GAIN_18DB, MIC_GAIN_21DB, MIC_GAIN_24DB};
int vol = limitValue(volume, 0, 100);
int idx = mapVolume(vol, 0, 100, 0, 8);
es_mic_gain_t gain = gains[idx];
AD_LOGD("input volume: %d -> gain %d [dB] (idx: %d of 0..8)", volume, gain,
idx);
return setMicrophoneGain(gain);
}
bool setMicrophoneGain(es_mic_gain_t gain) {
return es8388_set_mic_gain(gain) == RESULT_OK;
}
bool isInputVolumeSupported() { return true; }
void setVolumeHack(int volume_hack) { this->volume_hack = volume_hack; }
int getVolumeHack() { return volume_hack; }
protected:
bool line_active[2] = {true};
int volume_hack = AI_THINKER_ES8388_VOLUME_HACK;
bool init(codec_config_t codec_cfg) {
return es8388_init(&codec_cfg, getI2C(), getI2CAddress(), volume_hack) ==
RESULT_OK;
}
bool deinit() { return es8388_deinit() == RESULT_OK; }
bool controlState(codec_mode_t mode) {
return es8388_ctrl_state_active(mode, true) == RESULT_OK;
}
bool configInterface(codec_mode_t mode, I2SDefinition iface) {
return es8388_config_i2s(mode, &iface) == RESULT_OK;
}
};
/**
* @brief Driver API for TAS5805M codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverTAS5805MClass : public AudioDriver {
public:
AudioDriverTAS5805MClass() { i2c_default_address = 0x2E; }
bool setMute(bool mute) { return tas5805m_set_mute(mute) == RESULT_OK; }
bool setVolume(int volume) {
AD_LOGD("volume %d", volume);
return tas5805m_set_volume(limitValue(volume, 0, 100)) == RESULT_OK;
}
int getVolume() {
int vol;
tas5805m_get_volume(&vol);
return vol;
}
protected:
bool init(codec_config_t codec_cfg) {
return tas5805m_init(&codec_cfg, getI2C()) == RESULT_OK;
}
bool deinit() { return tas5805m_deinit() == RESULT_OK; }
};
/**
* @brief Driver API for WM8990 codec chip
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AudioDriverWM8960Class : public AudioDriver {
public:
AudioDriverWM8960Class() { i2c_default_address = 0x1A; }
bool begin(CodecConfig codecCfg, DriverPins &pins) {
codec_cfg = codecCfg;
// define wire object
mtb_wm8960_set_wire(getI2C());
mtb_wm8960_set_write_retry_count(i2c_retry_count);
// setup wm8960
int features = getFeatures(codecCfg);
if (!mtb_wm8960_init(features)) {
AD_LOGE("mtb_wm8960_init");
return false;
}
setVolume(DRIVER_DEFAULT_VOLUME);
if (!mtb_wm8960_activate()) {
AD_LOGE("mtb_wm8960_activate");
return false;
}
if (!configure_clocking()) {
AD_LOGE("configure_clocking");
return false;
}
return true;
}
bool end(void) {
mtb_wm8960_deactivate();
mtb_wm8960_free();
return true;
}
virtual bool setConfig(CodecConfig codecCfg) {
codec_cfg = codecCfg;
return configure_clocking();
}
bool setMute(bool enable) { return setVolume(enable ? 0 : volume_out); }
/// Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.
bool setVolume(int volume) {
volume_out = limitValue(volume, 0, 100);
int vol_int =
volume_out == 0.0 ? 0 : mapVolume(volume_out, 0, 100, 30, 0x7F);
return mtb_wm8960_set_output_volume(vol_int);
}
int getVolume() { return volume_out; }
bool setInputVolume(int volume) {
volume_in = limitValue(volume, 0, 100);
int vol_int = mapVolume(volume_in, 0, 100, 0, 30);
return mtb_wm8960_adjust_input_volume(vol_int);
}
bool isVolumeSupported() { return true; }
bool isInputVolumeSupported() { return true; }
/// Configuration: define retry count (default : 0)
void setI2CRetryCount(int cnt) { i2c_retry_count = cnt; }
/// Configuration: enable/disable PLL (active by default)
void setEnablePLL(bool active) { vs1053_enable_pll = active; }