uv_meter_scene_settings.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "uv_meter_app_i.hpp"
  2. #include "uv_meter_event.hpp"
  3. static const char* i2c_addresses[] = {
  4. [UVMeterI2CAddressAuto] = "Auto",
  5. [UVMeterI2CAddress74] = "0x74",
  6. [UVMeterI2CAddress75] = "0x75",
  7. [UVMeterI2CAddress76] = "0x76",
  8. [UVMeterI2CAddress77] = "0x77",
  9. };
  10. static const char* units[] = {
  11. [UVMeterUnituW_cm_2] = "uW/cm2",
  12. [UVMeterUnitW_m_2] = "W/m2",
  13. [UVMeterUnitmW_m_2] = "mW/m2",
  14. };
  15. static void i2c_address_change_callback(VariableItem* item) {
  16. UVMeterApp* app = static_cast<UVMeterApp*>(variable_item_get_context(item));
  17. uint8_t index = variable_item_get_current_value_index(item);
  18. variable_item_set_current_value_text(item, i2c_addresses[index]);
  19. app->app_state->i2c_address = static_cast<UVMeterI2CAddress>(index);
  20. }
  21. static void unit_change_callback(VariableItem* item) {
  22. UVMeterApp* app = static_cast<UVMeterApp*>(variable_item_get_context(item));
  23. uint8_t index = variable_item_get_current_value_index(item);
  24. variable_item_set_current_value_text(item, units[index]);
  25. app->app_state->unit = static_cast<UVMeterUnit>(index);
  26. }
  27. static void enter_callback(void* context, uint32_t index) {
  28. auto* app = static_cast<UVMeterApp*>(context);
  29. switch(index) {
  30. // Update indices when adding new setting items
  31. case 2:
  32. scene_manager_handle_custom_event(app->scene_manager, UVMeterCustomEventSceneEnterHelp);
  33. break;
  34. case 3:
  35. scene_manager_handle_custom_event(app->scene_manager, UVMeterCustomEventSceneEnterAbout);
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. void uv_meter_scene_settings_on_enter(void* context) {
  42. auto* app = static_cast<UVMeterApp*>(context);
  43. variable_item_list_reset(app->variable_item_list);
  44. VariableItem* item;
  45. item = variable_item_list_add(
  46. app->variable_item_list,
  47. "I2C Address",
  48. COUNT_OF(i2c_addresses),
  49. i2c_address_change_callback,
  50. app);
  51. variable_item_set_current_value_index(item, app->app_state->i2c_address);
  52. variable_item_set_current_value_text(item, i2c_addresses[app->app_state->i2c_address]);
  53. item = variable_item_list_add(
  54. app->variable_item_list, "Unit", COUNT_OF(units), unit_change_callback, app);
  55. variable_item_set_current_value_index(item, app->app_state->unit);
  56. variable_item_set_current_value_text(item, units[app->app_state->unit]);
  57. // Be aware when adding new items before "Help" to change index in `enter_callback()`
  58. variable_item_list_add(app->variable_item_list, "Help", 0, NULL, NULL);
  59. variable_item_list_add(app->variable_item_list, "About", 0, NULL, NULL);
  60. variable_item_list_set_enter_callback(app->variable_item_list, enter_callback, app);
  61. view_dispatcher_switch_to_view(app->view_dispatcher, UVMeterViewVariableItemList);
  62. }
  63. bool uv_meter_scene_settings_on_event(void* context, SceneManagerEvent event) {
  64. auto* app = static_cast<UVMeterApp*>(context);
  65. bool consumed = false;
  66. switch(event.type) {
  67. case SceneManagerEventTypeCustom:
  68. switch(event.event) {
  69. case UVMeterCustomEventSceneEnterHelp:
  70. scene_manager_next_scene(app->scene_manager, UVMeterSceneHelp);
  71. consumed = true;
  72. break;
  73. case UVMeterCustomEventSceneEnterAbout:
  74. scene_manager_next_scene(app->scene_manager, UVMeterSceneAbout);
  75. consumed = true;
  76. break;
  77. }
  78. break;
  79. default:
  80. break;
  81. }
  82. return consumed;
  83. }
  84. void uv_meter_scene_settings_on_exit(void* context) {
  85. auto* app = static_cast<UVMeterApp*>(context);
  86. variable_item_list_reset(app->variable_item_list);
  87. }