acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

Utilities.cpp (5010B)

      1 #include "Utilities.h"
      2 
      3 
      4 Pangodream_18650_CL BL(BOARD_BAT_ADC, CONV_FACTOR, READS);
      5 
      6 
      7 String formatBytes(size_t bytes) {
      8     if (bytes < 1024)
      9         return String(bytes) + " B";
     10     else if (bytes < (1024 * 1024))
     11         return String(bytes / 1024.0, 2) + " KB";
     12     else if (bytes < (1024 * 1024 * 1024))
     13         return String(bytes / 1024.0 / 1024.0, 2) + " MB";
     14     else
     15         return String(bytes / 1024.0 / 1024.0 / 1024.0, 2) + " GB";
     16 }
     17 
     18 
     19 char getKeyboardInput() {
     20     char incoming = 0;
     21     Wire.requestFrom(LILYGO_KB_SLAVE_ADDRESS, 1);
     22     if (Wire.available()) {
     23         incoming = Wire.read();
     24         if (incoming != (char)0x00) {
     25             return incoming;
     26         }
     27     }
     28     return 0;
     29 }
     30 
     31 
     32 void setBrightness(uint8_t value) {
     33     static uint8_t level = 16;
     34     static uint8_t steps = 16;
     35     value = constrain(value, 0, steps); // Ensure the brightness value is within the valid range
     36 
     37     if (value == 0) {
     38         digitalWrite(BOARD_BL_PIN, 0);
     39         delay(3);
     40         level = 0;
     41         return;
     42     }
     43 
     44     if (level == 0) {
     45         digitalWrite(BOARD_BL_PIN, 1);
     46         level = steps;
     47         delayMicroseconds(30);
     48     }
     49 
     50     int from = steps - level;
     51     int to = steps - value;
     52     int num = (steps + to - from) % steps;
     53     for (int i = 0; i < num; i++) {
     54         digitalWrite(BOARD_BL_PIN, 0);
     55         digitalWrite(BOARD_BL_PIN, 1);
     56     }
     57 
     58     level = value;
     59 }
     60 
     61 
     62 void updateTimeFromNTP() {
     63     Serial.println("Syncing time with NTP server...");
     64     configTime(-5 * 3600, 3600, "pool.ntp.org", "north-america.pool.ntp.org", "time.nist.gov");
     65 
     66     for (int i = 0; i < 10; ++i) { // Try up to 10 times
     67         delay(2000);
     68         struct tm timeinfo;
     69         if (getLocalTime(&timeinfo)) {
     70             Serial.println(&timeinfo, "Time synchronized: %A, %B %d %Y %H:%M:%S");
     71             return;
     72         } else {
     73             Serial.println("Failed to synchronize time, retrying...");
     74         }
     75     }
     76 
     77     Serial.println("Failed to synchronize time after multiple attempts."); // What do we do if we can't sync with the NTP server?
     78 }
     79 
     80 
     81 void printDeviceInfo() {
     82     Serial.println("Gathering device info...");
     83 
     84     // Get MAC Address
     85     uint8_t mac[6];
     86     esp_efuse_mac_get_default(mac);
     87     String macAddress = String(mac[0], HEX) + ":" + String(mac[1], HEX) + ":" + String(mac[2], HEX) + ":" + String(mac[3], HEX) + ":" + String(mac[4], HEX) + ":" + String(mac[5], HEX);
     88 
     89     // Get Chip Info
     90     uint32_t chipId = ESP.getEfuseMac();
     91     esp_chip_info_t chip_info;
     92     esp_chip_info(&chip_info);
     93     String chipInfo = String(chip_info.model) + " Rev " + String(chip_info.revision) + ", " + String(chip_info.cores) + " cores, " +  String(ESP.getCpuFreqMHz()) + " MHz";
     94 
     95     // Get Flash Info
     96     size_t flashSize = spi_flash_get_chip_size();
     97     size_t flashUsed = ESP.getFlashChipSize() - ESP.getFreeSketchSpace();
     98     String flashInfo = formatBytes(flashUsed) + " / " + formatBytes(flashSize);
     99     
    100     // Get PSRAM Info
    101     size_t total_psram = ESP.getPsramSize();
    102     size_t free_psram = ESP.getFreePsram();
    103     String psramInfo = formatBytes(total_psram - free_psram) + " / " + formatBytes(total_psram);
    104 
    105     // Get Heap Info
    106     size_t total_heap = ESP.getHeapSize();
    107     size_t free_heap = ESP.getFreeHeap();
    108     String heapInfo = formatBytes(total_heap - free_heap) + " / " + formatBytes(total_heap);
    109 
    110     // Get WiFi Info
    111     String wifiInfo = "Not connected";
    112     String wifiSSID = "";
    113     String wifiChannel = "";
    114     String wifiSignal = "";
    115     String wifiLocalIP = "";
    116     String wifiGatewayIP = "";
    117     if (WiFi.status() == WL_CONNECTED) {
    118         wifiSSID = WiFi.SSID();
    119         wifiChannel = String(WiFi.channel());
    120         wifiSignal = String(WiFi.RSSI()) + " dBm";
    121         wifiLocalIP = WiFi.localIP().toString();
    122         wifiGatewayIP = WiFi.gatewayIP().toString();
    123     }
    124 
    125     Serial.println("MCU: ESP32-S3FN16R8");
    126     Serial.println("LoRa Tranciever: Semtech SX1262 (915 MHz)"); // Need to set the frequency in pins.h
    127     Serial.println("LCD: ST7789 SPI"); // Update this
    128     Serial.println("Chip ID: " + String(chipId, HEX));
    129     Serial.println("MAC Address: " + macAddress);
    130     Serial.println("Chip Info: " + chipInfo);
    131     Serial.println("Battery:");
    132     Serial.println("  Pin Value: " + String(analogRead(34)));
    133     //Serial.println("  Average Pin Value: " + String(BL.pinRead()));
    134     //Serial.println("  Volts: " + String(BL.getBatteryVolts()));
    135     //Serial.println("  Charge Level: " + String(BL.getBatteryChargeLevel()) + "%");
    136     Serial.println("Memory:");
    137     Serial.println("  Flash: " + flashInfo);
    138     Serial.println("  PSRAM: " + psramInfo);
    139     Serial.println("  Heap: " + heapInfo);
    140     if (WiFi.status() == WL_CONNECTED) {
    141         Serial.println("WiFi Info: " + wifiInfo);
    142         Serial.println("  SSID: " + wifiSSID);
    143         Serial.println("  Channel: " + wifiChannel);
    144         Serial.println("  Signal: " + wifiSignal);
    145         Serial.println("  Local IP: " + wifiLocalIP);
    146         Serial.println("  Gateway IP: " + wifiGatewayIP);
    147     }
    148 }