Skip to content

Commit

Permalink
initial preview release of adc sink
Browse files Browse the repository at this point in the history
  • Loading branch information
wizmo2 committed Nov 12, 2023
1 parent bb81d4f commit 43d5df1
Show file tree
Hide file tree
Showing 22 changed files with 1,181 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build-scripts/PureS3-sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ CONFIG_LWIP_SO_REUSE=y
CONFIG_LWIP_SO_REUSE_RXTOALL=y
# CONFIG_LWIP_SO_RCVBUF is not set
# CONFIG_LWIP_NETBUF_RECVINFO is not set
# CONFIG_LWIP_IP4_FRAG is not set
# CONFIG_LWIP_IP4_FRAG=y
# CONFIG_LWIP_IP6_FRAG is not set
CONFIG_LWIP_IP4_REASSEMBLY=y
CONFIG_LWIP_IP6_REASSEMBLY=y
Expand Down
5 changes: 4 additions & 1 deletion build-scripts/tembed-sdconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ CONFIG_TARGET_LOCKED=y
#
# Factory Configuration
#
CONFIG_ADC_CONFIG="model=ES7210,bck=47,ws=21,di=14,mck=48,i2c=64,sda=18,scl=8"
CONFIG_ADC_LOCKED=y
CONFIG_AUDIO_CONTROLS=""
CONFIG_BAT_CONFIG="channel=3,scale=6.5,cells=1,atten=3"
# CONFIG_BAT_LOCKED is not set
Expand Down Expand Up @@ -228,6 +230,7 @@ CONFIG_A2DP_CONNECT_TIMEOUT_MS=1000
# CONFIG_BT_SINK is not set
# CONFIG_AIRPLAY_SINK is not set
# CONFIG_CSPOT_SINK is not set
CONFIG_ADC_SINK=y
# end of Audio Input

#
Expand Down Expand Up @@ -934,7 +937,7 @@ CONFIG_LWIP_SO_REUSE=y
CONFIG_LWIP_SO_REUSE_RXTOALL=y
# CONFIG_LWIP_SO_RCVBUF is not set
# CONFIG_LWIP_NETBUF_RECVINFO is not set
# CONFIG_LWIP_IP4_FRAG is not set
CONFIG_LWIP_IP4_FRAG=y
# CONFIG_LWIP_IP6_FRAG is not set
CONFIG_LWIP_IP4_REASSEMBLY=y
CONFIG_LWIP_IP6_REASSEMBLY=y
Expand Down
5 changes: 5 additions & 0 deletions components/adc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

idf_component_register(SRC_DIRS .
INCLUDE_DIRS .
PRIV_REQUIRES freertos pthread squeezelite services codecs tools display wifi-manager
)
123 changes: 123 additions & 0 deletions components/adc/adc_sink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#include "nvs.h"
#include "esp_netif.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_pthread.h"
#include "esp_system.h"
#include "freertos/timers.h"
#include "platform_config.h"
#include "input_i2s.h"
//#include "audio_controls.h"
#include "display.h"
#include "accessors.h"
#include "network_services.h"

static EXT_RAM_ATTR struct adc_cb_s {
adc_cmd_vcb_t cmd;
adc_data_cb_t data;
} adc_cbs;

static const char TAG[] = "adc_sink";

static struct adc_ctx_s *adc_i2s;
static adc_cmd_vcb_t cmd_handler_chain;

void adc_linein_start(uint16_t sample_rate) {
ESP_LOGI(TAG, "ADC Start (setup)");
adc_cmd(adc_i2s, ADC_SETUP, sample_rate);
ESP_LOGI(TAG, "ADC Start (play)");
adc_cmd(adc_i2s, ADC_PLAY, NULL);
ESP_LOGI(TAG, "ADC Start (done)");

}

/****************************************************************************************
* Command handler
*/
static bool cmd_handler(adc_event_t event, ...) {
va_list args;

va_start(args, event);

// handle audio event and stop if forbidden
if (!cmd_handler_chain(event, args)) {
va_end(args);
return false;
}

// now handle events for display
switch(event) {
case ADC_SETUP:
ESP_LOGI(TAG, "ADC Setup");
displayer_control(DISPLAYER_ACTIVATE, "ADC INPUT ACTIVE", true);
displayer_artwork(NULL);
break;
case ADC_PLAY:
ESP_LOGI(TAG, "ADC Play");
// control it internally
//displayer_control(DISPLAYER_TIMER_RUN);
displayer_control(DISPLAYER_SHUTDOWN); // hand back to lms
break;
case ADC_STALLED:
ESP_LOGI(TAG, "ADC Stalled");
adc_abort(adc_i2s);
displayer_control(DISPLAYER_SHUTDOWN);
break;

default:
ESP_LOGI(TAG, "ADC Unknown: %d", event);
break;
}

va_end(args);

return true;
}

/****************************************************************************************
* Airplay sink de-initialization
*/
void adc_sink_deinit(void) {
adc_delete(adc_i2s);
ESP_LOGI(TAG, "deinit ADC");
}

/****************************************************************************************
* ADC sink startup
*/
static void adc_sink_start(nm_state_t state_id, int sub_state) {
const char *hostname;

cmd_handler_chain = adc_cbs.cmd;
network_get_hostname(&hostname);

ESP_LOGI(TAG, "starting ADC on host %s", hostname);

adc_i2s = adc_create(cmd_handler, adc_cbs.data);
}

/****************************************************************************************
* ADC sink initialization
*/
void adc_sink_init(adc_cmd_vcb_t cmd_cb, adc_data_cb_t data_cb) {
adc_cbs.cmd = cmd_cb;
adc_cbs.data = data_cb;

network_register_state_callback(NETWORK_WIFI_ACTIVE_STATE, WIFI_CONNECTED_STATE, "adc_sink_start", adc_sink_start);
network_register_state_callback(NETWORK_ETH_ACTIVE_STATE, ETH_ACTIVE_CONNECTED_STATE, "adc_sink_start", adc_sink_start);
}

/****************************************************************************************
* ADC forced disconnection
*/
void adc_disconnect(void) {
ESP_LOGI(TAG, "forced disconnection");
displayer_control(DISPLAYER_SHUTDOWN);
adc_cmd(adc_i2s, ADC_CLOSE, NULL);
}

39 changes: 39 additions & 0 deletions components/adc/adc_sink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/

#ifndef ADC_SINK_H
#define ADC_SINK_H

#include <stdint.h>
#include <stdarg.h>


typedef enum { ADC_SETUP, ADC_STREAM, ADC_PLAY, ADC_FLUSH, ADC_STOP, ADC_STALLED,
ADC_TOGGLE, ADC_CLOSE, ADC_METADATA, ADC_GAIN } adc_event_t ; // , ADC_VOLUME_UP, ADC_VOLUME_DOWN,

typedef bool (*adc_cmd_cb_t)(adc_event_t event, ...);
typedef bool (*adc_cmd_vcb_t)(adc_event_t event, va_list args);
typedef void (*adc_data_cb_t)(const uint8_t *data, size_t len);

/**
* @brief init sink mode (need to be provided)
*/
void adc_sink_init(adc_cmd_vcb_t cmd_cb, adc_data_cb_t data_cb);

/**
* @brief deinit sink mode (need to be provided)
*/
void adc_sink_deinit(void);

/**
* @brief force disconnection
*/
void adc_disconnect(void);
void adc_linein_start(uint16_t sample_rate);

#endif /* ADC_SINK_H*/
Loading

0 comments on commit 43d5df1

Please sign in to comment.