yincheng.zhong
8 天以前 68c43c5adef03f00836d83b37cb83a294d8b0354
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
/*******************************************************************************
 * File Name         : BT.c
 * Description       :
 * Created on        : 2018Äê7ÔÂ23ÈÕ
 * Author            : ¶Å¼ü
 *******************************************************************************/
 
/*******************************************************************************
 *                              Include Files                                  *
 *******************************************************************************/
#include "stdio.h"
#include "stdarg.h"
#include "string.h"
#include "AppConfig.h"
#include "HIDO_VLQueue.h"
#include "HIDO_Input.h"
#include "HIDO_Timer.h"
#include "HIDO_Util.h"
#include "bluetooth.h"
#include "DBG.h"
#include "mainex.h"
#include "Uart.h"
 
/*******************************************************************************
 *                                  Macro                                      *
 *******************************************************************************/
// È«¾Ö״̬±êÖ¾
 
 
static HIDO_UINT8 l_au8BTUartRxBuf[BT_UART_RX_BUF_SIZE];
static HIDO_UINT8 l_au8BTUartTxBuf[BT_UART_TX_BUF_SIZE];
HIDO_UINT8 uart6_dma_rxbuf[UART6_DMA_RX_BUF_SIZE] = {0};
HIDO_UINT8 uart6_dma_recv_end_flag = 0;
HIDO_UINT8 uart6_dma_recv_len = 0;
 
/*******************************************************************************
 *                             Local Variable                                  *
 *******************************************************************************/
 
 
/*******************************************************************************
 * Function Name     : DBG_Init
 * Description       : µ÷ÊÔ´òÓ¡³õʼ»¯
 * Input             : None
 * Output            : None
 * Return            : None
 * Author            : ¶Å¼ü
 * Modified Date:    : 2018Äê7ÔÂ23ÈÕ
 *******************************************************************************/
HIDO_VOID BT_Init(void)
{
    ST_UartInit stInit;
 
    memset(&stInit, 0, sizeof(stInit));
    stInit.m_eRxMode = UART_RX_MODE_DMA;
    stInit.m_eTxMode = UART_TX_MODE_DMA;
    stInit.m_pu8RxBuf = l_au8BTUartRxBuf;
    stInit.m_u32RxBufSize = BT_UART_RX_BUF_SIZE;
    stInit.m_pu8TxBuf = l_au8BTUartTxBuf;
    stInit.m_u32TxBufSize = BT_UART_TX_BUF_SIZE;
    stInit.m_u32TxQueueMemberCnt = BT_UART_TX_QUEUE_MEMBER_CNT;
    Uart_Init(UART_ID_BT, &stInit);
 
 
}
 
/**
 * @brief Æô¶¯ UART6 ½ÓÊÕ£¨ÆôÓàIDLE ÖжϠ+ DMA£©
 */
void UART6_StartReceive(void)
{
    // Çå³ý±ê־룬·ÀÖ¹ÉϵçÎó´¥·¢
    __HAL_UART_CLEAR_IDLEFLAG(&huart6);
 
    // Ê¹ÄÜ IDLE ÖжÏ
    __HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE);
 
    // Æô¶¯ DMA ½ÓÊÕ£¨Ñ­»·Ä£Ê½£©
    HAL_UART_Receive_DMA(&huart6, uart6_dma_rxbuf, UART6_DMA_RX_BUF_SIZE);
}
 
/**
 * @brief ·¢ËÍ×Ö·û´®
 */
void UART6_SendString(const char *str)
{
    HAL_UART_Transmit_DMA(&huart6, (uint8_t *)str, strlen(str));
}
 
/**
 * @brief ·¢ËÍÊý¾ÝÊý×é
 */
void UART6_SendArray(uint8_t *data, uint16_t len)
{
    HAL_UART_Transmit_DMA(&huart6, data, len);
}
 
void Joystick_Process(Joystick_t *joy)
{
    // ÌáÈ¡ y1 ¿ØÖÆÇ°½ø/ºóÍË
    int16_t motor_val = joy->y1;  // -100 ~ +100
    Set_Motor_PWM(motor_val);
 
    // ÌáÈ¡ x2 ¿ØÖÆ×ªÏò
    int16_t steering_val = joy->x2;  // -100 ~ +100
    Set_Steering_PWM(steering_val);
 
}
 
// Ê¾ÀýÊäÈë: "[joystick,-22,-2,0,0]"
void Parse_Joystick_Data(char *data)
{
    Joystick_t joy = {0};
 
    if (strstr(data, "joystick") == NULL) return;
 
    // Ìø¹ý "[joystick,"
    char *ptr = strstr(data, "joystick");
 
    if (!ptr) return;
 
    ptr += 10;  // Ìø¹ý "joystick,"
 
    // ½âÎöËĸöÕûÊý
    char *end;
    joy.x1 = strtol(ptr, &end, 10);
 
    if (end == ptr) return;
 
    ptr = end + 1;
 
    joy.y1 = strtol(ptr, &end, 10);
 
    if (end == ptr) return;
 
    ptr = end + 1;
 
    joy.x2 = strtol(ptr, &end, 10);
 
    if (end == ptr) return;
 
    ptr = end + 1;
 
    joy.y2 = strtol(ptr, &end, 10);
 
    // ´¦ÀíÊý¾Ý
    Joystick_Process(&joy);
}
 
 
/*******************************************************************************
 * Function Name     : BT_Init
 * Description       : µ÷ÊÔ´òÓ¡ÂÖѯ
 * Input             : None
 * Output            : None
 * Return            : None
 * Author            : ¶Å¼ü
 * Modified Date:    : 2018Äê7ÔÂ23ÈÕ
 *******************************************************************************/
HIDO_VOID BT_Poll(void)
{
        static HIDO_UINT8 l_uart6_dma_rxbuf[100];
    if (uart6_dma_recv_len > 0) //HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_13) == GPIO_PIN_RESET
    {
        if (uart6_dma_recv_end_flag == 1) //½ÓÊÕÍê³É±êÖ¾
        {
            HIDO_UtilSnprintf((HIDO_CHAR *)l_uart6_dma_rxbuf, sizeof(l_uart6_dma_rxbuf), "buff=%s\r\n", uart6_dma_rxbuf);
            Uart_Send(UART_ID_DBG, (HIDO_UINT8 *)l_uart6_dma_rxbuf, strlen(l_uart6_dma_rxbuf));
            // ½âÎöÊý¾Ý
            Parse_Joystick_Data(uart6_dma_rxbuf);
        }
 
        uart6_dma_recv_len = 0;//Çå³ý¼ÆÊý
        uart6_dma_recv_end_flag = 0;//Çå³ý½ÓÊÕ½áÊø±ê־λ
        memset(uart6_dma_rxbuf, 0, UART6_DMA_RX_BUF_SIZE);
        __HAL_UART_CLEAR_IDLEFLAG(&huart6);
        HAL_UART_Receive_DMA(&huart6, uart6_dma_rxbuf, UART6_DMA_RX_BUF_SIZE); //ÖØÐ´ò¿ªDMA½ÓÊÕ
    }
 
}