display.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <stdint.h>
  22. #include <arpa/inet.h>
  23. #include "esp_log.h"
  24. #include "config.h"
  25. #include "embedded.h"
  26. #include "display.h"
  27. static const char *TAG = "display";
  28. static bool (*slimp_handler_chain)(u8_t *data, int len);
  29. static struct display_handle_s *handle;
  30. static void (*chained_notify)(in_addr_t ip, u16_t hport, u16_t cport);
  31. static void server_attach(in_addr_t ip, u16_t hport, u16_t cport);
  32. static bool display_handler(u8_t *data, int len);
  33. /* scrolling undocumented information
  34. grfs
  35. B: screen number
  36. B:1 = left, 2 = right,
  37. Q: scroll pause once done (ms)
  38. Q: scroll speed (ms)
  39. W: # of pixels to scroll each time
  40. W: 0 = continue scrolling after pause, 1 = scroll to scrollend and then stop, 2 = scroll to scrollend and then end animation (causing new update)
  41. W: width of total scroll area in pixels
  42. grfd
  43. W: screen number
  44. W: width of scrollable area in pixels
  45. ANIC flags
  46. ANIM_TRANSITION 0x01 # A transition animation has finished
  47. ANIM_SCROLL_ONCE 0x02 # A scrollonce has finished
  48. ANIM_SCREEN_1 0x04 # For scrollonce only, screen 1 was scrolling
  49. ANIM_SCREEN_2 0x08 # For scrollonce only, screen 2 was scrolling
  50. */
  51. /****************************************************************************************
  52. *
  53. */
  54. void display_init(char *welcome) {
  55. char *item = config_alloc_get(NVS_TYPE_STR, "display_config");
  56. if (item && *item) {
  57. char * drivername=strstr(item,"driver");
  58. if( !drivername || (drivername && (strstr(drivername,"SSD1306") || strstr(drivername,"ssd1306")))){
  59. handle = &SSD1306_handle;
  60. if (handle->init(item, welcome)) {
  61. slimp_handler_chain = slimp_handler;
  62. slimp_handler = display_handler;
  63. ESP_LOGI(TAG, "Display initialization successful");
  64. } else {
  65. ESP_LOGE(TAG, "Display initialization failed");
  66. }
  67. }else {
  68. ESP_LOGE(TAG,"Unknown display driver name in display config: %s",item);
  69. }
  70. } else {
  71. ESP_LOGW(TAG, "no display");
  72. }
  73. chained_notify = server_notify;
  74. server_notify = server_attach;
  75. if (item) free(item);
  76. }
  77. /****************************************************************************************
  78. *
  79. */
  80. static void server_attach(in_addr_t ip, u16_t hport, u16_t cport) {
  81. char msg[32];
  82. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  83. handle->print_message(msg);
  84. if (chained_notify) (*chained_notify)(ip, hport, cport);
  85. }
  86. /****************************************************************************************
  87. * Process graphic display data
  88. */
  89. static bool display_handler(u8_t *data, int len){
  90. bool res = true;
  91. if (!strncmp((char*) data, "vfdc", 4)) {
  92. handle->vfdc_handler(data, len);
  93. } else if (!strncmp((char*) data, "grfe", 4)) {
  94. handle->grfe_handler(data, len);
  95. } else {
  96. res = false;
  97. }
  98. // chain protocol handlers (bitwise or is fine)
  99. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  100. return res;
  101. }