embedded.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include <setjmp.h>
  12. #include "squeezelite.h"
  13. #include "pthread.h"
  14. #include "esp_pthread.h"
  15. #include "esp_system.h"
  16. #include "esp_timer.h"
  17. #include "esp_wifi.h"
  18. #include "esp_log.h"
  19. #include "monitor.h"
  20. #include "platform_config.h"
  21. #include "messaging.h"
  22. #include "gpio_exp.h"
  23. #include "accessors.h"
  24. #ifndef CONFIG_POWER_GPIO_LEVEL
  25. #define CONFIG_POWER_GPIO_LEVEL 1
  26. #endif
  27. static const char TAG[] = "embedded";
  28. static struct {
  29. int gpio, active;
  30. } power_control = { CONFIG_POWER_GPIO, CONFIG_POWER_GPIO_LEVEL };
  31. extern void sb_controls_init(void);
  32. extern bool sb_displayer_init(void);
  33. u8_t custom_player_id = 12;
  34. mutex_type slimp_mutex;
  35. static jmp_buf jumpbuf;
  36. #ifndef POWER_LOCKED
  37. static void set_power_gpio(int gpio, char *value) {
  38. if (strcasestr(value, "power")) {
  39. char *p = strchr(value, ':');
  40. if (p) power_control.active = atoi(p + 1);
  41. power_control.gpio = gpio;
  42. }
  43. }
  44. #endif
  45. void get_mac(u8_t mac[]) {
  46. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  47. }
  48. _sig_func_ptr signal(int sig, _sig_func_ptr func) {
  49. return NULL;
  50. }
  51. void em_logprint(const char *fmt, ...) {
  52. va_list args;
  53. va_start(args, fmt);
  54. vfprintf(stderr, fmt, args);
  55. vmessaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, fmt, args);
  56. va_end(args);
  57. fflush(stderr);
  58. }
  59. void *audio_calloc(size_t nmemb, size_t size) {
  60. return calloc(nmemb, size);
  61. }
  62. int pthread_create_name(pthread_t *thread, _CONST pthread_attr_t *attr,
  63. void *(*start_routine)( void * ), void *arg, char *name) {
  64. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  65. cfg.thread_name = name;
  66. cfg.inherit_cfg = true;
  67. esp_pthread_set_cfg(&cfg);
  68. return pthread_create(thread, attr, start_routine, arg);
  69. }
  70. uint32_t _gettime_ms_(void) {
  71. return (uint32_t) (esp_timer_get_time() / 1000);
  72. }
  73. int embedded_init(void) {
  74. mutex_create(slimp_mutex);
  75. sb_controls_init();
  76. custom_player_id = sb_displayer_init() ? 100 : 101;
  77. #ifndef POWER_LOCKED
  78. parse_set_GPIO(set_power_gpio);
  79. #endif
  80. if (power_control.gpio != -1) {
  81. gpio_pad_select_gpio_x(power_control.gpio);
  82. gpio_set_direction_x(power_control.gpio, GPIO_MODE_OUTPUT);
  83. gpio_set_level_x(power_control.gpio, !power_control.active);
  84. ESP_LOGI(TAG, "setting power GPIO %d (active:%d)", power_control.gpio, power_control.active);
  85. }
  86. return setjmp(jumpbuf);
  87. }
  88. void embedded_exit(int code) {
  89. longjmp(jumpbuf, code + 1);
  90. }
  91. void powering(bool on) {
  92. if (power_control.gpio != -1) {
  93. ESP_LOGI(TAG, "powering player %s", on ? "ON" : "OFF");
  94. gpio_set_level_x(power_control.gpio, on ? power_control.active : !power_control.active);
  95. }
  96. }
  97. u16_t get_RSSI(void) {
  98. wifi_ap_record_t wifidata;
  99. esp_wifi_sta_get_ap_info(&wifidata);
  100. // we'll assume dBm, -30 to -100
  101. if (wifidata.primary != 0) return 100 + wifidata.rssi + 30;
  102. else return 0xffff;
  103. }
  104. u16_t get_plugged(void) {
  105. return jack_inserted_svc() ? PLUG_HEADPHONE : 0;
  106. }
  107. u16_t get_battery(void) {
  108. return (u16_t) (battery_value_svc() * 128) & 0x0fff;
  109. }
  110. void set_name(char *name) {
  111. char *cmd = config_alloc_get(NVS_TYPE_STR, "autoexec1");
  112. char *p, *q;
  113. if (!cmd) return;
  114. if ((p = strstr(cmd, " -n")) != NULL) {
  115. q = p + 3;
  116. // in case some smart dude has a " -" in player's name
  117. while ((q = strstr(q, " -")) != NULL) {
  118. if (!strchr(q, '"') || !strchr(q+1, '"')) break;
  119. q++;
  120. }
  121. if (q) memmove(p, q, strlen(q) + 1);
  122. else *p = '\0';
  123. }
  124. asprintf(&q, "%s -n \"%s\"", cmd, name);
  125. config_set_value(NVS_TYPE_STR, "autoexec1", q);
  126. free(q);
  127. free(cmd);
  128. }