uv_meter_scene_data.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "uv_meter_app_i.hpp"
  2. #include "uv_meter_event.hpp"
  3. #include <gui/view_dispatcher.h>
  4. /**
  5. * @brief Start a new measurement
  6. *
  7. * Initializes a new measurement and updates the measurement state.
  8. *
  9. * @param app_state Pointer to the shared app_state
  10. */
  11. static void start_measurement(UVMeterAppState* app_state) {
  12. // Start measurement
  13. app_state->as7331.setMeasurementMode(MEASUREMENT_MODE_COMMAND);
  14. app_state->as7331.startMeasurement();
  15. }
  16. /**
  17. * @brief Process the measurement results
  18. *
  19. * Retrieves the measurement results and updates the application state.
  20. *
  21. * @param app Pointer to the app
  22. */
  23. static void process_measurement_results(UVMeterApp* app) {
  24. if(app->app_state->as7331.getResults(app->app_state->results, app->app_state->raw_results)) {
  25. FURI_LOG_D(
  26. "UV_Meter Data",
  27. "Irradiance UVA: %.2f µW/cm² UVB: %.2f µW/cm² UVC: %.2f µW/cm²",
  28. app->app_state->results.uv_a,
  29. app->app_state->results.uv_b,
  30. app->app_state->results.uv_c);
  31. uv_meter_data_set_results(
  32. app->uv_meter_data_view, &app->app_state->results, &app->app_state->raw_results);
  33. } else {
  34. FURI_LOG_E("UV_Meter Data", "Failed to get measurement results");
  35. }
  36. }
  37. static void uv_meter_scene_data_enter_settings_callback(void* context) {
  38. furi_assert(context);
  39. auto* app = static_cast<UVMeterApp*>(context);
  40. view_dispatcher_send_custom_event(app->view_dispatcher, UVMeterCustomEventSceneEnterSettings);
  41. }
  42. void uv_meter_scene_data_on_enter(void* context) {
  43. auto* app = static_cast<UVMeterApp*>(context);
  44. uv_meter_update_from_sensor(app->uv_meter_data_view);
  45. uv_meter_data_set_unit(app->uv_meter_data_view, app->app_state->unit);
  46. uv_meter_data_set_enter_settings_callback(
  47. app->uv_meter_data_view, uv_meter_scene_data_enter_settings_callback, app);
  48. view_dispatcher_switch_to_view(app->view_dispatcher, UVMeterViewData);
  49. }
  50. bool uv_meter_scene_data_on_event(void* context, SceneManagerEvent event) {
  51. furi_assert(context);
  52. auto* app = static_cast<UVMeterApp*>(context);
  53. bool consumed = false;
  54. if(event.type == SceneManagerEventTypeCustom) {
  55. if(event.event == UVMeterCustomEventSceneEnterSettings) {
  56. scene_manager_next_scene(app->scene_manager, UVMeterSceneSettings);
  57. consumed = true;
  58. }
  59. } else if(event.type == SceneManagerEventTypeTick) {
  60. furi_mutex_acquire(app->app_state->as7331_mutex, FuriWaitForever);
  61. // Check if sensor got disconnected
  62. if(!app->app_state->as7331_initialized || !app->app_state->as7331.deviceReady()) {
  63. app->app_state->as7331_initialized = false;
  64. scene_manager_search_and_switch_to_previous_scene(
  65. app->scene_manager, UVMeterSceneWiring);
  66. } else {
  67. // Getting the status will put sensor into measurement state
  68. as7331_osr_status_reg_t status;
  69. app->app_state->as7331.getStatus(status);
  70. // Check if measurement is complete
  71. if(status.new_data) {
  72. process_measurement_results(app);
  73. start_measurement(app->app_state);
  74. }
  75. // This happens when measurement was interrupted by changing settings
  76. else if(status.osr.start_state != 1) {
  77. start_measurement(app->app_state);
  78. }
  79. // Handle overflows
  80. if(status.adc_overflow || status.result_overflow || status.out_conv_overflow) {
  81. FURI_LOG_E(
  82. "UV Meter Data",
  83. "Overflow detected! ADCOF (%d) MRESOF (%d) OUTCONVOF (%d)",
  84. status.adc_overflow,
  85. status.result_overflow,
  86. status.out_conv_overflow);
  87. }
  88. }
  89. consumed = true;
  90. furi_mutex_release(app->app_state->as7331_mutex);
  91. } else if(event.type == SceneManagerEventTypeBack) {
  92. // Always quit app in data scene
  93. scene_manager_stop(app->scene_manager);
  94. view_dispatcher_stop(app->view_dispatcher);
  95. consumed = true;
  96. }
  97. return consumed;
  98. }
  99. void uv_meter_scene_data_on_exit(void* context) {
  100. UNUSED(context);
  101. }