zhyinch
2018-10-13 be9861e4fc5ac9984218975f3d5594f1db848224
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
#include "serial_at_cmd_app.h"
#include "global_param.h"
#include <string.h>
#include <stdio.h>
#include "dw_app.h"
 
uint8_t m_frame_data[MAX_FRAME_LEN] = {0};
uint8_t m_frame_data_len = 0;
 
void ParseFrame(void)
{
    if(!memcmp(m_frame_data, "DIS1", 4))
    {
        g_com_map[ALARM_DISTANCE1] =0;
        for(uint8_t i=5; i < m_frame_data_len; i++)
        {
            if(m_frame_data[i]<58 && m_frame_data[i]>47)
            {
                g_com_map[ALARM_DISTANCE1] = g_com_map[ALARM_DISTANCE1]*10 + m_frame_data[i]- '0';
            }
            else
            {
                printf("Error: Wrong DISTANCE.\r\n");
                break;
            }
        }
        printf("When distance1 < %d cm, alarm1 is on.\n", g_com_map[ALARM_DISTANCE1]);
        save_com_map_to_flash();
    }
    else if(!memcmp(m_frame_data, "DIS2", 4))
    {
        g_com_map[ALARM_DISTANCE2] =0;
        for(uint8_t i=5; i < m_frame_data_len; i++)
        {
            if(m_frame_data[i]<58 && m_frame_data[i]>47)
            {
                g_com_map[ALARM_DISTANCE2] = g_com_map[ALARM_DISTANCE2]*10 + m_frame_data[i]- '0';
            }
            else
            {
                printf("Error: Wrong DISTANCE.\r\n");
                break;
            }
        }
        printf("When distance2 < %d cm, alarm2 is on.\n", g_com_map[ALARM_DISTANCE2]);
        save_com_map_to_flash();
    }
    else if(!memcmp(m_frame_data, "DIS3", 4))
    {
        g_com_map[ALARM_DISTANCE3] =0;
        for(uint8_t i=5; i < m_frame_data_len; i++)
        {
            if(m_frame_data[i]<58 && m_frame_data[i]>47)
            {
                g_com_map[ALARM_DISTANCE3] = g_com_map[ALARM_DISTANCE3]*10 + m_frame_data[i]- '0';
            }
            else
            {
                printf("Error: Wrong DISTANCE.\r\n");
                break;
            }
        }
        printf("When distance3 < %d cm, alarm3 is on.\n", g_com_map[ALARM_DISTANCE3]);
        save_com_map_to_flash();
    }
    else if(!memcmp(m_frame_data, "DEV", 3))
    {
        g_com_map[ALARM_DEV] = m_frame_data[4]-'0';
        printf("Alarm device is %d. \n", g_com_map[ALARM_DEV]);
        save_com_map_to_flash();
    }
    else if(!memcmp(m_frame_data, "ID", 2))
    {
        g_com_map[DEV_ID] = 0;
        for(uint8_t i=3; i < m_frame_data_len; i++)
        {
            if(m_frame_data[i]<58 && m_frame_data[i]>47)
            {
                g_com_map[DEV_ID] = g_com_map[DEV_ID]*10 + m_frame_data[i]- '0';
            }
            else
            {
                printf("Error: Wrong ID.\r\n");
                break;
            }
        }
        Dw1000_App_Init();
        printf("set dev id = %d. \r\n", g_com_map[DEV_ID]);
        save_com_map_to_flash();
    }
}
 
void UsartParseDataHandler(uint8_t data)
{
    static uint8_t s_usart_state = 0;
    static uint8_t s_data_pos;
    
    switch(s_usart_state)
    {
        case 0:
            if(data=='A')    s_usart_state=  1;
        break;
            
        case 1:
            if(data=='T')    s_usart_state = 2;
        break;
        
        case 2:
            if(data=='+')
            { 
                s_usart_state = 3;
                s_data_pos = 0;
                m_frame_data_len = 0;
            }
        break;
            
        case 3:
            m_frame_data_len = s_data_pos;
            m_frame_data[s_data_pos++] = data;
            if(data == 0x0d)
            {
                s_usart_state = 4;
            }
            if(s_data_pos >= MAX_FRAME_LEN) //Èç¹û³¬¹ý×î´ó³¤¶È
            {
                m_frame_data_len = MAX_FRAME_LEN;
                ParseFrame();
                s_usart_state = 0;
            }
        break;
            
        case 4:
            if(data == 0x0a)
            {
                ParseFrame();
                s_usart_state = 0;
            }
        break;
            
        default: break;
    }
}