embedded.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "Config.h"
  21. #include "messaging.h"
  22. #include "gpio_exp.h"
  23. #include "accessors.h"
  24. static const char TAG[] = "embedded";
  25. static sys_gpio_config * power=NULL;
  26. extern void sb_controls_init(void);
  27. extern bool sb_displayer_init(void);
  28. u8_t custom_player_id = 12;
  29. mutex_type slimp_mutex;
  30. static jmp_buf jumpbuf;
  31. void get_mac(u8_t mac[]) {
  32. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  33. }
  34. _sig_func_ptr signal(int sig, _sig_func_ptr func) {
  35. return NULL;
  36. }
  37. void em_logprint(const char *fmt, ...) {
  38. va_list args;
  39. va_start(args, fmt);
  40. vfprintf(stderr, fmt, args);
  41. vmessaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, fmt, args);
  42. va_end(args);
  43. fflush(stderr);
  44. }
  45. void *audio_calloc(size_t nmemb, size_t size) {
  46. return calloc(nmemb, size);
  47. }
  48. int pthread_create_name(pthread_t *thread, _CONST pthread_attr_t *attr,
  49. void *(*start_routine)( void * ), void *arg, char *name) {
  50. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  51. cfg.thread_name = name;
  52. cfg.inherit_cfg = true;
  53. esp_pthread_set_cfg(&cfg);
  54. return pthread_create(thread, attr, start_routine, arg);
  55. }
  56. uint32_t _gettime_ms_(void) {
  57. return (uint32_t) (esp_timer_get_time() / 1000);
  58. }
  59. int embedded_init(void) {
  60. mutex_create(slimp_mutex);
  61. ESP_LOGI(TAG,"Initializing controls");
  62. sb_controls_init();
  63. ESP_LOGI(TAG,"Initializing displayer");
  64. custom_player_id = sb_displayer_init() ? 100 : 101;
  65. if(SYS_GPIOS_NAME(power,power) && power->pin >= 0){
  66. gpio_pad_select_gpio_x(power->pin);
  67. gpio_set_direction_x(power->pin, GPIO_MODE_OUTPUT);
  68. gpio_set_level_x(power->pin, !power->level);
  69. ESP_LOGI(TAG, "setting power GPIO %d (active:%d)", power->pin, power->level);
  70. }
  71. return setjmp(jumpbuf);
  72. }
  73. void embedded_exit(int code) {
  74. longjmp(jumpbuf, code + 1);
  75. }
  76. void powering(bool on) {
  77. if (SYS_GPIOS_NAME(power,power) && power->pin != -1) {
  78. ESP_LOGI(TAG, "powering player %s", on ? "ON" : "OFF");
  79. gpio_set_level_x(power->pin, on ? power->level : !power->level);
  80. }
  81. }
  82. u16_t get_RSSI(void) {
  83. wifi_ap_record_t wifidata;
  84. esp_wifi_sta_get_ap_info(&wifidata);
  85. // we'll assume dBm, -30 to -100
  86. if (wifidata.primary != 0) return 100 + wifidata.rssi + 30;
  87. else return 0xffff;
  88. }
  89. u16_t get_plugged(void) {
  90. return jack_inserted_svc() ? PLUG_HEADPHONE : 0;
  91. }
  92. u16_t get_battery(void) {
  93. return (u16_t) (battery_value_svc() * 128) & 0x0fff;
  94. }
  95. void set_name(char *name) {
  96. strncpy(platform->names.squeezelite,name,sizeof(platform->names.squeezelite));
  97. }