SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
utils Namespace Reference

A namespace providing utility functions. More...

Classes

struct  hhmmss_t
 A structure for handling elapsed time in hour, minute, second format. More...
 

Functions

int filecp (const char FileSource[], const char FileDestination[])
 Copy a file. More...
 
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. More...
 
uint32_t get_remoteIP (uint32_t ip, uint32_t mask, std::string *str=nullptr)
 Get the remote IP. More...
 
uint32_t get_broadcastIP (uint32_t ip, uint32_t mask, std::string *str=nullptr)
 Get the broadcast IP. More...
 
uint32_t ip_str2num (std::string ip_str)
 Convert IP from string to uint32. More...
 
std::string hex2str (unsigned int v)
 Convert HEX from unsigned int to string. More...
 
bool str2hex (std::string str, unsigned int *p_v)
 Convert HEX from string to unsigned int. More...
 
float average (std::vector< float > const &v)
 Get the average of a vector of float. More...
 
float rms (std::vector< float > const &v)
 Get the RMS of a vector of float. More...
 
std::string get_home_path ()
 Get the HOME path. More...
 
void beep (int mode)
 Send a beep message. More...
 
bool set_env_var (std::string key, std::string val)
 Set a local environment variable. More...
 
hhmmss_t sec2hhmmss (int secs)
 Convert elapsed seconds to hh::mm:ss format. More...
 

Detailed Description

A namespace providing utility functions.

Namespace providing a number of utility functions.

Author
Stefano Lovato
Date
2022

Function Documentation

◆ average()

float utils::average ( std::vector< float > const &  v)

Get the average of a vector of float.

Function to get the average of a std::vector<float>.

Returns
The average as a float type.

◆ beep()

void utils::beep ( int  mode)

Send a beep message.

Function to send a beep message in Windows. This is just a wrapper of the Windows function MessageBeep(mode). In Linux it does nothing.

Parameters
modeThe beep mode to be used. See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebeep for details.

◆ filecp()

int utils::filecp ( const char  FileSource[],
const char  FileDestination[] 
)

Copy a file.

Robust function to copy file from source to destination.

Parameters
FileSourceChar array with the source file path.
FileDestinationChar array with the destination file path.
Returns
0 if success, -1 and -2 if cannot open source or destination file.

◆ get_broadcastIP()

uint32_t utils::get_broadcastIP ( uint32_t  ip,
uint32_t  mask,
std::string *  str = nullptr 
)

Get the broadcast IP.

Function to get the broadcast IP from the local IP and subnet mask.

Parameters
ipLocal IP.
maskSubnet mask.
strPointer where save the broadcast IP in string format (XXX.XXX.XXX.XXX).
Returns
The broadcast IP.

◆ get_home_path()

std::string utils::get_home_path ( )

Get the HOME path.

Function to get a home path (i.e. HOME in Linux, Documents in Windows). If Documents is not found in Windows, C:\ is used. File separator used is consistent with the operative system.

Returns
A string denoting the home path.

◆ get_remoteIP()

uint32_t utils::get_remoteIP ( uint32_t  ip,
uint32_t  mask,
std::string *  str = nullptr 
)

Get the remote IP.

Function to get the remote IP from the local IP and subnet mask.

Parameters
ipLocal IP.
maskSubnet mask.
strPointer where save the remote IP in string format (XXX.XXX.XXX.XXX).
Returns
The remote IP.

◆ getIP_and_subnetmask()

int utils::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.

Function to get the local IP address and subnet mask.

Parameters
ipPointer where saving the local IP.
maskPointer where saving the subnet mask.
ip_strPointer where saving the local IP in string format (XXX.XXX.XXX.XXX).
mask_strPointer where saving the subnet mask in string format (XXX.XXX.XXX.XXX).
Returns
0 if success, 1 otherwise

◆ hex2str()

std::string utils::hex2str ( unsigned int  v)

Convert HEX from unsigned int to string.

Function to convert a HEX value (as an unsigned int) to a string.

Parameters
vThe HEX value as an unsigned int.
Returns
The HEX value as a string.

◆ ip_str2num()

uint32_t utils::ip_str2num ( std::string  ip_str)

Convert IP from string to uint32.

Function to convert IP from string to uint32.

Parameters
ip_strThe IP.
Returns
The IP.

◆ rms()

float utils::rms ( std::vector< float > const &  v)

Get the RMS of a vector of float.

Function to get the RMS (root-mean-squared) of a std::vector<float>.

Returns
The RMS value as a float type.

◆ sec2hhmmss()

hhmmss_t utils::sec2hhmmss ( int  secs)

Convert elapsed seconds to hh::mm:ss format.

Function to convert elapsed seconds in hours, minutes, and seconds format.

Parameters
secsThe elapsed seconds.
Returns
A hhmmss_t struct containing hours, minutes, and seconds.

◆ set_env_var()

bool utils::set_env_var ( std::string  key,
std::string  val 
)

Set a local environment variable.

Function to set a local environment variable. This is just a wrapper for the putenv function.

Parameters
keyThe name of the environment variable.
valThe value of the environment variable.
Returns
true if success, false otherwise.

◆ str2hex()

bool utils::str2hex ( std::string  str,
unsigned int *  p_v 
)

Convert HEX from string to unsigned int.

Function to convert a HEX (as a string) to an unsigned int

Parameters
strThe HEX value as an string.
p_vThe pointer to the unsigned int value.
Returns
true if success, false otherwise.