-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add radio transmission & reception examples using both PWM and I2S output
- Loading branch information
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
examples/Platforms/NRF52/NRF52_Radio/NRF52_PWM_I2S_RadioRX/NRF52_PWM_I2S_RadioRX.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* Example of receiving audio over radio and playing it back. | ||
* Note: This example uses I2S output. Modify the USE_I2S #define | ||
* below to enable PWM output. | ||
*/ | ||
|
||
#include <AutoAnalogAudio.h> | ||
#include <nrf_to_nrf.h> | ||
|
||
AutoAnalog aaAudio; | ||
nrf_to_nrf radio; | ||
|
||
uint8_t address[][6] = { "1Node", "2Node" }; | ||
#define BUFFER_SIZE 125 | ||
#define USE_I2S 0 // Change to 1 to enable I2S output instead of PWM | ||
|
||
void setup() { | ||
|
||
Serial.begin(115200); | ||
|
||
radio.begin(); | ||
radio.setPayloadSize(BUFFER_SIZE); | ||
radio.setAutoAck(0); | ||
radio.setDataRate(NRF_2MBPS); | ||
radio.openReadingPipe(1,address[1]); | ||
radio.startListening(); | ||
|
||
Serial.println("Analog Audio Begin"); | ||
aaAudio.begin(0, 1, USE_I2S); //Setup aaAudio using DAC and PWM. Change the third value to a 1 to enable I2S | ||
|
||
//Setup for audio: Use 8-bit, mono WAV files @ 16kHz | ||
aaAudio.dacBitsPerSample = 8; // 8-bit | ||
aaAudio.setSampleRate(16000, 0); // 16khz, mono | ||
|
||
pinMode(6,OUTPUT); // For I2S output: Connected to SD pin of MAX98357A | ||
digitalWrite(6,HIGH); | ||
|
||
} | ||
|
||
void loop() { | ||
|
||
if(radio.available()){ | ||
radio.read(&aaAudio.dacBuffer[0],BUFFER_SIZE); | ||
aaAudio.feedDAC(0,BUFFER_SIZE); | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
examples/Platforms/NRF52/NRF52_Radio/NRF52_SD_I2S_RadioTX/NRF52_SD_I2S_RadioTX.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* Example of reading audio from SD card and sending over radio | ||
* Note: This example uses I2S output. Modify the USE_I2S #define | ||
* below to enable PWM output. | ||
*/ | ||
|
||
#include <SPI.h> | ||
#include <SD.h> | ||
#include <AutoAnalogAudio.h> | ||
#include <nrf_to_nrf.h> | ||
|
||
AutoAnalog aaAudio; | ||
nrf_to_nrf radio; | ||
|
||
uint8_t address[][6] = { "1Node", "2Node" }; | ||
#define BUFFER_SIZE 125 | ||
#define USE_I2S 1 // Change to 0 to enable PWM output instead of I2S | ||
|
||
/*********************************************************/ | ||
/* Tested with MAX98357A I2S breakout on XIAO 52840 Sense | ||
/* BCLK connected to Arduino D1 (p0.03) | ||
/* LRCK connected to Arduino D3 (p0.29) | ||
/* DIN connected to Arduino D5 (p0.05) | ||
/* SD connected to Arduino D6 (p1.11) | ||
/*********************************************************/ | ||
|
||
void setup() { | ||
|
||
Serial.begin(115200); | ||
|
||
radio.begin(); | ||
radio.setPayloadSize(BUFFER_SIZE); | ||
radio.setAutoAck(0); | ||
radio.setDataRate(NRF_2MBPS); | ||
radio.openWritingPipe(address[1]); | ||
radio.stopListening(); | ||
|
||
Serial.print("Init SD card..."); | ||
if (!SD.begin(10000000, 2)) { | ||
Serial.println("init failed!"); | ||
return; | ||
} | ||
Serial.println("init ok"); | ||
Serial.println("Analog Audio Begin"); | ||
aaAudio.begin(0, 1, USE_I2S); //Setup aaAudio using DAC and I2S | ||
|
||
//Setup for audio: Use 8-bit, mono WAV files @ 16kHz | ||
aaAudio.dacBitsPerSample = 8; // 8-bit | ||
aaAudio.setSampleRate(16000, 0); // 16khz, mono | ||
|
||
pinMode(6, OUTPUT); //Connected to SD pin of MAX98357A | ||
digitalWrite(6, HIGH); | ||
|
||
playAudio("guitar/g8b16km.wav"); // 16kHz @ 8-bits is about the maximum when reading from SD card & streaming over radio | ||
} | ||
|
||
void loop() { | ||
|
||
loadBuffer(); | ||
} | ||
|
||
/*********************************************************/ | ||
/* A simple function to handle playing audio files | ||
/*********************************************************/ | ||
|
||
File myFile; | ||
|
||
void playAudio(const char *audioFile) { | ||
|
||
if (myFile) { | ||
myFile.close(); | ||
} | ||
//Open the designated file | ||
myFile = SD.open(audioFile); | ||
//Skip past the WAV header | ||
myFile.seek(44); | ||
} | ||
|
||
void loadBuffer() { | ||
|
||
if (myFile.available()) { | ||
myFile.read(aaAudio.dacBuffer, BUFFER_SIZE); // Change this to dacBuffer16 for 16-bit samples | ||
aaAudio.feedDAC(0, BUFFER_SIZE); | ||
radio.startWrite(&aaAudio.dacBuffer[0], BUFFER_SIZE, 1); | ||
} else { | ||
myFile.seek(44); | ||
} | ||
} |