HeltecV2Board.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #include <Arduino.h>
  3. #include <helpers/ESP32Board.h>
  4. // built-ins
  5. #define PIN_VBAT_READ 37
  6. #define PIN_LED_BUILTIN 25
  7. #include <driver/rtc_io.h>
  8. class HeltecV2Board : public ESP32Board {
  9. public:
  10. void begin() {
  11. ESP32Board::begin();
  12. esp_reset_reason_t reason = esp_reset_reason();
  13. if (reason == ESP_RST_DEEPSLEEP) {
  14. long wakeup_source = esp_sleep_get_ext1_wakeup_status();
  15. if (wakeup_source & (1 << P_LORA_DIO_0)) { // received a LoRa packet (while in deep sleep)
  16. startup_reason = BD_STARTUP_RX_PACKET;
  17. }
  18. rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
  19. rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_0);
  20. }
  21. }
  22. void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) {
  23. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  24. // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep
  25. rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_0, RTC_GPIO_MODE_INPUT_ONLY);
  26. rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_0);
  27. rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
  28. if (pin_wake_btn < 0) {
  29. esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_0), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet
  30. } else {
  31. esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_0) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn
  32. }
  33. if (secs > 0) {
  34. esp_sleep_enable_timer_wakeup(secs * 1000000);
  35. }
  36. // Finally set ESP32 into sleep
  37. esp_deep_sleep_start(); // CPU halts here and never returns!
  38. }
  39. uint16_t getBattMilliVolts() override {
  40. analogReadResolution(10);
  41. uint32_t raw = 0;
  42. for (int i = 0; i < 8; i++) {
  43. raw += analogRead(PIN_VBAT_READ);
  44. }
  45. raw = raw / 8;
  46. return (1.98 * (2 / 1024.0) * raw) * 1000;
  47. }
  48. const char* getManufacturerName() const override {
  49. return "Heltec V2";
  50. }
  51. uint32_t getIRQGpio() override {
  52. return P_LORA_DIO_0; // default for SX1276
  53. }
  54. };