battery_info.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "battery_info.h"
  2. #include <furi.h>
  3. #include <gui/elements.h>
  4. struct BatteryInfo {
  5. View* view;
  6. };
  7. static void draw_stat(Canvas* canvas, int x, int y, const Icon* icon, char* val) {
  8. canvas_draw_frame(canvas, x - 7, y + 7, 30, 13);
  9. canvas_draw_icon(canvas, x, y, icon);
  10. canvas_set_color(canvas, ColorWhite);
  11. canvas_draw_box(canvas, x - 4, y + 16, 24, 6);
  12. canvas_set_color(canvas, ColorBlack);
  13. canvas_draw_str_aligned(canvas, x + 8, y + 22, AlignCenter, AlignBottom, val);
  14. };
  15. static void draw_battery(Canvas* canvas, BatteryInfoModel* data, int x, int y) {
  16. char emote[20] = {};
  17. char header[20] = {};
  18. char value[20] = {};
  19. int32_t drain_current = data->gauge_current * (-1000);
  20. uint32_t charge_current = data->gauge_current * 1000;
  21. // Draw battery
  22. canvas_draw_icon(canvas, x, y, &I_BatteryBody_52x28);
  23. if(charge_current > 0) {
  24. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceCharging_29x14);
  25. } else if(drain_current > 100) {
  26. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceConfused_29x14);
  27. } else if(data->charge < 10) {
  28. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceNopower_29x14);
  29. } else {
  30. canvas_draw_icon(canvas, x + 16, y + 7, &I_FaceNormal_29x14);
  31. }
  32. // Draw bubble
  33. elements_bubble(canvas, 53, 0, 71, 39);
  34. // Set text
  35. if(charge_current > 0) {
  36. snprintf(emote, sizeof(emote), "%s", "Yummy!");
  37. snprintf(header, sizeof(header), "%s", "Charging at");
  38. snprintf(
  39. value,
  40. sizeof(value),
  41. "%ld.%ldV %ldmA",
  42. (uint32_t)(data->vbus_voltage),
  43. (uint32_t)(data->vbus_voltage * 10) % 10,
  44. charge_current);
  45. } else if(drain_current > 0) {
  46. snprintf(emote, sizeof(emote), "%s", drain_current > 100 ? "Oh no!" : "Om-nom-nom!");
  47. snprintf(header, sizeof(header), "%s", "Consumption is");
  48. snprintf(
  49. value, sizeof(value), "%ld %s", drain_current, drain_current > 100 ? "mA!" : "mA");
  50. } else if(charge_current != 0 || drain_current != 0) {
  51. snprintf(header, 20, "...");
  52. } else {
  53. snprintf(header, sizeof(header), "Charged!");
  54. }
  55. canvas_draw_str_aligned(canvas, 92, y + 3, AlignCenter, AlignCenter, emote);
  56. canvas_draw_str_aligned(canvas, 92, y + 15, AlignCenter, AlignCenter, header);
  57. canvas_draw_str_aligned(canvas, 92, y + 27, AlignCenter, AlignCenter, value);
  58. };
  59. static void battery_info_draw_callback(Canvas* canvas, void* context) {
  60. furi_assert(context);
  61. BatteryInfoModel* model = context;
  62. canvas_clear(canvas);
  63. canvas_set_color(canvas, ColorBlack);
  64. draw_battery(canvas, model, 0, 5);
  65. char batt_level[10];
  66. char temperature[10];
  67. char voltage[10];
  68. char health[10];
  69. snprintf(batt_level, sizeof(batt_level), "%ld%%", (uint32_t)model->charge);
  70. snprintf(temperature, sizeof(temperature), "%ld C", (uint32_t)model->gauge_temperature);
  71. snprintf(
  72. voltage,
  73. sizeof(voltage),
  74. "%ld.%01ld V",
  75. (uint32_t)model->gauge_voltage,
  76. (uint32_t)(model->gauge_voltage * 10) % 10);
  77. snprintf(health, sizeof(health), "%d%%", model->health);
  78. draw_stat(canvas, 8, 42, &I_Battery_16x16, batt_level);
  79. draw_stat(canvas, 40, 42, &I_Temperature_16x16, temperature);
  80. draw_stat(canvas, 72, 42, &I_Voltage_16x16, voltage);
  81. draw_stat(canvas, 104, 42, &I_Health_16x16, health);
  82. }
  83. BatteryInfo* battery_info_alloc() {
  84. BatteryInfo* battery_info = malloc(sizeof(BatteryInfo));
  85. battery_info->view = view_alloc();
  86. view_set_context(battery_info->view, battery_info);
  87. view_allocate_model(battery_info->view, ViewModelTypeLocking, sizeof(BatteryInfoModel));
  88. view_set_draw_callback(battery_info->view, battery_info_draw_callback);
  89. return battery_info;
  90. }
  91. void battery_info_free(BatteryInfo* battery_info) {
  92. furi_assert(battery_info);
  93. view_free(battery_info->view);
  94. free(battery_info);
  95. }
  96. View* battery_info_get_view(BatteryInfo* battery_info) {
  97. furi_assert(battery_info);
  98. return battery_info->view;
  99. }
  100. void battery_info_set_data(BatteryInfo* battery_info, BatteryInfoModel* data) {
  101. furi_assert(battery_info);
  102. furi_assert(data);
  103. with_view_model(
  104. battery_info->view,
  105. BatteryInfoModel * model,
  106. { memcpy(model, data, sizeof(BatteryInfoModel)); },
  107. true);
  108. }