lightmeter.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "lightmeter.h"
  2. #include "lightmeter_helper.h"
  3. #define TAG "MAIN APP"
  4. static bool lightmeter_custom_event_callback(void* context, uint32_t event) {
  5. furi_assert(context);
  6. LightMeterApp* app = context;
  7. return scene_manager_handle_custom_event(app->scene_manager, event);
  8. }
  9. static bool lightmeter_back_event_callback(void* context) {
  10. furi_assert(context);
  11. LightMeterApp* app = context;
  12. return scene_manager_handle_back_event(app->scene_manager);
  13. }
  14. static void lightmeter_tick_event_callback(void* context) {
  15. furi_assert(context);
  16. LightMeterApp* app = context;
  17. scene_manager_handle_tick_event(app->scene_manager);
  18. }
  19. LightMeterApp* lightmeter_app_alloc(uint32_t first_scene) {
  20. LightMeterApp* app = malloc(sizeof(LightMeterApp));
  21. // Set default values to config
  22. app->config = malloc(sizeof(LightMeterConfig));
  23. app->config->iso = DEFAULT_ISO;
  24. app->config->nd = DEFAULT_ND;
  25. app->config->aperture = DEFAULT_APERTURE;
  26. app->config->dome = DEFAULT_DOME;
  27. app->config->backlight = DEFAULT_BACKLIGHT;
  28. app->config->measurement_resolution = HIGH_RES;
  29. app->config->device_addr = ADDR_LOW;
  30. app->config->lux_only = LUX_ONLY_OFF;
  31. // Records
  32. app->gui = furi_record_open(RECORD_GUI);
  33. app->storage = furi_record_open(RECORD_STORAGE);
  34. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  35. app->cfg_path = furi_string_alloc();
  36. furi_string_printf(app->cfg_path, "%s/%s", APP_PATH_DIR, APP_PATH_CFG);
  37. FlipperFormat* cfg_fmt = flipper_format_file_alloc(app->storage);
  38. if(flipper_format_file_open_existing(cfg_fmt, furi_string_get_cstr(app->cfg_path))) {
  39. flipper_format_read_int32(cfg_fmt, "iso", &app->config->iso, 1);
  40. flipper_format_read_int32(cfg_fmt, "aperture", &app->config->aperture, 1);
  41. flipper_format_read_int32(cfg_fmt, "dome", &app->config->dome, 1);
  42. flipper_format_read_int32(cfg_fmt, "backlight", &app->config->backlight, 1);
  43. flipper_format_read_int32(
  44. cfg_fmt, "measurement_resolution", &app->config->measurement_resolution, 1);
  45. flipper_format_read_int32(cfg_fmt, "lux_only", &app->config->lux_only, 1);
  46. flipper_format_read_int32(cfg_fmt, "device_addr", &app->config->device_addr, 1);
  47. flipper_format_read_int32(cfg_fmt, "sensor_type", &app->config->sensor_type, 1);
  48. }
  49. flipper_format_free(cfg_fmt);
  50. // Sensor
  51. lightmeter_app_i2c_init_sensor(app);
  52. // View dispatcher
  53. app->view_dispatcher = view_dispatcher_alloc();
  54. app->scene_manager = scene_manager_alloc(&lightmeter_scene_handlers, app);
  55. view_dispatcher_enable_queue(app->view_dispatcher);
  56. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  57. view_dispatcher_set_custom_event_callback(
  58. app->view_dispatcher, lightmeter_custom_event_callback);
  59. view_dispatcher_set_navigation_event_callback(
  60. app->view_dispatcher, lightmeter_back_event_callback);
  61. view_dispatcher_set_tick_event_callback(
  62. app->view_dispatcher, lightmeter_tick_event_callback, furi_ms_to_ticks(200));
  63. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  64. // Views
  65. app->main_view = main_view_alloc();
  66. view_dispatcher_add_view(
  67. app->view_dispatcher, LightMeterAppViewMainView, main_view_get_view(app->main_view));
  68. // Set default values to main view from config
  69. main_view_set_iso(app->main_view, app->config->iso);
  70. main_view_set_nd(app->main_view, app->config->nd);
  71. main_view_set_aperture(app->main_view, app->config->aperture);
  72. main_view_set_speed(app->main_view, DEFAULT_SPEED);
  73. main_view_set_dome(app->main_view, app->config->dome);
  74. // Variable item list
  75. app->var_item_list = variable_item_list_alloc();
  76. view_dispatcher_add_view(
  77. app->view_dispatcher,
  78. LightMeterAppViewVarItemList,
  79. variable_item_list_get_view(app->var_item_list));
  80. // Widget
  81. app->widget = widget_alloc();
  82. view_dispatcher_add_view(
  83. app->view_dispatcher, LightMeterAppViewAbout, widget_get_view(app->widget));
  84. view_dispatcher_add_view(
  85. app->view_dispatcher, LightMeterAppViewHelp, widget_get_view(app->widget));
  86. // Set first scene
  87. scene_manager_next_scene(app->scene_manager, first_scene);
  88. return app;
  89. }
  90. void lightmeter_app_free(LightMeterApp* app) {
  91. furi_assert(app);
  92. // Views
  93. view_dispatcher_remove_view(app->view_dispatcher, LightMeterAppViewMainView);
  94. main_view_free(app->main_view);
  95. // Variable item list
  96. view_dispatcher_remove_view(app->view_dispatcher, LightMeterAppViewVarItemList);
  97. variable_item_list_free(app->var_item_list);
  98. // Widget
  99. view_dispatcher_remove_view(app->view_dispatcher, LightMeterAppViewAbout);
  100. view_dispatcher_remove_view(app->view_dispatcher, LightMeterAppViewHelp);
  101. widget_free(app->widget);
  102. // View dispatcher
  103. scene_manager_free(app->scene_manager);
  104. view_dispatcher_free(app->view_dispatcher);
  105. // Records
  106. furi_record_close(RECORD_GUI);
  107. if(app->config->backlight != BACKLIGHT_AUTO) {
  108. notification_message(
  109. app->notifications,
  110. &sequence_display_backlight_enforce_auto); // set backlight back to auto
  111. }
  112. furi_record_close(RECORD_STORAGE);
  113. furi_record_close(RECORD_NOTIFICATION);
  114. bh1750_set_power_state(0);
  115. // Free cfg path string
  116. furi_string_free(app->cfg_path);
  117. free(app->config);
  118. free(app);
  119. }
  120. int32_t lightmeter_app(void* p) {
  121. UNUSED(p);
  122. uint32_t first_scene = LightMeterAppSceneMain;
  123. LightMeterApp* app = lightmeter_app_alloc(first_scene);
  124. view_dispatcher_run(app->view_dispatcher);
  125. lightmeter_app_free(app);
  126. return 0;
  127. }
  128. void lightmeter_app_set_config(LightMeterApp* context, LightMeterConfig* config) {
  129. LightMeterApp* app = context;
  130. app->config = config;
  131. storage_common_mkdir(app->storage, APP_PATH_DIR);
  132. FlipperFormat* cfg_fmt = flipper_format_file_alloc(app->storage);
  133. if(flipper_format_file_open_always(cfg_fmt, furi_string_get_cstr(app->cfg_path))) {
  134. flipper_format_write_header_cstr(cfg_fmt, "lightmeter", 1);
  135. flipper_format_write_int32(cfg_fmt, "iso", &(app->config->iso), 1);
  136. flipper_format_write_int32(cfg_fmt, "nd", &(app->config->nd), 1);
  137. flipper_format_write_int32(cfg_fmt, "aperture", &(app->config->aperture), 1);
  138. flipper_format_write_int32(cfg_fmt, "dome", &(app->config->dome), 1);
  139. flipper_format_write_int32(cfg_fmt, "backlight", &(app->config->backlight), 1);
  140. flipper_format_write_int32(
  141. cfg_fmt, "measurement_resolution", &(app->config->measurement_resolution), 1);
  142. flipper_format_write_int32(cfg_fmt, "lux_only", &(app->config->lux_only), 1);
  143. flipper_format_write_int32(cfg_fmt, "device_addr", &(app->config->device_addr), 1);
  144. flipper_format_write_int32(cfg_fmt, "sensor_type", &(app->config->sensor_type), 1);
  145. }
  146. flipper_format_free(cfg_fmt);
  147. }
  148. void lightmeter_app_i2c_init_sensor(LightMeterApp* context) {
  149. LightMeterApp* app = context;
  150. switch(app->config->sensor_type) {
  151. case SENSOR_BH1750:
  152. bh1750_set_power_state(1);
  153. switch(app->config->device_addr) {
  154. case ADDR_HIGH:
  155. bh1750_init_with_addr(0x5C);
  156. break;
  157. case ADDR_LOW:
  158. bh1750_init_with_addr(0x23);
  159. break;
  160. default:
  161. bh1750_init_with_addr(0x23);
  162. break;
  163. }
  164. bh1750_set_mode(ONETIME_HIGH_RES_MODE);
  165. break;
  166. case SENSOR_MAX44009:
  167. switch(app->config->device_addr) {
  168. case ADDR_HIGH:
  169. max44009_init_with_addr(0x4B);
  170. break;
  171. case ADDR_LOW:
  172. max44009_init_with_addr(0x4A);
  173. break;
  174. default:
  175. max44009_init_with_addr(0x4A);
  176. break;
  177. }
  178. break;
  179. default:
  180. FURI_LOG_E(TAG, "Invalid sensor type %ld", app->config->sensor_type);
  181. return;
  182. }
  183. }
  184. void lightmeter_app_i2c_deinit_sensor(LightMeterApp* context) {
  185. LightMeterApp* app = context;
  186. switch(app->config->sensor_type) {
  187. case SENSOR_BH1750:
  188. bh1750_set_power_state(0);
  189. break;
  190. case SENSOR_MAX44009:
  191. // nothing
  192. break;
  193. default:
  194. FURI_LOG_E(TAG, "Invalid sensor type %ld", app->config->sensor_type);
  195. return;
  196. }
  197. }
  198. void lightmeter_app_i2c_callback(LightMeterApp* context) {
  199. LightMeterApp* app = context;
  200. float EV = 0;
  201. float lux = 0;
  202. bool response = 0;
  203. if(app->config->sensor_type == SENSOR_BH1750) {
  204. if(bh1750_trigger_manual_conversion() == BH1750_OK) {
  205. bh1750_read_light(&lux);
  206. response = 1;
  207. }
  208. } else if(app->config->sensor_type == SENSOR_MAX44009) {
  209. if(max44009_read_light(&lux)) response = 1;
  210. }
  211. if(main_view_get_dome(app->main_view)) lux *= DOME_COEFFICIENT;
  212. EV = lux2ev(lux);
  213. main_view_set_lux(app->main_view, lux);
  214. main_view_set_EV(app->main_view, EV);
  215. main_view_set_response(app->main_view, response);
  216. }
  217. void lightmeter_app_reset_callback(LightMeterApp* context) {
  218. LightMeterApp* app = context;
  219. main_view_reset_lux(app->main_view);
  220. }