battery_info.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "battery_info.h"
  2. #include <furi.h>
  3. #include <gui/elements.h>
  4. #include <assets_icons.h>
  5. #include <locale/locale.h>
  6. #define LOW_CHARGE_THRESHOLD (10)
  7. #define HIGH_DRAIN_CURRENT_THRESHOLD (-100)
  8. struct BatteryInfo {
  9. View* view;
  10. };
  11. static void draw_stat(Canvas* canvas, int x, int y, const Icon* icon, char* val) {
  12. canvas_draw_frame(canvas, x - 7, y + 7, 30, 13);
  13. canvas_draw_icon(canvas, x, y, icon);
  14. canvas_set_color(canvas, ColorWhite);
  15. canvas_draw_box(canvas, x - 4, y + 16, 24, 6);
  16. canvas_set_color(canvas, ColorBlack);
  17. canvas_draw_str_aligned(canvas, x + 8, y + 22, AlignCenter, AlignBottom, val);
  18. };
  19. static void draw_battery(Canvas* canvas, BatteryInfoModel* data, int x, int y) {
  20. char emote[20] = {};
  21. char header[20] = {};
  22. char value[20] = {};
  23. int32_t current = 1000.0f * data->gauge_current;
  24. // Draw battery
  25. canvas_draw_icon(canvas, x, y, &I_BatteryBody_52x28);
  26. if(current > 0) {
  27. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceCharging_29x14);
  28. } else if(current < HIGH_DRAIN_CURRENT_THRESHOLD) {
  29. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceConfused_29x14);
  30. } else if(data->charge < LOW_CHARGE_THRESHOLD) {
  31. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceNopower_29x14);
  32. } else {
  33. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceNormal_29x14);
  34. }
  35. // Draw bubble
  36. elements_bubble(canvas, 53, 0, 71, 39);
  37. // Set text
  38. if(current > 0) {
  39. snprintf(emote, sizeof(emote), "%s", "Yummy!");
  40. snprintf(header, sizeof(header), "%s", "Charging at");
  41. snprintf(
  42. value,
  43. sizeof(value),
  44. "%lu.%luV %lumA",
  45. (uint32_t)(data->vbus_voltage),
  46. (uint32_t)(data->vbus_voltage * 10) % 10,
  47. current);
  48. } else if(current < 0) {
  49. snprintf(
  50. emote,
  51. sizeof(emote),
  52. "%s",
  53. current < HIGH_DRAIN_CURRENT_THRESHOLD ? "Oh no!" : "Om-nom-nom!");
  54. snprintf(header, sizeof(header), "%s", "Consumption is");
  55. snprintf(
  56. value,
  57. sizeof(value),
  58. "%ld %s",
  59. ABS(current),
  60. current < HIGH_DRAIN_CURRENT_THRESHOLD ? "mA!" : "mA");
  61. } else if(data->vbus_voltage > 0) {
  62. if(data->charge_voltage_limit < 4.2) {
  63. // Non-default battery charging limit, mention it
  64. snprintf(emote, sizeof(emote), "Charged!");
  65. snprintf(header, sizeof(header), "Limited to");
  66. snprintf(
  67. value,
  68. sizeof(value),
  69. "%lu.%luV",
  70. (uint32_t)(data->charge_voltage_limit),
  71. (uint32_t)(data->charge_voltage_limit * 10) % 10);
  72. } else {
  73. snprintf(header, sizeof(header), "Charged!");
  74. }
  75. } else {
  76. snprintf(header, sizeof(header), "Napping...");
  77. }
  78. canvas_draw_str_aligned(canvas, 92, y + 3, AlignCenter, AlignCenter, emote);
  79. canvas_draw_str_aligned(canvas, 92, y + 15, AlignCenter, AlignCenter, header);
  80. canvas_draw_str_aligned(canvas, 92, y + 27, AlignCenter, AlignCenter, value);
  81. };
  82. static void battery_info_draw_callback(Canvas* canvas, void* context) {
  83. furi_assert(context);
  84. BatteryInfoModel* model = context;
  85. canvas_clear(canvas);
  86. canvas_set_color(canvas, ColorBlack);
  87. draw_battery(canvas, model, 0, 5);
  88. char batt_level[10];
  89. char temperature[10];
  90. char voltage[10];
  91. char health[10];
  92. snprintf(batt_level, sizeof(batt_level), "%lu%%", (uint32_t)model->charge);
  93. if(locale_get_measurement_unit() == LocaleMeasurementUnitsMetric) {
  94. snprintf(temperature, sizeof(temperature), "%lu C", (uint32_t)model->gauge_temperature);
  95. } else {
  96. snprintf(
  97. temperature,
  98. sizeof(temperature),
  99. "%lu F",
  100. (uint32_t)locale_celsius_to_fahrenheit(model->gauge_temperature));
  101. }
  102. snprintf(
  103. voltage,
  104. sizeof(voltage),
  105. "%lu.%01lu V",
  106. (uint32_t)model->gauge_voltage,
  107. (uint32_t)(model->gauge_voltage * 10) % 10UL);
  108. snprintf(health, sizeof(health), "%d%%", model->health);
  109. draw_stat(canvas, 8, 42, &I_Battery_16x16, batt_level);
  110. draw_stat(canvas, 40, 42, &I_Temperature_16x16, temperature);
  111. draw_stat(canvas, 72, 42, &I_Voltage_16x16, voltage);
  112. draw_stat(canvas, 104, 42, &I_Health_16x16, health);
  113. }
  114. BatteryInfo* battery_info_alloc() {
  115. BatteryInfo* battery_info = malloc(sizeof(BatteryInfo));
  116. battery_info->view = view_alloc();
  117. view_set_context(battery_info->view, battery_info);
  118. view_allocate_model(battery_info->view, ViewModelTypeLocking, sizeof(BatteryInfoModel));
  119. view_set_draw_callback(battery_info->view, battery_info_draw_callback);
  120. return battery_info;
  121. }
  122. void battery_info_free(BatteryInfo* battery_info) {
  123. furi_assert(battery_info);
  124. view_free(battery_info->view);
  125. free(battery_info);
  126. }
  127. View* battery_info_get_view(BatteryInfo* battery_info) {
  128. furi_assert(battery_info);
  129. return battery_info->view;
  130. }
  131. void battery_info_set_data(BatteryInfo* battery_info, BatteryInfoModel* data) {
  132. furi_assert(battery_info);
  133. furi_assert(data);
  134. with_view_model(
  135. battery_info->view,
  136. BatteryInfoModel * model,
  137. { memcpy(model, data, sizeof(BatteryInfoModel)); },
  138. true);
  139. }