lightmeter.c 9.0 KB

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