embedded.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "squeezelite.h"
  12. #include "pthread.h"
  13. #include "esp_pthread.h"
  14. #include "esp_system.h"
  15. #include "esp_timer.h"
  16. #include "esp_wifi.h"
  17. #include "monitor.h"
  18. #include "platform_config.h"
  19. mutex_type slimp_mutex;
  20. void get_mac(u8_t mac[]) {
  21. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  22. }
  23. _sig_func_ptr signal(int sig, _sig_func_ptr func) {
  24. return NULL;
  25. }
  26. void *audio_calloc(size_t nmemb, size_t size) {
  27. return calloc(nmemb, size);
  28. }
  29. int pthread_create_name(pthread_t *thread, _CONST pthread_attr_t *attr,
  30. void *(*start_routine)( void * ), void *arg, char *name) {
  31. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  32. cfg.thread_name = name;
  33. cfg.inherit_cfg = true;
  34. esp_pthread_set_cfg(&cfg);
  35. return pthread_create(thread, attr, start_routine, arg);
  36. }
  37. uint32_t _gettime_ms_(void) {
  38. return (uint32_t) (esp_timer_get_time() / 1000);
  39. }
  40. extern void sb_controls_init(void);
  41. extern bool sb_displayer_init(void);
  42. u8_t custom_player_id = 12;
  43. void embedded_init(void) {
  44. mutex_create(slimp_mutex);
  45. sb_controls_init();
  46. custom_player_id = sb_displayer_init() ? 100 : 101;
  47. }
  48. u16_t get_RSSI(void) {
  49. wifi_ap_record_t wifidata;
  50. esp_wifi_sta_get_ap_info(&wifidata);
  51. // we'll assume dBm, -30 to -100
  52. if (wifidata.primary != 0) return 100 + wifidata.rssi + 30;
  53. else return 0xffff;
  54. }
  55. u16_t get_plugged(void) {
  56. return jack_inserted_svc() ? PLUG_HEADPHONE : 0;
  57. }
  58. u16_t get_battery(void) {
  59. return (u16_t) (battery_value_svc() * 128) & 0x0fff;
  60. }
  61. void set_name(char *name) {
  62. char *cmd = config_alloc_get(NVS_TYPE_STR, "autoexec1");
  63. char *p, *q;
  64. if (!cmd) return;
  65. if ((p = strstr(cmd, " -n")) != NULL) {
  66. q = p + 3;
  67. // in case some smart dude has a " -" in player's name
  68. while ((q = strstr(q, " -")) != NULL) {
  69. if (!strchr(q, '"') || !strchr(q+1, '"')) break;
  70. q++;
  71. }
  72. if (q) memmove(p, q, strlen(q) + 1);
  73. else *p = '\0';
  74. }
  75. asprintf(&q, "%s -n \"%s\"", cmd, name);
  76. config_set_value(NVS_TYPE_STR, "autoexec1", q);
  77. free(q);
  78. free(cmd);
  79. }