From b0b1ee297d48e664331815d54973af45a16cd25f Mon Sep 17 00:00:00 2001 From: acidvegas Date: Thu, 6 Jun 2024 01:04:24 -0400 Subject: [PATCH] Splitting code into their own .cpp and header files --- src/Lora.cpp | 32 ++++--------- src/Lora.h | 11 +++++ src/Speaker.cpp | 75 +++++++++++++++++++++++++++++ src/Speaker.h | 78 ++---------------------------- src/Storage.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++++++ src/Storage.h | 26 ++++++++++ src/main.ino | 125 +++--------------------------------------------- 7 files changed, 251 insertions(+), 216 deletions(-) create mode 100644 src/Lora.h create mode 100644 src/Speaker.cpp create mode 100644 src/Storage.cpp create mode 100644 src/Storage.h diff --git a/src/Lora.cpp b/src/Lora.cpp index 198a603..b07ea4f 100644 --- a/src/Lora.cpp +++ b/src/Lora.cpp @@ -1,5 +1,4 @@ -#include -#include "pins.h" +#include "Lora.h" SX1262 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN); @@ -14,7 +13,7 @@ bool setupRadio() { if (state == RADIOLIB_ERR_NONE) { Serial.println("Start Radio success!"); } else { - Serial.print("Start Radio failed,code:"); + Serial.print("Start Radio failed, code: "); Serial.println(state); return false; } @@ -44,19 +43,19 @@ bool setupRadio() { return false; } - // set output power to 10 dBm (accepted range is -17 - 22 dBm) + // Set output power to 17 dBm (accepted range is -17 - 22 dBm) if (radio.setOutputPower(17) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) { Serial.println(F("Selected output power is invalid for this module!")); return false; } - // set over current protection limit to 140 mA (accepted range is 45 - 140 mA) (set value to 0 to disable overcurrent protection) + // Set over current protection limit to 140 mA (accepted range is 45 - 140 mA) if (radio.setCurrentLimit(140) == RADIOLIB_ERR_INVALID_CURRENT_LIMIT) { Serial.println(F("Selected current limit is invalid for this module!")); return false; } - // set LoRa preamble length to 15 symbols (accepted range is 0 - 65535) + // Set LoRa preamble length to 15 symbols (accepted range is 0 - 65535) if (radio.setPreambleLength(15) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) { Serial.println(F("Selected preamble length is invalid for this module!")); return false; @@ -67,9 +66,6 @@ bool setupRadio() { return false; } - // set the function that will be called when new packet is received - //radio.setDio1Action(setFlag); - return true; } @@ -77,14 +73,8 @@ bool setupRadio() { bool transmit() { int state = radio.transmit("Hello World!"); - // you can also transmit byte array up to 256 bytes long - /* - byte byteArr[] = {0x01, 0x23, 0x45, 0x56, 0x78, 0xAB, 0xCD, 0xEF}; - int state = radio.transmit(byteArr, 8); - */ - if (state == RADIOLIB_ERR_NONE) { - Serial.println(F("Radio tramsmittion successful!")); + Serial.println(F("Radio transmission successful!")); Serial.print(F("[SX1262] Datarate:\t")); Serial.print(radio.getDataRate()); Serial.println(F(" bps")); @@ -112,22 +102,18 @@ void recvLoop() { if (state == RADIOLIB_ERR_NONE) { Serial.print(F("[RADIO] Received packet!")); - Serial.print(F(" Data:")); Serial.print(recv); - Serial.print(F(" RSSI:")); Serial.print(radio.getRSSI()); Serial.print(F(" dBm")); - // snprintf(dispRecvicerBuff[1], sizeof(dispRecvicerBuff[1]), "RSSI:%.2f dBm", radio.getRSSI()); - Serial.print(F(" SNR:")); Serial.print(radio.getSNR()); Serial.println(F(" dB")); - } else if (state == RADIOLIB_ERR_CRC_MISMATCH) { + } else if (state == RADIOLIB_ERR_CRC_MISMATCH) { Serial.println(F("CRC error!")); } else { - Serial.print(F("failed, code ")); + Serial.print(F("Failed, code ")); Serial.println(state); } } else { @@ -135,4 +121,4 @@ void recvLoop() { break; } } -} \ No newline at end of file +} diff --git a/src/Lora.h b/src/Lora.h new file mode 100644 index 0000000..684c3de --- /dev/null +++ b/src/Lora.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +#include "pins.h" + +extern SX1262 radio; + +bool setupRadio(); +bool transmit(); +void recvLoop(); diff --git a/src/Speaker.cpp b/src/Speaker.cpp new file mode 100644 index 0000000..8afa617 --- /dev/null +++ b/src/Speaker.cpp @@ -0,0 +1,75 @@ +#include "Speaker.h" + + +void setupI2S() { + i2s_config_t i2s_config = { + .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX), + .sample_rate = SAMPLE_RATE, + .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, + .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, + .communication_format = I2S_COMM_FORMAT_STAND_I2S, + .intr_alloc_flags = 0, + .dma_buf_count = 8, + .dma_buf_len = 64, + .use_apll = false, + .tx_desc_auto_clear = true, + .fixed_mclk = 0 + }; + + i2s_pin_config_t pin_config = { + .bck_io_num = BOARD_I2S_BCK, + .ws_io_num = BOARD_I2S_WS, + .data_out_num = BOARD_I2S_DOUT, + .data_in_num = I2S_PIN_NO_CHANGE + }; + + i2s_driver_install(BOARD_I2S_PORT, &i2s_config, 0, NULL); + i2s_set_pin(BOARD_I2S_PORT, &pin_config); + i2s_set_clk(BOARD_I2S_PORT, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO); +} + + +void playTone(float frequency, int duration, int volume) { + volume = constrain(volume, 0, 32767); + const int wave_period = SAMPLE_RATE / frequency; + int16_t sample_buffer[wave_period]; + + for (int i = 0; i < wave_period; ++i) + sample_buffer[i] = (i < wave_period / 2) ? volume : -volume; + + int total_samples = SAMPLE_RATE * duration / 1000; + int samples_written = 0; + + while (samples_written < total_samples) { + int to_write = min(wave_period, total_samples - samples_written); + i2s_write(BOARD_I2S_PORT, sample_buffer, to_write * sizeof(int16_t), (size_t *)&to_write, portMAX_DELAY); + samples_written += to_write; + } +} + + +void playRTTTL(const char* rtttl) { + static AudioGeneratorRTTTL *rtttlGenerator = new AudioGeneratorRTTTL(); + static AudioOutputI2S *audioOutput = new AudioOutputI2S(); + static AudioFileSourcePROGMEM *fileSource = new AudioFileSourcePROGMEM(rtttl, strlen(rtttl)); + + audioOutput->begin(); + rtttlGenerator->begin(fileSource, audioOutput); + + while (rtttlGenerator->isRunning()) + rtttlGenerator->loop(); + + rtttlGenerator->stop(); + fileSource->close(); +} + + +void playNotificationSound() { + playTone(1000, 150); + delay(100); + playTone(1500, 150); + delay(100); + playTone(2000, 150); + delay(100); + playTone(500, 150); +} diff --git a/src/Speaker.h b/src/Speaker.h index e0d8c3b..d99afc8 100644 --- a/src/Speaker.h +++ b/src/Speaker.h @@ -8,80 +8,10 @@ #include "pins.h" - #define BOARD_I2S_PORT I2S_NUM_0 #define SAMPLE_RATE 44100 - -void setupI2S() { - i2s_config_t i2s_config = { - .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX), - .sample_rate = SAMPLE_RATE, - .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, - .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, - .communication_format = I2S_COMM_FORMAT_STAND_I2S, - .intr_alloc_flags = 0, - .dma_buf_count = 8, - .dma_buf_len = 64, - .use_apll = false, - .tx_desc_auto_clear = true, - .fixed_mclk = 0 - }; - - i2s_pin_config_t pin_config = { - .bck_io_num = BOARD_I2S_BCK, - .ws_io_num = BOARD_I2S_WS, - .data_out_num = BOARD_I2S_DOUT, - .data_in_num = I2S_PIN_NO_CHANGE - }; - - i2s_driver_install(BOARD_I2S_PORT, &i2s_config, 0, NULL); - i2s_set_pin(BOARD_I2S_PORT, &pin_config); - i2s_set_clk(BOARD_I2S_PORT, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO); -} - - -void playTone(float frequency, int duration, int volume = 16383) { - volume = constrain(volume, 0, 32767); - const int wave_period = SAMPLE_RATE / frequency; - int16_t sample_buffer[wave_period]; - - for (int i = 0; i < wave_period; ++i) - sample_buffer[i] = (i < wave_period / 2) ? volume : -volume; - - int total_samples = SAMPLE_RATE * duration / 1000; - int samples_written = 0; - - while (samples_written < total_samples) { - int to_write = min(wave_period, total_samples - samples_written); - i2s_write(BOARD_I2S_PORT, sample_buffer, to_write * sizeof(int16_t), (size_t *)&to_write, portMAX_DELAY); - samples_written += to_write; - } -} - - -void playRTTTL(const char* rtttl) { - static AudioGeneratorRTTTL *rtttlGenerator = new AudioGeneratorRTTTL(); - static AudioOutputI2S *audioOutput = new AudioOutputI2S(); - static AudioFileSourcePROGMEM *fileSource = new AudioFileSourcePROGMEM(rtttl, strlen(rtttl)); - - audioOutput->begin(); - rtttlGenerator->begin(fileSource, audioOutput); - - while (rtttlGenerator->isRunning()) - rtttlGenerator->loop(); - - rtttlGenerator->stop(); - fileSource->close(); -} - - -void playNotificationSound() { - playTone(1000, 150); - delay(100); - playTone(1500, 150); - delay(100); - playTone(2000, 150); - delay(100); - playTone(500, 150); -} +void setupI2S(); +void playTone(float frequency, int duration, int volume = 16383); +void playRTTTL(const char* rtttl); +void playNotificationSound(); diff --git a/src/Storage.cpp b/src/Storage.cpp new file mode 100644 index 0000000..d23e505 --- /dev/null +++ b/src/Storage.cpp @@ -0,0 +1,120 @@ +#include "Storage.h" + + +Preferences preferences; + +String irc_nickname; +String irc_username; +String irc_realname; +String irc_server; +int irc_port; +bool irc_tls; +String irc_channel; +String irc_nickserv; +String wifi_ssid; +String wifi_password; + + +void loadPreferences() { + preferences.begin("config", false); + + // IRC preferences + if (!preferences.isKey("irc_nickname")) + preferences.putString("irc_nickname", "ACID_" + String(random(1000, 10000))); + irc_nickname = preferences.getString("irc_nickname"); + + if (!preferences.isKey("irc_username")) + preferences.putString("irc_username", "tdeck"); + irc_username = preferences.getString("irc_username"); + + if (!preferences.isKey("irc_realname")) + preferences.putString("irc_realname", "ACID DROP Firmware"); + irc_realname = preferences.getString("irc_realname"); + + if (!preferences.isKey("irc_server")) + preferences.putString("irc_server", "irc.supernets.org"); + irc_server = preferences.getString("irc_server"); + + if (!preferences.isKey("irc_port")) + preferences.putInt("irc_port", 6667); + irc_port = preferences.getInt("irc_port"); + + if (!preferences.isKey("irc_tls")) + preferences.putBool("irc_tls", false); + irc_tls = preferences.getBool("irc_tls"); + + if (!preferences.isKey("irc_channel")) + preferences.putString("irc_channel", "#comms"); + irc_channel = preferences.getString("irc_channel"); + + if (!preferences.isKey("irc_nickserv")) + preferences.putString("irc_nickserv", ""); + irc_nickserv = preferences.getString("irc_nickserv"); + + // WiFi preferences + if (!preferences.isKey("wifi_ssid")) + preferences.putString("wifi_ssid", ""); + wifi_ssid = preferences.getString("wifi_ssid"); + + if (!preferences.isKey("wifi_password")) + preferences.putString("wifi_password", ""); + wifi_password = preferences.getString("wifi_password"); + + preferences.end(); +} + + +bool mountSD() { + if (SD.begin(BOARD_SDCARD_CS, SPI, 800000U)) { + uint8_t cardType = SD.cardType(); + + if (cardType == CARD_NONE) { + Serial.println("No SD card attached"); + return false; + } else { + Serial.print("SD Card Type: "); + if (cardType == CARD_MMC) + Serial.println("MMC"); + else if (cardType == CARD_SD) + Serial.println("SDSC"); + else if (cardType == CARD_SDHC) + Serial.println("SDHC"); + else + Serial.println("UNKNOWN"); + + uint32_t cardSize = SD.cardSize() / (1024 * 1024); + uint32_t cardTotal = SD.totalBytes() / (1024 * 1024); + uint32_t cardUsed = SD.usedBytes() / (1024 * 1024); + Serial.printf("SD Card Size: %lu MB\n", cardSize); + Serial.printf("Total space: %lu MB\n", cardTotal); + Serial.printf("Used space: %lu MB\n", cardUsed); + + return true; + } + } + + return false; +} + + +void setupSD() { + pinMode(BOARD_SDCARD_CS, OUTPUT); + digitalWrite(BOARD_SDCARD_CS, HIGH); + pinMode(BOARD_SPI_MISO, INPUT_PULLUP); + SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); +} + + +void wipeNVS() { + esp_err_t err = nvs_flash_erase(); + if (err == ESP_OK) + Serial.println("NVS flash erase successful."); + else + Serial.println("Error erasing NVS flash!"); + + err = nvs_flash_init(); + if (err == ESP_OK) + Serial.println("NVS flash init successful."); + else + Serial.println("Error initializing NVS flash!"); +} \ No newline at end of file diff --git a/src/Storage.h b/src/Storage.h new file mode 100644 index 0000000..a53da7c --- /dev/null +++ b/src/Storage.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include "nvs_flash.h" +#include + +#include "pins.h" + +extern Preferences preferences; + +extern String irc_nickname; +extern String irc_username; +extern String irc_realname; +extern String irc_server; +extern int irc_port; +extern bool irc_tls; +extern String irc_channel; +extern String irc_nickserv; +extern String wifi_ssid; +extern String wifi_password; + +void loadPreferences(); +bool mountSD(); +void setupSD(); +void wipeNVS(); diff --git a/src/main.ino b/src/main.ino index 798911e..0a28f06 100644 --- a/src/main.ino +++ b/src/main.ino @@ -5,7 +5,6 @@ // Aurduino includes #include // Needed for Mac spoofing -#include "nvs_flash.h" #include // Power management #include #include @@ -17,7 +16,9 @@ // Local includes #include "bootScreen.h" +#include "Lora.h" #include "pins.h" +#include "Storage.h" #include "Speaker.h" @@ -39,7 +40,6 @@ struct WiFiNetwork { // Initialize components and objects Pangodream_18650_CL BL(BOARD_BAT_ADC, CONV_FACTOR, READS); -Preferences preferences; TFT_eSPI tft = TFT_eSPI(); WiFiClient* client; @@ -50,16 +50,7 @@ std::vector mentions; std::vector wifiNetworks; // Global variables to cache preferences and buffers -String irc_nickname; -String irc_username; -String irc_realname; -String irc_server; -int irc_port; -bool irc_tls; -String irc_channel; -String irc_nickserv; -String wifi_ssid; -String wifi_password; + String inputBuffer = ""; // Leftover crack variables (will be removed when preferences are done) @@ -141,14 +132,11 @@ void setup() { setBrightness(8); // Set the screen brightness to 50%) // Give power to the SD card - pinMode(BOARD_SDCARD_CS, OUTPUT); - digitalWrite(BOARD_SDCARD_CS, HIGH); - pinMode(BOARD_SPI_MISO, INPUT_PULLUP); - SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); + //setupSD(); + //mountSD(); // Turn on power to the radio - pinMode(RADIO_CS_PIN, OUTPUT); - digitalWrite(RADIO_CS_PIN, HIGH); + //setupRadio(); // Start the I2C bus for the keyboard Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL); @@ -263,55 +251,6 @@ void loop() { } } - -void loadPreferences() { - preferences.begin("config", false); - - // IRC preferences - if (!preferences.isKey("irc_nickname")) - preferences.putString("irc_nickname", "ACID_" + String(random(1000, 10000))); - irc_nickname = preferences.getString("irc_nickname"); - - if (!preferences.isKey("irc_username")) - preferences.putString("irc_username", "tdeck"); - irc_username = preferences.getString("irc_username"); - - if (!preferences.isKey("irc_realname")) - preferences.putString("irc_realname", "ACID DROP Firmware"); - irc_realname = preferences.getString("irc_realname"); - - if (!preferences.isKey("irc_server")) - preferences.putString("irc_server", "irc.supernets.org"); - irc_server = preferences.getString("irc_server"); - - if (!preferences.isKey("irc_port")) - preferences.putInt("irc_port", 6667); - irc_port = preferences.getInt("irc_port"); - - if (!preferences.isKey("irc_tls")) - preferences.putBool("irc_tls", false); - irc_tls = preferences.getBool("irc_tls"); - - if (!preferences.isKey("irc_channel")) - preferences.putString("irc_channel", "#comms"); - irc_channel = preferences.getString("irc_channel"); - - if (!preferences.isKey("irc_nickserv")) - preferences.putString("irc_nickserv", ""); - irc_nickserv = preferences.getString("irc_nickserv"); - - // WiFi preferences - if (!preferences.isKey("wifi_ssid")) - preferences.putString("wifi_ssid", ""); - wifi_ssid = preferences.getString("wifi_ssid"); - - if (!preferences.isKey("wifi_password")) - preferences.putString("wifi_password", ""); - wifi_password = preferences.getString("wifi_password"); - - preferences.end(); -} - // ------------------------------------------------------------------------------------------------ @@ -866,43 +805,6 @@ void setBrightness(uint8_t value) { } -bool setupSD() { - digitalWrite(BOARD_SDCARD_CS, HIGH); - digitalWrite(RADIO_CS_PIN, HIGH); - digitalWrite(BOARD_TFT_CS, HIGH); - - if (SD.begin(BOARD_SDCARD_CS, SPI, 800000U)) { - uint8_t cardType = SD.cardType(); - - if (cardType == CARD_NONE) { - Serial.println("No SD_MMC card attached"); - return false; - } else { - Serial.print("SD_MMC Card Type: "); - if (cardType == CARD_MMC) - Serial.println("MMC"); - else if (cardType == CARD_SD) - Serial.println("SDSC"); - else if (cardType == CARD_SDHC) - Serial.println("SDHC"); - else - Serial.println("UNKNOWN"); - - uint32_t cardSize = SD.cardSize() / (1024 * 1024); - uint32_t cardTotal = SD.totalBytes() / (1024 * 1024); - uint32_t cardUsed = SD.usedBytes() / (1024 * 1024); - Serial.printf("SD Card Size: %lu MB\n", cardSize); - Serial.printf("Total space: %lu MB\n", cardTotal); - Serial.printf("Used space: %lu MB\n", cardUsed); - - return true; - } - } - - return false; -} - - void turnOffScreen() { Serial.println("Screen turned off"); tft.writecommand(TFT_DISPOFF); @@ -1538,19 +1440,4 @@ void printDeviceInfo() { } else { tft.setCursor(0, line * 16); tft.setTextColor(TFT_CYAN); tft.print("WiFi Info: "); tft.setTextColor(TFT_WHITE); tft.println("Not connected"); line++; } -} - - -void wipeNVS() { - esp_err_t err = nvs_flash_erase(); - if (err == ESP_OK) - Serial.println("NVS flash erase successful."); - else - Serial.println("Error erasing NVS flash!"); - - err = nvs_flash_init(); - if (err == ESP_OK) - Serial.println("NVS flash init successful."); - else - Serial.println("Error initializing NVS flash!"); } \ No newline at end of file