accessors.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "esp_log.h"
  9. #include "driver/gpio.h"
  10. #include <driver/i2c.h>
  11. #include "config.h"
  12. #include "accessors.h"
  13. #include "globdefs.h"
  14. static const char *TAG = "services";
  15. #define min(a,b) (((a) < (b)) ? (a) : (b))
  16. /****************************************************************************************
  17. *
  18. */
  19. esp_err_t config_i2c_set(const i2c_config_t * config, int port){
  20. int buffer_size=255;
  21. char * config_buffer=calloc(buffer_size,1);
  22. if(config_buffer) {
  23. snprintf(config_buffer,buffer_size,"scl=%u sda=%u speed=%u port=%u",config->scl_io_num,config->sda_io_num,config->master.clk_speed,port);
  24. ESP_LOGI(TAG,"Updating i2c configuration to %s",config_buffer);
  25. config_set_value(NVS_TYPE_STR, "i2c_config", config_buffer);
  26. free(config_buffer);
  27. }
  28. return ESP_OK;
  29. }
  30. /****************************************************************************************
  31. *
  32. */
  33. const i2c_config_t * config_i2c_get(int * i2c_port) {
  34. char *nvs_item, *p;
  35. static i2c_config_t i2c = {
  36. .mode = I2C_MODE_MASTER,
  37. .sda_io_num = -1,
  38. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  39. .scl_io_num = -1,
  40. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  41. .master.clk_speed = 400000,
  42. };
  43. nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
  44. if (nvs_item) {
  45. if ((p = strcasestr(nvs_item, "scl")) != NULL) i2c.scl_io_num = atoi(strchr(p, '=') + 1);
  46. if ((p = strcasestr(nvs_item, "sda")) != NULL) i2c.sda_io_num = atoi(strchr(p, '=') + 1);
  47. if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c.master.clk_speed = atoi(strchr(p, '=') + 1);
  48. if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1);
  49. free(nvs_item);
  50. }
  51. if(i2c_port) *i2c_port=i2c_system_port;
  52. return &i2c;
  53. }
  54. /****************************************************************************************
  55. *
  56. */
  57. char *config_metadata_format(char *artist, char *album, char *title, int *speed, int len) {
  58. char *nvs_item, *string = calloc(len + 1, 1), *p;
  59. nvs_item = config_alloc_get(NVS_TYPE_STR, "metadata_config");
  60. if (nvs_item) {
  61. if ((p = strcasestr(nvs_item, "format")) != NULL) {
  62. char token[16], *q;
  63. int space = len;
  64. bool skip = false;
  65. p = strchr(nvs_item, '=');
  66. while (p++) {
  67. // find token and copy what's after when reaching last one
  68. if (sscanf(p, "%*[^%%]%%%[^%]%%", token) < 0) {
  69. q = strchr(p, ',');
  70. strncat(string, p, q ? min(q - p, space) : space);
  71. break;
  72. }
  73. // copy what's before token (be safe)
  74. if ((q = strchr(p, '%')) == NULL) break;
  75. // skip whatever is after a token if this token is empty
  76. if (!skip) {
  77. strncat(string, p, min(q - p, space));
  78. space = len - strlen(string);
  79. }
  80. // then copy token's content
  81. if (strcasestr(q, "artist") && artist) strncat(string, p = artist, space);
  82. else if (strcasestr(q, "album") && album) strncat(string, p = album, space);
  83. else if (strcasestr(q, "title") && title) strncat(string, p = title, space);
  84. space = len - strlen(string);
  85. // flag to skip the data following an empty field
  86. if (*p) skip = false;
  87. else skip = true;
  88. // advance to next separator
  89. p = strchr(q + 1, '%');
  90. }
  91. } else {
  92. string = strdup(title ? title : "");
  93. }
  94. // get optional scroll speed
  95. if ((p = strcasestr(nvs_item, "speed")) != NULL) sscanf(p, "%*[^=]=%d", speed);
  96. else *speed = 0;
  97. }
  98. return string;
  99. }