-
Notifications
You must be signed in to change notification settings - Fork 10
/
Utility.h
153 lines (118 loc) · 5.02 KB
/
Utility.h
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
149
150
151
152
153
// MIT License
//
// Copyright (c) 2021 Daniel Robertson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef HX711_UTILITY_H_2F1DEBBD_F7BB_4202_BE2A_A33018D2AF5A
#define HX711_UTILITY_H_2F1DEBBD_F7BB_4202_BE2A_A33018D2AF5A
#include <algorithm>
#include <chrono>
#include <climits>
#include <cstdint>
#include <limits>
#include <numeric>
#include <pthread.h>
#include <stdexcept>
#include <time.h>
#include <vector>
namespace HX711 {
enum class GpioLevel : bool {
LOW = false,
HIGH = true
};
class Utility {
protected:
static constexpr const char* const _VERSION = "2.19.0";
static void _throwGpioExIfErr(const int code);
Utility();
public:
static const char* getVersion() noexcept;
static int openGpioHandle(const int chip);
static void closeGpioHandle(const int chip);
static void openGpioInput(const int handle, const int pin);
static void openGpioOutput(const int handle, const int pin);
static void closeGpioPin(const int handle, const int pin);
static GpioLevel readGpio(const int handle, const int pin);
static void writeGpio(const int handle, const int pin, const GpioLevel lev);
/**
* Sleep for ns nanoseconds. The _sleep/_delay functions are
* an attempt to be analogous to usleep/udelay in the kernel.
* https://www.kernel.org/doc/html/v5.10/timers/timers-howto.html
*/
static void sleep(const std::chrono::nanoseconds ns) noexcept;
static void delay(const std::chrono::nanoseconds ns) noexcept;
static std::chrono::nanoseconds getnanos() noexcept;
static std::chrono::nanoseconds timespec_to_nanos(const timespec* const ts) noexcept;
static void timespecclear(timespec* const tsp) noexcept;
static bool timespecisset(const timespec* const tsp) noexcept;
static bool timespecisvalid(const timespec* const tsp) noexcept;
static int timespeccmp(const timespec* const tsp, const timespec* const usp) noexcept;
static void timespecadd(const timespec* const tsp, const timespec* const usp, timespec* const vsp) noexcept;
static void timespecsub(const timespec* const tsp, const timespec* const usp, timespec* const vsp) noexcept;
static void setThreadPriority(
const int pri, const int policy, const pthread_t th) noexcept;
template <typename T>
static double average(const std::vector<T>* const vals) noexcept {
if(vals->empty()) {
std::length_error("average cannot be calculated from 0 values");
}
const auto sum = std::accumulate(
vals->begin(), vals->end(), static_cast<long long int>(0));
return static_cast<double>(sum) / vals->size();
}
template <typename T>
static double median(std::vector<T>* const vals) noexcept {
if(vals->empty()) {
std::length_error("median cannot be calculated from 0 values");
}
/**
* TODO: is this more efficient?
*/
if(vals->size() == 1) {
return static_cast<double>((*vals)[0]);
}
//https://stackoverflow.com/a/42791986/570787
if(vals->size() % 2 == 0) {
const auto median_it1 = vals->begin() + vals->size() / 2 - 1;
const auto median_it2 = vals->begin() + vals->size() / 2;
std::nth_element(vals->begin(), median_it1, vals->end());
const auto e1 = *median_it1;
std::nth_element(vals->begin(), median_it2, vals->end());
const auto e2 = *median_it2;
return (e1 + e2) / 2.0;
}
else {
const auto median_it = vals->begin() + vals->size() / 2;
std::nth_element(vals->begin(), median_it, vals->end());
return static_cast<double>(*median_it);
}
}
//reverseBits bits in int
//https://stackoverflow.com/a/2602871/570787
template <typename T>
static T reverseBits(T n, const std::size_t b = sizeof(T) * CHAR_BIT) noexcept {
T rv = 0;
for(size_t i = 0; i < b; ++i, n >>= 1) {
rv = (rv << 1) | (n & 0x01);
}
return rv;
}
};
};
#endif