text.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * (c) 2004,2006 Richard Titmuss for SlimProtoLib
  3. * (c) Philippe G. 2019, philippe_44@outlook.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <arpa/inet.h>
  22. #include "esp_log.h"
  23. #define LINELEN 40
  24. #define TAG "display"
  25. //Change special LCD chars to something more printable on screen
  26. unsigned char printable(unsigned char c) {
  27. switch (c) {
  28. case 11: /* block */
  29. return '#';
  30. break;;
  31. case 16: /* rightarrow */
  32. return '>';
  33. break;;
  34. case 22: /* circle */
  35. return '@';
  36. break;;
  37. case 145: /* note */
  38. return ' ';
  39. break;;
  40. case 152: /* bell */
  41. return 'o';
  42. break;
  43. default:
  44. return c;
  45. }
  46. }
  47. // Replace unprintable symbols in line
  48. void makeprintable(unsigned char * line) {
  49. for (int n = 0; n < LINELEN; n++) line[n] = printable(line[n]);
  50. }
  51. // Show the display
  52. void show_display_buffer(char *ddram) {
  53. char line1[LINELEN+1];
  54. char *line2;
  55. memset(line1, 0, LINELEN+1);
  56. strncpy(line1, ddram, LINELEN);
  57. line2 = &(ddram[LINELEN]);
  58. line2[LINELEN] = '\0';
  59. /* Convert special LCD chars */
  60. makeprintable((unsigned char *)line1);
  61. makeprintable((unsigned char *)line2);
  62. ESP_LOGI(TAG, "\n\t%.40s\n\t%.40s", line1, line2);
  63. }
  64. // Check if char is printable, or a valid symbol
  65. bool charisok(unsigned char c) {
  66. switch (c) {
  67. case 11: /* block */
  68. case 16: /* rightarrow */
  69. case 22: /* circle */
  70. case 145: /* note */
  71. case 152: /* bell */
  72. return true;
  73. break;;
  74. default:
  75. return isprint(c);
  76. }
  77. }
  78. // Process display data
  79. void vfd_data( unsigned short *data, int bytes_read) {
  80. unsigned short *display_data;
  81. char ddram[LINELEN * 2];
  82. int n;
  83. int addr = 0; /* counter */
  84. if (bytes_read % 2) bytes_read--; /* even number of bytes */
  85. // if we use Noritake VFD codes, display data starts at 12
  86. display_data = &(data[5]); /* display data starts at byte 10 */
  87. memset(ddram, ' ', LINELEN * 2);
  88. for (n = 0; n < (bytes_read/2); n++) {
  89. unsigned short d; /* data element */
  90. unsigned char t, c;
  91. d = ntohs(display_data[n]);
  92. t = (d & 0x00ff00) >> 8; /* type of display data */
  93. c = (d & 0x0000ff); /* character/command */
  94. switch (t) {
  95. case 0x03: /* character */
  96. if (!charisok(c)) c = ' ';
  97. if (addr <= LINELEN * 2) {
  98. ddram[addr++] = c;
  99. }
  100. break;
  101. case 0x02: /* command */
  102. switch (c) {
  103. case 0x06: /* display clear */
  104. memset(ddram, ' ', LINELEN * 2);
  105. break;
  106. case 0x02: /* cursor home */
  107. addr = 0;
  108. break;
  109. case 0xc0: /* cursor home2 */
  110. addr = LINELEN;
  111. break;
  112. }
  113. }
  114. }
  115. show_display_buffer(ddram);
  116. }