forked from Skuzzle-UK/ECUMASTER_CAN_DISPLAY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECM.cpp
148 lines (116 loc) · 4.04 KB
/
ECM.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "ECM.h"
//////////////////////////////////////////////////////////////
ECM::ECM() {
ECM::SetupEEPROM();
ECM::ReadEEPROM();
}
//////////////////////////////////////////////////////////////
//Getters
int32_t ECM::RPM() {return _rpm;}
byte ECM::MAP() {return _map;}
byte ECM::BARO() {return _baro;}
int8_t ECM::CLT() {return _clt;}
int8_t ECM::AIT() {return _ait;}
byte ECM::OIL_PRESSURE() {return _oilp;}
float ECM::AFR() {return _afr;}
float ECM::LAMBDA(){return _lambda;}
byte ECM::BOOST_PERCENT() {return _boostpercent;}
bool ECM::SPARK_MAP() {return _sparkmap;}
float ECM::FUEL_USAGE() {return _fuelusage;}
int32_t ECM::SPEED() {return _speed;}
bool ECM::LOCKED() {return _mutex;}
//////////////////////////////////////////////////////////////
byte ECM::IGNITION_SWITCH_CAN_OUT(){
if(_sparkmap){return 0xFF;}
else{return 0x00;}
}
//////////////////////////////////////////////////////////////
byte ECM::BOOST_PRESSURE_CAN_OUT(){
float inc = 255.0f / 100.0f;
float val = (float)_boostpercent * inc;
val = 255 - val;
return (byte)val;
}
//////////////////////////////////////////////////////////////
//Setter Functions
void ECM::MAP(byte byt){_map = byt;}
void ECM::BARO(byte byt){_baro = byt;}
void ECM::CLT(byte byt){_clt = byt;}
void ECM::AIT(byte byt){_ait = byt;}
void ECM::AFR(byte byt){}
//////////////////////////////////////////////////////////////
void ECM::SET_BOOST_PERCENTAGE(byte val) {
if (val <= 1) {
val = 1;
}
if (val >= 100) {
val = 100;
}
_boostpercent = val;
}
//////////////////////////////////////////////////////////////
void ECM::SET_BOOST_PRESSURE(DIRECTION dir){
switch(dir){
case DIRECTION::UP: {
ECM::SET_BOOST_PERCENTAGE(_boostpercent + 1);
break;
}
case DIRECTION::DOWN: {
ECM::SET_BOOST_PERCENTAGE(_boostpercent - 1);
break;
}
}
}
//////////////////////////////////////////////////////////////
void ECM::SET_SPARK_MAP() {_sparkmap = !_sparkmap;}
void ECM::LOAD_SPARK_MAP(bool val){ _sparkmap = val; }
void ECM::LOCK() { _mutex = true; }
void ECM::UNLOCK() {
_mutex = false;
ECM::WriteEEPROM();
}
//////////////////////////////////////////////////////////////
void ECM::RPM(byte firstbyte, byte secondbyte){
int32_t val = (int32_t)firstbyte | ((int32_t)secondbyte << 8);
_rpm = val;
}
//////////////////////////////////////////////////////////////
void ECM::OIL_PRESSURE(byte byt){
float val = (float)byt / 21.0f;
val = val * 14.5038f;
_oilp = (int32_t)val;
}
//////////////////////////////////////////////////////////////
void ECM::LAMBDA(byte byt){
float val = (float)byt / 128.0f;
_lambda = val;
val = val * 14.68f;
_afr = val;
}
//////////////////////////////////////////////////////////////
void ECM::SPEED(byte firstbyte, byte secondbyte){
int32_t val = (int32_t)firstbyte | ((int32_t)secondbyte << 8);
_speed = val;
}
//////////////////////////////////////////////////////////////
void ECM::SetupEEPROM(){
byte savedcheckbyte;
EEPROM.get(MEM_LOCATION_CHECKBYTE, savedcheckbyte);
if (savedcheckbyte != checkbyte){
EEPROM.put(MEM_LOCATION_CHECKBYTE, checkbyte);
EEPROM.put(MEM_LOCATION_BOOST, 0x01);
EEPROM.put(MEM_LOCATION_SPARK_MAP, false);
}
}
//////////////////////////////////////////////////////////////
void ECM::ReadEEPROM(){
EEPROM.get(MEM_LOCATION_BOOST, _boostpercent);
EEPROM.get(MEM_LOCATION_SPARK_MAP, _sparkmap);
}
//////////////////////////////////////////////////////////////
// Using EEPROM.put to avoid excessive data writing sue to limited number of writes before corruption
void ECM::WriteEEPROM(){
EEPROM.put(MEM_LOCATION_BOOST, _boostpercent);
EEPROM.put(MEM_LOCATION_SPARK_MAP, _sparkmap);
}
//////////////////////////////////////////////////////////////