TechoCardBoard.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include "TechoCardBoard.h"
  4. #ifdef LILYGO_TECHO_CARD
  5. Adafruit_NeoPixel Led_A(1, WS2812_DATA_2, NEO_GRB + NEO_KHZ800);
  6. Adafruit_NeoPixel Led_B(1, WS2812_DATA_3, NEO_GRB + NEO_KHZ800);
  7. Adafruit_NeoPixel Led_C(1, WS2812_DATA_1, NEO_GRB + NEO_KHZ800);
  8. Adafruit_NeoPixel *Led[] =
  9. {
  10. &Led_A,
  11. &Led_B,
  12. &Led_C,
  13. };
  14. void TechoCardBoard::begin() {
  15. NRF52BoardDCDC::begin();
  16. Wire.begin();
  17. for (uint8_t i = 0; i < sizeof(Led) / sizeof(Led[0]); i++)
  18. {
  19. Led[i]->begin();
  20. delay(3); // allow the LEDs to initialise, otherwise they can get stuck
  21. Led[i]->setPixelColor(0, Led[i]->Color(0, 0, 0));
  22. Led[i]->show();
  23. }
  24. // put IMU20948 to sleep
  25. // see https://product.tdk.com/system/files/dam/doc/product/sensor/mortion-inertial/imu/data_sheet/ds-000189-icm-20948-v1.5.pdf
  26. Wire.beginTransmission(0x68);
  27. Wire.write(0x06); // PWR_MGMT_1 register
  28. Wire.write(0x40); // set SLEEP bit
  29. Wire.endTransmission();
  30. }
  31. uint16_t TechoCardBoard::getBattMilliVolts() {
  32. int adcvalue = 0;
  33. analogReference(AR_INTERNAL_3_0);
  34. analogReadResolution(12);
  35. digitalWrite(PIN_BAT_CTL, HIGH); // enable vbat vdiv
  36. delay(10);
  37. // ADC range is 0..3000mV and resolution is 12-bit (0..4095)
  38. adcvalue = analogRead(PIN_VBAT_READ);
  39. digitalWrite(PIN_BAT_CTL, LOW);
  40. // Convert the raw value to compensated mv, taking the resistor-
  41. // divider into account (providing the actual LIPO voltage)
  42. return (uint16_t)((float)adcvalue * REAL_VBAT_MV_PER_LSB);
  43. }
  44. void TechoCardBoard::onBeforeTransmit() {
  45. Led_A.setPixelColor(0, 20, 20, 20); // turn TX LED on
  46. Led_A.show();
  47. }
  48. void TechoCardBoard::onAfterTransmit() {
  49. Led_A.setPixelColor(0, 0, 0, 0); // turn TX LED off
  50. Led_A.show();
  51. }
  52. void TechoCardBoard::toggleTorch() {
  53. if (!_torchStatus) {
  54. Led_C.setPixelColor(0, 255, 255, 255);
  55. Led_C.show();
  56. _torchStatus = true;
  57. } else {
  58. Led_C.setPixelColor(0, 0, 0, 0);
  59. Led_C.show();
  60. _torchStatus = false;
  61. }
  62. }
  63. void TechoCardBoard::turnOffLeds() {
  64. for (uint8_t i = 0; i < sizeof(Led) / sizeof(*Led); i++)
  65. {
  66. Led[i]->setPixelColor(0, 0, 0, 0);
  67. Led[i]->show();
  68. }
  69. }
  70. void TechoCardBoard::powerOff() {
  71. nrf_gpio_cfg_sense_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
  72. turnOffLeds();
  73. digitalWrite(PIN_PWR_EN, LOW);
  74. sd_power_system_off();
  75. }
  76. #endif