You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.1 KiB

/*
* Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2019. All rights reserved.
* Description: amp sample
* Author: audio
* Create: 2019-09-17
*/
#include <stdio.h>
#include <string.h>
#include "uapi_system.h"
#include "uapi_amp.h"
#include "uapi_pdm.h"
#include "drv_audio_ext.h"
#include "adp_common_ext.h"
#include "drv_ioctl_amp.h"
typedef struct {
td_bool init;
td_bool mute;
} sample_ctx;
uapi_amp_multi_info g_amp_info = {0};
#define INPUT_CMD_LENGTH 32
static td_s32 sample_amp_init(sample_ctx *ctx)
{
td_s32 ret;
if (ctx->init == TD_TRUE) {
return TD_SUCCESS;
}
ret = uapi_amp_open();
if (ret != TD_SUCCESS) {
printf("uapi_amp_open failed(0x%x)\n", ret);
return ret;
}
printf("call uapi_amp_open\n");
ctx->init = TD_TRUE;
ctx->mute = TD_FALSE;
return TD_SUCCESS;
}
static td_s32 sample_amp_deinit(sample_ctx *ctx)
{
td_s32 ret;
if (ctx->init == TD_FALSE) {
return TD_SUCCESS;
}
ret = uapi_amp_close();
if (ret != TD_SUCCESS) {
printf("uapi_amp_close failed(0x%x)\n", ret);
return ret;
}
printf("call uapi_amp_close\n");
ctx->init = TD_FALSE;
ctx->mute = TD_FALSE;
return TD_SUCCESS;
}
static td_s32 sample_amp_set_mute(sample_ctx *ctx)
{
td_s32 ret;
ctx->mute = !(ctx->mute);
ret = uapi_amp_get_info(&g_amp_info);
if (ret != TD_SUCCESS) {
printf("uapi_amp_get_info failed(0x%x)\n", ret);
return ret;
}
ret = uapi_amp_mute_channel(g_amp_info.all_channel_mask, ctx->mute);
if (ret != TD_SUCCESS) {
printf("uapi_amp_set_mute failed(0x%x)\n", ret);
return ret;
}
printf("uapi_amp_set_mute channel_mask: %u; mute: %s\n", g_amp_info.all_channel_mask,
(ctx->mute == TD_TRUE) ? "mute" : "unmute");
return TD_SUCCESS;
}
static td_s32 sample_amp_get_mute(sample_ctx *ctx)
{
td_s32 ret;
ret = uapi_amp_get_channel_mute(g_amp_info.all_channel_mask, &ctx->mute);
if (ret != TD_SUCCESS) {
printf("uapi_amp_get_mute failed(0x%x)\n", ret);
return ret;
}
printf("uapi_amp_get_mute return channel_mask: %u; mute: %s\n", g_amp_info.all_channel_mask,
(ctx->mute == TD_TRUE) ? "mute" : "unmute");
return TD_SUCCESS;
}
typedef struct {
const td_char *cmd;
td_s32 (*action)(sample_ctx *ctx);
} amp_cmd_map;
static const amp_cmd_map g_amp_cmd_map[] = {
{"start", sample_amp_init},
{"stop", sample_amp_deinit},
{"set_mute", sample_amp_set_mute},
{"get_mute", sample_amp_get_mute},
};
static td_void usage(td_void)
{
printf("Input q to quit\n");
printf("***************amp cmd************************\n");
printf("start: use default amp config to start\n");
printf("stop: deinit amp\n");
printf("set_mute: set amp mute on/off\n");
printf("get_mute: get amp mute status\n");
}
static td_void sample_amp_process_cmd(sample_ctx *ctx, td_char *cmd, td_u32 cmd_len)
{
td_u32 i;
td_u32 num = sizeof(g_amp_cmd_map) / sizeof(g_amp_cmd_map[0]);
(void)(cmd_len);
for (i = 0; i < num; i++) {
if (strncmp(cmd, g_amp_cmd_map[i].cmd, strlen(g_amp_cmd_map[i].cmd)) == 0) {
g_amp_cmd_map[i].action(ctx);
return;
}
}
printf("Invalid input\n");
usage();
}
td_s32 main(td_s32 argc, td_char *argv[])
{
td_s32 ret;
td_char input_cmd[INPUT_CMD_LENGTH];
sample_ctx ctx = {TD_FALSE, TD_FALSE};
(void)(argc);
(void)(argv);
usage();
ret = sample_amp_init(&ctx);
if (ret != TD_SUCCESS) {
printf("sample_amp_init failed(0x%x)\n", ret);
return ret;
}
while (1) {
printf("input q to quit!\n");
(td_void)fgets((char *)(input_cmd), (sizeof(input_cmd) - 1), stdin);
sample_amp_process_cmd(&ctx, input_cmd, INPUT_CMD_LENGTH - 1);
if (input_cmd[0] == 'q') {
printf("prepare to quit!\n");
break;
}
}
ret = sample_amp_deinit(&ctx);
if (ret != TD_SUCCESS) {
printf("sample_amp_deinit failed(0x%x)\n", ret);
return ret;
}
return 0;
}