//
|
// Created by fctom on 2020/2/13.
|
//
|
|
#include <pthread.h>
|
#include "car_sensor.h"
|
#include "../driver_test.h"
|
#include "../defs.h"
|
#include "../common/apptimer.h"
|
#include "../jni_log.h"
|
|
#define DEBUG(fmt, args...) LOGD("<car_sensor> <%s>: " fmt, __func__, ##args)
|
|
#define MAX_SENSOR_NUM 32
|
|
static uint16_t gpioStore;
|
static int left_turn_signal, right_turn_signal;
|
static int flashMainBeamCnt;
|
|
enum {
|
SENSOR_SEATBELT,
|
SENSOR_LEFT_TURN_SIGNAL,
|
SENSOR_RIGHT_TURN_SIGNAL,
|
SENSOR_HANDBREAK,
|
SENSOR_BREAK,
|
SENSOR_CLEARANCE_LIGHT,
|
SENSOR_DIPPED_BEAM_LIGHT,
|
SENSOR_MAIN_BEAM_LIGHT,
|
SENSOR_DOOR,
|
SENSOR_SHIFT_N,
|
SENSOR_SHIFT_1,
|
SENSOR_SHIFT_2,
|
SENSOR_SHIFT_3,
|
SENSOR_SHIFT_4,
|
SENSOR_SHIFT_5,
|
SENSOR_SHIFT_R,
|
SENSOR_ENGINE_START,
|
SENSOR_ENGINE_RPM,
|
SENSOR_SPEED
|
};
|
|
|
|
static int CarStatus[CAR_STATUS_END];
|
|
static struct sensor_cfg {
|
int gpioId;
|
int funId;
|
int validLvl;
|
} SensorConfig[MAX_SENSOR_NUM];
|
static int SensorNum = 0;
|
|
static pthread_mutex_t sonser_mutex = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t status_rw_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
static void WriteCarStatus(uint16_t id, int value);
|
static void LRLightTimeout(union sigval sig);
|
static void ChangeLRLight(int light);
|
static void ConfirmTurnSigalLater(union sigval sig);
|
static void flashBeamLightClose(union sigval sig);
|
static void confirmFlashBeamLightLater(union sigval sig);
|
static void SensorChanged(int id, int value);
|
|
void CarSensorInit(void)
|
{
|
gpioStore = 0;
|
SensorNum = 0;
|
memset(SensorConfig, 0, sizeof(SensorConfig));
|
|
memset(CarStatus, 0, sizeof(CarStatus));
|
|
left_turn_signal = right_turn_signal = 0;
|
flashMainBeamCnt = 0;
|
|
pthread_mutex_init(&sonser_mutex, NULL);
|
pthread_mutex_init(&status_rw_mutex, NULL);
|
}
|
|
void SetSensorCfg(int (*sensor)[3], int sensorNum)
|
{
|
DEBUG("加入传感器配置 sensorNum %d", sensorNum);
|
|
pthread_mutex_lock(&sonser_mutex);
|
|
SensorNum = sensorNum;
|
for (int i = 0; i < sensorNum; ++i) {
|
SensorConfig[i].gpioId = sensor[i][0];
|
SensorConfig[i].funId = sensor[i][1];
|
if (sensor[i][2] > 0) {
|
SensorConfig[i].validLvl = 1;
|
} else {
|
SensorConfig[i].validLvl = 0;
|
}
|
|
int level = 0;
|
|
if (gpioStore & BV(SensorConfig[i].gpioId)) {
|
level = 1;
|
}
|
|
if (level == SensorConfig[i].validLvl) {
|
SensorChanged(SensorConfig[i].funId, 1);
|
} else {
|
SensorChanged(SensorConfig[i].funId, 0);
|
}
|
}
|
|
pthread_mutex_unlock(&sonser_mutex);
|
}
|
|
void UpdateSensor(uint16_t gpio, uint16_t speed, uint16_t rpm)
|
{
|
uint16_t chg = 0;
|
|
pthread_mutex_lock(&sonser_mutex);
|
chg = gpioStore^gpio;
|
gpioStore = gpio;
|
pthread_mutex_unlock(&sonser_mutex);
|
|
WriteCarStatus(OBD_SPEED, speed);
|
WriteCarStatus(ENGINE_RPM, rpm);
|
|
for (int i = 0; i < SensorNum; ++i) {
|
if (chg & BV(SensorConfig[i].gpioId)) {
|
int level = 0;
|
|
if (gpio & BV(SensorConfig[i].gpioId)) {
|
level = 1;
|
}
|
|
if (level == SensorConfig[i].validLvl) {
|
SensorChanged(SensorConfig[i].funId, 1);
|
} else {
|
SensorChanged(SensorConfig[i].funId, 0);
|
}
|
}
|
}
|
|
|
}
|
|
int ReadCarStatus(uint16_t id)
|
{
|
int value;
|
|
pthread_mutex_lock(&status_rw_mutex);
|
value = CarStatus[id];
|
pthread_mutex_unlock(&status_rw_mutex);
|
|
return value;
|
}
|
|
static void WriteCarStatus(uint16_t id, int value)
|
{
|
pthread_mutex_lock(&status_rw_mutex);
|
CarStatus[id] = value;
|
pthread_mutex_unlock(&status_rw_mutex);
|
}
|
|
static void LRLightTimeout(union sigval sig)
|
{
|
AppTimer_delete(LRLightTimeout);
|
|
WriteCarStatus(TURN_SIGNAL_LAMP, OFF_LIGHT);
|
}
|
|
static void ChangeLRLight(int light)
|
{
|
WriteCarStatus(TURN_SIGNAL_LAMP, light);
|
|
AppTimer_delete(LRLightTimeout);
|
AppTimer_add(LRLightTimeout, 1500);
|
}
|
|
static void ConfirmTurnSigalLater(union sigval sig)
|
{
|
AppTimer_delete(ConfirmTurnSigalLater);
|
|
ChangeLRLight(sig.sival_int);
|
}
|
|
static void flashBeamLightClose(union sigval sig)
|
{
|
WriteCarStatus(FLASH_BEAM_LAMP, OFF_LIGHT);
|
}
|
|
static void confirmFlashBeamLightLater(union sigval sig)
|
{
|
AppTimer_delete(confirmFlashBeamLightLater);
|
flashMainBeamCnt = 0;
|
}
|
|
static void SensorChanged(int id, int value)
|
{
|
switch (id) {
|
case SENSOR_LEFT_TURN_SIGNAL: {
|
left_turn_signal = value;
|
|
if (left_turn_signal) {
|
AppTimer_delete(ConfirmTurnSigalLater);
|
if (right_turn_signal) {
|
// 判定为双闪
|
ChangeLRLight(HAZARD_LIGHTS);
|
} else {
|
AppTimer_add(ConfirmTurnSigalLater, 200, LEFT_TURN_LIGHT);
|
}
|
}
|
break;
|
}
|
case SENSOR_RIGHT_TURN_SIGNAL: {
|
right_turn_signal = value;
|
|
if (right_turn_signal) {
|
AppTimer_delete(ConfirmTurnSigalLater);
|
if (left_turn_signal) {
|
// 判定为双闪
|
ChangeLRLight(HAZARD_LIGHTS);
|
} else {
|
AppTimer_add(ConfirmTurnSigalLater, 200, RIGHT_TURN_LIGHT);
|
}
|
}
|
break;
|
}
|
case SENSOR_CLEARANCE_LIGHT: {
|
if (value == 0) {
|
WriteCarStatus(CLEARANCE_LAMP, OFF_LIGHT);
|
} else {
|
WriteCarStatus(CLEARANCE_LAMP, CLEARANCE_LIGHT);
|
}
|
break;
|
}
|
case SENSOR_DIPPED_BEAM_LIGHT: {
|
if (value == 0) {
|
WriteCarStatus(DIPPED_BEAM_LAMP, OFF_LIGHT);
|
} else {
|
WriteCarStatus(DIPPED_BEAM_LAMP, DIPPED_BEAM_LIGHT);
|
}
|
break;
|
}
|
case SENSOR_MAIN_BEAM_LIGHT: {
|
if (value == 0) {
|
WriteCarStatus(MAIN_BEAM_LAMP, OFF_LIGHT);
|
} else {
|
WriteCarStatus(MAIN_BEAM_LAMP, MAIN_BEAM_LIGHT);
|
}
|
|
if (++flashMainBeamCnt > 3) { // 亮-灭-亮-灭
|
WriteCarStatus(FLASH_BEAM_LAMP, FLASH_BEAM_LIGHT);
|
|
AppTimer_delete(flashBeamLightClose);
|
AppTimer_add(flashBeamLightClose, D_SEC(5));
|
}
|
|
AppTimer_delete(confirmFlashBeamLightLater);
|
AppTimer_add(confirmFlashBeamLightLater, D_SEC(3));
|
break;
|
}
|
case SENSOR_SEATBELT: {
|
if (value == 0) {
|
WriteCarStatus(SEATBELT, EJECT_SEATBELT);
|
} else {
|
WriteCarStatus(SEATBELT, INSERT_SEATBELT);
|
}
|
break;
|
}
|
case SENSOR_ENGINE_START: {
|
break;
|
}
|
default:
|
break;
|
}
|
}
|