fctom1215
2020-02-19 dc2a01d4764efd33a23afcaf4f1d7543dc35c4fa
lib/src/main/cpp/test_items/comm_test.cpp
@@ -5,20 +5,37 @@
#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 uint16_t engineStore = 0;
static void SensorChange(int index, bool value);
static void SensorChange(int gpio, bool value);
static void EngineStartHold(union sigval sig);
void CommTestStart(void)
void CommTestInit(void)
{
    gpioStore = engineStore = 0;
    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)
@@ -27,37 +44,128 @@
    uint16_t chg = gpioStore^gpio;
    if (chg == 0)
        return;
    gpioStore = gpio;
    for (int i = 0; i < 16; ++i) {
        if (chg & BV(i)) {
            SensorChange(i, (bool)(gpio & BV(i)));
    if (commTest) {
        for (int i = 0; i < 16; ++i) {
            if (chg & BV(i)) {
                SensorChange(i, (bool) (gpio & BV(i)));
            }
        }
    }
    gpioStore = gpio;
    // 安全带
    // 挡位
    // 启动指示
    // 熄火监控
    if (engine < ENGINE_MIN_ROTATE) {
        if (engineStart) {
            if (commTest) {
                // 熄火1次,扣10分
                struct RtkTime rt;
                GetRtkClock(&rt);
                AddExamFault(5, &rt);
            }
            engineStart = false;
        }
    } else {
        engineStart = true;
    }
}
static void SensorChange(int index, bool value)
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:
            break;
        case SENSOR_TURNRIGHT:
            break;
            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;
    }
}