SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
SBBUtils.h
Go to the documentation of this file.
1/*
2* File for utility (non-class) functions
3*/
4
5#ifndef UTILS_H
6#define UTILS_H
7
8#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__) || defined(WIN64) || defined(_WIN64) || defined(__WIN64) || defined(__WIN64__)
9#ifndef OS_WIN
10#define OS_WIN
11#endif
12#elif defined(linux) || defined(__linux) || defined(__linux__) || defined(__GNUC__) || defined(__GNUG__) || defined(unix) || defined(__unix) || defined(__unix__)
13#ifndef OS_LINUX
14#define OS_LINUX
15#endif
16#else
17#error OS not supported
18#endif
19
20#if defined(OS_WIN)
21#pragma warning(disable : 4996)
22#include <shlobj.h> //for get document path in Win
23#include <WinUser.h> //for beep
24#include <winsock2.h>
25#include <ws2tcpip.h>
26#elif defined(OS_LINUX)
27#define sscanf_s sscanf //in linux use sscanf for sscanf_s
28#define sprintf_s(buf, len, ...) snprintf((buf), (len), __VA_ARGS__)
29#include <arpa/inet.h>
30#include <sys/socket.h>
31#include <ifaddrs.h>
32#endif
33
34#include <time.h>
35#include <string>
36#include <vector>
37#include <numeric>
38#include <algorithm>
39#include <math.h>
40
41
47namespace Colors {
50 constexpr double red[] = { 0, 0.85, 0.929, 0.494, 0.466, 0.301, 0.635 };
51
54 constexpr double green[] = { 0.447, 0.325, 0.694, 0.184, 0.674, 0.745, 0.078 };
55
58 constexpr double blue[] = { 0.741, 0.098, 0.125, 0.556, 0.188, 0.933, 0.184 };
59
62 constexpr int size = 7;
63};
64
70namespace utils {
71
78 int filecp(const char FileSource [], const char FileDestination []);
79
88 int getIP_and_subnetmask(uint32_t* ip = nullptr, uint32_t* mask = nullptr, std::string* ip_str = nullptr, std::string* mask_str = nullptr);
89
97 uint32_t get_remoteIP(uint32_t ip, uint32_t mask, std::string* str = nullptr);
98
106 uint32_t get_broadcastIP(uint32_t ip, uint32_t mask, std::string* str = nullptr);
107
113 uint32_t ip_str2num(std::string ip_str);
114
120 std::string hex2str(unsigned int v);
121
128 bool str2hex(std::string str, unsigned int *p_v);
129
134 float average(std::vector<float> const& v);
135
140 float rms(std::vector<float> const& v);
141
148 std::string get_home_path();
149
155 void beep(int mode);
156
163 bool set_env_var(std::string key, std::string val);
164
167 struct hhmmss_t {
168 int s;
169 int m;
170 int h;
171 };
172
179
180};
181
182#endif
A namespace providing a set of standard colors.
Definition: SBBUtils.h:47
constexpr double green[]
green tone of the set of colors
Definition: SBBUtils.h:54
constexpr double red[]
red tone of the set of colors
Definition: SBBUtils.h:50
constexpr double blue[]
blue tone of the set of colors
Definition: SBBUtils.h:58
constexpr int size
number of color in the color set.
Definition: SBBUtils.h:62
A namespace providing utility functions.
Definition: SBBUtils.h:70
uint32_t get_remoteIP(uint32_t ip, uint32_t mask, std::string *str=nullptr)
Get the remote IP.
bool set_env_var(std::string key, std::string val)
Set a local environment variable.
uint32_t ip_str2num(std::string ip_str)
Convert IP from string to uint32.
uint32_t get_broadcastIP(uint32_t ip, uint32_t mask, std::string *str=nullptr)
Get the broadcast IP.
std::string hex2str(unsigned int v)
Convert HEX from unsigned int to string.
bool str2hex(std::string str, unsigned int *p_v)
Convert HEX from string to unsigned int.
hhmmss_t sec2hhmmss(int secs)
Convert elapsed seconds to hh::mm:ss format.
void beep(int mode)
Send a beep message.
float rms(std::vector< float > const &v)
Get the RMS of a vector of float.
int getIP_and_subnetmask(uint32_t *ip=nullptr, uint32_t *mask=nullptr, std::string *ip_str=nullptr, std::string *mask_str=nullptr)
Get the local IP and subnet mask.
int filecp(const char FileSource[], const char FileDestination[])
Copy a file.
float average(std::vector< float > const &v)
Get the average of a vector of float.
std::string get_home_path()
Get the HOME path.
A structure for handling elapsed time in hour, minute, second format.
Definition: SBBUtils.h:167
int s
Definition: SBBUtils.h:168
int h
Definition: SBBUtils.h:170
int m
Definition: SBBUtils.h:169