auto-sync

This commit is contained in:
rusEfi 2016-01-05 00:02:56 -05:00
parent e8b7e99093
commit 72caa24450
2 changed files with 18 additions and 3 deletions

View File

@ -63,6 +63,8 @@ floatms_t WallFuel::getWallFuel(int injectorIndex) {
} }
float AccelEnrichmemnt::getDelta() { float AccelEnrichmemnt::getDelta() {
if (cb.getCount() == 0)
return 0; // no recent data
return cb.maxValue(cb.getSize()); return cb.maxValue(cb.getSize());
} }
@ -90,6 +92,7 @@ float AccelEnrichmemnt::getEngineLoadEnrichment(DECLARE_ENGINE_PARAMETER_F) {
} }
void AccelEnrichmemnt::reset() { void AccelEnrichmemnt::reset() {
cb.clear();
delta = 0; delta = 0;
currentValue = NAN; currentValue = NAN;
} }

View File

@ -17,6 +17,8 @@
static const short CB_MAX_SIZE = 64; static const short CB_MAX_SIZE = 64;
#define BUFFER_MAX_VALUE 200123
template<typename T> template<typename T>
class cyclic_buffer class cyclic_buffer
{ {
@ -40,12 +42,16 @@ class cyclic_buffer
T minValue(int length); T minValue(int length);
void setSize(int size); void setSize(int size);
int getSize(); int getSize();
int getCount();
void clear(); void clear();
private: private:
void baseC(int size); void baseC(int size);
volatile T elements[CB_MAX_SIZE]; volatile T elements[CB_MAX_SIZE];
volatile int currentIndex; volatile int currentIndex;
/**
* number of elements added into this buffer, would be eventually bigger then size
*/
volatile int count; volatile int count;
int size; int size;
}; };
@ -115,6 +121,11 @@ int cyclic_buffer<T>::getSize() {
return size; return size;
} }
template<typename T>
int cyclic_buffer<T>::getCount() {
return count;
}
template<typename T> template<typename T>
T cyclic_buffer<T>::get(int index) { T cyclic_buffer<T>::get(int index) {
while (index < 0) { while (index < 0) {
@ -129,10 +140,11 @@ T cyclic_buffer<T>::get(int index) {
template<typename T> template<typename T>
T cyclic_buffer<T>::maxValue(int length) { T cyclic_buffer<T>::maxValue(int length) {
if (length > count) { if (length > count) {
// not enough data in buffer
length = count; length = count;
} }
int ci = currentIndex; // local copy to increase thread-safety int ci = currentIndex; // local copy to increase thread-safety
T result = -2000000; // todo: better min value? T result = -BUFFER_MAX_VALUE; // todo: better min value?
for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) {
int index = ci - i; int index = ci - i;
while (index < 0) { while (index < 0) {
@ -152,7 +164,7 @@ T cyclic_buffer<T>::minValue(int length) {
length = count; length = count;
} }
int ci = currentIndex; // local copy to increase thread-safety int ci = currentIndex; // local copy to increase thread-safety
T result = +2000000; // todo: better max value? T result = +BUFFER_MAX_VALUE; // todo: better max value?
for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) {
int index = ci - i; int index = ci - i;
while (index < 0) { while (index < 0) {
@ -191,7 +203,7 @@ template<typename T>
void cyclic_buffer<T>::clear() { void cyclic_buffer<T>::clear() {
memset((void*) elements, 0, sizeof(elements)); // I would usually use static_cast, but due to the elements being volatile we cannot. memset((void*) elements, 0, sizeof(elements)); // I would usually use static_cast, but due to the elements being volatile we cannot.
count = 0; count = 0;
count = 0; currentIndex = 0;
} }
#endif //CYCLIC_BUFFER_H #endif //CYCLIC_BUFFER_H