diff --git a/examples/arduino-rx/arduino-rx.ino b/examples/arduino-rx/arduino-rx.ino index 74430aa..7530528 100644 --- a/examples/arduino-rx/arduino-rx.ino +++ b/examples/arduino-rx/arduino-rx.ino @@ -35,6 +35,7 @@ #include // Pin configuration +const int kPinLED0 = 2; const int kPinButton0 = 5; const int kPinSpeaker = 10; @@ -108,9 +109,12 @@ void setup() { Serial.begin(57600); //while (!Serial); + pinMode(kPinLED0, OUTPUT); pinMode(kPinSpeaker, OUTPUT); pinMode(kPinButton0, INPUT_PULLUP); + digitalWrite(kPinLED0, LOW); + #ifdef DISPLAY_OUTPUT { // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally @@ -167,15 +171,16 @@ void setup() { //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::tx()[GGWAVE_PROTOCOL_MT_FASTEST].freqStart += 48; // Protocols to use for RX // Remove the ones that you don't need to reduce memory usage 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_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_NORMAL, true); + //GGWave::Protocols::rx().toggle(GGWAVE_PROTOCOL_MT_FAST, true); GGWave::Protocols::rx().toggle(GGWAVE_PROTOCOL_MT_FASTEST, true); // Initialize the ggwave instance and print the memory usage @@ -209,7 +214,10 @@ void loop() { int but0Prev = HIGH; GGWave::TxRxData result; + GGWave::Spectrum rxSpectrum; + char resultLast[17]; + int tLastReceive = -10000; // Main loop .. while (true) { @@ -245,20 +253,50 @@ void loop() { Serial.println((char *) result.data()); -#ifdef DISPLAY_OUTPUT - { - display.clearDisplay(); - - display.setTextSize(2); - display.setTextColor(SSD1306_WHITE); - display.setCursor(0, 0); - display.println((char *) result.data()); - - display.display(); - } -#endif strcpy(resultLast, (char *) result.data()); + tLastReceive = tEnd; } + +#ifdef DISPLAY_OUTPUT + const auto t = millis(); + + if (ggwave.rxTakeSpectrum(rxSpectrum) && t > 2000) { + const bool isNew = t - tLastReceive < 2000; + + if (isNew) { + digitalWrite(kPinLED0, HIGH); + } else { + digitalWrite(kPinLED0, LOW); + } + + display.clearDisplay(); + + display.setTextSize(isNew ? 2 : 1); + display.setTextColor(SSD1306_WHITE); + display.setCursor(0, 0); + display.println(resultLast); + + const int nBin0 = 16; + const int nBins = 64; + const int dX = SCREEN_WIDTH/nBins; + + float smax = 0.0f; + for (int x = 0; x < nBins; x++) { + smax = std::max(smax, rxSpectrum[nBin0 + x]); + } + smax = smax == 0.0f ? 1.0f : 1.0f/smax; + + const float h = isNew ? 0.25f: 0.75f; + for (int x = 0; x < nBins; x++) { + const int x0 = x*dX; + const int x1 = x0 + dX; + const int y = (int) (h*SCREEN_HEIGHT*(rxSpectrum[nBin0 + x]*smax)); + display.fillRect(x0, SCREEN_HEIGHT - y, dX, y, SSD1306_WHITE); + } + + display.display(); + } +#endif } // This should never happen. diff --git a/examples/esp32-rx/esp32-rx.ino b/examples/esp32-rx/esp32-rx.ino index 9201ae1..cab1336 100644 --- a/examples/esp32-rx/esp32-rx.ino +++ b/examples/esp32-rx/esp32-rx.ino @@ -41,6 +41,9 @@ #include #include +// Pin configuration +const int kPinLED0 = 2; + // Global GGwave instance GGWave ggwave; @@ -104,6 +107,9 @@ void setup() { Serial.begin(115200); while (!Serial); + pinMode(kPinLED0, OUTPUT); + digitalWrite(kPinLED0, LOW); + #ifdef DISPLAY_OUTPUT { // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally @@ -193,12 +199,13 @@ void setup() { } } +int niter = 0; +int tLastReceive = -10000; + +GGWave::TxRxData result; +GGWave::Spectrum rxSpectrum; + void loop() { - int nr = 0; - int niter = 0; - - GGWave::TxRxData result; - // Read from i2s - the samples are 12-bit so we need to do some massaging to make them 16-bit { size_t bytes_read = 0; @@ -248,7 +255,7 @@ void loop() { } // Check if we have successfully decoded any data: - nr = ggwave.rxTakeData(result); + int nr = ggwave.rxTakeData(result); if (nr > 0) { Serial.println(tEnd - tStart); Serial.print(F("Received data with length ")); @@ -257,17 +264,47 @@ void loop() { Serial.println((char *) result.data()); -#ifdef DISPLAY_OUTPUT - { - display.clearDisplay(); - - display.setTextSize(2); - display.setTextColor(SSD1306_WHITE); - display.setCursor(0, 0); - display.println((char *) result.data()); - - display.display(); - } -#endif + tLastReceive = tEnd; } + +#ifdef DISPLAY_OUTPUT + const auto t = millis(); + + if (ggwave.rxTakeSpectrum(rxSpectrum) && t > 2000) { + const bool isNew = t - tLastReceive < 2000; + + if (isNew) { + digitalWrite(kPinLED0, HIGH); + } else { + digitalWrite(kPinLED0, LOW); + } + + display.clearDisplay(); + + display.setTextSize(isNew ? 2 : 1); + display.setTextColor(SSD1306_WHITE); + display.setCursor(0, 0); + display.println((char *) result.data()); + + const int nBin0 = 16; + const int nBins = 64; + const int dX = SCREEN_WIDTH/nBins; + + float smax = 0.0f; + for (int x = 0; x < nBins; x++) { + smax = std::max(smax, rxSpectrum[nBin0 + x]); + } + smax = smax == 0.0f ? 1.0f : 1.0f/smax; + + const float h = isNew ? 0.25f: 0.75f; + for (int x = 0; x < nBins; x++) { + const int x0 = x*dX; + const int x1 = x0 + dX; + const int y = (int) (h*SCREEN_HEIGHT*(rxSpectrum[nBin0 + x]*smax)); + display.fillRect(x0, SCREEN_HEIGHT - y, dX, y, SSD1306_WHITE); + } + + display.display(); + } +#endif }