mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-21 10:25:54 +08:00
更新硬件SDK
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
#include "FreeRTOS.h"
|
||||
#include "common_api.h"
|
||||
#include "audio_task.h"
|
||||
#include "bsp_custom.h"
|
||||
#include "audio_extern.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_audio_play_ec618.h"
|
||||
#include "luat_i2s_ec618.h"
|
||||
#include "ivTTSSDKID_all.h"
|
||||
#include "ivTTS.h"
|
||||
#include "luat_gpio.h"
|
||||
#include "luat_debug.h"
|
||||
|
||||
//AIR780E+TM8211开发板配置
|
||||
#define CODEC_PWR_PIN HAL_GPIO_12
|
||||
#define CODEC_PWR_PIN_ALT_FUN 4
|
||||
#define PA_PWR_PIN HAL_GPIO_25
|
||||
#define PA_PWR_PIN_ALT_FUN 0
|
||||
#define CHARGE_EN_PIN HAL_GPIO_2
|
||||
#define CHARGE_EN_PIN_ALT_FUN 0
|
||||
|
||||
#define AUDIO_QUEUE_SIZE 100
|
||||
|
||||
|
||||
extern const unsigned char audiopoweron[];
|
||||
static luat_rtos_semaphore_t audio_semaphore_handle;
|
||||
static luat_rtos_task_handle audio_task_handle;
|
||||
|
||||
|
||||
luat_rtos_queue_t audio_queue_handle;
|
||||
static uint8_t audio_sleep_handler = 0xff;
|
||||
static HANDLE g_s_delay_timer;
|
||||
|
||||
void audio_data_cb(uint8_t *data, uint32_t len, uint8_t bits, uint8_t channels)
|
||||
{
|
||||
int value = 15;
|
||||
int ret = luat_kv_get("volume", &value, sizeof(int));
|
||||
if(ret > 0)
|
||||
{
|
||||
// LUAT_DEBUG_PRINT("cloud_speaker_audio_task get volume success %d", value); //这里的打印打开会出来很多,影响日志查看,有需要可自行打开
|
||||
HAL_I2sSrcAdjustVolumn(data, len, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// LUAT_DEBUG_PRINT("cloud_speaker_audio_task get volume fail %d", value); //这里的打印打开会出来很多,影响日志查看,有需要可自行打开
|
||||
HAL_I2sSrcAdjustVolumn(data, len, 15);
|
||||
}
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task %x,%d,%d,%d,%d", data, len, bits, channels);
|
||||
}
|
||||
void app_pa_on(uint32_t arg)
|
||||
{
|
||||
luat_gpio_set(PA_PWR_PIN, 1); //如果是780E+音频扩展小板,可以注释掉此行代码,因为PA长开
|
||||
}
|
||||
void audio_event_cb(uint32_t event, void *param)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task event_cb %d", event);
|
||||
switch (event)
|
||||
{
|
||||
case MULTIMEDIA_CB_AUDIO_DECODE_START:
|
||||
luat_gpio_set(CODEC_PWR_PIN, 1);
|
||||
luat_audio_play_write_blank_raw(0, 6, 1);
|
||||
break;
|
||||
case MULTIMEDIA_CB_AUDIO_OUTPUT_START:
|
||||
luat_rtos_timer_start(g_s_delay_timer, 200, 0, app_pa_on, NULL); //如果是780E+音频扩展小板,可以注释掉此行代码,因为PA长开
|
||||
break;
|
||||
case MULTIMEDIA_CB_TTS_INIT:
|
||||
break;
|
||||
case LUAT_MULTIMEDIA_CB_TTS_DONE:
|
||||
if (!luat_audio_play_get_last_error(0))
|
||||
{
|
||||
luat_audio_play_write_blank_raw(0, 1, 0);
|
||||
}
|
||||
break;
|
||||
case MULTIMEDIA_CB_AUDIO_DONE:
|
||||
luat_rtos_timer_stop(g_s_delay_timer);
|
||||
LUAT_DEBUG_PRINT("audio play done, result=%d!", luat_audio_play_get_last_error(0));
|
||||
luat_gpio_set(PA_PWR_PIN, 0); //如果是780E+音频扩展小板,可以注释掉此行代码,因为PA长开
|
||||
luat_gpio_set(CODEC_PWR_PIN, 0);
|
||||
luat_rtos_semaphore_release(audio_semaphore_handle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void audio_task(void *param)
|
||||
{
|
||||
audioQueueData audioQueueRecv = {0};
|
||||
uint32_t result = 0;
|
||||
while (1)
|
||||
{
|
||||
if (luat_rtos_queue_recv(audio_queue_handle, &audioQueueRecv, NULL, portMAX_DELAY) == 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task this is play priority %d", audioQueueRecv.priority);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task this is play playType %d", audioQueueRecv.playType);
|
||||
if (audioQueueRecv.priority == MONEY_PLAY)
|
||||
{
|
||||
|
||||
if (audioQueueRecv.playType == TTS_PLAY)
|
||||
{
|
||||
luat_audio_play_tts_text(0, audioQueueRecv.message.tts.data, audioQueueRecv.message.tts.len);
|
||||
}
|
||||
else if (audioQueueRecv.playType == FILE_PLAY)
|
||||
{
|
||||
luat_audio_play_multi_files(0, audioQueueRecv.message.file.info, audioQueueRecv.message.file.count);
|
||||
}
|
||||
}
|
||||
else if (audioQueueRecv.priority == PAD_PLAY)
|
||||
{
|
||||
}
|
||||
luat_rtos_semaphore_take(audio_semaphore_handle, LUAT_WAIT_FOREVER);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task this is play wait result %d", result);
|
||||
if (audioQueueRecv.playType == TTS_PLAY) {
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task free tts data");
|
||||
free(audioQueueRecv.message.tts.data);
|
||||
}
|
||||
else if(audioQueueRecv.playType == FILE_PLAY)
|
||||
{
|
||||
free(audioQueueRecv.message.file.info);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task free file data");
|
||||
}
|
||||
}
|
||||
}
|
||||
luat_rtos_task_delete(audio_task_handle);
|
||||
}
|
||||
|
||||
void audio_task_init(void)
|
||||
{
|
||||
luat_rtos_timer_create(&g_s_delay_timer);
|
||||
|
||||
luat_gpio_cfg_t gpio_cfg;
|
||||
luat_gpio_set_default_cfg(&gpio_cfg);
|
||||
|
||||
gpio_cfg.pull = LUAT_GPIO_DEFAULT;
|
||||
|
||||
//如果是780E+音频扩展小板,可以注释掉下面两行代码,因为PA长开
|
||||
gpio_cfg.pin = PA_PWR_PIN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
|
||||
gpio_cfg.pin = CODEC_PWR_PIN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
gpio_cfg.alt_fun = CODEC_PWR_PIN_ALT_FUN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
|
||||
luat_audio_play_global_init(audio_event_cb, audio_data_cb, luat_audio_play_file_default_fun, luat_audio_play_tts_default_fun, NULL);
|
||||
ivCStrA sdk_id = AISOUND_SDK_USERID_16K;
|
||||
luat_audio_play_tts_set_resource(ivtts_16k, sdk_id, NULL);
|
||||
// luat_i2s_base_setup(0, I2S_MODE_I2S, I2S_FRAME_SIZE_16_16); //如果是780E+音频扩展小板,打开这行注释代码,这个配置对应ES7148/ES7149
|
||||
luat_i2s_base_setup(0, I2S_MODE_MSB, I2S_FRAME_SIZE_16_16); //此处配置对应TM8211
|
||||
luat_rtos_semaphore_create(&audio_semaphore_handle, 1);
|
||||
|
||||
luat_rtos_queue_create(&audio_queue_handle, AUDIO_QUEUE_SIZE, sizeof(audioQueueData));
|
||||
audioQueueData powerOn = {0};
|
||||
powerOn.playType = TTS_PLAY;
|
||||
powerOn.priority = MONEY_PLAY;
|
||||
char str[] = "正在开机";
|
||||
powerOn.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(powerOn.message.tts.data, str, sizeof(str));
|
||||
powerOn.message.tts.len = sizeof(str);
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &powerOn, NULL, 0))
|
||||
{
|
||||
free(powerOn.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task start send audio fail");
|
||||
}
|
||||
int result = luat_rtos_task_create(&audio_task_handle, 2048, 20, "mqtt", audio_task, NULL, NULL);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_audio_task create task result %d", result);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "FreeRTOS.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "common_api.h"
|
||||
#include "luat_debug.h"
|
||||
#include "luat_adc.h"
|
||||
static luat_rtos_task_handle charge_task_handle;
|
||||
|
||||
#define ADC_ID_VBAT 11
|
||||
static volatile uint16_t vbat = 0;
|
||||
|
||||
uint16_t get_vbat()
|
||||
{
|
||||
return vbat;
|
||||
}
|
||||
|
||||
static void charge_task(void *param)
|
||||
{
|
||||
int origin, revert;
|
||||
luat_adc_open(ADC_ID_VBAT, NULL);
|
||||
luat_adc_read(ADC_ID_VBAT, &origin, &revert);
|
||||
luat_rtos_task_sleep(1000);
|
||||
while (1)
|
||||
{
|
||||
luat_adc_read(ADC_ID_VBAT, &origin, &revert);
|
||||
vbat = revert;
|
||||
luat_rtos_task_sleep(60000);
|
||||
}
|
||||
luat_rtos_task_delete(charge_task_handle);
|
||||
}
|
||||
|
||||
|
||||
void charge_task_init(void)
|
||||
{
|
||||
int result = luat_rtos_task_create(&charge_task_handle, 256, 20, "charge task", charge_task, NULL, NULL);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_charge_task create task result %d", result);
|
||||
}
|
||||
@@ -0,0 +1,696 @@
|
||||
/*
|
||||
* Copyright (c) 2022 OpenLuat & AirM2M
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "bsp.h"
|
||||
#include "bsp_custom.h"
|
||||
#include "common_api.h"
|
||||
#include "cJSON.h"
|
||||
#include "MQTTClient.h"
|
||||
#include "audio_task.h"
|
||||
#include "audio_file.h"
|
||||
#include "math.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_mobile.h"
|
||||
#include "luat_debug.h"
|
||||
#include "luat_pm.h"
|
||||
#include "mqtt_publish.h"
|
||||
uint8_t g_s_is_link_up = 0;
|
||||
static luat_rtos_semaphore_t net_semaphore_handle;
|
||||
static luat_rtos_task_handle mqtt_task_handle;
|
||||
extern luat_rtos_queue_t audio_queue_handle;
|
||||
|
||||
#define MQTT_HOST "lbsmqtt.airm2m.com" // MQTT服务器的地址和端口号
|
||||
#define MQTT_PORT 1884
|
||||
|
||||
#define MQTT_SEND_BUFF_LEN (1024)
|
||||
#define MQTT_RECV_BUFF_LEN (1024)
|
||||
|
||||
#define CLIENTID "12345678"
|
||||
const static char mqtt_sub_topic_head[] = "/sub/topic/money/"; //订阅的主题头,待与设备imei进行拼接
|
||||
const static char mqtt_pub_topic[] = "/pub/topic/message"; //发布的主题
|
||||
static char mqtt_send_payload[] = "hello mqtt_test!!!";
|
||||
static char mqtt_sub_topic[40]; //订阅的主题,待存放订阅的主题头和设备imei,共计17+15,32个字符
|
||||
static bool netStatus = false;
|
||||
static bool serverStatus = false;
|
||||
|
||||
bool getNetStatus()
|
||||
{
|
||||
return netStatus;
|
||||
}
|
||||
bool getServerStatus()
|
||||
{
|
||||
return serverStatus;
|
||||
}
|
||||
|
||||
int fomatMoney(int num, audioQueueData *data, int *index, BOOL flag)
|
||||
{
|
||||
uint32_t audioArray[10][2] =
|
||||
{
|
||||
{audio0, sizeof(audio0)},
|
||||
{audio1, sizeof(audio1)},
|
||||
{audio2, sizeof(audio2)},
|
||||
{audio3, sizeof(audio3)},
|
||||
{audio4, sizeof(audio4)},
|
||||
{audio5, sizeof(audio5)},
|
||||
{audio6, sizeof(audio6)},
|
||||
{audio7, sizeof(audio7)},
|
||||
{audio8, sizeof(audio8)},
|
||||
{audio9, sizeof(audio9)}
|
||||
};
|
||||
int thousand = (num - num % 1000) / 1000;
|
||||
int hundred = ((num % 1000) - ((num % 1000) % 100)) / 100;
|
||||
int ten = ((num % 100) - ((num % 100) % 10)) / 10;
|
||||
int unit = num % 10;
|
||||
if (thousand == 0)
|
||||
{
|
||||
thousand = -1;
|
||||
if (hundred == 0)
|
||||
{
|
||||
hundred = -1;
|
||||
if (ten == 0)
|
||||
{
|
||||
ten = -1;
|
||||
if (unit == 0)
|
||||
{
|
||||
unit = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (unit == 0)
|
||||
{
|
||||
unit = -1;
|
||||
if (ten == 0)
|
||||
{
|
||||
ten = -1;
|
||||
if (hundred == 0)
|
||||
{
|
||||
hundred = -1;
|
||||
if (thousand == 0)
|
||||
{
|
||||
thousand = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ten == 0 && hundred == 0)
|
||||
{
|
||||
ten = -1;
|
||||
}
|
||||
if (thousand != -1)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
if(2 == thousand)
|
||||
{
|
||||
data->message.file.info[*index].address = audioliang;
|
||||
data->message.file.info[*index].rom_data_len = audioliangSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
data->message.file.info[*index].address = audioArray[thousand][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[thousand][1];
|
||||
}
|
||||
}
|
||||
*index += 1;
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audio1000;
|
||||
data->message.file.info[*index].rom_data_len = sizeof(audio1000);
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
if (hundred != -1)
|
||||
{
|
||||
if(flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
if(2 == hundred)
|
||||
{
|
||||
data->message.file.info[*index].address = audioliang;
|
||||
data->message.file.info[*index].rom_data_len = audioliangSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
data->message.file.info[*index].address = audioArray[hundred][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[hundred][1];
|
||||
}
|
||||
}
|
||||
*index += 1;
|
||||
if(flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audio100;
|
||||
data->message.file.info[*index].rom_data_len = sizeof(audio100);
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
if (ten != -1)
|
||||
{
|
||||
if (!(ten == 1 && hundred == -1 && thousand == -1))
|
||||
{
|
||||
if(flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[ten][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[ten][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
if (ten != 0)
|
||||
{
|
||||
if(flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audio10;
|
||||
data->message.file.info[*index].rom_data_len = sizeof(audio10);
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
if (unit != -1)
|
||||
{
|
||||
if(flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[unit][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[unit][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int strToFile(char *money, audioQueueData *data, int *index, bool flag)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].address = audiozhifubao;
|
||||
data->message.file.info[*index].rom_data_len = sizeof(audiozhifubao);
|
||||
}
|
||||
*index += 1;
|
||||
uint32_t audioArray[10][2] =
|
||||
{
|
||||
{audio0, sizeof(audio0)},
|
||||
{audio1, sizeof(audio1)},
|
||||
{audio2, sizeof(audio2)},
|
||||
{audio3, sizeof(audio3)},
|
||||
{audio4, sizeof(audio4)},
|
||||
{audio5, sizeof(audio5)},
|
||||
{audio6, sizeof(audio6)},
|
||||
{audio7, sizeof(audio7)},
|
||||
{audio8, sizeof(audio8)},
|
||||
{audio9, sizeof(audio9)}
|
||||
};
|
||||
int count = 0;
|
||||
int integer = 0;
|
||||
char *str = NULL;
|
||||
char intStr[8] = {0};
|
||||
char decStr[3] = {0};
|
||||
str = strstr(money, ".");
|
||||
if (str != NULL)
|
||||
{
|
||||
memcpy(intStr, money, str - money);
|
||||
str = str + 1;
|
||||
memcpy(decStr, str, 2);
|
||||
integer = atoi(intStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
integer = atoi(money);
|
||||
}
|
||||
if (integer >= 10000)
|
||||
{
|
||||
int filecount = fomatMoney(integer / 10000, data, index, flag);
|
||||
if (flag)
|
||||
{
|
||||
if((2 == *index) && (data->message.file.info[1].address == audio2))
|
||||
{
|
||||
data->message.file.info[1].address = audioliang;
|
||||
data->message.file.info[1].rom_data_len = sizeof(audioliang);
|
||||
}
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audio10000;
|
||||
data->message.file.info[*index].rom_data_len = sizeof(audio10000);
|
||||
}
|
||||
*index += 1;
|
||||
if (((integer % 10000) < 1000) && ((integer % 10000) != 0))
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[0][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[0][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
if ((integer % 10000) > 0)
|
||||
{
|
||||
int filecount = fomatMoney(integer % 10000, data, index, flag);
|
||||
}
|
||||
if (*index == 1)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[0][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[0][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
int fraction = atoi(decStr);
|
||||
if (fraction > 0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audiodot;
|
||||
data->message.file.info[*index].rom_data_len = sizeof(audiodot);
|
||||
}
|
||||
*index += 1;
|
||||
if (fraction >= 10)
|
||||
{
|
||||
int ten = fraction / 10;
|
||||
int unit = fraction % 10;
|
||||
if (ten != 0 && unit != 0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[ten][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[ten][1];
|
||||
}
|
||||
*index += 1;
|
||||
if(flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[unit][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[unit][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
else if(ten == 0 && unit!=0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[0][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[0][1];
|
||||
}
|
||||
*index += 1;
|
||||
if(flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[unit][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[0][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
else if(ten !=0 && unit == 0)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[ten][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[ten][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(decStr[0] == 0x30)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[0][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[0][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioArray[fraction][0];
|
||||
data->message.file.info[*index].rom_data_len = audioArray[fraction][1];
|
||||
}
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
data->message.file.info[*index].path = NULL;
|
||||
data->message.file.info[*index].address = audioyuan;
|
||||
data->message.file.info[*index].rom_data_len = sizeof(audioyuan);
|
||||
}
|
||||
*index += 1;
|
||||
return count;
|
||||
}
|
||||
|
||||
void messageArrived(MessageData* data)
|
||||
{
|
||||
if (strcmp(mqtt_sub_topic, data->topicName->lenstring.data) == 0)
|
||||
{
|
||||
cJSON *boss = NULL;
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt mqtt Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data, data->message->payloadlen, data->message->payload);
|
||||
boss = cJSON_Parse((const char *)data->message->payload);
|
||||
if (boss == NULL){
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt cjson parse fail");
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt cjson parse success");
|
||||
cJSON *money = cJSON_GetObjectItem(boss, "money");
|
||||
if(money == NULL)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt Missing amount field %d", money);
|
||||
return 0;
|
||||
}
|
||||
if (cJSON_IsString(money))
|
||||
{
|
||||
audioQueueData moneyPlay = {0};
|
||||
moneyPlay.priority = MONEY_PLAY;
|
||||
moneyPlay.playType = FILE_PLAY;
|
||||
char* str = NULL;
|
||||
str = strstr(money->valuestring, ".");
|
||||
//判断金额长度是否大于8个,也就是千万级别的金额,如果是,则播报收款成功,如果不是,则播报对应金额,这里并未对金额字段做合法性判断
|
||||
if (str != NULL)
|
||||
{
|
||||
if((str - money->valuestring) > 8)
|
||||
{
|
||||
moneyPlay.message.file.info = (audio_play_info_t *)calloc(1, sizeof(audio_play_info_t));
|
||||
moneyPlay.message.file.info->address = audioshoukuanchenggong;
|
||||
moneyPlay.message.file.info->rom_data_len = audioshoukuanchenggongSize;
|
||||
moneyPlay.message.file.count = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
str++;
|
||||
//如果小数点位数大于两位,则说明数字金额不合法,播报收款成功
|
||||
if(strlen(str) > 2)
|
||||
{
|
||||
moneyPlay.message.file.info = (audio_play_info_t *)calloc(1, sizeof(audio_play_info_t));
|
||||
moneyPlay.message.file.info->address = audioshoukuanchenggong;
|
||||
moneyPlay.message.file.info->rom_data_len = audioshoukuanchenggongSize;
|
||||
moneyPlay.message.file.count = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
//调用strToFile来将金额格式化为对应的文件播报数据,需要调用两次,第一次获取需要malloc的空间,第二次将文件数据放进空间里
|
||||
int index = 0;
|
||||
strToFile(money->valuestring, &moneyPlay, &index, false);
|
||||
moneyPlay.message.file.info = (audio_play_info_t *)calloc(index, sizeof(audio_play_info_t));
|
||||
index = 0;
|
||||
strToFile(money->valuestring, &moneyPlay, &index, true);
|
||||
moneyPlay.message.file.count = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(strlen(money->valuestring) > 8)
|
||||
{
|
||||
moneyPlay.message.file.info = (audio_play_info_t *)calloc(1, sizeof(audio_play_info_t));
|
||||
moneyPlay.message.file.info->address = audioshoukuanchenggong;
|
||||
moneyPlay.message.file.info->rom_data_len = audioshoukuanchenggongSize;
|
||||
moneyPlay.message.file.count = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
//调用strToFile来将金额格式化为对应的文件播报数据,需要调用两次,第一次获取需要malloc的空间,第二次将文件数据放进空间里
|
||||
int index = 0;
|
||||
strToFile(money->valuestring, &moneyPlay, &index, false);
|
||||
moneyPlay.message.file.info = (audio_play_info_t *)calloc(index, sizeof(audio_play_info_t));
|
||||
index = 0;
|
||||
strToFile(money->valuestring, &moneyPlay, &index, true);
|
||||
moneyPlay.message.file.count = index;
|
||||
}
|
||||
}
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &moneyPlay, NULL, 0)){
|
||||
free(moneyPlay.message.file.info);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt sub queue send error");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt money data is invalid %d", cJSON_IsString(money));
|
||||
}
|
||||
}
|
||||
cJSON_Delete(boss);
|
||||
}
|
||||
}
|
||||
|
||||
static void mobile_event_cb(LUAT_MOBILE_EVENT_E event, uint8_t index, uint8_t status)
|
||||
{
|
||||
switch(event)
|
||||
{
|
||||
case LUAT_MOBILE_EVENT_CFUN:
|
||||
LUAT_DEBUG_PRINT("CFUN消息,status %d", status);
|
||||
break;
|
||||
case LUAT_MOBILE_EVENT_SIM:
|
||||
LUAT_DEBUG_PRINT("SIM卡消息");
|
||||
switch(status)
|
||||
{
|
||||
case LUAT_MOBILE_SIM_READY:
|
||||
LUAT_DEBUG_PRINT("SIM卡正常工作");
|
||||
break;
|
||||
case LUAT_MOBILE_NO_SIM:
|
||||
LUAT_DEBUG_PRINT("SIM卡不存在");
|
||||
break;
|
||||
case LUAT_MOBILE_SIM_NEED_PIN:
|
||||
LUAT_DEBUG_PRINT("SIM卡需要输入PIN码");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case LUAT_MOBILE_EVENT_REGISTER_STATUS:
|
||||
LUAT_DEBUG_PRINT("移动网络服务状态变更,当前为%d", status);
|
||||
break;
|
||||
case LUAT_MOBILE_EVENT_CELL_INFO:
|
||||
switch(status)
|
||||
{
|
||||
case LUAT_MOBILE_CELL_INFO_UPDATE:
|
||||
break;
|
||||
case LUAT_MOBILE_SIGNAL_UPDATE:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case LUAT_MOBILE_EVENT_PDP:
|
||||
LUAT_DEBUG_PRINT("CID %d PDP激活状态变更为 %d", index, status);
|
||||
break;
|
||||
case LUAT_MOBILE_EVENT_NETIF:
|
||||
LUAT_DEBUG_PRINT("internet工作状态变更为 %d", status);
|
||||
switch (status)
|
||||
{
|
||||
case LUAT_MOBILE_NETIF_LINK_ON:
|
||||
LUAT_DEBUG_PRINT("可以上网");
|
||||
g_s_is_link_up = 1;
|
||||
break;
|
||||
default:
|
||||
g_s_is_link_up = 0;
|
||||
LUAT_DEBUG_PRINT("不能上网");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case LUAT_MOBILE_EVENT_TIME_SYNC:
|
||||
LUAT_DEBUG_PRINT("通过移动网络同步了UTC时间");
|
||||
break;
|
||||
case LUAT_MOBILE_EVENT_CSCON:
|
||||
LUAT_DEBUG_PRINT("RRC状态 %d", status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
extern luat_rtos_task_handle mqtt_publish_task_handle;
|
||||
extern void mqtt_publish_task(void *args);
|
||||
static void mqtt_demo(void){
|
||||
int rc = 0;
|
||||
unsigned char mqttSendbuf[MQTT_SEND_BUFF_LEN] = {0}, mqttReadbuf[MQTT_RECV_BUFF_LEN] = {0};
|
||||
static MQTTClient mqttClient;
|
||||
static Network n = {0};
|
||||
|
||||
MQTTMessage message = {0};
|
||||
MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer;
|
||||
connectData.MQTTVersion = 4;
|
||||
int ret = 0;
|
||||
char str[32] = {0};
|
||||
char clientId[17] = {0};
|
||||
char username[17] = {0};
|
||||
char password[17] = {0};
|
||||
ret = luat_kv_get("clientId", str, 17); //从数据库中读取clientId
|
||||
if(ret > 0 )
|
||||
{
|
||||
memcpy(clientId, str, 16); //留一位确保字符串结尾能有0x00
|
||||
connectData.clientID.cstring = clientId;
|
||||
}
|
||||
else //数据库中没有写入过clientId,获取设备imei作为clientId
|
||||
{
|
||||
int result = 0;
|
||||
result = luat_mobile_get_imei(0, clientId, 15); //imei是15位,留一个位置放0x00
|
||||
if(result <= 0)
|
||||
{
|
||||
connectData.clientID.cstring = CLIENTID;
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt clientid get fail");
|
||||
}
|
||||
else
|
||||
{
|
||||
connectData.clientID.cstring = clientId;
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt clientid get success %s", clientId);
|
||||
}
|
||||
}
|
||||
memset(str, 0, 32);
|
||||
ret = luat_kv_get("username", str, 17); //从数据库中读取username,如果没读到,则用默认的
|
||||
if(ret > 0 )
|
||||
{
|
||||
memcpy(username, str, 16); //留一位确保字符串结尾能有0x00
|
||||
connectData.username.cstring = username;
|
||||
}
|
||||
else
|
||||
{
|
||||
connectData.username.cstring = NULL;
|
||||
}
|
||||
memset(str, 0, 32);
|
||||
ret = luat_kv_get("password", str, 17); //从数据库中读取password,如果没读到,则用默认的
|
||||
if(ret > 0 )
|
||||
{
|
||||
memcpy(password, str, 16); //留一位确保字符串结尾能有0x00
|
||||
connectData.password.cstring = password;
|
||||
}
|
||||
else
|
||||
{
|
||||
connectData.password.cstring = NULL;
|
||||
}
|
||||
memset(str, 0, 32);
|
||||
memset(mqtt_sub_topic, 0x00, sizeof(mqtt_sub_topic));
|
||||
snprintf(mqtt_sub_topic, 40, "%s%s", mqtt_sub_topic_head, clientId);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt subscribe_topic %s %s %s %s", mqtt_sub_topic, clientId, username, password);
|
||||
connectData.keepAliveInterval = 120;
|
||||
|
||||
while(!g_s_is_link_up)
|
||||
{
|
||||
luat_rtos_task_sleep(1000);
|
||||
}
|
||||
// 设置my_app标记可以休眠到LIGHT等级
|
||||
luat_pm_set_sleep_mode(LUAT_PM_SLEEP_MODE_LIGHT, "my_app");
|
||||
|
||||
NetworkInit(&n);
|
||||
MQTTClientInit(&mqttClient, &n, 40000, mqttSendbuf, MQTT_SEND_BUFF_LEN, mqttReadbuf, MQTT_RECV_BUFF_LEN);
|
||||
|
||||
if ((NetworkConnect(&n, MQTT_HOST, MQTT_PORT)) != 0){
|
||||
mqttClient.keepAliveInterval = connectData.keepAliveInterval;
|
||||
mqttClient.ping_outstanding = 1;
|
||||
goto error;
|
||||
}else{
|
||||
if ((MQTTConnect(&mqttClient, &connectData)) != 0){
|
||||
mqttClient.ping_outstanding = 1;
|
||||
goto error;
|
||||
}else{
|
||||
LUAT_DEBUG_PRINT("MQTTStartTask \n");
|
||||
#if defined(MQTT_TASK)
|
||||
if ((MQTTStartTask(&mqttClient)) != pdPASS){
|
||||
goto error;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
while (1){
|
||||
// 支付报文一般来说不能丢失,也最好不要重复发送,这里设置qos为1即可,保证只收到一次
|
||||
if ((rc = MQTTSubscribe(&mqttClient, mqtt_sub_topic, 1, messageArrived)) != 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("mqtt Return code from MQTT subscribe error is %d\n", rc);
|
||||
}
|
||||
else
|
||||
{
|
||||
serverStatus = true;
|
||||
audioQueueData welcome = {0};
|
||||
welcome.playType = TTS_PLAY;
|
||||
welcome.priority = MONEY_PLAY;
|
||||
char str[] = "服务器连接成功";
|
||||
welcome.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(welcome.message.tts.data, str, sizeof(str));
|
||||
welcome.message.tts.len = sizeof(str);
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &welcome, NULL, 0)){
|
||||
free(welcome.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt sub audio queue send error");
|
||||
}
|
||||
if (mqtt_publish_task_handle == NULL)
|
||||
{
|
||||
luat_rtos_task_create(&mqtt_publish_task_handle, 2048, 20, "mqtt_publish_task", mqtt_publish_task, (void *)&mqttClient, 10);
|
||||
}
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
if(MQTTIsConnected(&mqttClient) == 0){
|
||||
goto error;
|
||||
}
|
||||
|
||||
|
||||
luat_rtos_task_sleep(5000);
|
||||
}
|
||||
error:
|
||||
while(!g_s_is_link_up){
|
||||
luat_rtos_task_sleep(1000);
|
||||
}
|
||||
|
||||
if (rc = MQTTReConnect(&mqttClient, &connectData) != 0){
|
||||
luat_rtos_task_sleep(5000);
|
||||
LUAT_DEBUG_PRINT("MQTTReConnect %d\n", rc);
|
||||
goto error;
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("MQTTReConnect OK");
|
||||
}
|
||||
}
|
||||
luat_rtos_task_delete(mqtt_task_handle);
|
||||
}
|
||||
|
||||
static void task_init(void){
|
||||
luat_mobile_event_register_handler(mobile_event_cb);
|
||||
}
|
||||
|
||||
static void mqttclient_task_init(void){
|
||||
int result = luat_rtos_task_create(&mqtt_task_handle, 4096, 20, "mqtt", mqtt_demo, NULL, NULL);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_mqtt create task result %d", result);
|
||||
}
|
||||
|
||||
extern void led_task_init(void);
|
||||
extern void key_task_init(void);
|
||||
extern void charge_task_init(void);
|
||||
extern void usb_uart_init(void);
|
||||
extern void fdb_init(void);
|
||||
extern void mqtt_send_task_init(void);
|
||||
|
||||
|
||||
INIT_HW_EXPORT(task_init, "1");
|
||||
INIT_HW_EXPORT(fdb_init, "1");
|
||||
INIT_TASK_EXPORT(mqttclient_task_init, "2");
|
||||
INIT_TASK_EXPORT(audio_task_init, "2");
|
||||
INIT_TASK_EXPORT(led_task_init, "2");
|
||||
INIT_TASK_EXPORT(key_task_init, "3");
|
||||
INIT_TASK_EXPORT(charge_task_init, "2");
|
||||
INIT_TASK_EXPORT(usb_uart_init, "2");
|
||||
INIT_TASK_EXPORT(mqtt_send_task_init, "3");
|
||||
|
||||
@@ -0,0 +1,335 @@
|
||||
#include "FreeRTOS.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_gpio.h"
|
||||
#include "common_api.h"
|
||||
#include "luat_debug.h"
|
||||
#include "luat_pm.h"
|
||||
#include "audio_task.h"
|
||||
#define KEY1_MESSAGE (0x1)
|
||||
#define KEY2_MESSAGE (0x2)
|
||||
#define KEY3_MESSAGE (0x3)
|
||||
#define PWR_MESSAGE (0x4)
|
||||
#define CHARGE_START_MESSAGE (0x5)
|
||||
#define CHARGE_END_MESSAGE (0x6)
|
||||
|
||||
#define PAD_PIN_ALT_FUN 0
|
||||
#define KEY_QUEUE_SIZE 2
|
||||
extern uint16_t get_vbat();
|
||||
typedef struct
|
||||
{
|
||||
uint8_t messageId;
|
||||
} key_message_t;
|
||||
|
||||
static luat_rtos_task_handle key_task_handle;
|
||||
static luat_rtos_queue_t key_queue_handle;
|
||||
static luat_rtos_timer_t pwrkey_timer_handle;
|
||||
static luat_rtos_timer_t powoff_timer_handle;
|
||||
extern luat_rtos_queue_t audio_queue_handle;
|
||||
|
||||
static luat_rtos_timer_callback_t pwrkey_long_press_callback(void *param)
|
||||
{
|
||||
if (param == &pwrkey_timer_handle)
|
||||
{
|
||||
luat_stop_rtos_timer(pwrkey_timer_handle);
|
||||
luat_rtos_timer_start(powoff_timer_handle, 3000, 0, pwrkey_long_press_callback, &powoff_timer_handle);
|
||||
audioQueueData powerOff = {0};
|
||||
powerOff.playType = TTS_PLAY;
|
||||
powerOff.priority = MONEY_PLAY;
|
||||
char str[] = "正在关机";
|
||||
powerOff.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(powerOff.message.tts.data, str, sizeof(str));
|
||||
powerOff.message.tts.len = sizeof(str);
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &powerOff, NULL, 0))
|
||||
{
|
||||
free(powerOff.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_key_task start send audio fail");
|
||||
}
|
||||
}
|
||||
else if (param == &powoff_timer_handle)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("poweroff");
|
||||
luat_pm_poweroff();
|
||||
}
|
||||
}
|
||||
|
||||
luat_pm_pwrkey_callback_t pwrkey_callback(LUAT_PM_POWERKEY_STATE_E status)
|
||||
{
|
||||
if (LUAT_PM_PWRKEY_PRESS == status)
|
||||
{
|
||||
luat_rtos_timer_start(pwrkey_timer_handle, 3000, 0, pwrkey_long_press_callback, &pwrkey_timer_handle);
|
||||
}
|
||||
else if (LUAT_PM_PWRKEY_RELEASE == status)
|
||||
{
|
||||
uint8_t id = PWR_MESSAGE;
|
||||
luat_rtos_queue_send(key_queue_handle, &id, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void key_task(void *param)
|
||||
{
|
||||
uint8_t msgId = 0;
|
||||
int volume = 15;
|
||||
int ret = 0;
|
||||
while (1)
|
||||
{
|
||||
if (luat_rtos_queue_recv(key_queue_handle, &msgId, NULL, LUAT_WAIT_FOREVER) == 0)
|
||||
{
|
||||
switch (msgId)
|
||||
{
|
||||
case KEY1_MESSAGE:
|
||||
{
|
||||
audioQueueData volMinus = {0};
|
||||
volMinus.playType = TTS_PLAY;
|
||||
volMinus.priority = MONEY_PLAY;
|
||||
volume = 15;
|
||||
ret = luat_kv_get("volume", &volume, sizeof(int));
|
||||
if (ret > 0)
|
||||
{
|
||||
if (volume > 3)
|
||||
{
|
||||
volume = volume - 3;
|
||||
if (volume < 3)
|
||||
{
|
||||
volume = 3;
|
||||
}
|
||||
luat_kv_set("volume", &volume, sizeof(int));
|
||||
}
|
||||
if (volume == 3)
|
||||
{
|
||||
char str[] = "音量最小";
|
||||
volMinus.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(volMinus.message.tts.data, str, sizeof(str));
|
||||
volMinus.message.tts.len = sizeof(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
char str[] = "音量减";
|
||||
volMinus.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(volMinus.message.tts.data, str, sizeof(str));
|
||||
volMinus.message.tts.len = sizeof(str);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char str[] = "音量减";
|
||||
volMinus.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(volMinus.message.tts.data, str, sizeof(str));
|
||||
volMinus.message.tts.len = sizeof(str);
|
||||
}
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &volMinus, NULL, 0))
|
||||
{
|
||||
free(volMinus.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_key_task start send audio fail");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case KEY2_MESSAGE:
|
||||
{
|
||||
audioQueueData volPlus = {0};
|
||||
volPlus.playType = TTS_PLAY;
|
||||
volPlus.priority = MONEY_PLAY;
|
||||
volume = 4;
|
||||
ret = luat_kv_get("volume", &volume, sizeof(int));
|
||||
if (ret > 0)
|
||||
{
|
||||
if (volume < 21)
|
||||
{
|
||||
volume = volume + 3;
|
||||
if (volume > 21)
|
||||
{
|
||||
volume = 21;
|
||||
}
|
||||
luat_kv_set("volume", &volume, sizeof(int));
|
||||
}
|
||||
if (volume == 21)
|
||||
{
|
||||
char str[] = "音量最大";
|
||||
volPlus.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(volPlus.message.tts.data, str, sizeof(str));
|
||||
volPlus.message.tts.len = sizeof(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
char str[] = "音量加";
|
||||
volPlus.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(volPlus.message.tts.data, str, sizeof(str));
|
||||
volPlus.message.tts.len = sizeof(str);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char str[] = "音量加";
|
||||
volPlus.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(volPlus.message.tts.data, str, sizeof(str));
|
||||
volPlus.message.tts.len = sizeof(str);
|
||||
}
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &volPlus, NULL, 0))
|
||||
{
|
||||
free(volPlus.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_key_task start send audio fail");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case KEY3_MESSAGE:
|
||||
{
|
||||
audioQueueData func = {0};
|
||||
func.playType = TTS_PLAY;
|
||||
func.priority = MONEY_PLAY;
|
||||
char str[] = "功能键";
|
||||
func.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(func.message.tts.data, str, sizeof(str));
|
||||
func.message.tts.len = sizeof(str);
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &func, NULL, 0))
|
||||
{
|
||||
free(func.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_key_task start send audio fail");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PWR_MESSAGE:
|
||||
{
|
||||
if (1 == luat_rtos_timer_is_active(pwrkey_timer_handle))
|
||||
{
|
||||
luat_stop_rtos_timer(pwrkey_timer_handle);
|
||||
uint16_t vbat = get_vbat();
|
||||
audioQueueData currentElec = {0};
|
||||
currentElec.playType = TTS_PLAY;
|
||||
currentElec.priority = MONEY_PLAY;
|
||||
if (vbat > 4000)
|
||||
{
|
||||
char str[] = "当前电量高";
|
||||
currentElec.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(currentElec.message.tts.data, str, sizeof(str));
|
||||
currentElec.message.tts.len = sizeof(str);
|
||||
}
|
||||
else if (vbat > 3700 && vbat < 4000)
|
||||
{
|
||||
char str[] = "当前电量中";
|
||||
currentElec.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(currentElec.message.tts.data, str, sizeof(str));
|
||||
currentElec.message.tts.len = sizeof(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
char str[] = "当前电量低";
|
||||
currentElec.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(currentElec.message.tts.data, str, sizeof(str));
|
||||
currentElec.message.tts.len = sizeof(str);
|
||||
}
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, ¤tElec, NULL, 0))
|
||||
{
|
||||
free(currentElec.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_key_task start send audio fail");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CHARGE_START_MESSAGE:
|
||||
{
|
||||
audioQueueData chargeIn = {0};
|
||||
chargeIn.playType = TTS_PLAY;
|
||||
chargeIn.priority = MONEY_PLAY;
|
||||
char str[] = "正在充电";
|
||||
chargeIn.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(chargeIn.message.tts.data, str, sizeof(str));
|
||||
chargeIn.message.tts.len = sizeof(str);
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &chargeIn, NULL, 0))
|
||||
{
|
||||
free(chargeIn.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_key_task start send audio fail");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CHARGE_END_MESSAGE:
|
||||
{
|
||||
audioQueueData chargeOut = {0};
|
||||
chargeOut.playType = TTS_PLAY;
|
||||
chargeOut.priority = MONEY_PLAY;
|
||||
char str[] = "充电结束";
|
||||
chargeOut.message.tts.data = malloc(sizeof(str));
|
||||
memcpy(chargeOut.message.tts.data, str, sizeof(str));
|
||||
chargeOut.message.tts.len = sizeof(str);
|
||||
if (-1 == luat_rtos_queue_send(audio_queue_handle, &chargeOut, NULL, 0))
|
||||
{
|
||||
free(chargeOut.message.tts.data);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_key_task start send audio fail");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
luat_rtos_task_delete(key_task_handle);
|
||||
}
|
||||
|
||||
luat_pm_wakeup_pad_isr_callback_t down_key_callback(int num)
|
||||
{
|
||||
if (LUAT_PM_WAKEUP_PAD_0 == num)
|
||||
{
|
||||
if (0 == luat_pm_wakeup_pad_get_value(LUAT_PM_WAKEUP_PAD_0))
|
||||
{
|
||||
uint8_t id = KEY1_MESSAGE;
|
||||
luat_rtos_queue_send(key_queue_handle, &id, NULL, 0);
|
||||
}
|
||||
}
|
||||
else if (LUAT_PM_WAKEUP_PAD_3 == num)
|
||||
{
|
||||
if (0 == luat_pm_wakeup_pad_get_value(LUAT_PM_WAKEUP_PAD_3))
|
||||
{
|
||||
uint8_t id = KEY2_MESSAGE;
|
||||
luat_rtos_queue_send(key_queue_handle, &id, NULL, 0);
|
||||
}
|
||||
}
|
||||
else if (LUAT_PM_WAKEUP_PAD_4 == num)
|
||||
{
|
||||
if (0 == luat_pm_wakeup_pad_get_value(LUAT_PM_WAKEUP_PAD_4))
|
||||
{
|
||||
uint8_t id = KEY3_MESSAGE;
|
||||
luat_rtos_queue_send(key_queue_handle, &id, NULL, 0);
|
||||
}
|
||||
}
|
||||
// 新的开发板如果不接电池只插usb,这个中断会一直触发
|
||||
/* else if (LUAT_PM_WAKEUP_PAD_5 == num)
|
||||
{
|
||||
if (0 == luat_pm_wakeup_pad_get_value(LUAT_PM_WAKEUP_PAD_5))
|
||||
{
|
||||
uint8_t id = CHARGE_START_MESSAGE;
|
||||
luat_rtos_queue_send(key_queue_handle, &id, NULL, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t id = CHARGE_END_MESSAGE;
|
||||
luat_rtos_queue_send(key_queue_handle, &id, NULL, 0);
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
void key_task_init(void)
|
||||
{
|
||||
|
||||
luat_rtos_timer_create(&pwrkey_timer_handle);
|
||||
luat_rtos_timer_create(&powoff_timer_handle);
|
||||
|
||||
luat_pm_wakeup_pad_set_callback(down_key_callback);
|
||||
luat_pm_wakeup_pad_cfg_t cfg = {0};
|
||||
cfg.neg_edge_enable = 1;
|
||||
cfg.pos_edge_enable = 0;
|
||||
cfg.pull_up_enable = 1;
|
||||
cfg.pull_down_enable = 0;
|
||||
luat_pm_wakeup_pad_set(true, LUAT_PM_WAKEUP_PAD_0, &cfg);
|
||||
luat_pm_wakeup_pad_set(true, LUAT_PM_WAKEUP_PAD_3, &cfg);
|
||||
luat_pm_wakeup_pad_set(true, LUAT_PM_WAKEUP_PAD_4, &cfg);
|
||||
// 新的开发板如果不接电池只插usb,这个中断会一直触发
|
||||
/* cfg.pos_edge_enable = 1;
|
||||
luat_pm_wakeup_pad_set(true, LUAT_PM_WAKEUP_PAD_5, &cfg); */
|
||||
luat_pm_pwrkey_cfg_t pwrkey_cfg = {0};
|
||||
pwrkey_cfg.long_press_timeout = 3000;
|
||||
pwrkey_cfg.repeat_timeout = 3000;
|
||||
luat_pm_set_pwrkey(LUAT_PM_PWRKEY_WAKEUP_LOWACTIVE_MODE, true, &pwrkey_cfg, pwrkey_callback);
|
||||
|
||||
luat_rtos_queue_create(&key_queue_handle, KEY_QUEUE_SIZE, sizeof(key_message_t));
|
||||
int result = luat_rtos_task_create(&key_task_handle, 1024, 20, "key task", key_task, NULL, NULL);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_key_task create task result %d", result);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "common_api.h"
|
||||
#include "luat_debug.h"
|
||||
void fdb_init(void)
|
||||
{
|
||||
luat_kv_init();
|
||||
char value[2];
|
||||
int ret = luat_kv_get("flag", value, 2);
|
||||
//读取kv数据库用户是否初始化过,如果没有,则写入一个flag和需要初始化的值,表示用户已初始化;如果用户初始化过,则不做任何操作
|
||||
LUAT_DEBUG_PRINT("get value result %d", ret);
|
||||
if (ret > 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("get value %s", value);
|
||||
if(memcmp("1", value, strlen("1")))
|
||||
{
|
||||
LUAT_DEBUG_PRINT("need init");
|
||||
ret = luat_kv_set("flag", "1", 2);
|
||||
LUAT_DEBUG_PRINT("init result1 %d", ret);
|
||||
int volume = 15;
|
||||
ret = luat_kv_set("volume", &volume, sizeof(int));
|
||||
}
|
||||
else
|
||||
{
|
||||
LUAT_DEBUG_PRINT("no need init");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = luat_kv_set("flag", "1", 2);
|
||||
int volume = 15;
|
||||
ret = luat_kv_set("volume", &volume, sizeof(int));
|
||||
LUAT_DEBUG_PRINT("init result2 %d", ret);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_gpio.h"
|
||||
#include "common_api.h"
|
||||
#include "luat_debug.h"
|
||||
|
||||
#define LED_PIN_ALT_FUN 0
|
||||
|
||||
#define LED1_PIN HAL_GPIO_24
|
||||
#define LED2_PIN HAL_GPIO_28
|
||||
#define LED3_PIN HAL_GPIO_27
|
||||
|
||||
static luat_rtos_task_handle led_task_handle;
|
||||
|
||||
static void led_task(void *param)
|
||||
{
|
||||
luat_gpio_cfg_t gpio_cfg;
|
||||
luat_gpio_set_default_cfg(&gpio_cfg);
|
||||
luat_rtos_task_handle task_handle;
|
||||
|
||||
gpio_cfg.pull = LUAT_GPIO_DEFAULT;
|
||||
|
||||
//如果是780E+音频扩展小板,需要注释掉下面四行代码,因为这个板子上只有一个可控LED
|
||||
gpio_cfg.pin = LED1_PIN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
gpio_cfg.pin = LED2_PIN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
|
||||
|
||||
gpio_cfg.pin = LED3_PIN;
|
||||
luat_gpio_open(&gpio_cfg);
|
||||
|
||||
while (1)
|
||||
{
|
||||
luat_rtos_task_sleep(200);
|
||||
luat_gpio_set(LED1_PIN, 1); //如果是780E+音频扩展小板,需要注释掉此行代码
|
||||
luat_gpio_set(LED2_PIN, 1); //如果是780E+音频扩展小板,需要注释掉此行代码
|
||||
luat_gpio_set(LED3_PIN, 1);
|
||||
luat_rtos_task_sleep(200);
|
||||
luat_gpio_set(LED1_PIN, 0); //如果是780E+音频扩展小板,需要注释掉此行代码
|
||||
luat_gpio_set(LED2_PIN, 0); //如果是780E+音频扩展小板,需要注释掉此行代码
|
||||
luat_gpio_set(LED3_PIN, 0);
|
||||
}
|
||||
luat_rtos_task_delete(led_task_handle);
|
||||
}
|
||||
|
||||
void led_task_init(void)
|
||||
{
|
||||
int result = luat_rtos_task_create(&led_task_handle, 256, 20, "led task", led_task, NULL, NULL);
|
||||
LUAT_DEBUG_PRINT("cloud_speaker_led_task create task result %d", result);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "luat_debug.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "mqtt_publish.h"
|
||||
#include "MQTTClient.h"
|
||||
luat_rtos_task_handle mqtt_publish_task_handle = NULL;
|
||||
extern uint8_t g_s_is_link_up;
|
||||
void mqtt_publish_task(void *args)
|
||||
{
|
||||
uint32_t id; // 暂时没什么用,因为只有publish的时候才会触发这条消息
|
||||
int rc;
|
||||
MQTTClient *mqttClient;
|
||||
mqttClient = (MQTTClient *)(args);
|
||||
mqtt_publish_para_t *publish_para = NULL;
|
||||
while (1)
|
||||
{
|
||||
if (luat_rtos_message_recv(mqtt_publish_task_handle, &id, (void **)&publish_para, LUAT_WAIT_FOREVER) == 0)
|
||||
{
|
||||
if (!g_s_is_link_up)
|
||||
{
|
||||
if (publish_para->callback != NULL)
|
||||
publish_para->callback(NETWORK_NOT_READY);
|
||||
}
|
||||
else if (MQTTIsConnected(&mqttClient) == 0)
|
||||
{
|
||||
if (publish_para->callback != NULL)
|
||||
publish_para->callback(MQTT_NOT_READY);
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = MQTTPublish(mqttClient, publish_para->publish->topic, &publish_para->publish->message);
|
||||
if (rc != 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("MQTTPublish error %d", rc);
|
||||
if (publish_para->callback != NULL)
|
||||
publish_para->callback(MQTT_PUBLISH_FAIL);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (publish_para->callback != NULL)
|
||||
publish_para->callback(MQTT_PUBLISH_SUCCESS);
|
||||
}
|
||||
}
|
||||
if (publish_para->publish->topic != NULL)
|
||||
{
|
||||
free(publish_para->publish->topic);
|
||||
}
|
||||
|
||||
if (publish_para->publish->message.payload != NULL)
|
||||
{
|
||||
free(publish_para->publish->message.payload);
|
||||
}
|
||||
|
||||
if (publish_para->publish != NULL)
|
||||
{
|
||||
free(publish_para->publish);
|
||||
}
|
||||
|
||||
if (publish_para != NULL)
|
||||
{
|
||||
free(publish_para);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "luat_debug.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "MQTTClient.h"
|
||||
#include "mqtt_publish.h"
|
||||
luat_rtos_task_handle mqtt_send_task_handle;
|
||||
|
||||
extern luat_rtos_task_handle mqtt_publish_task_handle;
|
||||
|
||||
void publish_callback(int result)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("mqtt_send_task send mqtt message result %d", result);
|
||||
}
|
||||
|
||||
static void mqtt_send_task(void)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
mqtt_publish_para_t *publish_para = NULL;
|
||||
publish_para = (mqtt_publish_para_t *)malloc(sizeof(mqtt_publish_para_t));
|
||||
memset(publish_para, 0x00, sizeof(mqtt_publish_para_t));
|
||||
|
||||
publish_para->publish = (mqttSendMsg *)malloc(sizeof(mqttSendMsg));
|
||||
memset(publish_para->publish, 0x00, sizeof(mqttSendMsg));
|
||||
|
||||
char topic[] = "/cloud_speaker/publish/test_topic";
|
||||
publish_para->publish->topic = malloc(sizeof(topic));
|
||||
memcpy(publish_para->publish->topic, topic, sizeof(topic));
|
||||
publish_para->publish->topicLen = sizeof(topic);
|
||||
|
||||
char payload[] = "{\"time\": \"2000-00-00\"}";
|
||||
publish_para->publish->message.payload = malloc(sizeof(payload));
|
||||
memcpy(publish_para->publish->message.payload, payload, sizeof(payload));
|
||||
|
||||
publish_para->publish->message.payloadlen = sizeof(payload) - 1;
|
||||
publish_para->publish->message.qos = 1;
|
||||
publish_para->callback = publish_callback;
|
||||
uint32_t id = 0;
|
||||
if (luat_rtos_message_send(mqtt_publish_task_handle, &id, (void *)publish_para) != 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("colud_speaker_mqtt publish message send error");
|
||||
free(publish_para->publish->topic);
|
||||
free(publish_para->publish->message.payload);
|
||||
free(publish_para->publish);
|
||||
free(publish_para);
|
||||
|
||||
}
|
||||
luat_rtos_task_sleep(5000);
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_send_task_init(void)
|
||||
{
|
||||
luat_rtos_task_create(&mqtt_send_task_handle, 2048, 20, "mqtt_send_task", mqtt_send_task, NULL, 0);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#include "luat_uart.h"
|
||||
#include "luat_debug.h"
|
||||
|
||||
void luat_uart_recv_cb(int uart_id, uint32_t data_len){
|
||||
char* data_buff = malloc(data_len+1);
|
||||
memset(data_buff,0,data_len+1);
|
||||
luat_uart_read(uart_id, data_buff, data_len);
|
||||
LUAT_DEBUG_PRINT("luat_uart_cb uart_id:%d data:%s data_len:%d",uart_id,data_buff,data_len);
|
||||
if (strcmp("AT+CLIENTID?\r\n", data_buff) == 0)
|
||||
{
|
||||
char clientId[32] = {0};
|
||||
int ret = luat_kv_get("clientId", clientId, 16);
|
||||
if(ret > 0 )
|
||||
{
|
||||
luat_uart_write(LUAT_VUART_ID_0, clientId, strlen(clientId));
|
||||
}
|
||||
else
|
||||
{
|
||||
luat_uart_write(LUAT_VUART_ID_0, "OK", strlen("OK"));
|
||||
}
|
||||
}
|
||||
else if (strstr(data_buff, "AT+CLIENTID=") != NULL)
|
||||
{
|
||||
char *flag = NULL;
|
||||
flag = strstr(data_buff, "=");
|
||||
if(flag != NULL)
|
||||
{
|
||||
flag++;
|
||||
char *flag2 = NULL;
|
||||
flag2 = strstr(flag, "\r\n");
|
||||
if (flag2 != NULL)
|
||||
{
|
||||
int ret = luat_kv_set("clientId", flag, strlen(flag) - 2); //减去末尾的\r\n
|
||||
luat_uart_write(LUAT_VUART_ID_0, "OK", strlen("OK"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else if (strcmp("AT+USERNAME?\r\n", data_buff) == 0)
|
||||
{
|
||||
int ret = 0;
|
||||
char name[32] = {0};
|
||||
ret = luat_kv_get("username", name, 31);
|
||||
if (ret > 0)
|
||||
{
|
||||
luat_uart_write(LUAT_VUART_ID_0, name, strlen(name));
|
||||
}
|
||||
else
|
||||
{
|
||||
luat_uart_write(LUAT_VUART_ID_0, "OK", strlen("OK"));
|
||||
}
|
||||
}
|
||||
else if (strstr(data_buff, "AT+USERNAME=") != NULL)
|
||||
{
|
||||
char *flag = NULL;
|
||||
flag = strstr(data_buff, "=");
|
||||
if(flag != NULL)
|
||||
{
|
||||
flag++;
|
||||
char *flag2 = NULL;
|
||||
flag2 = strstr(flag, "\r\n");
|
||||
if (flag2 != NULL)
|
||||
{
|
||||
int ret = luat_kv_set("username", flag, strlen(flag) - 2); //减去末尾的\r\n
|
||||
luat_uart_write(LUAT_VUART_ID_0, "OK", strlen("OK"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else if (strcmp("AT+PASSWORD?\r\n", data_buff) == 0)
|
||||
{
|
||||
int ret = 0;
|
||||
char name[32] = {0};
|
||||
ret = luat_kv_get("password", name, 31);
|
||||
if (ret > 0)
|
||||
{
|
||||
luat_uart_write(LUAT_VUART_ID_0, name, strlen(name));
|
||||
}
|
||||
else
|
||||
{
|
||||
luat_uart_write(LUAT_VUART_ID_0, "OK", strlen("OK"));
|
||||
}
|
||||
}
|
||||
else if (strstr(data_buff, "AT+PASSWORD=") != NULL)
|
||||
{
|
||||
char *flag = NULL;
|
||||
flag = strstr(data_buff, "=");
|
||||
if(flag != NULL)
|
||||
{
|
||||
flag++;
|
||||
char *flag2 = NULL;
|
||||
flag2 = strstr(flag, "\r\n");
|
||||
if (flag2 != NULL)
|
||||
{
|
||||
int ret = luat_kv_set("password", flag, strlen(flag) - 2); //减去末尾的\r\n
|
||||
luat_uart_write(LUAT_VUART_ID_0, "OK", strlen("OK"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
free(data_buff);
|
||||
}
|
||||
|
||||
void usb_uart_init(void)
|
||||
{
|
||||
char send_buff[] = "hello LUAT!!!\n";
|
||||
luat_uart_t uart = {
|
||||
.id = LUAT_VUART_ID_0,
|
||||
};
|
||||
luat_uart_setup(&uart);
|
||||
luat_uart_ctrl(LUAT_VUART_ID_0, LUAT_UART_SET_RECV_CALLBACK, luat_uart_recv_cb);
|
||||
}
|
||||
Reference in New Issue
Block a user