From 7918594b96fd59e8548d28f8513c09bf4cdf95e1 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Sat, 11 Jun 2022 17:55:52 +0300 Subject: [PATCH] arduino : updating examples + fix memory alignment issue for rp2040 --- examples/arduino-rx/arduino-rx.ino | 102 +++++++++++++++------------ examples/arduino-tx2/arduino-tx2.ino | 9 ++- include/ggwave/ggwave.h | 4 +- src/ggwave.cpp | 2 +- 4 files changed, 64 insertions(+), 53 deletions(-) diff --git a/examples/arduino-rx/arduino-rx.ino b/examples/arduino-rx/arduino-rx.ino index 972a0f9..5eb3afc 100644 --- a/examples/arduino-rx/arduino-rx.ino +++ b/examples/arduino-rx/arduino-rx.ino @@ -30,28 +30,17 @@ GGWave * g_ggwave = nullptr; // 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) { + Serial.print(F("Sending text: ")); + Serial.println(text); + 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) { + const auto & protocol = GGWave::Protocols::tx()[protocolId]; + const auto tones = ggwave.txTones(); + const auto duration_ms = protocol.txDuration_ms(ggwave.samplesPerFrame(), ggwave.sampleRateOut()); + for (auto & curTone : tones) { + const auto freq_hz = (protocol.freqStart + curTone)*ggwave.hzPerSample(); tone(pin, freq_hz); delay(duration_ms); } @@ -66,6 +55,48 @@ void setup() { pinMode(kPinSpeaker, OUTPUT); + Serial.println(F("Trying to create ggwave instance")); + + auto p = GGWave::getDefaultParameters(); + + p.payloadLength = 16; + p.sampleRateInp = frequency; + p.sampleRateOut = frequency; + p.sampleRate = frequency; + p.samplesPerFrame = samplesPerFrame; + p.sampleFormatInp = GGWAVE_SAMPLE_FORMAT_I16; + p.sampleFormatOut = GGWAVE_SAMPLE_FORMAT_I16; + p.operatingMode = (ggwave_OperatingMode) (GGWAVE_OPERATING_MODE_RX | GGWAVE_OPERATING_MODE_TX | GGWAVE_OPERATING_MODE_USE_DSS | GGWAVE_OPERATING_MODE_TX_ONLY_TONES); + + GGWave::Protocols::tx().disableAll(); + GGWave::Protocols::tx().toggle(GGWAVE_PROTOCOL_DT_NORMAL, true); + GGWave::Protocols::tx().toggle(GGWAVE_PROTOCOL_DT_FAST, true); + GGWave::Protocols::tx().toggle(GGWAVE_PROTOCOL_DT_FASTEST, true); + GGWave::Protocols::tx().toggle(GGWAVE_PROTOCOL_MT_NORMAL, true); + GGWave::Protocols::tx().toggle(GGWAVE_PROTOCOL_MT_FAST, true); + GGWave::Protocols::tx().toggle(GGWAVE_PROTOCOL_MT_FASTEST, true); + + GGWave::Protocols::rx().disableAll(); + //GGWave::Protocols::rx().toggle(GGWAVE_PROTOCOL_DT_NORMAL, true); + //GGWave::Protocols::rx().toggle(GGWAVE_PROTOCOL_DT_FAST, true); + //GGWave::Protocols::rx().toggle(GGWAVE_PROTOCOL_DT_FASTEST, true); + //GGWave::Protocols::rx().toggle(GGWAVE_PROTOCOL_MT_NORMAL, true); + //GGWave::Protocols::rx().toggle(GGWAVE_PROTOCOL_MT_FAST, true); + GGWave::Protocols::rx().toggle(GGWAVE_PROTOCOL_MT_FASTEST, true); + + delay(1000); + + static GGWave ggwave(p); + Serial.println(ggwave.heapSize()); + + delay(1000); + + ggwave.setLogFile(nullptr); + + g_ggwave = &ggwave; + + Serial.println(F("Instance initialized")); + // Configure the data receive callback PDM.onReceive(onPDMdata); @@ -81,29 +112,6 @@ void setup() { Serial.println(F("Failed to start PDM!")); while (1); } - - Serial.println(F("Trying to create ggwave instance")); - - auto p = GGWave::getDefaultParameters(); - - p.payloadLength = 16; - p.sampleRateInp = frequency; - p.sampleRateOut = frequency; - p.sampleRate = frequency; - p.samplesPerFrame = samplesPerFrame; - p.sampleFormatInp = GGWAVE_SAMPLE_FORMAT_I16; - p.sampleFormatOut = GGWAVE_SAMPLE_FORMAT_I16; - p.operatingMode = (ggwave_OperatingMode) (GGWAVE_OPERATING_MODE_RX | GGWAVE_OPERATING_MODE_TX | GGWAVE_OPERATING_MODE_USE_DSS | GGWAVE_OPERATING_MODE_TX_ONLY_TONES); - - GGWave::Protocols::tx().only({GGWAVE_PROTOCOL_MT_FASTEST, GGWAVE_PROTOCOL_DT_FASTEST}); - GGWave::Protocols::rx().only({GGWAVE_PROTOCOL_MT_FASTEST, GGWAVE_PROTOCOL_DT_FASTEST}); - - static GGWave ggwave(p); - ggwave.setLogFile(nullptr); - - g_ggwave = &ggwave; - - Serial.println(F("Instance initialized")); } void loop() { @@ -131,16 +139,16 @@ void loop() { // for example: samplesPerFrame = 128, frequency = 6000 => not more than 20 ms Serial.println(tEnd - tStart); if (tEnd - tStart > 1000*(float(samplesPerFrame)/frequency)) { - Serial.println("Warning: decode() took too long to execute!"); + Serial.println(F("Warning: decode() took too long to execute!")); } } nr = ggwave.rxTakeData(result); if (nr > 0) { Serial.println(tEnd - tStart); - Serial.print("Received data with length "); + Serial.print(F("Received data with length ")); Serial.print(nr); // should be equal to p.payloadLength - Serial.println(" bytes:"); + Serial.println(F(" bytes:")); Serial.println((char *) result.data()); @@ -153,7 +161,7 @@ void loop() { // resume microphone capture if (!PDM.begin(channels, frequency)) { - Serial.println("Failed to start PDM!"); + Serial.println(F("Failed to start PDM!")); while (1); } } @@ -161,7 +169,7 @@ void loop() { } if (err > 0) { - Serial.println("ERRROR"); + Serial.println(F("ERRROR")); Serial.println(err); err = 0; } diff --git a/examples/arduino-tx2/arduino-tx2.ino b/examples/arduino-tx2/arduino-tx2.ino index d67b264..3cc452c 100644 --- a/examples/arduino-tx2/arduino-tx2.ino +++ b/examples/arduino-tx2/arduino-tx2.ino @@ -13,7 +13,9 @@ GGWave * g_ggwave = nullptr; char txt[64]; #define P(str) (strcpy_P(txt, PSTR(str)), txt) +// 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) { + Serial.print(F("Sending text: ")); Serial.println(text); ggwave.init(text, protocolId); @@ -40,7 +42,7 @@ void setup() { pinMode(kPinButton0, INPUT); pinMode(kPinButton1, INPUT); - Serial.println(P("Trying to create ggwave instance")); + Serial.println(F("Trying to create ggwave instance")); auto p = GGWave::getDefaultParameters(); p.payloadLength = 16; @@ -59,7 +61,7 @@ void setup() { g_ggwave = &ggwave; - Serial.println(P("Instance initialized")); + Serial.println(F("Instance initialized")); } int pressed = 0; @@ -109,7 +111,8 @@ void loop() { send_text(ggwave, kPinSpeaker, P("Press the button!"), GGWAVE_PROTOCOL_MT_FASTEST); digitalWrite(kPinLed0, LOW); - Serial.println(P("Starting main loop")); + Serial.println(F("Starting main loop")); + while (true) { int but0 = digitalRead(kPinButton0); int but1 = digitalRead(kPinButton1); diff --git a/include/ggwave/ggwave.h b/include/ggwave/ggwave.h index 4cad003..e43c44f 100644 --- a/include/ggwave/ggwave.h +++ b/include/ggwave/ggwave.h @@ -48,10 +48,10 @@ extern "C" { GGWAVE_PROTOCOL_ULTRASOUND_NORMAL, GGWAVE_PROTOCOL_ULTRASOUND_FAST, GGWAVE_PROTOCOL_ULTRASOUND_FASTEST, +#endif GGWAVE_PROTOCOL_DT_NORMAL, GGWAVE_PROTOCOL_DT_FAST, GGWAVE_PROTOCOL_DT_FASTEST, -#endif GGWAVE_PROTOCOL_MT_NORMAL, GGWAVE_PROTOCOL_MT_FAST, GGWAVE_PROTOCOL_MT_FASTEST, @@ -495,10 +495,10 @@ public: protocols.data[GGWAVE_PROTOCOL_ULTRASOUND_NORMAL] = { PSTR("[U] Normal"), 320, 9, 3, 1, true, }; protocols.data[GGWAVE_PROTOCOL_ULTRASOUND_FAST] = { PSTR("[U] Fast"), 320, 6, 3, 1, true, }; protocols.data[GGWAVE_PROTOCOL_ULTRASOUND_FASTEST] = { PSTR("[U] Fastest"), 320, 3, 3, 1, true, }; +#endif protocols.data[GGWAVE_PROTOCOL_DT_NORMAL] = { PSTR("[DT] Normal"), 24, 9, 1, 1, true, }; protocols.data[GGWAVE_PROTOCOL_DT_FAST] = { PSTR("[DT] Fast"), 24, 6, 1, 1, true, }; protocols.data[GGWAVE_PROTOCOL_DT_FASTEST] = { PSTR("[DT] Fastest"), 24, 3, 1, 1, true, }; -#endif protocols.data[GGWAVE_PROTOCOL_MT_NORMAL] = { PSTR("[MT] Normal"), 24, 9, 1, 2, true, }; protocols.data[GGWAVE_PROTOCOL_MT_FAST] = { PSTR("[MT] Fast"), 24, 6, 1, 2, true, }; protocols.data[GGWAVE_PROTOCOL_MT_FASTEST] = { PSTR("[MT] Fastest"), 24, 3, 1, 2, true, }; diff --git a/src/ggwave.cpp b/src/ggwave.cpp index a770ab4..a2f1c14 100644 --- a/src/ggwave.cpp +++ b/src/ggwave.cpp @@ -369,7 +369,7 @@ GGWave::RxProtocols & GGWave::Protocols::rx() { // this probably does not matter, but adding it anyway #ifdef ARDUINO -const int kAlignment = 1; +const int kAlignment = 4; #else const int kAlignment = 8; #endif