yy1717
2020-03-05 1308a26e8b89ff2ef9435e609580469182bdd0c0
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
//
// Created by fctom on 2020/2/13.
//
 
#include "comm_test.h"
#include "../driver_test.h"
#include "../defs.h"
#include "../common/apptimer.h"
 
static bool seatbeltInsert;
static bool engineStart;
 
const int ENGINE_MIN_ROTATE = 200;
 
static bool commTest = false;
static uint16_t gpioStore = 0;
 
static void SensorChange(int gpio, bool value);
static void EngineStartHold(union sigval sig);
 
void CommTestInit(void)
{
    gpioStore = 0;
    engineStart = true;
    commTest = false;
}
 
void CommTestStart(bool start)
{
    commTest = start;
    if (start) {
        // 检查安全带
        if (CheckSensorX(SEATBELT) == 0) {
            struct RtkTime rt;
            GetRtkClock(&rt);
//            AddExamFault(1, &rt);
        }
    }
}
 
void UpdateSensor(uint16_t gpio, uint16_t speed, uint16_t engine)
{
    int idx, lvl;
 
    uint16_t chg = gpioStore^gpio;
 
    gpioStore = gpio;
 
    if (commTest) {
        for (int i = 0; i < 16; ++i) {
            if (chg & BV(i)) {
                SensorChange(i, (bool) (gpio & BV(i)));
            }
        }
    }
 
    if (engine < ENGINE_MIN_ROTATE) {
        if (engineStart) {
            if (commTest) {
                // 熄火1次,扣10分
                struct RtkTime rt;
                GetRtkClock(&rt);
                AddExamFault(5, &rt);
            }
            engineStart = false;
        }
    } else {
        engineStart = true;
    }
}
 
int CheckSensorX(int func)
{
    int gpio;
    bool level;
    int validLevel = GetSensorValidLevel();
 
    // 传感器无效,认为通过
    switch (func) {
        case SEATBELT:
            FindSensorCfg(SENSOR_SEATBELT, gpio, level);
            if (gpio >= 0 && (gpioStore & BV(gpio)) != (validLevel & BV(gpio)) ) {
                return 0;
            }
            return 1;
        case LEFT_TURN_SIGNAL:
            FindSensorCfg(SENSOR_LEFT_TURN_SIGNAL, gpio, level);
            if (gpio == -1 || (gpioStore & BV(gpio)) == (validLevel & BV(gpio)) ) {
                return 1;
            }
            return 0;
        case RIGHT_TURN_SIGNAL:
            FindSensorCfg(SENSOR_RIGHT_TURN_SIGNAL, gpio, level);
            if (gpio == -1 || (gpioStore & BV(gpio)) == (validLevel & BV(gpio)) ) {
                return 1;
            }
            return 0;
        case HANDBREAK:
            FindSensorCfg(SENSOR_HANDBREAK, gpio, level);
            if (gpio == -1 || (gpioStore & BV(gpio)) == (validLevel & BV(gpio)) ) {
                return 1;
            }
            return 0;
        case SHIFT:
            FindSensorCfg(SENSOR_SHIFT_N, gpio, level);
            if (gpio == -1 || (gpioStore & BV(gpio)) == (validLevel & BV(gpio))) {
                return 'N';
            }
            return 0;
        default:
            break;
    }
 
    return -1;
}
 
static void EngineStartHold(union sigval sig) {
    AppTimer_delete(EngineStartHold);
 
    // 不及时松开启动开关,扣10分
    struct RtkTime rt;
    GetRtkClock(&rt);
    AddExamFault(4, &rt);
}
 
static void SensorChange(int gpio, bool value)
{
    int func;
    bool level;
 
    GetSensorCfg(gpio, func, level);
 
    switch (func) {
        case SENSOR_SEATBELT:
            if (level != value) {
                // 不使用安全带,不合格
                seatbeltInsert = false;
 
                struct RtkTime rt;
                GetRtkClock(&rt);
                AddExamFault(1, &rt);
            } else {
                seatbeltInsert = true;
            }
            break;
        case SENSOR_RIGHT_TURN_SIGNAL:
            break;
        case SENSOR_LEFT_TURN_SIGNAL:
            break;
        case SENSOR_HANDBREAK:
            break;
        case SENSOR_ENGINE_START:
            AppTimer_delete(EngineStartHold);
            if (level == value) {
                AppTimer_add(EngineStartHold, D_SEC(2));
 
                // 检查是否在空挡
                if (CheckSensorX(SHIFT) != 'N') {
                    // 不是空挡点火,不合格
                    struct RtkTime rt;
                    GetRtkClock(&rt);
                    AddExamFault(3, &rt);
                }
            } else {
 
            }
            break;
        default:
            break;
    }
}