uv_meter_scene_wiring.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "uv_meter_app_i.hpp"
  2. #include "uv_meter_event.hpp"
  3. static void uv_meter_scene_wiring_enter_settings_callback(void* context) {
  4. furi_assert(context);
  5. auto* app = static_cast<UVMeterApp*>(context);
  6. view_dispatcher_send_custom_event(app->view_dispatcher, UVMeterCustomEventSceneEnterSettings);
  7. }
  8. void uv_meter_scene_wiring_on_enter(void* context) {
  9. furi_assert(context);
  10. auto* app = static_cast<UVMeterApp*>(context);
  11. uv_meter_wiring_set_enter_settings_callback(
  12. app->uv_meter_wiring_view, uv_meter_scene_wiring_enter_settings_callback, app);
  13. view_dispatcher_switch_to_view(app->view_dispatcher, UVMeterViewWiring);
  14. }
  15. bool uv_meter_scene_wiring_on_event(void* context, SceneManagerEvent event) {
  16. furi_assert(context);
  17. auto* app = static_cast<UVMeterApp*>(context);
  18. bool consumed = false;
  19. if(event.type == SceneManagerEventTypeCustom) {
  20. if(event.event == UVMeterCustomEventSceneEnterSettings) {
  21. scene_manager_next_scene(app->scene_manager, UVMeterSceneSettings);
  22. consumed = true;
  23. }
  24. } else if(event.type == SceneManagerEventTypeTick) {
  25. uint32_t current_time = furi_get_tick();
  26. // Check once per second if AS7331 sensor is available
  27. if(current_time - app->app_state->last_sensor_check_timestamp >= furi_ms_to_ticks(1000)) {
  28. furi_mutex_acquire(app->app_state->as7331_mutex, FuriWaitForever);
  29. if(!app->app_state->as7331_initialized) {
  30. // Initialize the sensor, also wake it up if in power down mode
  31. uint8_t i2c_address = 0x0;
  32. switch(app->app_state->i2c_address) {
  33. case UVMeterI2CAddressAuto:
  34. break;
  35. case UVMeterI2CAddress74:
  36. i2c_address = DefaultI2CAddr;
  37. break;
  38. case UVMeterI2CAddress75:
  39. i2c_address = SecondaryI2CAddr;
  40. break;
  41. case UVMeterI2CAddress76:
  42. i2c_address = TertiaryI2CAddr;
  43. break;
  44. case UVMeterI2CAddress77:
  45. i2c_address = QuaternaryI2CAddr;
  46. break;
  47. }
  48. app->app_state->as7331_initialized = app->app_state->as7331.init(i2c_address);
  49. }
  50. if(!app->app_state->as7331_initialized || !app->app_state->as7331.deviceReady()) {
  51. app->app_state->as7331_initialized = false;
  52. } else {
  53. // Set default Gain and Integration Time
  54. app->app_state->as7331.setGain(GAIN_8);
  55. app->app_state->as7331.setIntegrationTime(TIME_128MS);
  56. }
  57. furi_mutex_release(app->app_state->as7331_mutex);
  58. if(app->app_state->as7331_initialized) {
  59. scene_manager_next_scene(app->scene_manager, UVMeterSceneData);
  60. consumed = true;
  61. }
  62. app->app_state->last_sensor_check_timestamp = current_time;
  63. }
  64. } else if(event.type == SceneManagerEventTypeBack) {
  65. // Always quit app in wiring scene
  66. scene_manager_stop(app->scene_manager);
  67. view_dispatcher_stop(app->view_dispatcher);
  68. consumed = true;
  69. }
  70. return consumed;
  71. }
  72. void uv_meter_scene_wiring_on_exit(void* context) {
  73. UNUSED(context);
  74. }