mirror of
https://github.com/Ralim/IronOS.git
synced 2025-02-26 07:53:55 +00:00
Extracting Utils class
This commit is contained in:
33
source/Core/Drivers/Utils.cpp
Normal file
33
source/Core/Drivers/Utils.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Utils.cpp
|
||||
*
|
||||
* Created on: 28 Apr 2021
|
||||
* Author: Ralim
|
||||
*/
|
||||
|
||||
#include <Utils.h>
|
||||
|
||||
int32_t Utils::InterpolateLookupTable(const uint16_t *lookupTable,
|
||||
const uint16_t value) {
|
||||
if (value) {
|
||||
int noItems = sizeof(lookupTable) / (2 * sizeof(uint16_t));
|
||||
for (int i = 1; i < (noItems - 1); i++) {
|
||||
// If current tip temp is less than current lookup, then this current lookup is the higher point to interpolate
|
||||
if (value < lookupTable[i * 2]) {
|
||||
return LinearInterpolate(lookupTable[(i - 1) * 2],
|
||||
lookupTable[((i - 1) * 2) + 1], lookupTable[i * 2],
|
||||
lookupTable[(i * 2) + 1], value);
|
||||
}
|
||||
}
|
||||
return LinearInterpolate(lookupTable[(noItems - 2) * 2],
|
||||
lookupTable[((noItems - 2) * 2) + 1],
|
||||
lookupTable[(noItems - 1) * 2],
|
||||
lookupTable[((noItems - 1) * 2) + 1], value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t Utils::LinearInterpolate(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
|
||||
int32_t x) {
|
||||
return y1 + (((((x - x1) * 1000) / (x2 - x1)) * (y2 - y1))) / 1000;
|
||||
}
|
||||
20
source/Core/Drivers/Utils.h
Normal file
20
source/Core/Drivers/Utils.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Utils.h
|
||||
*
|
||||
* Created on: 28 Apr 2021
|
||||
* Author: Ralim
|
||||
*/
|
||||
|
||||
#ifndef CORE_DRIVERS_UTILS_H_
|
||||
#define CORE_DRIVERS_UTILS_H_
|
||||
#include <stdint.h>
|
||||
class Utils {
|
||||
public:
|
||||
static int32_t InterpolateLookupTable(const uint16_t *lookupTable,
|
||||
const uint16_t value);
|
||||
static int32_t LinearInterpolate(int32_t x1, int32_t y1, int32_t x2,
|
||||
int32_t y2, int32_t x) ;
|
||||
|
||||
};
|
||||
|
||||
#endif /* CORE_DRIVERS_UTILS_H_ */
|
||||
Reference in New Issue
Block a user