43 const WordType (&adc_matrix)[NWords])
46 static_assert(std::is_integral_v<WordType> && std::is_unsigned_v<WordType>,
47 "WordType must be an unsigned integral type");
49 constexpr int bits_per_word = std::numeric_limits<WordType>::digits;
51 static_assert(BitsPerADC > 0 && BitsPerADC <= bits_per_word);
52 static_assert(ADCSPerChannel * NChannels * BitsPerADC == NWords * bits_per_word);
54 if (i_channel < 0 || i_channel >= NChannels) {
55 throw std::out_of_range(
56 std::format(
"Requested channel of {} is out of channel range 0-{}", i_channel, NChannels - 1));
59 if (i_adc < 0 || i_adc >= ADCSPerChannel) {
60 throw std::out_of_range(std::format(
"Requested ADC index of {} if out of range 0-{}", i_adc, ADCSPerChannel - 1));
64 int i_abs = i_adc * NChannels + i_channel;
66 if constexpr (BitsPerADC == bits_per_word) {
67 return adc_matrix[i_abs];
71 int i_word = BitsPerADC * i_abs / bits_per_word;
72 assert(i_word < NWords);
75 int first_bit_position = (BitsPerADC * i_abs) % bits_per_word;
78 int bits_from_first_word = std::min(BitsPerADC, bits_per_word - first_bit_position);
80 WordType adc_val = adc_matrix[i_word] >> first_bit_position;
82 if (bits_from_first_word < BitsPerADC) {
83 assert(i_word < NWords - 1);
84 adc_val |= adc_matrix[i_word + 1] << bits_from_first_word;
88 return adc_val & ((
static_cast<WordType
>(1) << BitsPerADC) - 1);
99 const WordType adc_val,
100 WordType (&adc_matrix)[NWords])
102 static_assert(std::is_integral_v<WordType> && std::is_unsigned_v<WordType>,
103 "WordType must be an unsigned integral type");
105 constexpr int bits_per_word = std::numeric_limits<WordType>::digits;
107 static_assert(BitsPerADC > 0 && BitsPerADC <= bits_per_word);
108 static_assert(ADCSPerChannel * NChannels * BitsPerADC == NWords * bits_per_word);
110 if (i_channel < 0 || i_channel >= NChannels) {
111 throw std::out_of_range(
112 std::format(
"Requested channel of {} is out of channel range 0-{}", i_channel, NChannels - 1));
115 if (i_adc < 0 || i_adc >= ADCSPerChannel) {
116 throw std::out_of_range(std::format(
"Requested ADC index of {} is out of range 0-{}", i_adc, ADCSPerChannel - 1));
119 if constexpr (BitsPerADC < bits_per_word) {
120 if (adc_val >= (
static_cast<WordType
>(1) << BitsPerADC)) {
121 throw std::out_of_range(std::format(
122 "Requested ADC value of {} exceeds max value of {}", adc_val, (
static_cast<WordType
>(1) << BitsPerADC) - 1));
127 int i_abs = i_adc * NChannels + i_channel;
129 if constexpr (BitsPerADC == bits_per_word) {
130 adc_matrix[i_abs] = adc_val;
134 int i_word = BitsPerADC * i_abs / bits_per_word;
135 assert(i_word < NWords);
138 int first_bit_position = (BitsPerADC * i_abs) % bits_per_word;
141 int bits_in_first_word = std::min(BitsPerADC, bits_per_word - first_bit_position);
143 WordType mask = ((
static_cast<WordType
>(1) << bits_in_first_word) - 1) << first_bit_position;
145 adc_matrix[i_word] = (adc_matrix[i_word] & ~mask) | ((
static_cast<WordType
>(adc_val) << first_bit_position) & mask);
148 if (bits_in_first_word < BitsPerADC) {
149 assert(i_word < NWords - 1);
150 int bits_in_second_word = BitsPerADC - bits_in_first_word;
151 WordType mask2 = (
static_cast<WordType
>(1) << bits_in_second_word) - 1;
152 adc_matrix[i_word + 1] = (adc_matrix[i_word + 1] & ~mask2) | ((adc_val >> bits_in_first_word) & mask2);
203set_adc_1d(
const int i_adc, WordType adc_val, WordType (&adc_array)[NWords])
205 static_assert(std::is_integral_v<WordType> && std::is_unsigned_v<WordType>,
206 "WordType must be an unsigned integral type");
208 constexpr int bits_per_word = std::numeric_limits<WordType>::digits;
210 static_assert(BitsPerADC > 0 && BitsPerADC <= bits_per_word);
211 static_assert((NWords * bits_per_word) % BitsPerADC == 0);
213 constexpr int num_adcs = NWords * bits_per_word / BitsPerADC;