lightmeter.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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(app->config);
  115. free(app);
  116. }
  117. int32_t lightmeter_app(void* p) {
  118. UNUSED(p);
  119. uint32_t first_scene = LightMeterAppSceneMain;
  120. LightMeterApp* app = lightmeter_app_alloc(first_scene);
  121. view_dispatcher_run(app->view_dispatcher);
  122. lightmeter_app_free(app);
  123. return 0;
  124. }
  125. void lightmeter_app_set_config(LightMeterApp* context, LightMeterConfig* config) {
  126. LightMeterApp* app = context;
  127. app->config = config;
  128. storage_common_mkdir(app->storage, APP_PATH_DIR);
  129. FlipperFormat* cfg_fmt = flipper_format_file_alloc(app->storage);
  130. if(flipper_format_file_open_always(cfg_fmt, furi_string_get_cstr(app->cfg_path))) {
  131. flipper_format_write_header_cstr(cfg_fmt, "lightmeter", 1);
  132. flipper_format_write_int32(cfg_fmt, "iso", &(app->config->iso), 1);
  133. flipper_format_write_int32(cfg_fmt, "nd", &(app->config->nd), 1);
  134. flipper_format_write_int32(cfg_fmt, "aperture", &(app->config->aperture), 1);
  135. flipper_format_write_int32(cfg_fmt, "dome", &(app->config->dome), 1);
  136. flipper_format_write_int32(cfg_fmt, "backlight", &(app->config->backlight), 1);
  137. flipper_format_write_int32(
  138. cfg_fmt, "measurement_resolution", &(app->config->measurement_resolution), 1);
  139. flipper_format_write_int32(cfg_fmt, "lux_only", &(app->config->lux_only), 1);
  140. flipper_format_write_int32(cfg_fmt, "device_addr", &(app->config->device_addr), 1);
  141. flipper_format_write_int32(cfg_fmt, "sensor_type", &(app->config->sensor_type), 1);
  142. }
  143. flipper_format_free(cfg_fmt);
  144. }
  145. void lightmeter_app_i2c_init_sensor(LightMeterApp* context) {
  146. LightMeterApp* app = context;
  147. switch(app->config->sensor_type) {
  148. case SENSOR_BH1750:
  149. bh1750_set_power_state(1);
  150. switch(app->config->device_addr) {
  151. case ADDR_HIGH:
  152. bh1750_init_with_addr(0x5C);
  153. break;
  154. case ADDR_LOW:
  155. bh1750_init_with_addr(0x23);
  156. break;
  157. default:
  158. bh1750_init_with_addr(0x23);
  159. break;
  160. }
  161. bh1750_set_mode(ONETIME_HIGH_RES_MODE);
  162. break;
  163. case SENSOR_MAX44009:
  164. switch(app->config->device_addr) {
  165. case ADDR_HIGH:
  166. max44009_init_with_addr(0x4B);
  167. break;
  168. case ADDR_LOW:
  169. max44009_init_with_addr(0x4A);
  170. break;
  171. default:
  172. max44009_init_with_addr(0x4A);
  173. break;
  174. }
  175. break;
  176. default:
  177. FURI_LOG_E(TAG, "Invalid sensor type %ld", app->config->sensor_type);
  178. return;
  179. }
  180. }
  181. void lightmeter_app_i2c_deinit_sensor(LightMeterApp* context) {
  182. LightMeterApp* app = context;
  183. switch(app->config->sensor_type) {
  184. case SENSOR_BH1750:
  185. bh1750_set_power_state(0);
  186. break;
  187. case SENSOR_MAX44009:
  188. // nothing
  189. break;
  190. default:
  191. FURI_LOG_E(TAG, "Invalid sensor type %ld", app->config->sensor_type);
  192. return;
  193. }
  194. }
  195. void lightmeter_app_i2c_callback(LightMeterApp* context) {
  196. LightMeterApp* app = context;
  197. float EV = 0;
  198. float lux = 0;
  199. bool response = 0;
  200. if(app->config->sensor_type == SENSOR_BH1750) {
  201. if(bh1750_trigger_manual_conversion() == BH1750_OK) {
  202. bh1750_read_light(&lux);
  203. response = 1;
  204. }
  205. } else if(app->config->sensor_type == SENSOR_MAX44009) {
  206. if(max44009_read_light(&lux)) response = 1;
  207. }
  208. if(main_view_get_dome(app->main_view)) lux *= DOME_COEFFICIENT;
  209. EV = lux2ev(lux, app->config->iso);
  210. main_view_set_lux(app->main_view, lux);
  211. main_view_set_EV(app->main_view, EV);
  212. main_view_set_response(app->main_view, response);
  213. }
  214. void lightmeter_app_reset_callback(LightMeterApp* context) {
  215. LightMeterApp* app = context;
  216. main_view_reset_lux(app->main_view);
  217. }