yy1717
2020-03-13 4c550a479530c4c1b2569b194dae2be6afb66088
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
//
// 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;
}