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_set_event_callback_context(app->view_dispatcher, app);
  56. view_dispatcher_set_custom_event_callback(
  57. app->view_dispatcher, lightmeter_custom_event_callback);
  58. view_dispatcher_set_navigation_event_callback(
  59. app->view_dispatcher, lightmeter_back_event_callback);
  60. view_dispatcher_set_tick_event_callback(
  61. app->view_dispatcher, lightmeter_tick_event_callback, furi_ms_to_ticks(200));
  62. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  63. // Views
  64. app->main_view = main_view_alloc();
  65. view_dispatcher_add_view(
  66. app->view_dispatcher, LightMeterAppViewMainView, main_view_get_view(app->main_view));
  67. // Set default values to main view from config
  68. main_view_set_iso(app->main_view, app->config->iso);
  69. main_view_set_nd(app->main_view, app->config->nd);
  70. main_view_set_aperture(app->main_view, app->config->aperture);
  71. main_view_set_speed(app->main_view, DEFAULT_SPEED);
  72. main_view_set_dome(app->main_view, app->config->dome);
  73. // Variable item list
  74. app->var_item_list = variable_item_list_alloc();
  75. view_dispatcher_add_view(
  76. app->view_dispatcher,
  77. LightMeterAppViewVarItemList,
  78. variable_item_list_get_view(app->var_item_list));
  79. // Widget
  80. app->widget = widget_alloc();
  81. view_dispatcher_add_view(
  82. app->view_dispatcher, LightMeterAppViewAbout, widget_get_view(app->widget));
  83. view_dispatcher_add_view(
  84. app->view_dispatcher, LightMeterAppViewHelp, widget_get_view(app->widget));
  85. // Set first scene
  86. scene_manager_next_scene(app->scene_manager, first_scene);
  87. return app;
  88. }
  89. void lightmeter_app_free(LightMeterApp* app) {
  90. furi_assert(app);
  91. // Views
  92. view_dispatcher_remove_view(app->view_dispatcher, LightMeterAppViewMainView);
  93. main_view_free(app->main_view);
  94. // Variable item list
  95. view_dispatcher_remove_view(app->view_dispatcher, LightMeterAppViewVarItemList);
  96. variable_item_list_free(app->var_item_list);
  97. // Widget
  98. view_dispatcher_remove_view(app->view_dispatcher, LightMeterAppViewAbout);
  99. view_dispatcher_remove_view(app->view_dispatcher, LightMeterAppViewHelp);
  100. widget_free(app->widget);
  101. // View dispatcher
  102. scene_manager_free(app->scene_manager);
  103. view_dispatcher_free(app->view_dispatcher);
  104. // Records
  105. furi_record_close(RECORD_GUI);
  106. if(app->config->backlight != BACKLIGHT_AUTO) {
  107. notification_message(
  108. app->notifications,
  109. &sequence_display_backlight_enforce_auto); // set backlight back to auto
  110. }
  111. furi_record_close(RECORD_STORAGE);
  112. furi_record_close(RECORD_NOTIFICATION);
  113. bh1750_set_power_state(0);
  114. // Free cfg path string
  115. furi_string_free(app->cfg_path);
  116. free(app->config);
  117. free(app);
  118. }
  119. int32_t lightmeter_app(void* p) {
  120. UNUSED(p);
  121. uint32_t first_scene = LightMeterAppSceneMain;
  122. LightMeterApp* app = lightmeter_app_alloc(first_scene);
  123. view_dispatcher_run(app->view_dispatcher);
  124. lightmeter_app_free(app);
  125. return 0;
  126. }
  127. void lightmeter_app_set_config(LightMeterApp* context, LightMeterConfig* config) {
  128. LightMeterApp* app = context;
  129. app->config = config;
  130. storage_common_mkdir(app->storage, APP_PATH_DIR);
  131. FlipperFormat* cfg_fmt = flipper_format_file_alloc(app->storage);
  132. if(flipper_format_file_open_always(cfg_fmt, furi_string_get_cstr(app->cfg_path))) {
  133. flipper_format_write_header_cstr(cfg_fmt, "lightmeter", 1);
  134. flipper_format_write_int32(cfg_fmt, "iso", &(app->config->iso), 1);
  135. flipper_format_write_int32(cfg_fmt, "nd", &(app->config->nd), 1);
  136. flipper_format_write_int32(cfg_fmt, "aperture", &(app->config->aperture), 1);
  137. flipper_format_write_int32(cfg_fmt, "dome", &(app->config->dome), 1);
  138. flipper_format_write_int32(cfg_fmt, "backlight", &(app->config->backlight), 1);
  139. flipper_format_write_int32(
  140. cfg_fmt, "measurement_resolution", &(app->config->measurement_resolution), 1);
  141. flipper_format_write_int32(cfg_fmt, "lux_only", &(app->config->lux_only), 1);
  142. flipper_format_write_int32(cfg_fmt, "device_addr", &(app->config->device_addr), 1);
  143. flipper_format_write_int32(cfg_fmt, "sensor_type", &(app->config->sensor_type), 1);
  144. }
  145. flipper_format_free(cfg_fmt);
  146. }
  147. void lightmeter_app_i2c_init_sensor(LightMeterApp* context) {
  148. LightMeterApp* app = context;
  149. switch(app->config->sensor_type) {
  150. case SENSOR_BH1750:
  151. bh1750_set_power_state(1);
  152. switch(app->config->device_addr) {
  153. case ADDR_HIGH:
  154. bh1750_init_with_addr(0x5C);
  155. break;
  156. case ADDR_LOW:
  157. bh1750_init_with_addr(0x23);
  158. break;
  159. default:
  160. bh1750_init_with_addr(0x23);
  161. break;
  162. }
  163. bh1750_set_mode(ONETIME_HIGH_RES_MODE);
  164. break;
  165. case SENSOR_MAX44009:
  166. switch(app->config->device_addr) {
  167. case ADDR_HIGH:
  168. max44009_init_with_addr(0x4B);
  169. break;
  170. case ADDR_LOW:
  171. max44009_init_with_addr(0x4A);
  172. break;
  173. default:
  174. max44009_init_with_addr(0x4A);
  175. break;
  176. }
  177. break;
  178. default:
  179. FURI_LOG_E(TAG, "Invalid sensor type %ld", app->config->sensor_type);
  180. return;
  181. }
  182. }
  183. void lightmeter_app_i2c_deinit_sensor(LightMeterApp* context) {
  184. LightMeterApp* app = context;
  185. switch(app->config->sensor_type) {
  186. case SENSOR_BH1750:
  187. bh1750_set_power_state(0);
  188. break;
  189. case SENSOR_MAX44009:
  190. // nothing
  191. break;
  192. default:
  193. FURI_LOG_E(TAG, "Invalid sensor type %ld", app->config->sensor_type);
  194. return;
  195. }
  196. }
  197. void lightmeter_app_i2c_callback(LightMeterApp* context) {
  198. LightMeterApp* app = context;
  199. float EV = 0;
  200. float lux = 0;
  201. bool response = 0;
  202. if(app->config->sensor_type == SENSOR_BH1750) {
  203. if(bh1750_trigger_manual_conversion() == BH1750_OK) {
  204. bh1750_read_light(&lux);
  205. response = 1;
  206. }
  207. } else if(app->config->sensor_type == SENSOR_MAX44009) {
  208. if(max44009_read_light(&lux)) response = 1;
  209. }
  210. if(main_view_get_dome(app->main_view)) lux *= DOME_COEFFICIENT;
  211. EV = lux2ev(lux);
  212. main_view_set_lux(app->main_view, lux);
  213. main_view_set_EV(app->main_view, EV);
  214. main_view_set_response(app->main_view, response);
  215. }
  216. void lightmeter_app_reset_callback(LightMeterApp* context) {
  217. LightMeterApp* app = context;
  218. main_view_reset_lux(app->main_view);
  219. }