display.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /****************************************************************************************
  34. *
  35. */
  36. void display_init(char *welcome) {
  37. char *item = config_alloc_get(NVS_TYPE_STR, "display_config");
  38. if (item && *item) {
  39. char * drivername=strstr(item,"driver");
  40. if( !drivername || (drivername && (strstr(drivername,"SSD1306") || strstr(drivername,"ssd1306")))){
  41. handle = &SSD1306_handle;
  42. if (handle->init(item)) {
  43. slimp_handler_chain = slimp_handler;
  44. slimp_handler = display_handler;
  45. ESP_LOGI(TAG, "Display initialization successful");
  46. } else {
  47. ESP_LOGE(TAG, "Display initialization failed");
  48. }
  49. }else {
  50. ESP_LOGE(TAG,"Unknown display driver name in display config: %s",item);
  51. }
  52. } else {
  53. ESP_LOGW(TAG, "no display");
  54. }
  55. chained_notify = server_notify;
  56. server_notify = server_attach;
  57. if (item) free(item);
  58. }
  59. /****************************************************************************************
  60. *
  61. */
  62. static void server_attach(in_addr_t ip, u16_t hport, u16_t cport) {
  63. char msg[32];
  64. sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
  65. handle->print_message(msg);
  66. if (chained_notify) (*chained_notify)(ip, hport, cport);
  67. }
  68. /****************************************************************************************
  69. * Process graphic display data
  70. */
  71. static bool display_handler(u8_t *data, int len){
  72. bool res = true;
  73. if (!strncmp((char*) data, "vfdc", 4)) {
  74. handle->vfdc_handler(data, len);
  75. } else if (!strncmp((char*) data, "grfe", 4)) {
  76. handle->grfe_handler(data, len);
  77. } else {
  78. res = false;
  79. }
  80. // chain protocol handlers (bitwise or is fine)
  81. if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
  82. return res;
  83. }