UMRT Arm Firmware Library
Functions
utils.hpp File Reference

A collection of helper functions related to encoding/decoding data for communication over a Firmata link. More...

#include <cstdint>
#include <string>
#include <vector>
Include dependency graph for utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::vector< uint8_t > pack_32 (const uint32_t integer)
 Packs a 32-bit integer into a vector of 8-bit integers, in little-endian format. More...
 
std::vector< uint8_t > pack_32_big (const uint32_t integer)
 Packs a 32-bit integer into a vector of 8-bit integers, in big-endian format. More...
 
std::vector< uint8_t > pack_24_big (const uint32_t integer)
 Packs a 24-bit integer into a vector of 8-bit integers, in big-endian format. More...
 
std::vector< uint8_t > pack_16 (const uint16_t integer)
 Packs a 16-bit integer into a vector of 8-bit integers, in little-endian format. More...
 
std::vector< uint8_t > pack_16_big (const uint16_t integer)
 Packs a 16-bit integer into a vector of 8-bit integers, in big-endian format. More...
 
std::vector< uint8_t > firmatify_32 (const std::vector< uint8_t >::const_iterator &pack)
 Converts a packed byte vector to the 7-bit packets Firmata receives. More...
 
std::vector< uint8_t > firmatify_32 (const std::vector< uint8_t > &pack)
 Calls firmatify_32(const std::vector<uint8_t>::const_iterator&) with pack.cbegin() More...
 
std::vector< uint8_t > firmatify_16 (const std::vector< uint8_t >::const_iterator &pack)
 Converts a packed byte vector to the 7-bit packets Firmata receives. More...
 
std::vector< uint8_t > firmatify_16 (const std::vector< uint8_t > &pack)
 Calls firmatify_16(const std::vector<uint8_t>::const_iterator&) with pack.cbegin() More...
 
std::vector< uint8_t > firmatify_8 (const uint8_t val)
 Converts an 8-bit integer to the two 7-bit packets Firmata uses. More...
 
uint32_t decode_32 (const std::vector< uint8_t >::const_iterator &data)
 Reconstructs a little-endian byte vector into a 32-bit integer. More...
 
uint32_t decode_32 (const std::vector< uint8_t > &data)
 Calls decode_32(const std::vector<uint8_t>::const_iterator&) with data.cbegin() More...
 
uint32_t decode_32_big (const std::vector< uint8_t >::const_iterator &data)
 Reconstructs a big-endian byte vector into a 32-bit integer. More...
 
uint32_t decode_32_big (const std::vector< uint8_t > &data)
 Calls decode_32_big(const std::vector<uint8_t>::const_iterator&) with data.cbegin() More...
 
uint16_t decode_16 (const std::vector< uint8_t >::const_iterator &data)
 Reconstructs a little-endian byte vector into a 16-bit integer. More...
 
uint16_t decode_16 (const std::vector< uint8_t > &data)
 Calls decode_16(const std::vector<uint8_t>::const_iterator&) with data.cbegin() More...
 
uint16_t decode_16_big (const std::vector< uint8_t >::const_iterator &data)
 Reconstructs a big-endian byte vector into a 16-bit integer. More...
 
uint16_t decode_16_big (const std::vector< uint8_t > &data)
 Calls decode_16(const std::vector<uint8_t>::const_iterator&) with data.cbegin() More...
 
std::vector< uint8_t > encode_string (const std::string &str)
 Packs an std::string into a vector of 8-bit integers, in little-endian format. More...
 
std::string decode_string (const std::vector< uint8_t > &data)
 Decodes a little-endian byte vector into an std::string. More...
 

Detailed Description

A collection of helper functions related to encoding/decoding data for communication over a Firmata link.

Function Documentation

◆ decode_16() [1/2]

uint16_t decode_16 ( const std::vector< uint8_t > &  data)
inline

Calls decode_16(const std::vector<uint8_t>::const_iterator&) with data.cbegin()

Parameters
datathe byte vector to convert
Returns
a 16-bit integer

◆ decode_16() [2/2]

uint16_t decode_16 ( const std::vector< uint8_t >::const_iterator &  data)
inline

Reconstructs a little-endian byte vector into a 16-bit integer.

Parameters
dataiterator to the byte vector subset to convert
Returns
a 16-bit integer

◆ decode_16_big() [1/2]

uint16_t decode_16_big ( const std::vector< uint8_t > &  data)
inline

Calls decode_16(const std::vector<uint8_t>::const_iterator&) with data.cbegin()

Parameters
datathe byte vector to convert
Returns
a 16-bit integer

◆ decode_16_big() [2/2]

uint16_t decode_16_big ( const std::vector< uint8_t >::const_iterator &  data)
inline

Reconstructs a big-endian byte vector into a 16-bit integer.

Parameters
dataiterator to the byte vector subset to convert
Returns
a 16-bit integer

◆ decode_32() [1/2]

uint32_t decode_32 ( const std::vector< uint8_t > &  data)
inline

Calls decode_32(const std::vector<uint8_t>::const_iterator&) with data.cbegin()

Parameters
datathe byte vector to convert
Returns
a 32-bit integer

◆ decode_32() [2/2]

uint32_t decode_32 ( const std::vector< uint8_t >::const_iterator &  data)
inline

Reconstructs a little-endian byte vector into a 32-bit integer.

Parameters
dataiterator to the byte vector subset to convert
Returns
a 32-bit integer

◆ decode_32_big() [1/2]

uint32_t decode_32_big ( const std::vector< uint8_t > &  data)
inline

Calls decode_32_big(const std::vector<uint8_t>::const_iterator&) with data.cbegin()

