mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-04-21 05:36:33 +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-rx)
|
||||||
add_subdirectory(arduino-tx)
|
add_subdirectory(arduino-tx)
|
||||||
|
add_subdirectory(arduino-tx2)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (GGWAVE_SUPPORT_SDL2)
|
if (GGWAVE_SUPPORT_SDL2)
|
||||||
|
|||||||
@@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
#include <PDM.h>
|
#include <PDM.h>
|
||||||
|
|
||||||
|
const int kPinLed0 = 13;
|
||||||
|
const int kPinSpeaker = 10;
|
||||||
|
const int kPinButton0 = 2;
|
||||||
|
const int kPinButton1 = 4;
|
||||||
|
|
||||||
using TSample = int16_t;
|
using TSample = int16_t;
|
||||||
static const size_t kSampleSize_bytes = sizeof(TSample);
|
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
|
// Buffer to read samples into, each sample is 16-bits
|
||||||
TSample sampleBuffer[qmax];
|
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() {
|
void setup() {
|
||||||
Serial.begin(57600);
|
Serial.begin(57600);
|
||||||
while (!Serial);
|
while (!Serial);
|
||||||
|
|
||||||
|
pinMode(kPinLed0, OUTPUT);
|
||||||
|
pinMode(kPinSpeaker, OUTPUT);
|
||||||
|
pinMode(kPinButton0, INPUT);
|
||||||
|
pinMode(kPinButton1, INPUT);
|
||||||
|
|
||||||
// Configure the data receive callback
|
// Configure the data receive callback
|
||||||
PDM.onReceive(onPDMdata);
|
PDM.onReceive(onPDMdata);
|
||||||
|
|
||||||
@@ -48,6 +90,7 @@ void loop() {
|
|||||||
Serial.println("trying to create ggwave instance");
|
Serial.println("trying to create ggwave instance");
|
||||||
|
|
||||||
auto p = GGWave::getDefaultParameters();
|
auto p = GGWave::getDefaultParameters();
|
||||||
|
|
||||||
p.payloadLength = 16;
|
p.payloadLength = 16;
|
||||||
p.sampleRateInp = frequency;
|
p.sampleRateInp = frequency;
|
||||||
p.sampleRateOut = frequency;
|
p.sampleRateOut = frequency;
|
||||||
@@ -73,15 +116,6 @@ void loop() {
|
|||||||
while (qsize >= p.samplesPerFrame) {
|
while (qsize >= p.samplesPerFrame) {
|
||||||
auto tStart = millis();
|
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);
|
ggwave.decode(sampleBuffer + qhead, p.samplesPerFrame*kSampleSize_bytes);
|
||||||
qsize -= p.samplesPerFrame;
|
qsize -= p.samplesPerFrame;
|
||||||
qhead += p.samplesPerFrame;
|
qhead += p.samplesPerFrame;
|
||||||
@@ -91,35 +125,40 @@ void loop() {
|
|||||||
|
|
||||||
auto tEnd = millis();
|
auto tEnd = millis();
|
||||||
if (++niter % 10 == 0) {
|
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);
|
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);
|
nr = ggwave.rxTakeData(result);
|
||||||
if (nr > 0) {
|
if (nr > 0) {
|
||||||
Serial.println(tEnd - tStart);
|
Serial.println(tEnd - tStart);
|
||||||
Serial.println(nr);
|
Serial.print("Received data with length ");
|
||||||
Serial.println((char *)result.data());
|
Serial.print(nr); // should be equal to p.payloadLength
|
||||||
|
Serial.println(" bytes:");
|
||||||
|
|
||||||
|
Serial.println((char *) result.data());
|
||||||
|
|
||||||
if (strcmp((char *)result.data(), "test") == 0) {
|
if (strcmp((char *)result.data(), "test") == 0) {
|
||||||
ggwave.init("hello", GGWave::TxProtocolId(GGWAVE_PROTOCOL_MT_FASTEST));
|
// pause microphone capture while transmitting
|
||||||
ggwave.encode();
|
PDM.end();
|
||||||
|
delay(500);
|
||||||
|
|
||||||
const auto & tones = ggwave.txTones();
|
send_text(ggwave, kPinSpeaker, "hello", GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
for (int i = 0; i < (int) tones.size(); ++i) {
|
|
||||||
Serial.print(" - frame ");
|
// resume microphone capture
|
||||||
Serial.print(i);
|
if (!PDM.begin(channels, frequency)) {
|
||||||
Serial.print(", ");
|
Serial.println("Failed to start PDM!");
|
||||||
Serial.print(tones[i].size());
|
while (1);
|
||||||
Serial.print(": ");
|
|
||||||
for (int j = 0; j < (int) tones[i].size(); ++j) {
|
|
||||||
Serial.print((int)(tones[i][j].freq_hz));
|
|
||||||
Serial.print(" ");
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (err > 0) {
|
if (err > 0) {
|
||||||
Serial.println("ERRROR");
|
Serial.println("ERRROR");
|
||||||
Serial.println(err);
|
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 kPinLed0 = 13;
|
||||||
const int kPinSpeaker = 10;
|
const int kPinSpeaker = 10;
|
||||||
@@ -9,118 +15,76 @@ const int kPinButton1 = 4;
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(57600);
|
Serial.begin(57600);
|
||||||
display_freeram();
|
|
||||||
|
|
||||||
//pinMode(kPinLed0, OUTPUT);
|
pinMode(kPinLed0, OUTPUT);
|
||||||
//pinMode(kPinSpeaker, OUTPUT);
|
pinMode(kPinSpeaker, OUTPUT);
|
||||||
//pinMode(kPinButton0, INPUT);
|
pinMode(kPinButton0, INPUT);
|
||||||
//pinMode(kPinButton1, INPUT);
|
pinMode(kPinButton1, INPUT);
|
||||||
|
|
||||||
//delay(3000);
|
delay(3000);
|
||||||
|
|
||||||
//digitalWrite(kPinLed0, HIGH);
|
digitalWrite(kPinLed0, HIGH);
|
||||||
//GGWave::send_text(kPinSpeaker, "Hello!", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "Hello!", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//digitalWrite(kPinLed0, LOW);
|
digitalWrite(kPinLed0, LOW);
|
||||||
|
|
||||||
//delay(2000);
|
delay(2000);
|
||||||
|
|
||||||
//digitalWrite(kPinLed0, HIGH);
|
digitalWrite(kPinLed0, HIGH);
|
||||||
//GGWave::send_text(kPinSpeaker, "This is a", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "This is a", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//GGWave::send_text(kPinSpeaker, "ggwave demo", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "ggwave demo", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//digitalWrite(kPinLed0, LOW);
|
digitalWrite(kPinLed0, LOW);
|
||||||
|
|
||||||
//delay(2000);
|
delay(2000);
|
||||||
|
|
||||||
//digitalWrite(kPinLed0, HIGH);
|
digitalWrite(kPinLed0, HIGH);
|
||||||
//GGWave::send_text(kPinSpeaker, "The arduino", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "The arduino", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//delay(200);
|
delay(200);
|
||||||
//GGWave::send_text(kPinSpeaker, "transmits data", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "transmits data", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//delay(200);
|
delay(200);
|
||||||
//GGWave::send_text(kPinSpeaker, "using sound", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "using sound", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//delay(200);
|
delay(200);
|
||||||
//GGWave::send_text(kPinSpeaker, "through a buzzer", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "through a buzzer", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//digitalWrite(kPinLed0, LOW);
|
digitalWrite(kPinLed0, LOW);
|
||||||
|
|
||||||
//delay(1000);
|
delay(1000);
|
||||||
|
|
||||||
//digitalWrite(kPinLed0, HIGH);
|
digitalWrite(kPinLed0, HIGH);
|
||||||
//GGWave::send_text(kPinSpeaker, "The sound is", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "The sound is", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//delay(200);
|
delay(200);
|
||||||
//GGWave::send_text(kPinSpeaker, "decoded in a", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "decoded in a", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//delay(200);
|
delay(200);
|
||||||
//GGWave::send_text(kPinSpeaker, "web page.", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "web page.", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//digitalWrite(kPinLed0, LOW);
|
digitalWrite(kPinLed0, LOW);
|
||||||
|
|
||||||
//delay(1000);
|
delay(1000);
|
||||||
|
|
||||||
//digitalWrite(kPinLed0, HIGH);
|
digitalWrite(kPinLed0, HIGH);
|
||||||
//GGWave::send_text(kPinSpeaker, "Press the button!", GGWave::TX_ARDUINO_512_FASTEST);
|
GGWave::send_text(kPinSpeaker, "Press the button!", GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
//digitalWrite(kPinLed0, LOW);
|
digitalWrite(kPinLed0, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
char txt[16];
|
char txt[16];
|
||||||
int pressed = 0;
|
int pressed = 0;
|
||||||
bool isDown = false;
|
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() {
|
void loop() {
|
||||||
Serial.println("hello");
|
int but0 = digitalRead(kPinButton0);
|
||||||
|
int but1 = digitalRead(kPinButton1);
|
||||||
|
|
||||||
auto p = GGWave::getDefaultParameters();
|
if (but1 == LOW && isDown == false) {
|
||||||
p.payloadLength = 4;
|
delay(200);
|
||||||
p.sampleRateInp = 6000;
|
++pressed;
|
||||||
p.sampleRateOut = 6000;
|
isDown = true;
|
||||||
p.sampleRate = 6000;
|
} else if (but1 == HIGH) {
|
||||||
p.samplesPerFrame = 128;
|
isDown = false;
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//int but0 = digitalRead(kPinButton0);
|
if (but0 == LOW) {
|
||||||
//int but1 = digitalRead(kPinButton1);
|
snprintf(txt, 16, "Pressed: %d", pressed);
|
||||||
|
|
||||||
//if (but1 == LOW && isDown == false) {
|
digitalWrite(kPinLed0, HIGH);
|
||||||
// delay(200);
|
GGWave::send_text(kPinSpeaker, txt, GGWave::GGWAVE_PROTOCOL_MT_FASTEST);
|
||||||
// ++pressed;
|
digitalWrite(kPinLed0, LOW);
|
||||||
// isDown = true;
|
pressed = 0;
|
||||||
//} 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;
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
const uint8_t kECCLength_bytes = getECCBytesForLength(kDataLength_bytes);
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TX_ARDUINO_512_NORMAL,
|
GGWAVE_PROTOCOL_MT_NORMAL,
|
||||||
TX_ARDUINO_512_FAST,
|
GGWAVE_PROTOCOL_MT_FAST,
|
||||||
TX_ARDUINO_512_FASTEST,
|
GGWAVE_PROTOCOL_MT_FASTEST,
|
||||||
TX_ARDUINO_1024_NORMAL,
|
|
||||||
TX_ARDUINO_1024_FAST,
|
|
||||||
TX_ARDUINO_1024_FASTEST,
|
|
||||||
} TxProtocolId;
|
} TxProtocolId;
|
||||||
|
|
||||||
struct Parameters {
|
struct Parameters {
|
||||||
@@ -913,22 +910,19 @@ void send(uint8_t pin, const uint8_t * data, const Parameters & parameters) {
|
|||||||
digitalWrite(pin, LOW);
|
digitalWrite(pin, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void send(uint8_t pin, const uint8_t * data, TxProtocolId protocolId = TX_ARDUINO_512_FASTEST) {
|
void send(uint8_t pin, const uint8_t * data, TxProtocolId protocolId = GGWAVE_PROTOCOL_MT_FASTEST) {
|
||||||
Parameters parameters = { 512, 16, 32 };
|
Parameters parameters = { 1024, 24, 64 };
|
||||||
|
|
||||||
switch (protocolId) {
|
switch (protocolId) {
|
||||||
case TX_ARDUINO_512_NORMAL: parameters = { 512, 16, 96 }; break;
|
case GGWAVE_PROTOCOL_MT_NORMAL: parameters = { 1024, 24, 192 }; break;
|
||||||
case TX_ARDUINO_512_FAST: parameters = { 512, 16, 64 }; break;
|
case GGWAVE_PROTOCOL_MT_FAST: parameters = { 1024, 24, 128 }; break;
|
||||||
case TX_ARDUINO_512_FASTEST: parameters = { 512, 16, 32 }; break;
|
case GGWAVE_PROTOCOL_MT_FASTEST: parameters = { 1024, 24, 64 }; 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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
send(pin, data, parameters);
|
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];
|
char tx[kDataLength_bytes];
|
||||||
memset(tx, 0, sizeof(tx));
|
memset(tx, 0, sizeof(tx));
|
||||||
strncpy(tx, text, 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 "fft.h"
|
||||||
#include "reed-solomon/rs.hpp"
|
#include "reed-solomon/rs.hpp"
|
||||||
|
|
||||||
#include <cstdio>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <cstdio>
|
||||||
//#include <random>
|
//#include <random>
|
||||||
|
|
||||||
#ifndef M_PI
|
#ifndef M_PI
|
||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef ARDUINO
|
||||||
|
#define ggprintf(...)
|
||||||
|
#else
|
||||||
#define ggprintf(...) \
|
#define ggprintf(...) \
|
||||||
g_fptr && fprintf(g_fptr, __VA_ARGS__)
|
g_fptr && fprintf(g_fptr, __VA_ARGS__)
|
||||||
|
#endif
|
||||||
|
|
||||||
//
|
//
|
||||||
// C interface
|
// C interface
|
||||||
|
|||||||
Reference in New Issue
Block a user