eth_view_process.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. void ethernet_view_process_draw(EthViewProcess* process, Canvas* canvas) {
  11. furi_assert(canvas);
  12. furi_assert(process);
  13. canvas_set_font(canvas, FontSecondary);
  14. const uint8_t x = process->x;
  15. const uint8_t y = process->y;
  16. const uint8_t str_height = 11;
  17. const uint8_t str_count = (64 - y) / str_height;
  18. uint8_t carriage = process->carriage;
  19. uint8_t position = process->position;
  20. if(process->autofill) {
  21. position = (carriage + SCREEN_STRINGS_COUNT - str_count) % SCREEN_STRINGS_COUNT;
  22. process->position = position;
  23. }
  24. for(uint8_t i = 0; i < str_count; ++i) {
  25. uint8_t y1 = y + (i + 1) * str_height;
  26. canvas_draw_str(canvas, x, y1, process->fifo[(position + i) % SCREEN_STRINGS_COUNT]);
  27. }
  28. }
  29. void ethernet_view_process_move(EthViewProcess* process, int8_t shift) {
  30. furi_assert(process);
  31. uint8_t position = process->position;
  32. if(shift <= -SCREEN_STRINGS_COUNT) {
  33. position = 0;
  34. } else if(shift >= SCREEN_STRINGS_COUNT) {
  35. position = process->carriage - 1;
  36. } else {
  37. position = (position + (SCREEN_STRINGS_COUNT + shift)) % SCREEN_STRINGS_COUNT;
  38. }
  39. process->position = position;
  40. process->autofill = !shift;
  41. }
  42. void ethernet_view_process_autofill(EthViewProcess* process, uint8_t state) {
  43. furi_assert(process);
  44. process->autofill = state;
  45. }
  46. static uint16_t get_string_with_width(const char* str, uint16_t width) {
  47. u8g2_t canvas_memory;
  48. Canvas* canvas = &canvas_memory; // grazniy hack
  49. canvas_set_font(canvas, FontSecondary);
  50. uint8_t end = 0;
  51. char copy[SCREEN_SYMBOLS_WIDTH + 1] = {0};
  52. for(;;) {
  53. if(str[end] == '\0') {
  54. break;
  55. }
  56. if(end == SCREEN_SYMBOLS_WIDTH) {
  57. break;
  58. }
  59. copy[end] = str[end];
  60. if(canvas_string_width(canvas, copy) > width) {
  61. end -= 1;
  62. break;
  63. }
  64. end += 1;
  65. }
  66. return end;
  67. }
  68. void ethernet_view_process_print(EthViewProcess* process, const char* str) {
  69. furi_assert(process);
  70. uint16_t max_width = 126 - process->x;
  71. uint16_t ptr = 0;
  72. uint16_t len = strlen(str);
  73. while(ptr < len) {
  74. uint16_t start = ptr;
  75. ptr += get_string_with_width(str + ptr, max_width);
  76. uint8_t carriage = process->carriage;
  77. uint8_t carriage1 = (carriage + 1) % SCREEN_STRINGS_COUNT;
  78. uint8_t carriage2 = (carriage + 2) % SCREEN_STRINGS_COUNT;
  79. memset(process->fifo[carriage], 0, SCREEN_SYMBOLS_WIDTH);
  80. memset(process->fifo[carriage1], 0, SCREEN_SYMBOLS_WIDTH);
  81. memset(process->fifo[carriage2], 0, SCREEN_SYMBOLS_WIDTH);
  82. memcpy(process->fifo[carriage], str + start, ptr - start);
  83. process->carriage = carriage1;
  84. }
  85. }