Skip to content

Commit

Permalink
Added a 'bool WM8978::begin()' function which doesn't setup i2c.
Browse files Browse the repository at this point in the history
  • Loading branch information
CelliesProjects committed Oct 30, 2020
1 parent 6cdcf41 commit 959e379
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Arduino IDE library for wm8978 codec on ESP32 mcu.


### Example code:

#### Setup i2c and wm8978 in one call

```c++
#include <WM8978.h> /* https://github.com/CelliesProjects/wm8978-esp32 */
#include <Audio.h> /* https://github.com/schreibfaul1/ESP32-audioI2S */
Expand Down Expand Up @@ -35,13 +38,63 @@ void setup() {
while (1) delay(100);
}

/* Select I2S MCLK pin */
WiFi.begin("xxx", "xxx");
while (!WiFi.isConnected()) {
delay(10);
}

/* Start MCLK */
audio.i2s_mclk_pin_select(I2S_MCLKPIN);

ESP_LOGI(TAG, "Connected. Starting MP3...");
audio.connecttohost("http://icecast.omroep.nl/3fm-bb-mp3");

dac.setSPKvol(40); /* max 63 */
dac.setHPvol(32, 32);
}

void loop() {
audio.loop();
}

```
#### Setup i2c and wm8978 separately
```c++
#include <Wire.h>
#include <WM8978.h> /* https://github.com/CelliesProjects/wm8978-esp32 */
#include <Audio.h> /* https://github.com/schreibfaul1/ESP32-audioI2S */
/* M5Stack Node I2S pins */
#define I2S_BCK 5
#define I2S_WS 13
#define I2S_DOUT 2
#define I2S_DIN 34
/* M5Stack WM8978 MCLK gpio number */
#define I2S_MCLKPIN 0
WM8978 dac;
Audio audio(I2S_BCK, I2S_WS, I2S_DOUT);
void setup() {
if (!Wire.begin(21, 22, 400000))
ESP_LOGE(TAG, "i2c setup error!");
if (!dac.begin())
ESP_LOGE(TAG, "WM8978 setup error!");
WiFi.begin("xxx", "xxx");
while (!WiFi.isConnected()) {
delay(10);
}
/* Start MCLK */
audio.i2s_mclk_pin_select(0);
ESP_LOGI(TAG, "Connected. Starting MP3...");
audio.connecttohost("http://icecast.omroep.nl/3fm-bb-mp3");
Expand All @@ -52,6 +105,6 @@ void setup() {
void loop() {
audio.loop();
}

```

To show `ESP_LOGx` messages on the Serial port, compile with `Tools->Core Debug Level` set to `Info`.
28 changes: 17 additions & 11 deletions src/WM8978.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,21 +300,17 @@ void WM8978::setNoise(uint8_t enable, uint8_t gain)
Write_Reg(35, regval); //R18,EQ1设置
}

bool WM8978::begin(const uint8_t sda, const uint8_t scl, const uint32_t frequency) {
ESP_LOGD(TAG, "WM8978 I2C init sda=%i scl=%i frequency=%i", sda, scl, frequency);
if (!Wire.begin(sda, scl, frequency)) {
ESP_LOGD(TAG, "Wire setup error");
return false;
}

bool WM8978::begin() {
Wire.beginTransmission(WM8978_ADDR);
uint8_t error = Wire.endTransmission();
const uint8_t error = Wire.endTransmission();
if (error) {
ESP_LOGD(TAG, "No WM8978 @ I2C address: 0x%X", WM8978_ADDR);
ESP_LOGE(TAG, "No WM8978 dac @ i2c address: 0x%X", WM8978_ADDR);
return false;
}
int err = Init();
const int err = Init();
if (err) {
ESP_LOGD(TAG, "WM8978 init err: 0x%X", err);
ESP_LOGE(TAG, "WM8978 init err: 0x%X", err);
return false;
}
cfgI2S(2, 0); //Philips 16bit
Expand All @@ -333,4 +329,14 @@ bool WM8978::begin(const uint8_t sda, const uint8_t scl, const uint32_t frequenc
setEQ5(0, 24);
cfgOutput(1, 0); //Output enabled, bypass disabled
return true;
} //begin
}

bool WM8978::begin(const uint8_t sda, const uint8_t scl, const uint32_t frequency) {
ESP_LOGD(TAG, "i2c init sda=%i scl=%i frequency=%i", sda, scl, frequency);
if (!Wire.begin(sda, scl, frequency)) {
ESP_LOGE(TAG, "Wire setup error sda=%i scl=%i frequency=%i", sda, scl, frequency);
return false;
}
return begin();
}

1 change: 1 addition & 0 deletions src/WM8978.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WM8978
public:
WM8978() {}
~WM8978() {}
bool begin(); /* use this function if you want to setup i2c before */
bool begin(const uint8_t sda, const uint8_t scl, const uint32_t frequency = 100000);
void cfgADDA(uint8_t dacen, uint8_t adcen);
void cfgInput(uint8_t micen, uint8_t lineinen, uint8_t auxen);
Expand Down

0 comments on commit 959e379

Please sign in to comment.