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
130// DBC bit numbers
131// byte 0 7 6 5 4 3 2 1 0
132// byte 1 15 14 13 12 11 10 9 8
133// byte 2 23 22 21 20 19 18 17 16
134// byte 3 31 30 29 28 27 26 25 24
135// byte 4 39 38 37 36 35 34 33 32
136// byte 5 47 46 45 44 43 42 41 40
137// byte 6 55 54 53 52 51 50 49 48
138// byte 7 63 62 61 60 57 58 57 56
139
140// Intel byte order, bitIndex is the least significant bit of the value
141// DBC sample: 0|16@1+
142uint32_t getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth);
143void setBitRangeLsb(uint8_t data[], int totalBitIndex, int bitWidth, uint32_t value);
144
145// Motorola byte order, bitIndex is the least significant bit of the value
146// not used in DBC
147uint32_t getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth);
148void setBitRangeMsb(uint8_t data[], int totalBitIndex, int bitWidth, uint32_t value);
149
150// Motorola byte order, bitIndex is the most significant bit of the value
151// DBC sample: 7|16@0+
152uint32_t getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth);
153void setBitRangeMoto(uint8_t data[], int totalBitIndex, int bitWidth, uint32_t value);
154
155// convert bitIndex from LSB to MSB style
156int motorolaMagicFromDbc(int b, int length);
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 setBitRangeLsb(uint8_t data[], int totalBitIndex, int bitWidth, uint32_t value)
Definition efilib.cpp:221
uint32_t getBitRangeMoto(const uint8_t data[], int bitIndex, int bitWidth)
Definition efilib.cpp:264
int motorolaMagicFromDbc(int b, int length)
Definition efilib.cpp:253
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, uint32_t value)
Definition efilib.cpp:237
constexpr Gpio operator+(Gpio a, size_t b)
Definition efilib.h:94
uint32_t getBitRangeLsb(const uint8_t data[], int bitIndex, int bitWidth)
Definition efilib.cpp:212
char * efiTrim(char *param)
Definition efilib.cpp:40
char * itoa10(char *p, int num)
Definition efilib.cpp:107
int efiPow10(int param)
Definition efilib.cpp:111
void setBitRangeMoto(uint8_t data[], int totalBitIndex, int bitWidth, uint32_t value)
Definition efilib.cpp:269
uint32_t SWAP_UINT32(uint32_t x)
Definition efilib.h:27
uint32_t getBitRangeMsb(const uint8_t data[], int bitIndex, int bitWidth)
Definition efilib.cpp:217
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