Subversion Repositories libIgnTiming

Rev

Rev 8 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8 Rev 11
Line 1... Line 1...
1
#include <cstdint>
1
#include <cstdint>
2
#include <assert.h>
2
#include <assert.h>
3
#include "libIgnTiming/timing.h"
3
#include "libIgnTiming/timing.h"
-
 
4
 
-
 
5
#if !defined WRITABLE_TABLE
-
 
6
#define CONST_ATTR constexpr
-
 
7
#endif
-
 
8
 
4
#if defined __cplusplus
9
#if defined __cplusplus
5
extern "C"
10
extern "C"
6
{
11
{
7
#endif
12
#endif
8
 
13
 
9
    namespace
14
    namespace
10
    {
15
    {
-
 
16
        int8_t timingAdjust = 0 * TIMING_SCALE; // in TIMING_SCALE
11
 
17
 
12
        unsigned constexpr INTERP_SCALE = 256;
18
        unsigned constexpr INTERP_SCALE = 256;
13
 
19
 
14
        int constexpr TimingScale = TIMING_SCALE;
20
        int constexpr TimingScale = TIMING_SCALE;
15
        int16_t constexpr NO_DATA = -1;
21
        int16_t constexpr NO_DATA = -1;
16
 
22
 
17
        int16_t MAX_ADVANCE = 50 * TIMING_SCALE;
23
        int16_t constexpr MAX_ADVANCE = 50 * TIMING_SCALE;
18
        int16_t MIN_ADVANCE = 7 * TIMING_SCALE;
24
        int16_t constexpr MIN_ADVANCE = 7 * TIMING_SCALE;
19
 
25
 
20
        int8_t timingAdjust = 0; // in TIMING_SCALE
-
 
21
        // array of column headings
26
        // array of column headings
22
        int16_t rpmMap[MAX_RPM_POINTS] = {400, 750, 1000, 1500, 2500, 3500, 4500, 6000};
27
        int16_t CONST_ATTR rpmMap[MAX_RPM_POINTS] = {400, 750, 1000, 1500, 2500, 3500, 4500, 6000};
23
        // column of row values - in 1000-pressure 
28
        // column of row values - in 1000-pressure
24
        int16_t vacuumMap[MAX_VACUUM_POINTS] = {0, 166, 225, 300, 700, 1000, NO_DATA, NO_DATA};
29
        int16_t CONST_ATTR vacuumMap[MAX_VACUUM_POINTS] = {0, 166, 225, 300, 700, 1000, NO_DATA, NO_DATA};
25
        uint8_t mapping[MAX_VACUUM_POINTS][MAX_RPM_POINTS] = {
30
        uint8_t CONST_ATTR mapping[MAX_VACUUM_POINTS][MAX_RPM_POINTS] = {
26
            /* Table in degrees. */
31
            /* Table in degrees. */
27
            /* row for 0mb = centrifugal only */
32
            /* row for 0mb = centrifugal only */
28
            {12, 7, 7, 19, 25, 29, 29, 22},
33
            {12, 7, 7, 19, 25, 29, 29, 22},
29
            /* row for 166 mB*/
34
            /* row for 166 mB*/
30
            {12, 7, 7, 21, 27, 31, 31, 24},
35
            {12, 7, 7, 21, 27, 31, 31, 24},
31
            /*   row for 225 mB */
36
            /*   row for 225 mB */
32
            {12, 7, 7, 25, 31, 35, 35, 28},
37
            {12, 7, 7, 25, 31, 35, 35, 28},
33
            /* row for 300 mB*/
38
            /* row for 300 mB*/
34
            {12, 7, 7, 29, 35, 39, 39, 33},
39
            {12, 7, 7, 29, 35, 39, 39, 33},
35
            /* row for 700 mB*/
40
            /* row for 700 mB*/
36
            {12, 7, 7, 29, 35, 39, 39, 33},
41
            {12, 7, 7, 19, 25, 29, 29, 22},
37
            /* row for 1000 mB - used when pressure drops off the scale */
42
            /* row for 1000 mB - used when pressure drops off the scale */
38
            {7, 7, 7, 7, 7, 7, 7, 7},
43
            {7, 7, 7, 7, 7, 7, 7, 7},
39
            /* unused */
44
            /* unused */
40
            {0, 0, 0, 0, 0, 0, 0, 0},
45
            {0, 0, 0, 0, 0, 0, 0, 0},
41
            /* unused */
46
            /* unused */
Line 58... Line 63...
58
            return 0;
63
            return 0;
59
    }
64
    }
60
 
65
 
61
    void setRpmMap(unsigned int i, uint16_t val)
66
    void setRpmMap(unsigned int i, uint16_t val)
62
    {
67
    {
-
 
68
#if WRITABLE_TABLE
63
        if (i >= 0 && i < MAX_RPM_POINTS)
69
        if (i >= 0 && i < MAX_RPM_POINTS)
64
            rpmMap[i] = val;
70
            rpmMap[i] = val;
-
 
71
#endif
65
    }
72
    }
66
 
73
 
67
    uint16_t getVacuumMap(unsigned int i)
74
    uint16_t getVacuumMap(unsigned int i)
68
    {
75
    {
69
        if (i >= 0 && i < MAX_VACUUM_POINTS)
76
        if (i >= 0 && i < MAX_VACUUM_POINTS)
Line 72... Line 79...
72
            return 0;
79
            return 0;
73
    }
80
    }
74
 
81
 
75
    void setVacuumMap(unsigned int i, uint16_t val)
82
    void setVacuumMap(unsigned int i, uint16_t val)
76
    {
83
    {
-
 
84
#if WRITABLE_TABLE
77
        if (i >= 0 && i < MAX_VACUUM_POINTS)
85
        if (i >= 0 && i < MAX_VACUUM_POINTS)
78
            vacuumMap[i] = val;
86
            vacuumMap[i] = val;
-
 
87
#endif
79
    }
88
    }
80
 
89
 
81
    void setTiming(unsigned int vacuumIndex, unsigned int rpmIndex, uint8_t value)
90
    void setTiming(unsigned int vacuumIndex, unsigned int rpmIndex, uint8_t value)
82
    {
91
    {
-
 
92
 
-
 
93
#if WRITABLE_TABLE
83
        if (vacuumIndex < 0 && vacuumIndex >= MAX_VACUUM_POINTS)
94
        if (vacuumIndex < 0 && vacuumIndex >= MAX_VACUUM_POINTS)
84
            return;
95
            return;
85
        if (rpmIndex < 0 && rpmIndex >= MAX_RPM_POINTS)
96
        if (rpmIndex < 0 && rpmIndex >= MAX_RPM_POINTS)
86
            return;
97
            return;
87
        mapping[vacuumIndex][rpmIndex] = value;
98
        mapping[vacuumIndex][rpmIndex] = value;
-
 
99
#endif
88
    }
100
    }
89
 
101
 
90
    uint8_t getTiming(unsigned int vacuumIndex, unsigned int rpmIndex)
102
    uint8_t getTiming(unsigned int vacuumIndex, unsigned int rpmIndex)
91
    {
103
    {
92
        if (vacuumIndex < 0 && vacuumIndex >= MAX_VACUUM_POINTS)
104
        if (vacuumIndex < 0 && vacuumIndex >= MAX_VACUUM_POINTS)
Line 94... Line 106...
94
        if (rpmIndex < 0 && rpmIndex >= MAX_RPM_POINTS)
106
        if (rpmIndex < 0 && rpmIndex >= MAX_RPM_POINTS)
95
            return 0;
107
            return 0;
96
        return mapping[vacuumIndex][rpmIndex];
108
        return mapping[vacuumIndex][rpmIndex];
97
    }
109
    }
98
 
110
 
-
 
111
    /// @brief Lookup a point in a 1 dimensional array -
-
 
112
    /// @param point Value to lookup
-
 
113
    /// @param curve Lookup table
-
 
114
    /// @param size Size of lookup table
-
 
115
    /// @param [out] frac fraction of distance from first point in array
-
 
116
    /// @return index in array or NO_DATA if operations fail 
99
    int lookup(int point, int16_t const curve[], int size, int16_t *frac)
117
    int lookup(int point, int16_t const curve[], int size, int16_t *frac)
100
    {
118
    {
101
        // check lower bounds
119
        // check lower bounds
102
        if (point < curve[0])
120
        if (point < curve[0])
103
        {
121
        {
Line 136... Line 154...
136
                *frac = ((offset * range2) / range1);
154
                *frac = ((offset * range2) / range1);
137
                return pt - 1;
155
                return pt - 1;
138
            }
156
            }
139
        }
157
        }
140
        *frac = 0;
158
        *frac = 0;
141
        return -1; // give up.
159
        return NO_DATA; // give up.
142
    };
160
    };
143
 
161
 
144
    extern "C"
162
    extern "C"
145
    {
163
    {
146
 
164