adc.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef KBF_ADC_H
  2. #define KBF_ADC_H
  3. // TODO put these in Kconfig
  4. #define KBF_ADC_ATTEN ADC_ATTEN_DB_0
  5. #define KBF_ADC_WIDTH ADC_WIDTH_BIT_12
  6. #define KBF_ADC_DEFAULT_VREF 1100
  7. #define KBF_ADC_NUM_SAMPLES 64
  8. #include <driver/adc.h>
  9. #include <esp_adc_cal.h>
  10. /**
  11. * Analog to Digital Converter functions
  12. */
  13. namespace kbf::adc {
  14. static constexpr const char *TAG = "kbf::adc";
  15. /**
  16. * ADC1 channel functions
  17. */
  18. class ADC1 {
  19. public:
  20. /**
  21. * Initialises ADC1 channel.
  22. *
  23. * @param channel ADC1 channel to use
  24. */
  25. explicit ADC1(int channel);
  26. /**
  27. * Reads raw data from the channel.
  28. *
  29. * @return read raw data
  30. */
  31. [[nodiscard]] uint32_t readRaw() const;
  32. /**
  33. * Reads raw data from the channel and converts it into voltage.
  34. *
  35. * @return read input voltage in mV
  36. */
  37. [[nodiscard]] uint32_t readVoltage() const;
  38. /**
  39. * Calculate voltage from raw input data using characteristics.
  40. *
  41. * @param raw input data
  42. * @return voltage value in mV
  43. */
  44. [[nodiscard]] uint32_t rawToVoltage(int raw) const;
  45. /**
  46. * ADC1 channel to use.
  47. */
  48. const adc1_channel_t channel;
  49. private:
  50. /**
  51. * ADC characteristics.
  52. */
  53. esp_adc_cal_characteristics_t *adcChars;
  54. };
  55. }
  56. #endif //KBF_ADC_H