I'm working with a peripheral which sends lowest 8 bit word first. So the first word (in this case 16bit are send) has to be on the right hand side. As seen in variable v1.
I would like to have a more expressive version of this so I wanted to add a constexpr function that shifts the first parameter on the right most position of the resulting integer. While still using it as the first parameter of the function.
I didn't really came up with a good solution. As I build up a "shift value" and decrease it upon leaving the recursion. Also, this is not working in a constexpr environment, which is for me a "no go". Maybe someone of you has a suggestion ?
I did try a lot of things. But mostly not working. fold expressions also didn't really help. Cpp Version doesn't matter (c++2a is also okay)
// send 0x05 first then 0xFF
std::uint16_t v1 = (0xFFU << 8U | 0x05U); // right align first 8bit word so it is send first
std::uint16_t v2 = lower_word_first(0x05U, 0xFFU);
// lower_word_first
static std::size_t shift = 0;
auto lower_word_first(){
return 0;
}
template<typename unsigned_word_type, typename... words>
auto lower_word_first(unsigned_word_type word, words... ws){
shift += 1;
auto val = lower_word_first(ws...); // just for debugging purposes split into val
auto res = val | word << (shift - 1) * 8U;
shift -= 1;
return res;
}