//
|
// 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 = 0;
|
static int engine_rpm = 0;
|
|
static struct sensor_cfg {
|
int gpioId;
|
int funId;
|
int validLvl;
|
int currValue;
|
} SensorConfig[MAX_SENSOR_NUM];
|
static int SensorNum = 0;
|
|
static pthread_mutex_t sonser_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
void CarSensorInit(void)
|
{
|
gpioStore = 0;
|
SensorNum = 0;
|
memset(SensorConfig, 0, sizeof(SensorConfig));
|
|
pthread_mutex_init(&sonser_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;
|
}
|
if (gpioStore & BV(SensorConfig[i].gpioId)) {
|
SensorConfig[i].currValue = 1;
|
} else {
|
SensorConfig[i].currValue = 0;
|
}
|
}
|
|
pthread_mutex_unlock(&sonser_mutex);
|
}
|
|
void UpdateSensor(uint16_t gpio, uint16_t speed, uint16_t engine)
|
{
|
uint16_t chg = 0;
|
|
int reportSensor[MAX_SENSOR_NUM];
|
int reportValue[MAX_SENSOR_NUM];
|
int reportNum = 0;
|
|
pthread_mutex_lock(&sonser_mutex);
|
|
chg = gpioStore^gpio;
|
gpioStore = gpio;
|
engine_rpm = engine;
|
|
for (int i = 0; i < SensorNum; ++i) {
|
if (chg & BV(SensorConfig[i].gpioId)) {
|
if (gpio & BV(SensorConfig[i].gpioId)) {
|
SensorConfig[i].currValue = 1;
|
} else {
|
SensorConfig[i].currValue = 0;
|
}
|
|
reportSensor[reportNum] = SensorConfig[i].funId;
|
reportValue[reportNum] = (SensorConfig[i].validLvl == SensorConfig[i].currValue)?1:0;
|
reportNum++;
|
}
|
}
|
|
reportSensor[reportNum] = SENSOR_ENGINE_RPM;
|
reportValue[reportNum] = engine;
|
reportNum++;
|
|
pthread_mutex_unlock(&sonser_mutex);
|
|
SensorChanged(reportSensor, reportValue, reportNum);
|
}
|
|
int ReadSensor(uint16_t sensor_id)
|
{
|
int ret = -1;
|
|
pthread_mutex_lock(&sonser_mutex);
|
for (int i = 0; i < SensorNum; ++i) {
|
if (sensor_id == SensorConfig[i].funId) {
|
ret = (SensorConfig[i].validLvl == SensorConfig[i].currValue)?1:0;
|
}
|
}
|
|
if (sensor_id == SENSOR_ENGINE_RPM) {
|
ret = engine_rpm;
|
}
|
pthread_mutex_unlock(&sonser_mutex);
|
|
return ret;
|
}
|