41get_adc_2d_as_1d(
const int i_adc,
const int i_channel,
const WordType (&adc_matrix)[NWords])
44 static_assert(std::is_integral_v<WordType> && std::is_unsigned_v<WordType>,
45 "WordType must be an unsigned integral type");
47 constexpr int bits_per_word = std::numeric_limits<WordType>::digits;
49 static_assert(BitsPerADC > 0 && BitsPerADC <= bits_per_word);
50 static_assert(ADCSPerChannel * NChannels * BitsPerADC == NWords * bits_per_word);
52 if (i_channel < 0 || i_channel >= NChannels) {
53 throw std::out_of_range(
54 std::format(
"Requested channel of {} is out of channel range 0-{}", i_channel, NChannels - 1));
57 if (i_adc < 0 || i_adc >= ADCSPerChannel) {
58 throw std::out_of_range(std::format(
"Requested ADC index of {} if out of range 0-{}", i_adc, ADCSPerChannel - 1));
62 int i_abs = i_adc * NChannels + i_channel;
64 if constexpr (BitsPerADC == bits_per_word) {
65 return adc_matrix[i_abs];
69 int i_word = BitsPerADC * i_abs / bits_per_word;
70 assert(i_word < NWords);
73 int first_bit_position = (BitsPerADC * i_abs) % bits_per_word;
76 int bits_from_first_word = std::min(BitsPerADC, bits_per_word - first_bit_position);
78 WordType adc_val = adc_matrix[i_word] >> first_bit_position;
80 if (bits_from_first_word < BitsPerADC) {
81 assert(i_word < NWords - 1);
82 adc_val |= adc_matrix[i_word + 1] << bits_from_first_word;
86 return adc_val & ((
static_cast<WordType
>(1) << BitsPerADC) - 1);
95set_adc_2d_as_1d(
const int i_adc,
const int i_channel,
const WordType adc_val, WordType (&adc_matrix)[NWords])
97 static_assert(std::is_integral_v<WordType> && std::is_unsigned_v<WordType>,
98 "WordType must be an unsigned integral type");
100 constexpr int bits_per_word = std::numeric_limits<WordType>::digits;
102 static_assert(BitsPerADC > 0 && BitsPerADC <= bits_per_word);
103 static_assert(ADCSPerChannel * NChannels * BitsPerADC == NWords * bits_per_word);
105 if (i_channel < 0 || i_channel >= NChannels) {
106 throw std::out_of_range(
107 std::format(
"Requested channel of {} is out of channel range 0-{}", i_channel, NChannels - 1));
110 if (i_adc < 0 || i_adc >= ADCSPerChannel) {
111 throw std::out_of_range(std::format(
"Requested ADC index of {} is out of range 0-{}", i_adc, ADCSPerChannel - 1));
114 if constexpr (BitsPerADC < bits_per_word) {
115 if (adc_val >= (
static_cast<WordType
>(1) << BitsPerADC)) {
116 throw std::out_of_range(std::format(
117 "Requested ADC value of {} exceeds max value of {}", adc_val, (
static_cast<WordType
>(1) << BitsPerADC) - 1));
122 int i_abs = i_adc * NChannels + i_channel;
124 if constexpr (BitsPerADC == bits_per_word) {
125 adc_matrix[i_abs] = adc_val;
129 int i_word = BitsPerADC * i_abs / bits_per_word;
130 assert(i_word < NWords);
133 int first_bit_position = (BitsPerADC * i_abs) % bits_per_word;
136 int bits_in_first_word = std::min(BitsPerADC, bits_per_word - first_bit_position);
138 WordType mask = ((
static_cast<WordType
>(1) << bits_in_first_word) - 1) << first_bit_position;
140 adc_matrix[i_word] = (adc_matrix[i_word] & ~mask) | ((
static_cast<WordType
>(adc_val) << first_bit_position) & mask);
143 if (bits_in_first_word < BitsPerADC) {
144 assert(i_word < NWords - 1);
145 int bits_in_second_word = BitsPerADC - bits_in_first_word;
146 WordType mask2 = (
static_cast<WordType
>(1) << bits_in_second_word) - 1;
147 adc_matrix[i_word + 1] = (adc_matrix[i_word + 1] & ~mask2) | ((adc_val >> bits_in_first_word) & mask2);
196set_adc_1d(
const int i_adc, WordType adc_val, WordType (&adc_array)[NWords])
198 static_assert(std::is_integral_v<WordType> && std::is_unsigned_v<WordType>,
199 "WordType must be an unsigned integral type");
201 constexpr int bits_per_word = std::numeric_limits<WordType>::digits;
203 static_assert(BitsPerADC > 0 && BitsPerADC <= bits_per_word);
204 static_assert((NWords * bits_per_word) % BitsPerADC == 0);
206 constexpr int num_adcs = NWords * bits_per_word / BitsPerADC;