eth_view_process.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "eth_view_process.h"
  2. #include "eth_worker.h"
  3. #include "eth_worker_i.h"
  4. #include <furi_hal.h>
  5. #include <gui/gui.h>
  6. #include <gui/canvas.h>
  7. #include <string.h>
  8. #include "u8g2.h"
  9. #define TAG "EthView"
  10. EthViewProcess* ethernet_view_process_malloc() {
  11. EthViewProcess* evp = malloc(sizeof(EthViewProcess));
  12. evp->autofill = 1;
  13. evp->carriage = 0;
  14. evp->position = 0;
  15. evp->x = 27;
  16. evp->y = 6;
  17. return evp;
  18. }
  19. void ethernet_view_process_free(EthViewProcess* evp) {
  20. free(evp);
  21. }
  22. void ethernet_view_process_draw(EthViewProcess* process, Canvas* canvas) {
  23. furi_assert(canvas);
  24. furi_assert(process);
  25. canvas_set_font(canvas, FontSecondary);
  26. const uint8_t x = process->x;
  27. const uint8_t y = process->y;
  28. const uint8_t str_height = 11;
  29. const uint8_t str_count = (64 - y) / str_height;
  30. uint8_t carriage = process->carriage;
  31. uint8_t position = process->position;
  32. if(process->autofill) {
  33. position = (carriage + SCREEN_STRINGS_COUNT - str_count) % SCREEN_STRINGS_COUNT;
  34. process->position = position;
  35. }
  36. for(uint8_t i = 0; i < str_count; ++i) {
  37. uint8_t y1 = y + (i + 1) * str_height;
  38. canvas_draw_str(canvas, x, y1, process->fifo[(position + i) % SCREEN_STRINGS_COUNT]);
  39. }
  40. }
  41. void ethernet_view_process_move(EthViewProcess* process, int8_t shift) {
  42. furi_assert(process);
  43. uint8_t position = process->position;
  44. if(shift <= -SCREEN_STRINGS_COUNT) {
  45. position = 0;
  46. } else if(shift >= SCREEN_STRINGS_COUNT) {
  47. position = process->carriage - 1;
  48. } else {
  49. position = (position + (SCREEN_STRINGS_COUNT + shift)) % SCREEN_STRINGS_COUNT;
  50. }
  51. process->position = position;
  52. process->autofill = !shift;
  53. }
  54. void ethernet_view_process_autofill(EthViewProcess* process, uint8_t state) {
  55. furi_assert(process);
  56. process->autofill = state;
  57. }
  58. static uint16_t get_string_with_width(const char* str, uint16_t width) {
  59. u8g2_t canvas_memory;
  60. Canvas* canvas = &canvas_memory; // grazniy hack
  61. canvas_set_font(canvas, FontSecondary);
  62. uint8_t end = 0;
  63. char copy[SCREEN_SYMBOLS_WIDTH + 1] = {0};
  64. for(;;) {
  65. if(str[end] == '\0') {
  66. break;
  67. }
  68. if(end == SCREEN_SYMBOLS_WIDTH) {
  69. break;
  70. }
  71. copy[end] = str[end];
  72. if(canvas_string_width(canvas, copy) > width) {
  73. end -= 1;
  74. break;
  75. }
  76. end += 1;
  77. }
  78. return end;
  79. }
  80. void ethernet_view_process_print(EthViewProcess* process, const char* str) {
  81. furi_assert(process);
  82. uint16_t max_width = 126 - process->x;
  83. uint16_t ptr = 0;
  84. uint16_t len = strlen(str);
  85. while(ptr < len) {
  86. uint16_t start = ptr;
  87. ptr += get_string_with_width(str + ptr, max_width);
  88. uint8_t carriage = process->carriage;
  89. uint8_t carriage1 = (carriage + 1) % SCREEN_STRINGS_COUNT;
  90. uint8_t carriage2 = (carriage + 2) % SCREEN_STRINGS_COUNT;
  91. FURI_LOG_I(TAG, "print %d %d %d %d %d", max_width, len, start, carriage, carriage1);
  92. memset(process->fifo[carriage], 0, SCREEN_SYMBOLS_WIDTH);
  93. memset(process->fifo[carriage1], 0, SCREEN_SYMBOLS_WIDTH);
  94. memset(process->fifo[carriage2], 0, SCREEN_SYMBOLS_WIDTH);
  95. memcpy(process->fifo[carriage], str + start, ptr - start);
  96. process->carriage = carriage1;
  97. }
  98. }