display.c 2.6 KB

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