Modular Electronics  0.1
 All Classes Namespaces Functions Variables Pages
utils.h
1 // Utils
2 // Copyright (C) 2018 Carlos Kometter
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 
17 #ifndef MODULARELECTRONICS_UTILS_h_
18 #define MODULARELECTRONICS_UTILS_h_
19 
27 namespace spi_utils {
35  struct Message {
39  static const uint8_t kdata_len_ = 10;
44  byte msg[kdata_len_];
50  uint8_t block_size;
55  uint8_t n_blocks;
56  };
57 }
58 
66 namespace interface_utils {
73  inline uint8_t query_serial(String cmd[]) {
74  char received;
75  String cmd_element = "";
76  uint8_t cmd_size = 0;
77  while (received != '\r') {
78  if(Serial.available()) {
79  received = Serial.read();
80  if (received == '\n' || received == ' ') {
81  } else if (received == ',' || received == '\r' || received == ':') {
82  cmd[cmd_size] = cmd_element;
83  cmd_element = "";
84  ++cmd_size;
85  } else {
86  cmd_element += received;
87  }
88  }
89  }
90  return cmd_size;
91  }
98  inline uint8_t string_to_int_array(String string_in, uint8_t array_out[]) {
99  for (uint8_t index = 0; index < string_in.length(); ++index) {
100  array_out[index] = String(string_in[index]).toInt();
101  }
102  return string_in.length();
103  }
111  inline void shift_array_left(String array_in[], uint8_t array_in_size,
112  String array_out[], uint8_t n_shift) {
113  for (uint8_t index = 0; index < array_in_size; ++index) {
114  array_out[index] = array_in[index + n_shift];
115  }
116  }
117 }
118 
126 namespace meas_utils {
143  template <typename T1, typename T2>
144  uint8_t BufferRamp(T1 dac, T2 adc,
145  uint8_t dac_channels[], uint8_t n_dac_channels,
146  uint8_t adc_channels[], uint8_t n_adc_channels,
147  double start_voltages[], double end_voltages[],
148  uint32_t n_steps, uint32_t step_delay,
149  uint8_t delay_unit=0) {
150  byte meas[10]; // Storage for measurements
151  uint8_t n_meas; // # of bytes of meas[] used by the adc to store a measurement
152  // Buffer ramp starts here
153  for (uint32_t step = 0; step < n_steps; step++) {
154  // Writes the dac register of each channel in dac_channels[]
155  for (uint8_t dac_channel_index = 0;
156  dac_channel_index < n_dac_channels;
157  dac_channel_index++) {
158  uint8_t channel = dac_channels[dac_channel_index];
159  double start_voltage = start_voltages[dac_channel_index];
160  double end_voltage = end_voltages[dac_channel_index];
161  // Linear ramp from start_voltage to end_voltage in n_steps
162  double next_voltage = start_voltage +
163  (end_voltage - start_voltage) * step / (n_steps - 1);
164  // false: Writes the dac register but does not update the analog output.
165  dac.SetVoltage(channel, next_voltage, false);
166  }
167  // Updates the analog outputs of all channels at the same time
168  dac.UpdateAnalogOutputs();
169  // Waits for system to settle
170  if (delay_unit == 0) {
171  delayMicroseconds(step_delay);
172  }
173  else {
174  delay(step_delay);
175  }
176  // Read the adc voltage
177  for (uint8_t adc_channel_index = 0;
178  adc_channel_index < n_adc_channels;
179  adc_channel_index++) {
180  uint8_t channel = adc_channels[adc_channel_index];
181  // The adc takes some time to convert the voltage to bytes.
182  // While the adc is converting we send the previous measurement
183  // to take advantage of the dead time.
184  if (step > 0 || adc_channel_index > 0) {
185  // Sends the previous measurement stored in meas.
186  n_meas = adc.ReadVoltage(channel, meas, true);
187  }
188  else {
189  // First measurement, nothing to send so false
190  n_meas = adc.ReadVoltage(channel, meas, false);
191  }
192  }
193  }
194  // Sends the last measurement
195  for (uint8_t meas_index = 0; meas_index < n_meas; meas_index++) {
196  Serial.write(meas[meas_index]);
197  }
198  return 0;
199  }
200 }
201 #endif
uint8_t block_size
Definition: utils.h:50
void shift_array_left(String array_in[], uint8_t array_in_size, String array_out[], uint8_t n_shift)
Definition: utils.h:111
uint8_t string_to_int_array(String string_in, uint8_t array_out[])
Definition: utils.h:98
static const uint8_t kdata_len_
Definition: utils.h:39
Definition: utils.h:35
uint8_t n_blocks
Definition: utils.h:55
uint8_t BufferRamp(T1 dac, T2 adc, uint8_t dac_channels[], uint8_t n_dac_channels, uint8_t adc_channels[], uint8_t n_adc_channels, double start_voltages[], double end_voltages[], uint32_t n_steps, uint32_t step_delay, uint8_t delay_unit=0)
Definition: utils.h:144
uint8_t query_serial(String cmd[])
Definition: utils.h:73
byte msg[kdata_len_]
Definition: utils.h:44