commit 637117240d0629a65c524a6c2a5c50d242173306 Author: Malko Gindrat Date: Wed May 20 00:02:26 2026 +0200 my message diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..8d5edec --- /dev/null +++ b/platformio.ini @@ -0,0 +1,21 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:lilygo-t3-s3] +platform = espressif32 +board = lilygo-t3-s3 +framework = arduino +monitor_speed = 115200 +build_flags = + -D ARDUINO_USB_CDC_ON_BOOT=1 +lib_deps = + jgromes/RadioLib + adafruit/Adafruit SSD1306 + adafruit/Adafruit GFX Library \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..ef7963b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,260 @@ +#include +#include +#include +#include +#include +#include + +// ========================= +// LoRa pins - LilyGO T3S3 SX1262 +// ========================= +static const int PIN_LORA_SCK = 5; +static const int PIN_LORA_MISO = 3; +static const int PIN_LORA_MOSI = 6; +static const int PIN_LORA_CS = 7; +static const int PIN_LORA_RST = 8; +static const int PIN_LORA_DIO1 = 33; +static const int PIN_LORA_BUSY = 34; + +// ========================= +// OLED pins - LilyGO T3S3 OLED +// ========================= +static const int OLED_SDA = 18; +static const int OLED_SCL = 17; +static const int OLED_ADDR = 0x3C; +static const int SCREEN_WIDTH = 128; +static const int SCREEN_HEIGHT = 64; + +// ========================= +// ACTIVE PROFILE (Shelly / custom LoRa) +// ========================= +static const float LORA_FREQ_MHZ = 865.000; +static const float LORA_BW_KHZ = 125.0; +static const uint8_t LORA_SF = 12; +static const uint8_t LORA_CR = 5; // 4/5 +static const uint8_t LORA_SYNC_WORD = 0x12; +static const int8_t LORA_TX_POWER = 10; +static const uint16_t LORA_PREAMBLE = 10; +static const float LORA_TCXO_VOLTAGE = 1.6; +static const bool LORA_USE_LDO = false; +static const bool LORA_USE_CRC = false; +static const bool LORA_EXPLICIT_HDR = true; + + +// ========================= +// MESHCORE SWITZERLAND PROFILE (COMMENTED) +// ========================= +/* +static const float LORA_FREQ_MHZ = 869.618; +static const float LORA_BW_KHZ = 62.5; +static const uint8_t LORA_SF = 8; +static const uint8_t LORA_CR = 8; // 4/8 +static const uint8_t LORA_SYNC_WORD = 0x34; +static const int8_t LORA_TX_POWER = 10; +static const uint16_t LORA_PREAMBLE = 10; +static const float LORA_TCXO_VOLTAGE = 1.6; +static const bool LORA_USE_LDO = false; +static const bool LORA_USE_CRC = true; +static const bool LORA_EXPLICIT_HDR = true; +*/ + +// ========================= +// Objects +// ========================= +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); +SX1262 radio = new Module(PIN_LORA_CS, PIN_LORA_DIO1, PIN_LORA_RST, PIN_LORA_BUSY); + +// ========================= +// Runtime state +// ========================= +String lastMsg = "none"; +float lastRssi = 0.0; +float lastSnr = 0.0; +unsigned long rxCount = 0; +unsigned long lastIdleRefresh = 0; +unsigned long showPacketUntil = 0; + +// ========================= +// Helpers +// ========================= +String formatCR(uint8_t cr) { + return "4/" + String(cr); +} + +String formatHexByte(uint8_t value) { + char buf[5]; + snprintf(buf, sizeof(buf), "%02X", value); + return String(buf); +} + +// Draw one line safely +void drawLine(int x, int y, const String& text) { + display.setCursor(x, y); + display.print(text); +} + +// Simple text wrapper for packet display +void drawWrappedText(const String& text, int startY, int maxLines) { + const int charsPerLine = 21; // approx for 128 px at text size 1 + int len = text.length(); + int index = 0; + int line = 0; + + while (index < len && line < maxLines) { + String chunk = text.substring(index, min(index + charsPerLine, len)); + drawLine(0, startY + line * 8, chunk); + index += charsPerLine; + line++; + } +} + +void drawListeningScreen() { + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); + + drawLine(0, 0, "Listening"); + drawLine(70, 0, String(rxCount) + " pk"); + + drawLine(0, 10, "F:" + String(LORA_FREQ_MHZ, 3)); + drawLine(0, 20, "BW:" + String(LORA_BW_KHZ, 1) + " SF:" + String(LORA_SF)); + drawLine(0, 30, "CR:" + formatCR(LORA_CR) + " SW:0x" + formatHexByte(LORA_SYNC_WORD)); + + drawLine(0, 40, "Last:"); + drawWrappedText(lastMsg, 48, 2); + + display.display(); +} + +void drawPacketScreen() { + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); + + drawLine(0, 0, "PACKET RECEIVED"); + drawLine(0, 10, "RSSI:" + String(lastRssi, 1) + " SNR:" + String(lastSnr, 1)); + drawLine(0, 20, "Len:" + String(lastMsg.length())); + + drawLine(0, 32, "Text:"); + drawWrappedText(lastMsg, 40, 3); + + display.display(); +} + +void drawErrorScreen(const String& title, int code) { + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); + + drawLine(0, 0, title); + drawLine(0, 12, "code: " + String(code)); + + display.display(); +} + +// ========================= +// Setup +// ========================= +void setup() { + Serial.begin(115200); + delay(1500); + + Wire.begin(OLED_SDA, OLED_SCL); + + if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { + Serial.println("OLED init failed"); + while (true) delay(1000); + } + + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(SSD1306_WHITE); + drawLine(0, 0, "Booting..."); + display.display(); + + SPI.begin(PIN_LORA_SCK, PIN_LORA_MISO, PIN_LORA_MOSI, PIN_LORA_CS); + + Serial.println(); + Serial.println("Starting LoRa receiver..."); + + int state = radio.begin( + LORA_FREQ_MHZ, + LORA_BW_KHZ, + LORA_SF, + LORA_CR, + LORA_SYNC_WORD, + LORA_TX_POWER, + LORA_PREAMBLE, + LORA_TCXO_VOLTAGE, + LORA_USE_LDO + ); + + if (state != RADIOLIB_ERR_NONE) { + Serial.print("radio.begin failed, code "); + Serial.println(state); + drawErrorScreen("LoRa init failed", state); + while (true) delay(1000); + } + + if (LORA_EXPLICIT_HDR) { + state = radio.explicitHeader(); + } else { + state = radio.implicitHeader(LORA_PREAMBLE); + } + + if (state != RADIOLIB_ERR_NONE) { + Serial.print("header mode failed, code "); + Serial.println(state); + drawErrorScreen("Header config failed", state); + while (true) delay(1000); + } + + state = radio.setCRC(LORA_USE_CRC); + if (state != RADIOLIB_ERR_NONE) { + Serial.print("CRC config failed, code "); + Serial.println(state); + drawErrorScreen("CRC config failed", state); + while (true) delay(1000); + } + + Serial.println("Listening..."); + drawListeningScreen(); +} + +// ========================= +// Loop +// ========================= +void loop() { + String str; + int state = radio.receive(str); + + if (state == RADIOLIB_ERR_NONE) { + lastMsg = str; + lastRssi = radio.getRSSI(); + lastSnr = radio.getSNR(); + rxCount++; + + Serial.print("RX: "); + Serial.println(lastMsg); + Serial.print("RSSI: "); + Serial.println(lastRssi); + Serial.print("SNR: "); + Serial.println(lastSnr); + Serial.println(); + + drawPacketScreen(); + showPacketUntil = millis() + 2500; // show packet screen for 2.5 s + } + else if (state != RADIOLIB_ERR_RX_TIMEOUT) { + Serial.print("Receive failed, code "); + Serial.println(state); + } + + // after packet screen timeout, return to listening screen + if (millis() > showPacketUntil) { + if (millis() - lastIdleRefresh > 500) { + drawListeningScreen(); + lastIdleRefresh = millis(); + } + } +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html