rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
efilib.h
Go to the documentation of this file.
1/**
2 * @file efilib.h
3 *
4 * @date Feb 21, 2014
5 * @author Andrey Belomutskiy, (c) 2012-2020
6 */
7
8#pragma once
9
10#include "unused.h"
11#include "efi_quote.h"
12#include <stdint.h>
13
14#include <rusefi/arrays.h>
15
16int djb2lowerCase(const char *str);
17
18#define _MAX_FILLER 11
19
20// http://en.wikipedia.org/wiki/Endianness
21
22inline uint16_t SWAP_UINT16(uint16_t x)
23{
24 return ((x << 8) | (x >> 8));
25}
26
27inline uint32_t SWAP_UINT32(uint32_t x)
28{
29 return (((x >> 24) & 0x000000ff) | ((x << 8) & 0x00ff0000) |
30 ((x >> 8) & 0x0000ff00) | ((x << 24) & 0xff000000));
31}
32
33#define BIT(n) (UINT32_C(1) << (n))
34
35// also known as 'HUMAN_INDEX'
36#define HUMAN_OFFSET 1
37
38// human-readable IDs start from 1 while computer-readable indices start from 0
39#define ID2INDEX(id) ((id) - HUMAN_OFFSET)
40
41// number of milliseconds in one period of given frequency (per second)
42#define frequency2periodMs(freq) ((1000.0f) / (freq))
43
44// number of microseconds in one period of given frequency (per second)
45#define frequency2periodUs(freq) ((1000000.0f) / (freq))
46
47const char * boolToString(bool value);
48
49char * efiTrim(char *param);
50int efiPow10(int param);
51
52/**
53 * Rounds value to specified precision.
54 * @param precision some pow of 10 value - for example, 100 for two digit precision
55 */
56float efiRound(float value, float precision);
57
58// sometimes known as 'itoa'
59char* itoa10(char *p, int num);
60
61/**
62 * clamps value into the [0, 100] range
63 */
64#define clampPercentValue(x) (clampF(0, x, 100))
65
66// Currently used by air-interp. tCharge mode (see EngineState::updateTChargeK()).
67float limitRateOfChange(float newValue, float oldValue, float incrLimitPerSec, float decrLimitPerSec, float secsPassed);
68
69bool isPhaseInRange(float test, float current, float next);
70
71#include <cstddef>
72#include <cstring>
73
74#define IS_NEGATIVE_ZERO(value) (__builtin_signbit(value) && value==0)
75#define fixNegativeZero(value) (IS_NEGATIVE_ZERO(value) ? 0 : value)
76
77#define assertIsInBounds(length, array, msg) criticalAssertVoid(std::is_unsigned_v<decltype(length)> && (length) < efi::size(array), msg)
78
79#define assertIsInBoundsWithResult(length, array, msg, failedResult) efiAssert(ObdCode::OBD_PCM_Processor_Fault, std::is_unsigned_v<decltype(length)> && (length) < efi::size(array), msg, failedResult)
80
81template <typename T>
82bool isInRange(T min, T val, T max) {
83 return val >= min && val <= max;
84}
85
86inline constexpr size_t operator-(Gpio a, Gpio b) {
87 return (size_t)a - (size_t)b;
88}
89
90inline constexpr Gpio operator-(Gpio a, size_t b) {
91 return (Gpio)((size_t)a - b);
92}
93
94inline constexpr Gpio operator+(Gpio a, size_t b) {
95 return (Gpio)((size_t)a + b);
96}
97
98inline constexpr Gpio operator+(size_t a, Gpio b) {
99 // addition is commutative, just use the other operator
100 return b + a;
101}
102
103namespace efi
104{
105template <class _Ty>
107 using type = _Ty;
108};
109
110template <class _Ty>
111struct remove_reference<_Ty&> {
112 using type = _Ty;
113};
114
115template <class _Ty>
116struct remove_reference<_Ty&&> {
117 using type = _Ty;
118};
119
120template <class _Ty>
122
123// FUNCTION TEMPLATE move
124template <class _Ty>
125constexpr remove_reference_t<_Ty>&& move(_Ty&& _Arg) noexcept {
126 return static_cast<remove_reference_t<_Ty>&&>(_Arg);
127}
128}
129
130int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth);
131/**
132 for instance DBC 8|16@0
133 */
134int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth);
135void setBitRangeMsb(uint8_t data[], int totalBitIndex, int bitWidth, int value);
136
137int motorolaMagicFromDbc(int b, int length);
138int getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth);
139void setBitRangeMoto(uint8_t data[], int totalBitIndex, int bitWidth, int value);
int getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth)
Definition efilib.cpp:243
constexpr size_t operator-(Gpio a, Gpio b)
Definition efilib.h:86
float limitRateOfChange(float newValue, float oldValue, float incrLimitPerSec, float decrLimitPerSec, float secsPassed)
Definition efilib.cpp:170
bool isInRange(T min, T val, T max)
Definition efilib.h:82
int djb2lowerCase(const char *str)
Definition efilib.cpp:135
void setBitRangeMoto(uint8_t data[], int totalBitIndex, int bitWidth, int value)
Definition efilib.cpp:248
int motorolaMagicFromDbc(int b, int length)
Definition efilib.cpp:232
const char * boolToString(bool value)
Definition efilib.cpp:19
bool isPhaseInRange(float test, float current, float next)
Definition efilib.cpp:176
float efiRound(float value, float precision)
Definition efilib.cpp:34
void setBitRangeMsb(uint8_t data[], int totalBitIndex, int bitWidth, int value)
Definition efilib.cpp:216
constexpr Gpio operator+(Gpio a, size_t b)
Definition efilib.h:94
char * efiTrim(char *param)
Definition efilib.cpp:40
int getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth)
Definition efilib.cpp:207
char * itoa10(char *p, int num)
Definition efilib.cpp:107
int efiPow10(int param)
Definition efilib.cpp:111
int getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth)
Definition efilib.cpp:212
uint32_t SWAP_UINT32(uint32_t x)
Definition efilib.h:27
uint16_t SWAP_UINT16(uint16_t x)
Definition efilib.h:22
union test_buffers test
Definition test.c:50
Definition efilib.h:104
constexpr remove_reference_t< _Ty > && move(_Ty &&_Arg) noexcept
Definition efilib.h:125
typename remove_reference< _Ty >::type remove_reference_t
Definition efilib.h:121
static tstrWifiInitParam param