eth_view_process.c 2.7 KB

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