mp_flipper_modflipperzero_adc.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <furi_hal.h>
  2. #include <mp_flipper_modflipperzero.h>
  3. #include <mp_flipper_runtime.h>
  4. #include "mp_flipper_context.h"
  5. inline static FuriHalAdcChannel decode_pin_to_adc_channel(uint8_t pin) {
  6. switch(pin) {
  7. case MP_FLIPPER_GPIO_PIN_PC0:
  8. return FuriHalAdcChannel1;
  9. case MP_FLIPPER_GPIO_PIN_PC1:
  10. return FuriHalAdcChannel2;
  11. case MP_FLIPPER_GPIO_PIN_PC3:
  12. return FuriHalAdcChannel4;
  13. case MP_FLIPPER_GPIO_PIN_PA4:
  14. return FuriHalAdcChannel9;
  15. case MP_FLIPPER_GPIO_PIN_PA6:
  16. return FuriHalAdcChannel11;
  17. case MP_FLIPPER_GPIO_PIN_PA7:
  18. return FuriHalAdcChannel12;
  19. default:
  20. return FuriHalAdcChannelNone;
  21. }
  22. }
  23. inline uint16_t mp_flipper_adc_read_pin(uint8_t raw_pin) {
  24. mp_flipper_context_t* ctx = mp_flipper_context;
  25. FuriHalAdcChannel channel = decode_pin_to_adc_channel(raw_pin);
  26. if(channel == FuriHalAdcChannelNone) {
  27. return 0;
  28. }
  29. if(ctx->gpio_pins[raw_pin] != MP_FLIPPER_GPIO_MODE_ANALOG) {
  30. return 0;
  31. }
  32. return ctx->adc_handle ? furi_hal_adc_read(ctx->adc_handle, channel) : 0;
  33. }
  34. inline float mp_flipper_adc_convert_to_voltage(uint16_t value) {
  35. mp_flipper_context_t* ctx = mp_flipper_context;
  36. return ctx->adc_handle ? furi_hal_adc_convert_to_voltage(ctx->adc_handle, value) : 0.0f;
  37. }