forked from MrBuddyCasino/ESP32_Alexa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_main.c
193 lines (144 loc) · 4.63 KB
/
app_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "http.h"
#include "driver/i2s.h"
#include "ui.h"
#include "spiram_fifo.h"
#include "audio_renderer.h"
#include "audio_recorder.h"
#include "web_radio.h"
#include "playerconfig.h"
#include "wifi.h"
#include "app_main.h"
#include "alexa_public.h"
#ifdef CONFIG_BT_SPEAKER_MODE
#include "bt_speaker.h"
#endif
#define WIFI_LIST_NUM 10
#define TAG "main"
//Priorities of the reader and the decoder thread. bigger number = higher prio
#define PRIO_READER configMAX_PRIORITIES -3
#define PRIO_MQTT configMAX_PRIORITIES - 3
#define PRIO_CONNECT configMAX_PRIORITIES -1
static void alexa_task(void *pvParameters)
{
alexa_init();
ESP_LOGI(TAG, "alexa_task stack: %d\n", uxTaskGetStackHighWaterMark(NULL));
// controls_init();
vTaskDelete(NULL);
}
static void init_hardware()
{
nvs_flash_init();
// init UI
// ui_init(GPIO_NUM_32);
//Initialize the SPI RAM chip communications and see if it actually retains some bytes. If it
//doesn't, warn user.
if (!spiRamFifoInit()) {
printf("\n\nSPI RAM chip fail!\n");
while(1);
}
ESP_LOGI(TAG, "hardware initialized");
}
static void start_wifi()
{
ESP_LOGI(TAG, "starting network");
/* FreeRTOS event group to signal when we are connected & ready to make a request */
EventGroupHandle_t wifi_event_group = xEventGroupCreate();
/* init wifi */
initialise_wifi(wifi_event_group);
/* Wait for the callback to set the CONNECTED_BIT in the event group. */
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
false, true, portMAX_DELAY);
}
static renderer_config_t *create_renderer_config()
{
renderer_config_t *renderer_config = calloc(1, sizeof(renderer_config_t));
renderer_config->bit_depth = I2S_BITS_PER_SAMPLE_16BIT;
renderer_config->i2s_num = I2S_NUM_0;
renderer_config->sample_rate = 44100;
renderer_config->sample_rate_modifier = 0.52;
renderer_config->output_mode = AUDIO_OUTPUT_MODE;
if(renderer_config->output_mode == I2S_MERUS) {
renderer_config->bit_depth = I2S_BITS_PER_SAMPLE_32BIT;
}
if(renderer_config->output_mode == DAC_BUILT_IN) {
renderer_config->bit_depth = I2S_BITS_PER_SAMPLE_16BIT;
}
return renderer_config;
}
static void start_web_radio()
{
// init web radio
web_radio_t *radio_config = calloc(1, sizeof(web_radio_t));
radio_config->url = PLAY_URL;
// init player config
radio_config->player_config = calloc(1, sizeof(player_t));
radio_config->player_config->command = CMD_NONE;
radio_config->player_config->decoder_status = UNINITIALIZED;
radio_config->player_config->decoder_command = CMD_NONE;
radio_config->player_config->buffer_pref = BUF_PREF_SAFE;
radio_config->player_config->media_stream = calloc(1, sizeof(media_stream_t));
// init renderer
renderer_init(create_renderer_config());
// start radio
web_radio_init(radio_config);
web_radio_start(radio_config);
}
#include "common_buffer.h"
#include "url_parser.h"
#include "nghttp2/nghttp2.h"
#include "nghttp2_client.h"
#include "asio.h"
#include "asio_http.h"
#include "asio_http2.h"
static void signal_strength()
{
start_wifi();
wifi_ap_record_t ap_info;
while(1) {
esp_wifi_sta_get_ap_info(&ap_info);
printf("rssi: %" PRIi8 "\n", ap_info.rssi);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
/**
* entry point
*/
void app_main()
{
ESP_LOGI(TAG, "starting app_main()");
ESP_LOGW(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());
//signal_strength();
/* print MAC */
uint8_t sta_mac[6];
esp_efuse_mac_get_default(sta_mac);
ESP_LOGE(TAG, "MAC address: " MACSTR, MAC2STR(sta_mac));
init_hardware();
#ifdef CONFIG_BT_SPEAKER_MODE
bt_speaker_start(create_renderer_config());
#else
/*
ESP_LOGW(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());
//start_web_radio();
// can't mix cores when allocating interrupts
ESP_LOGW(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());
*/
renderer_init(create_renderer_config());
audio_recorder_init();
xTaskCreatePinnedToCore(&alexa_task, "alexa_task", 16384, NULL, 1, NULL, 1);
#endif
ESP_LOGW(TAG, "%d: - RAM left %d", __LINE__, esp_get_free_heap_size());
// ESP_LOGI(TAG, "app_main stack: %d\n", uxTaskGetStackHighWaterMark(NULL));
vTaskDelete(NULL);
}