//
|
// 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;
|
}
|
}
|