mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-02-06 16:47:59 +08:00
arduino : update the examples for Tx and Rx
This commit is contained in:
@@ -89,6 +89,7 @@ else()
|
||||
|
||||
add_subdirectory(arduino-rx)
|
||||
add_subdirectory(arduino-tx)
|
||||
add_subdirectory(arduino-tx2)
|
||||
endif()
|
||||
|
||||
if (GGWAVE_SUPPORT_SDL2)
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
#include <PDM.h>
|
||||
|
||||
const int kPinLed0 = 13;
|
||||
const int kPinSpeaker = 10;
|
||||
const int kPinButton0 = 2;
|
||||
const int kPinButton1 = 4;
|
||||
|
||||
using TSample = int16_t;
|
||||
static const size_t kSampleSize_bytes = sizeof(TSample);
|
||||
|
||||
@@ -21,10 +26,47 @@ volatile int qsize = 0;
|
||||
// Buffer to read samples into, each sample is 16-bits
|
||||
TSample sampleBuffer[qmax];
|
||||
|
||||
// helper function to output the generated GGWave waveform via a buzzer
|
||||
void send_text(GGWave & ggwave, uint8_t pin, const char * text, GGWave::TxProtocolId protocolId) {
|
||||
ggwave.init(text, protocolId);
|
||||
ggwave.encode();
|
||||
|
||||
const auto & tones = ggwave.txTones();
|
||||
float freq_hz = -1.0f;
|
||||
float duration_ms = -1.0f;
|
||||
for (int i = 0; i < (int) tones.size(); ++i) {
|
||||
if (tones[i].size() == 0) continue;
|
||||
const auto & curTone = tones[i].front();
|
||||
|
||||
if (curTone.freq_hz != freq_hz) {
|
||||
if (duration_ms > 0) {
|
||||
tone(pin, freq_hz);
|
||||
delay(duration_ms);
|
||||
}
|
||||
freq_hz = curTone.freq_hz;
|
||||
duration_ms = 0.0f;
|
||||
}
|
||||
duration_ms += curTone.duration_ms;
|
||||
}
|
||||
|
||||
if (duration_ms > 0) {
|
||||
tone(pin, freq_hz);
|
||||
delay(duration_ms);
|
||||
}
|
||||
|
||||
noTone(pin);
|
||||
digitalWrite(pin, LOW);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(57600);
|
||||
while (!Serial);
|
||||
|
||||
pinMode(kPinLed0, OUTPUT);
|
||||
pinMode(kPinSpeaker, OUTPUT);
|
||||
pinMode(kPinButton0, INPUT);
|
||||
pinMode(kPinButton1, INPUT);
|
||||
|
||||
// Configure the data receive callback
|
||||
PDM.onReceive(onPDMdata);
|
||||
|
||||
@@ -48,6 +90,7 @@ void loop() {
|
||||
Serial.println("trying to create ggwave instance");
|
||||
|
||||
auto p = GGWave::getDefaultParameters();
|
||||
|
||||
p.payloadLength = 16;
|
||||
p.sampleRateInp = frequency;
|
||||
p.sampleRateOut = frequency;
|
||||
@@ -73,15 +116,6 @@ void loop() {
|
||||
while (qsize >= p.samplesPerFrame) {
|
||||
auto tStart = millis();
|
||||
|
||||
//Serial.print(qhead);
|
||||
//Serial.print(" ");
|
||||
//Serial.print(qtail);
|
||||
//Serial.print(" ");
|
||||
//Serial.print(qsize);
|
||||
//Serial.print(" ");
|
||||
//Serial.print(ggwave.getSamplesNeeded());
|
||||
//Serial.println("");
|
||||
|
||||
ggwave.decode(sampleBuffer + qhead, p.samplesPerFrame*kSampleSize_bytes);
|
||||
qsize -= p.samplesPerFrame;
|
||||
qhead += p.samplesPerFrame;
|
||||
@@ -91,35 +125,40 @@ void loop() {
|
||||
|
||||
auto tEnd = millis();
|
||||
if (++niter % 10 == 0) {
|
||||
// print the time it took the last decode() call to complete
|
||||
// should be smaller than p.samplesPerFrame/frequency seconds
|
||||
// for example: p.samplesPerFrame = 128, frequency = 6000 => not more than 20 ms
|
||||
Serial.println(tEnd - tStart);
|
||||
if (tEnd - tStart > 1000*(float(p.samplesPerFrame)/frequency)) {
|
||||
Serial.println("Warning: decode() took too long to execute!");
|
||||
}
|
||||
}
|
||||
|
||||
nr = ggwave.rxTakeData(result);
|
||||
if (nr > 0) {
|
||||
Serial.println(tEnd - tStart);
|
||||
Serial.println(nr);
|
||||
Serial.println((char *)result.data());
|
||||
Serial.print("Received data with length ");
|
||||
Serial.print(nr); // should be equal to p.payloadLength
|
||||
Serial.println(" bytes:");
|
||||
|
||||
Serial.println((char *) result.data());
|
||||
|
||||
if (strcmp((char *)result.data(), "test") == 0) {
|
||||
ggwave.init("hello", GGWave::TxProtocolId(GGWAVE_PROTOCOL_MT_FASTEST));
|
||||
ggwave.encode();
|
||||
// pause microphone capture while transmitting
|
||||
PDM.end();
|
||||
delay(500);
|
||||
|
||||
const auto & tones = ggwave.txTones();
|
||||
for (int i = 0; i < (int) tones.size(); ++i) {
|
||||
Serial.print(" - frame ");
|
||||
Serial.print(i);
|
||||
Serial.print(", ");
|
||||
Serial.print(tones[i].size());
|
||||
Serial.print(": ");
|
||||
for (int j = 0; j < (int) tones[i].size(); ++j) {
|
||||
Serial.print((int)(tones[i][j].freq_hz));
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println();
|
||||
send_text(ggwave, kPinSpeaker, "hello", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
|
||||
// resume microphone capture
|
||||
if (!PDM.begin(channels, frequency)) {
|
||||
Serial.println("Failed to start PDM!");
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (err > 0) {
|
||||
Serial.println("ERRROR");
|
||||
Serial.println(err);
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#
|
||||
# arduino-tx
|
||||
|
||||
configure_file(${CMAKE_SOURCE_DIR}/include/ggwave/ggwave.h ${CMAKE_CURRENT_SOURCE_DIR}/ggwave/ggwave.h COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/ggwave.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ggwave.cpp COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/fft.h ${CMAKE_CURRENT_SOURCE_DIR}/fft.h COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/reed-solomon/gf.hpp ${CMAKE_CURRENT_SOURCE_DIR}/reed-solomon/gf.hpp COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/reed-solomon/rs.hpp ${CMAKE_CURRENT_SOURCE_DIR}/reed-solomon/rs.hpp COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/reed-solomon/poly.hpp ${CMAKE_CURRENT_SOURCE_DIR}/reed-solomon/poly.hpp COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/reed-solomon/LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/reed-solomon/LICENSE COPYONLY)
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
#include "Arduino_AVRSTL.h"
|
||||
// To build this example for Arduino UNO, make sure to install the ArduinoSTL library:
|
||||
#include <ArduinoSTL.h>
|
||||
|
||||
#include "ggwave/ggwave.h"
|
||||
// This example uses a custom ggwave imlpementation specifically for Arduino UNO.
|
||||
// Since the Arduino UNO has only 2KB of RAM, the ggwave library is not able to
|
||||
// to fit into the Arduino's memory (eventhough it is very close).
|
||||
// If your microcontroller has more than 2KB of RAM, you should check the other Tx
|
||||
// examples to see if you can use the original ggwave library.
|
||||
#include "ggwave.h"
|
||||
|
||||
const int kPinLed0 = 13;
|
||||
const int kPinSpeaker = 10;
|
||||
@@ -9,118 +15,76 @@ const int kPinButton1 = 4;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(57600);
|
||||
display_freeram();
|
||||
|
||||
//pinMode(kPinLed0, OUTPUT);
|
||||
//pinMode(kPinSpeaker, OUTPUT);
|
||||
//pinMode(kPinButton0, INPUT);
|
||||
//pinMode(kPinButton1, INPUT);
|
||||
pinMode(kPinLed0, OUTPUT);
|
||||
pinMode(kPinSpeaker, OUTPUT);
|
||||
pinMode(kPinButton0, INPUT);
|
||||
pinMode(kPinButton1, INPUT);
|
||||
|
||||
//delay(3000);
|
||||
delay(3000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "Hello!", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
GGWave::send_text(kPinSpeaker, "Hello!", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
//delay(2000);
|
||||
delay(2000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "This is a", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//GGWave::send_text(kPinSpeaker, "ggwave demo", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
GGWave::send_text(kPinSpeaker, "This is a", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
GGWave::send_text(kPinSpeaker, "ggwave demo", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
//delay(2000);
|
||||
delay(2000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "The arduino", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "transmits data", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "using sound", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "through a buzzer", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
GGWave::send_text(kPinSpeaker, "The arduino", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
GGWave::send_text(kPinSpeaker, "transmits data", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
GGWave::send_text(kPinSpeaker, "using sound", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
GGWave::send_text(kPinSpeaker, "through a buzzer", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
//delay(1000);
|
||||
delay(1000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "The sound is", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "decoded in a", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "web page.", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
GGWave::send_text(kPinSpeaker, "The sound is", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
GGWave::send_text(kPinSpeaker, "decoded in a", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
GGWave::send_text(kPinSpeaker, "web page.", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
//delay(1000);
|
||||
delay(1000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "Press the button!", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
GGWave::send_text(kPinSpeaker, "Press the button!", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
}
|
||||
|
||||
char txt[16];
|
||||
int pressed = 0;
|
||||
bool isDown = false;
|
||||
|
||||
void display_freeram() {
|
||||
Serial.print(F("- SRAM left: "));
|
||||
Serial.println(freeRam());
|
||||
}
|
||||
|
||||
int freeRam() {
|
||||
extern int __heap_start,*__brkval;
|
||||
int v;
|
||||
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("hello");
|
||||
int but0 = digitalRead(kPinButton0);
|
||||
int but1 = digitalRead(kPinButton1);
|
||||
|
||||
auto p = GGWave::getDefaultParameters();
|
||||
p.payloadLength = 4;
|
||||
p.sampleRateInp = 6000;
|
||||
p.sampleRateOut = 6000;
|
||||
p.sampleRate = 6000;
|
||||
p.samplesPerFrame = 128;
|
||||
p.sampleFormatInp = GGWAVE_SAMPLE_FORMAT_I16;
|
||||
p.sampleFormatOut = GGWAVE_SAMPLE_FORMAT_U8;
|
||||
p.operatingMode = (ggwave_OperatingMode) (GGWAVE_OPERATING_MODE_TX | GGWAVE_OPERATING_MODE_TX_ONLY_TONES);
|
||||
|
||||
delay(1000);
|
||||
GGWave::Protocols::kDefault() = {};
|
||||
Serial.println("aaaaaaaaaaaa");
|
||||
GGWave::Protocols::tx() = { { "[MT] Fastest", 24, 3, 1, 2, true, } };
|
||||
|
||||
delay(1000);
|
||||
display_freeram();
|
||||
Serial.println("xxxxxxxxxxx");
|
||||
delay(1000);
|
||||
Serial.println("yyyyyyyyyyyy");
|
||||
GGWave ggwave(p);
|
||||
display_freeram();
|
||||
|
||||
while (true) {
|
||||
delay(1000);
|
||||
Serial.println("working");
|
||||
if (but1 == LOW && isDown == false) {
|
||||
delay(200);
|
||||
++pressed;
|
||||
isDown = true;
|
||||
} else if (but1 == HIGH) {
|
||||
isDown = false;
|
||||
}
|
||||
|
||||
//int but0 = digitalRead(kPinButton0);
|
||||
//int but1 = digitalRead(kPinButton1);
|
||||
if (but0 == LOW) {
|
||||
snprintf(txt, 16, "Pressed: %d", pressed);
|
||||
|
||||
//if (but1 == LOW && isDown == false) {
|
||||
// delay(200);
|
||||
// ++pressed;
|
||||
// isDown = true;
|
||||
//} else if (but1 == HIGH) {
|
||||
// isDown = false;
|
||||
//}
|
||||
|
||||
//if (but0 == LOW) {
|
||||
// snprintf(txt, 16, "Pressed: %d", pressed);
|
||||
|
||||
// digitalWrite(kPinLed0, HIGH);
|
||||
// GGWave::send_text(kPinSpeaker, txt, GGWave::TX_ARDUINO_512_FASTEST);
|
||||
// digitalWrite(kPinLed0, LOW);
|
||||
// pressed = 0;
|
||||
//}
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
GGWave::send_text(kPinSpeaker, txt, GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
pressed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
#include <ArduinoSTL.h>
|
||||
|
||||
#include "ggwave/ggwave.h"
|
||||
|
||||
const int kPinLed0 = 13;
|
||||
const int kPinSpeaker = 10;
|
||||
const int kPinButton0 = 2;
|
||||
const int kPinButton1 = 4;
|
||||
|
||||
void setup() {
|
||||
//Serial.begin(57600);
|
||||
|
||||
//pinMode(kPinLed0, OUTPUT);
|
||||
//pinMode(kPinSpeaker, OUTPUT);
|
||||
//pinMode(kPinButton0, INPUT);
|
||||
//pinMode(kPinButton1, INPUT);
|
||||
|
||||
//delay(3000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "Hello!", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
|
||||
//delay(2000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "This is a", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//GGWave::send_text(kPinSpeaker, "ggwave demo", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
|
||||
//delay(2000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "The arduino", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "transmits data", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "using sound", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "through a buzzer", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
|
||||
//delay(1000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "The sound is", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "decoded in a", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//delay(200);
|
||||
//GGWave::send_text(kPinSpeaker, "web page.", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
|
||||
//delay(1000);
|
||||
|
||||
//digitalWrite(kPinLed0, HIGH);
|
||||
//GGWave::send_text(kPinSpeaker, "Press the button!", GGWave::TX_ARDUINO_512_FASTEST);
|
||||
//digitalWrite(kPinLed0, LOW);
|
||||
}
|
||||
|
||||
char txt[16];
|
||||
int pressed = 0;
|
||||
bool isDown = false;
|
||||
|
||||
void loop() {
|
||||
Serial.println("hello");
|
||||
|
||||
//int but0 = digitalRead(kPinButton0);
|
||||
//int but1 = digitalRead(kPinButton1);
|
||||
|
||||
//if (but1 == LOW && isDown == false) {
|
||||
// delay(200);
|
||||
// ++pressed;
|
||||
// isDown = true;
|
||||
//} else if (but1 == HIGH) {
|
||||
// isDown = false;
|
||||
//}
|
||||
|
||||
//if (but0 == LOW) {
|
||||
// snprintf(txt, 16, "Pressed: %d", pressed);
|
||||
|
||||
// digitalWrite(kPinLed0, HIGH);
|
||||
// GGWave::send_text(kPinSpeaker, txt, GGWave::TX_ARDUINO_512_FASTEST);
|
||||
// digitalWrite(kPinLed0, LOW);
|
||||
// pressed = 0;
|
||||
//}
|
||||
}
|
||||
@@ -867,12 +867,9 @@ const uint8_t kDataLength_bytes = 16;
|
||||
const uint8_t kECCLength_bytes = getECCBytesForLength(kDataLength_bytes);
|
||||
|
||||
typedef enum {
|
||||
TX_ARDUINO_512_NORMAL,
|
||||
TX_ARDUINO_512_FAST,
|
||||
TX_ARDUINO_512_FASTEST,
|
||||
TX_ARDUINO_1024_NORMAL,
|
||||
TX_ARDUINO_1024_FAST,
|
||||
TX_ARDUINO_1024_FASTEST,
|
||||
GGWAVE_PROTOCOL_MT_NORMAL,
|
||||
GGWAVE_PROTOCOL_MT_FAST,
|
||||
GGWAVE_PROTOCOL_MT_FASTEST,
|
||||
} TxProtocolId;
|
||||
|
||||
struct Parameters {
|
||||
@@ -913,22 +910,19 @@ void send(uint8_t pin, const uint8_t * data, const Parameters & parameters) {
|
||||
digitalWrite(pin, LOW);
|
||||
}
|
||||
|
||||
void send(uint8_t pin, const uint8_t * data, TxProtocolId protocolId = TX_ARDUINO_512_FASTEST) {
|
||||
Parameters parameters = { 512, 16, 32 };
|
||||
void send(uint8_t pin, const uint8_t * data, TxProtocolId protocolId = GGWAVE_PROTOCOL_MT_FASTEST) {
|
||||
Parameters parameters = { 1024, 24, 64 };
|
||||
|
||||
switch (protocolId) {
|
||||
case TX_ARDUINO_512_NORMAL: parameters = { 512, 16, 96 }; break;
|
||||
case TX_ARDUINO_512_FAST: parameters = { 512, 16, 64 }; break;
|
||||
case TX_ARDUINO_512_FASTEST: parameters = { 512, 16, 32 }; break;
|
||||
case TX_ARDUINO_1024_NORMAL: parameters = { 1024, 16, 192 }; break;
|
||||
case TX_ARDUINO_1024_FAST: parameters = { 1024, 16, 128 }; break;
|
||||
case TX_ARDUINO_1024_FASTEST: parameters = { 1024, 16, 64 }; break;
|
||||
case GGWAVE_PROTOCOL_MT_NORMAL: parameters = { 1024, 24, 192 }; break;
|
||||
case GGWAVE_PROTOCOL_MT_FAST: parameters = { 1024, 24, 128 }; break;
|
||||
case GGWAVE_PROTOCOL_MT_FASTEST: parameters = { 1024, 24, 64 }; break;
|
||||
};
|
||||
|
||||
send(pin, data, parameters);
|
||||
}
|
||||
|
||||
void send_text(uint8_t pin, const char * text, TxProtocolId protocolId = TX_ARDUINO_512_FASTEST) {
|
||||
void send_text(uint8_t pin, const char * text, TxProtocolId protocolId = GGWAVE_PROTOCOL_MT_FASTEST) {
|
||||
char tx[kDataLength_bytes];
|
||||
memset(tx, 0, sizeof(tx));
|
||||
strncpy(tx, text, sizeof(tx));
|
||||
|
||||
6
examples/arduino-tx2/.gitignore
vendored
Normal file
6
examples/arduino-tx2/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
ggwave
|
||||
ggwave.cpp
|
||||
fft.h
|
||||
resampler.h
|
||||
resampler.cpp
|
||||
reed-solomon
|
||||
10
examples/arduino-tx2/CMakeLists.txt
Normal file
10
examples/arduino-tx2/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
#
|
||||
# arduino-tx2
|
||||
|
||||
configure_file(${CMAKE_SOURCE_DIR}/include/ggwave/ggwave.h ${CMAKE_CURRENT_SOURCE_DIR}/ggwave/ggwave.h COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/ggwave.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ggwave.cpp COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/fft.h ${CMAKE_CURRENT_SOURCE_DIR}/fft.h COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/reed-solomon/gf.hpp ${CMAKE_CURRENT_SOURCE_DIR}/reed-solomon/gf.hpp COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/reed-solomon/rs.hpp ${CMAKE_CURRENT_SOURCE_DIR}/reed-solomon/rs.hpp COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/reed-solomon/poly.hpp ${CMAKE_CURRENT_SOURCE_DIR}/reed-solomon/poly.hpp COPYONLY)
|
||||
configure_file(${CMAKE_SOURCE_DIR}/src/reed-solomon/LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/reed-solomon/LICENSE COPYONLY)
|
||||
133
examples/arduino-tx2/arduino-tx2.ino
Normal file
133
examples/arduino-tx2/arduino-tx2.ino
Normal file
@@ -0,0 +1,133 @@
|
||||
// To build this example for Arduino UNO, make sure to install the ArduinoSTL library:
|
||||
//#include <ArduinoSTL.h>
|
||||
|
||||
#include "ggwave/ggwave.h"
|
||||
|
||||
const int kPinLed0 = 13;
|
||||
const int kPinSpeaker = 10;
|
||||
const int kPinButton0 = 2;
|
||||
const int kPinButton1 = 4;
|
||||
|
||||
void send_text(GGWave & ggwave, uint8_t pin, const char * text, GGWave::TxProtocolId protocolId) {
|
||||
ggwave.init(text, protocolId);
|
||||
ggwave.encode();
|
||||
|
||||
const auto & tones = ggwave.txTones();
|
||||
float freq_hz = -1.0f;
|
||||
float duration_ms = -1.0f;
|
||||
for (int i = 0; i < (int) tones.size(); ++i) {
|
||||
if (tones[i].size() == 0) continue;
|
||||
const auto & curTone = tones[i].front();
|
||||
|
||||
if (curTone.freq_hz != freq_hz) {
|
||||
if (duration_ms > 0) {
|
||||
tone(pin, freq_hz);
|
||||
delay(duration_ms);
|
||||
}
|
||||
freq_hz = curTone.freq_hz;
|
||||
duration_ms = 0.0f;
|
||||
}
|
||||
duration_ms += curTone.duration_ms;
|
||||
}
|
||||
|
||||
if (duration_ms > 0) {
|
||||
tone(pin, freq_hz);
|
||||
delay(duration_ms);
|
||||
}
|
||||
|
||||
noTone(pin);
|
||||
digitalWrite(pin, LOW);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(57600);
|
||||
}
|
||||
|
||||
char txt[16];
|
||||
int pressed = 0;
|
||||
bool isDown = false;
|
||||
|
||||
void loop() {
|
||||
Serial.println("hello");
|
||||
|
||||
auto p = GGWave::getDefaultParameters();
|
||||
p.payloadLength = 16;
|
||||
p.sampleRateInp = 6000;
|
||||
p.sampleRateOut = 6000;
|
||||
p.sampleRate = 6000;
|
||||
p.samplesPerFrame = 128;
|
||||
p.sampleFormatInp = GGWAVE_SAMPLE_FORMAT_I16;
|
||||
p.sampleFormatOut = GGWAVE_SAMPLE_FORMAT_U8;
|
||||
p.operatingMode = (ggwave_OperatingMode) (GGWAVE_OPERATING_MODE_TX | GGWAVE_OPERATING_MODE_TX_ONLY_TONES | GGWAVE_OPERATING_MODE_USE_DSS);
|
||||
|
||||
GGWave::Protocols::tx().only(GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
GGWave ggwave(p);
|
||||
|
||||
pinMode(kPinLed0, OUTPUT);
|
||||
pinMode(kPinSpeaker, OUTPUT);
|
||||
pinMode(kPinButton0, INPUT);
|
||||
pinMode(kPinButton1, INPUT);
|
||||
|
||||
delay(3000);
|
||||
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
send_text(ggwave, kPinSpeaker, "Hello!", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
delay(2000);
|
||||
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
send_text(ggwave, kPinSpeaker, "This is a", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
send_text(ggwave, kPinSpeaker, "ggwave demo", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
delay(2000);
|
||||
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
send_text(ggwave, kPinSpeaker, "The arduino", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
send_text(ggwave, kPinSpeaker, "transmits data", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
send_text(ggwave, kPinSpeaker, "using sound", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
send_text(ggwave, kPinSpeaker, "through a buzzer", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
delay(1000);
|
||||
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
send_text(ggwave, kPinSpeaker, "The sound is", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
send_text(ggwave, kPinSpeaker, "decoded in a", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
delay(200);
|
||||
send_text(ggwave, kPinSpeaker, "web page.", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
delay(1000);
|
||||
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
send_text(ggwave, kPinSpeaker, "Press the button!", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
|
||||
while (true) {
|
||||
int but0 = digitalRead(kPinButton0);
|
||||
int but1 = digitalRead(kPinButton1);
|
||||
|
||||
if (but1 == LOW && isDown == false) {
|
||||
delay(200);
|
||||
++pressed;
|
||||
isDown = true;
|
||||
} else if (but1 == HIGH) {
|
||||
isDown = false;
|
||||
}
|
||||
|
||||
if (but0 == LOW) {
|
||||
snprintf(txt, 16, "Pressed: %d", pressed);
|
||||
|
||||
digitalWrite(kPinLed0, HIGH);
|
||||
send_text(ggwave, kPinSpeaker, txt, GGWAVE_PROTOCOL_MT_FASTEST);
|
||||
digitalWrite(kPinLed0, LOW);
|
||||
pressed = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,21 @@
|
||||
#include "fft.h"
|
||||
#include "reed-solomon/rs.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <cstdio>
|
||||
//#include <random>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO
|
||||
#define ggprintf(...)
|
||||
#else
|
||||
#define ggprintf(...) \
|
||||
g_fptr && fprintf(g_fptr, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
//
|
||||
// C interface
|
||||
|
||||
Reference in New Issue
Block a user