From f7d9e900787bdf24eb0680ae516fbb3bd0164f87 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Wed, 8 Jun 2022 00:04:03 +0300 Subject: [PATCH] remove-stl : update arduino tx examples --- examples/arduino-tx/arduino-tx.ino | 3 - examples/arduino-tx2/arduino-tx2.ino | 138 ++++++++++++--------------- examples/r2t2/main.cpp | 4 +- include/ggwave/ggwave.h | 7 +- src/ggwave.cpp | 14 ++- 5 files changed, 75 insertions(+), 91 deletions(-) diff --git a/examples/arduino-tx/arduino-tx.ino b/examples/arduino-tx/arduino-tx.ino index b57aba7..2e107b0 100644 --- a/examples/arduino-tx/arduino-tx.ino +++ b/examples/arduino-tx/arduino-tx.ino @@ -1,6 +1,3 @@ -// To build this example for Arduino UNO, make sure to install the ArduinoSTL library: -#include - // 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). diff --git a/examples/arduino-tx2/arduino-tx2.ino b/examples/arduino-tx2/arduino-tx2.ino index e10d7c2..a53791d 100644 --- a/examples/arduino-tx2/arduino-tx2.ino +++ b/examples/arduino-tx2/arduino-tx2.ino @@ -9,7 +9,7 @@ const int kPinButton0 = 2; const int kPinButton1 = 4; const int samplesPerFrame = 128; -const int sampleRate = 6000; +const int sampleRate = 6000; GGWave * g_ggwave = nullptr; @@ -17,28 +17,11 @@ void send_text(GGWave & ggwave, uint8_t pin, const char * text, GGWave::TxProtoc ggwave.init(text, protocolId); ggwave.encode(); - const auto & tones = ggwave.txTones(); - //Serial.println(tones.size()); - float freq_hz = -1.0f; - //float frameDuration_ms = (1000.0*samplesPerFrame)/sampleRate; - 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; - //duration_ms += frameDuration_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); } @@ -68,7 +51,6 @@ void setup() { 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); - delay(1000); static GGWave ggwave(p); ggwave.setLogFile(nullptr); Serial.println(ggwave.heapSize()); @@ -82,73 +64,71 @@ char txt[16]; int pressed = 0; bool isDown = false; +#define P(str) (strcpy_P(txt, PSTR(str)), txt) + void loop() { auto & ggwave = *g_ggwave; - //Serial.println(F("hello")); + delay(1000); + + digitalWrite(kPinLed0, HIGH); + send_text(ggwave, kPinSpeaker, P("Hello!"), GGWAVE_PROTOCOL_MT_FASTEST); + digitalWrite(kPinLed0, LOW); + + delay(2000); + + digitalWrite(kPinLed0, HIGH); + send_text(ggwave, kPinSpeaker, P("This is a"), GGWAVE_PROTOCOL_MT_FASTEST); + send_text(ggwave, kPinSpeaker, P("ggwave demo"), GGWAVE_PROTOCOL_MT_FASTEST); + digitalWrite(kPinLed0, LOW); + + delay(2000); + + digitalWrite(kPinLed0, HIGH); + send_text(ggwave, kPinSpeaker, P("The arduino"), GGWAVE_PROTOCOL_MT_FASTEST); + delay(200); + send_text(ggwave, kPinSpeaker, P("transmits data"), GGWAVE_PROTOCOL_MT_FASTEST); + delay(200); + send_text(ggwave, kPinSpeaker, P("using sound"), GGWAVE_PROTOCOL_MT_FASTEST); + delay(200); + send_text(ggwave, kPinSpeaker, P("through a buzzer"), GGWAVE_PROTOCOL_MT_FASTEST); + digitalWrite(kPinLed0, LOW); delay(1000); - ////Serial.println(F("sending ..")); digitalWrite(kPinLed0, HIGH); - send_text(ggwave, kPinSpeaker, "Hello!", GGWAVE_PROTOCOL_MT_FASTEST); + send_text(ggwave, kPinSpeaker, P("The sound is"), GGWAVE_PROTOCOL_MT_FASTEST); + delay(200); + send_text(ggwave, kPinSpeaker, P("decoded in a"), GGWAVE_PROTOCOL_MT_FASTEST); + delay(200); + send_text(ggwave, kPinSpeaker, P("web page."), GGWAVE_PROTOCOL_MT_FASTEST); digitalWrite(kPinLed0, LOW); - ////Serial.println(F("done ..")); - //delay(2000); + delay(1000); - //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); + digitalWrite(kPinLed0, HIGH); + send_text(ggwave, kPinSpeaker, P("Press the button!"), GGWAVE_PROTOCOL_MT_FASTEST); + digitalWrite(kPinLed0, LOW); - //delay(2000); + while (true) { + int but0 = digitalRead(kPinButton0); + int but1 = digitalRead(kPinButton1); - //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); + if (but1 == LOW && isDown == false) { + delay(200); + ++pressed; + isDown = true; + } else if (but1 == HIGH) { + isDown = false; + } - //delay(1000); + if (but0 == LOW) { + snprintf(txt, 16, "Pressed: %d", pressed); - //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; - // } - //} + digitalWrite(kPinLed0, HIGH); + send_text(ggwave, kPinSpeaker, txt, GGWAVE_PROTOCOL_MT_FASTEST); + digitalWrite(kPinLed0, LOW); + pressed = 0; + } + } } diff --git a/examples/r2t2/main.cpp b/examples/r2t2/main.cpp index 1540bd9..1f44689 100644 --- a/examples/r2t2/main.cpp +++ b/examples/r2t2/main.cpp @@ -89,7 +89,9 @@ int main(int argc, char** argv) { printf("Available Tx protocols:\n"); for (int i = 0; i < (int) protocols.size(); ++i) { const auto & protocol = protocols[i]; - printf(" -t%-2d : %-16s\n", i, protocol.name); + if (protocol.enabled && protocol.name) { + printf(" -t%-2d : %-16s\n", i, protocol.name); + } } printf("\n"); diff --git a/include/ggwave/ggwave.h b/include/ggwave/ggwave.h index 84dbc56..7512917 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, @@ -427,7 +427,7 @@ public: int nTones() const { return (2*bytesPerTx)/extra; } int nDataBitsPerTx() const { return 8*bytesPerTx; } int txDuration_ms(int samplesPerFrame, float sampleRate) const { - return extra*framesPerTx*((1000.0f*samplesPerFrame)/sampleRate); + return framesPerTx*((1000.0f*samplesPerFrame)/sampleRate); } }; @@ -468,6 +468,7 @@ public: Protocols & operator=(const std::initializer_list & list) { int i = 0; + disableAll(); for (auto & p : list) { data[i++] = p; } @@ -498,10 +499,10 @@ public: protocols.data[GGWAVE_PROTOCOL_ULTRASOUND_NORMAL] = { "[U] Normal", 320, 9, 3, 1, true, }; protocols.data[GGWAVE_PROTOCOL_ULTRASOUND_FAST] = { "[U] Fast", 320, 6, 3, 1, true, }; protocols.data[GGWAVE_PROTOCOL_ULTRASOUND_FASTEST] = { "[U] Fastest", 320, 3, 3, 1, true, }; -#endif protocols.data[GGWAVE_PROTOCOL_DT_NORMAL] = { "[DT] Normal", 24, 9, 1, 1, true, }; protocols.data[GGWAVE_PROTOCOL_DT_FAST] = { "[DT] Fast", 24, 6, 1, 1, true, }; protocols.data[GGWAVE_PROTOCOL_DT_FASTEST] = { "[DT] Fastest", 24, 3, 1, 1, true, }; +#endif protocols.data[GGWAVE_PROTOCOL_MT_NORMAL] = { "[MT] Normal", 24, 9, 1, 2, true, }; protocols.data[GGWAVE_PROTOCOL_MT_FAST] = { "[MT] Fast", 24, 6, 1, 2, true, }; protocols.data[GGWAVE_PROTOCOL_MT_FASTEST] = { "[MT] Fastest", 24, 3, 1, 2, true, }; diff --git a/src/ggwave.cpp b/src/ggwave.cpp index 02582b3..300a51d 100644 --- a/src/ggwave.cpp +++ b/src/ggwave.cpp @@ -513,9 +513,9 @@ bool GGWave::prepare(const Parameters & parameters) { const auto heapSize0 = m_heapSize; + m_heap = malloc(m_heapSize); // not sure if allocating alligned memory makes any difference - //m_heap = malloc(m_heapSize); - m_heap = aligned_alloc(kAlignment, m_heapSize); + //m_heap = aligned_alloc(kAlignment, m_heapSize); m_heapSize = 0; if (this->alloc(m_heap, m_heapSize) == false) { @@ -617,7 +617,11 @@ bool GGWave::alloc(void * p, int & n) { ::ggalloc(m_workRSData, RS::ReedSolomon::getWorkSize_bytes(maxLength, getECCBytesForLength(maxLength)), p, n); } - return m_resampler.alloc(p, n); + if (m_needResampling) { + m_resampler.alloc(p, n); + } + + return true; } void GGWave::setLogFile(FILE * fptr) { @@ -865,12 +869,12 @@ uint32_t GGWave::encode() { for (int i = 0; i < m_samplesPerFrame; i++) { const double curi = i; - m_tx.bit1Amplitude[k][i] = std::sin((2.0*M_PI)*(curi*m_isamplesPerFrame)*(freq*curIHzPerSample) + phaseOffset); + m_tx.bit1Amplitude[k][i] = sin((2.0*M_PI)*(curi*m_isamplesPerFrame)*(freq*curIHzPerSample) + phaseOffset); } for (int i = 0; i < m_samplesPerFrame; i++) { const double curi = i; - m_tx.bit0Amplitude[k][i] = std::sin((2.0*M_PI)*(curi*m_isamplesPerFrame)*((freq + m_hzPerSample*m_freqDelta_bin)*curIHzPerSample) + phaseOffset); + m_tx.bit0Amplitude[k][i] = sin((2.0*M_PI)*(curi*m_isamplesPerFrame)*((freq + m_hzPerSample*m_freqDelta_bin)*curIHzPerSample) + phaseOffset); } } }