TDeckBoard.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <Wire.h>
  3. #include <Arduino.h>
  4. #include "helpers/ESP32Board.h"
  5. #include <driver/rtc_io.h>
  6. #define PIN_VBAT_READ 4
  7. #define BATTERY_SAMPLES 8
  8. #define ADC_MULTIPLIER (2.0f * 3.3f * 1000)
  9. class TDeckBoard : public ESP32Board {
  10. public:
  11. void begin();
  12. #ifdef P_LORA_TX_LED
  13. void onBeforeTransmit() override{
  14. digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on - invert pin for SX1276
  15. }
  16. void onAfterTransmit() override{
  17. digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED off - invert pin for SX1276
  18. }
  19. #endif
  20. void enterDeepSleep(uint32_t secs, int pin_wake_btn) {
  21. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  22. // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep
  23. rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
  24. rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
  25. rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
  26. if (pin_wake_btn < 0) {
  27. esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
  28. } else {
  29. esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn
  30. }
  31. if (secs > 0) {
  32. esp_sleep_enable_timer_wakeup(secs * 1000000);
  33. }
  34. // Finally set ESP32 into sleep
  35. esp_deep_sleep_start(); // CPU halts here and never returns!
  36. }
  37. uint16_t getBattMilliVolts() {
  38. #if defined(PIN_VBAT_READ) && defined(ADC_MULTIPLIER)
  39. analogReadResolution(12);
  40. uint32_t raw = 0;
  41. for (int i = 0; i < BATTERY_SAMPLES; i++) {
  42. raw += analogRead(PIN_VBAT_READ);
  43. }
  44. raw = raw / BATTERY_SAMPLES;
  45. return (ADC_MULTIPLIER * raw) / 4096;
  46. #else
  47. return 0;
  48. #endif
  49. }
  50. const char* getManufacturerName() const{
  51. return "LilyGo T-Deck";
  52. }
  53. };