Parameters
datathe byte vector to convert
Returns
a 32-bit integer

◆ decode_32_big() [2/2]

uint32_t decode_32_big ( const std::vector< uint8_t >::const_iterator &  data)
inline

Reconstructs a big-endian byte vector into a 32-bit integer.

Parameters
dataiterator to the byte vector subset to convert
Returns
a 32-bit integer

◆ decode_string()

std::string decode_string ( const std::vector< uint8_t > &  data)
inline

Decodes a little-endian byte vector into an std::string.

Parameters
datathe byte vector to convert
Returns
an std::string

◆ encode_string()

std::vector<uint8_t> encode_string ( const std::string str)
inline

Packs an std::string into a vector of 8-bit integers, in little-endian format.

Parameters
strthe std::string to pack
Returns
the byte representation of str

◆ firmatify_16() [1/2]

std::vector<uint8_t> firmatify_16 ( const std::vector< uint8_t > &  pack)
inline

Calls firmatify_16(const std::vector<uint8_t>::const_iterator&) with pack.cbegin()

Parameters
packthe byte vector to convert
Returns
the firmatified representation of pack

◆ firmatify_16() [2/2]

std::vector<uint8_t> firmatify_16 ( const std::vector< uint8_t >::const_iterator &  pack)
inline

Converts a packed byte vector to the 7-bit packets Firmata receives.

Useful for checking a == decode_16(firmatify_16(pack_16(a)))

e.g. for { 0xEF, 0xBE }:

input = { 1101 1110, 1010 1101 }
firmatified = { 0101 1110, 0000 0001, 0010 1101, 0000 0001 }
= { 0x5E, 0x01, 0x2D, 0x01 }
Parameters
packiterator to the byte vector subset to convert
Returns
the firmatified representation of pack

◆ firmatify_32() [1/2]

std::vector<uint8_t> firmatify_32 ( const std::vector< uint8_t > &  pack)
inline

Calls firmatify_32(const std::vector<uint8_t>::const_iterator&) with pack.cbegin()

Parameters
packthe byte vector to convert
Returns
the firmatified representation of pack

◆ firmatify_32() [2/2]

std::vector<uint8_t> firmatify_32 ( const std::vector< uint8_t >::const_iterator &  pack)
inline

Converts a packed byte vector to the 7-bit packets Firmata receives.

Useful for checking a == decode_32(firmatify_32(pack_32(a)))

E.g. for { 0xEF, 0xBE, 0xAD, 0xDE }:

input = { 1101 1110, 1010 1101, 1011 1110, 1110 1111 }
firmatified = { 0101 1110, 0000 0001, 0010 1101, 0000 0001, 0011 1110, 0000 0001, 0110 1111, 0000 0001 }
= { 0x5E, 0x01, 0x2D, 0x01, 0x3E, 0x01, 0x6F, 0x01 }
Parameters
packiterator to the byte vector subset to convert
Returns
the firmatified representation of pack

◆ firmatify_8()

std::vector<uint8_t> firmatify_8 ( const uint8_t  val)
inline

Converts an 8-bit integer to the two 7-bit packets Firmata uses.

E.g. for 0xBE: input = 1010 1101 firmatified = { 0010 1101, 0000 0001 } = { 0x2D, 0x01 }

Parameters
valthe integer to convert
Returns
the firmatified representation of val

◆ pack_16()

std::vector<uint8_t> pack_16 ( const uint16_t  integer)
inline

Packs a 16-bit integer into a vector of 8-bit integers, in little-endian format.

E.g. for 0xBEEF:

input: 1011 1110 1110 1111
bit grouping: 1111 1111 0000 0000
output: { 0xEF, 0xBE }
Parameters
integerthe integer to pack
Returns
the byte representation of integer

◆ pack_16_big()

std::vector<uint8_t> pack_16_big ( const uint16_t  integer)
inline

Packs a 16-bit integer into a vector of 8-bit integers, in big-endian format.

E.g. for 0xBEEF:

input: 1011 1110 1110 1111
bit grouping: 0000 0000 1111 1111
output: { 0xBE, 0xEF }
Parameters
integerthe integer to pack
Returns
the byte representation of integer

◆ pack_24_big()

std::vector<uint8_t> pack_24_big ( const uint32_t  integer)
inline

Packs a 24-bit integer into a vector of 8-bit integers, in big-endian format.

E.g. for 0AD_xBEEF:

input: 1010 1101 1011 1110 1110 1111
bit grouping: 0000 0000 1111 1111 2222 2222
output: { 0xAD, 0xBE, 0xEF }
Parameters
integerthe integer to pack, with bits [31, 24] ignored
Returns
the byte representation of integer

◆ pack_32()

std::vector<uint8_t> pack_32 ( const uint32_t  integer)
inline

Packs a 32-bit integer into a vector of 8-bit integers, in little-endian format.

E.g. for 0xDEAD_BEEF:

input: 1101 1110 1010 1101 1011 1110 1110 1111
bit grouping: 3333 3333 2222 2222 1111 1111 0000 0000
output: { 0xEF, 0xBE, 0xAD, 0xDE }
Parameters
integerthe integer to pack
Returns
the byte representation of integer

◆ pack_32_big()

std::vector<uint8_t> pack_32_big ( const uint32_t  integer)
inline

Packs a 32-bit integer into a vector of 8-bit integers, in big-endian format.

E.g. for 0xDEAD_BEEF:

input: 1101 1110 1010 1101 1011 1110 1110 1111
bit grouping: 0000 0000 1111 1111 2222 2222 3333 3333
output: { 0xDE, 0xAD, 0xBE, 0xEF }
Parameters
integerthe integer to pack
Returns
the byte representation of integer