battery_info.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 < -5) {
  49. // Often gauge reports anything in the range 1~5ma as 5ma
  50. // That brings confusion, so we'll treat it as Napping
  51. snprintf(
  52. emote,
  53. sizeof(emote),
  54. "%s",
  55. current < HIGH_DRAIN_CURRENT_THRESHOLD ? "Oh no!" : "Om-nom-nom!");
  56. snprintf(header, sizeof(header), "%s", "Consumption is");
  57. snprintf(
  58. value,
  59. sizeof(value),
  60. "%ld %s",
  61. ABS(current),
  62. current < HIGH_DRAIN_CURRENT_THRESHOLD ? "mA!" : "mA");
  63. } else if(data->vbus_voltage > 0) {
  64. if(data->charge_voltage_limit < 4.2) {
  65. // Non-default battery charging limit, mention it
  66. snprintf(emote, sizeof(emote), "Charged!");
  67. snprintf(header, sizeof(header), "Limited to");
  68. snprintf(
  69. value,
  70. sizeof(value),
  71. "%lu.%luV",
  72. (uint32_t)(data->charge_voltage_limit),
  73. (uint32_t)(data->charge_voltage_limit * 10) % 10);
  74. } else {
  75. snprintf(header, sizeof(header), "Charged!");
  76. }
  77. } else {
  78. snprintf(header, sizeof(header), "Napping...");
  79. }
  80. canvas_draw_str_aligned(canvas, 92, y + 3, AlignCenter, AlignCenter, emote);
  81. canvas_draw_str_aligned(canvas, 92, y + 15, AlignCenter, AlignCenter, header);
  82. canvas_draw_str_aligned(canvas, 92, y + 27, AlignCenter, AlignCenter, value);
  83. };
  84. static void battery_info_draw_callback(Canvas* canvas, void* context) {
  85. furi_assert(context);
  86. BatteryInfoModel* model = context;
  87. canvas_clear(canvas);
  88. canvas_set_color(canvas, ColorBlack);
  89. draw_battery(canvas, model, 0, 5);
  90. char batt_level[10];
  91. char temperature[10];
  92. char voltage[10];
  93. char health[10];
  94. snprintf(batt_level, sizeof(batt_level), "%lu%%", (uint32_t)model->charge);
  95. if(locale_get_measurement_unit() == LocaleMeasurementUnitsMetric) {
  96. snprintf(temperature, sizeof(temperature), "%lu C", (uint32_t)model->gauge_temperature);
  97. } else {
  98. snprintf(
  99. temperature,
  100. sizeof(temperature),
  101. "%lu F",
  102. (uint32_t)locale_celsius_to_fahrenheit(model->gauge_temperature));
  103. }
  104. snprintf(
  105. voltage,
  106. sizeof(voltage),
  107. "%lu.%01lu V",
  108. (uint32_t)model->gauge_voltage,
  109. (uint32_t)(model->gauge_voltage * 10) % 10UL);
  110. snprintf(health, sizeof(health), "%d%%", model->health);
  111. draw_stat(canvas, 8, 42, &I_Battery_16x16, batt_level);
  112. draw_stat(canvas, 40, 42, &I_Temperature_16x16, temperature);
  113. draw_stat(canvas, 72, 42, &I_Voltage_16x16, voltage);
  114. draw_stat(canvas, 104, 42, &I_Health_16x16, health);
  115. }
  116. BatteryInfo* battery_info_alloc() {
  117. BatteryInfo* battery_info = malloc(sizeof(BatteryInfo));
  118. battery_info->view = view_alloc();
  119. view_set_context(battery_info->view, battery_info);
  120. view_allocate_model(battery_info->view, ViewModelTypeLocking, sizeof(BatteryInfoModel));
  121. view_set_draw_callback(battery_info->view, battery_info_draw_callback);
  122. return battery_info;
  123. }
  124. void battery_info_free(BatteryInfo* battery_info) {
  125. furi_assert(battery_info);
  126. view_free(battery_info->view);
  127. free(battery_info);
  128. }
  129. View* battery_info_get_view(BatteryInfo* battery_info) {
  130. furi_assert(battery_info);
  131. return battery_info->view;
  132. }
  133. void battery_info_set_data(BatteryInfo* battery_info, BatteryInfoModel* data) {
  134. furi_assert(battery_info);
  135. furi_assert(data);
  136. with_view_model(
  137. battery_info->view,
  138. BatteryInfoModel * model,
  139. { memcpy(model, data, sizeof(BatteryInfoModel)); },
  140. true);
  141. }