arduinodaq2pc-structs.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016-17, Universidad de Almeria
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 
36 #pragma once
37 
38 #include <stdlib.h>
39 
40 #if !defined(__AVR_MEGA__)
41 # pragma pack(push, 1) // exact fit - no padding
42 #endif
43 
44 /*
45 START_FLAG | OPCODE | DATA_LEN | DATA | CHECKSUM | END_FLAG |
46  0x69 1 byte 1 byte N bytes =sum(data) 0x96
47 
48 ## Computer => controller
49 * 0x10: Set DAC value. DATA_LEN = 3
50  * DATA[0] = DAC index
51  * DATA[1:2] = DAC value (0x0000-0xffff) (MSByte first!)
52 * 0x11: Set GPIO pin. DATA_LEN = 2
53  * DATA[0] = Arduino-based pin number
54  * DATA[1] = 0/1
55 * 0x12: Read GPIO pin. DATA_LEN = 1
56  * DATA[0] = Arduino-based pin number
57 * 0x20: Start ADC continuous acquisition task
58 * 0x21: Stop ADC task
59 */
60 
61 #define FRAME_START_FLAG 0x69
62 #define FRAME_END_FLAG 0x96
63 
64 enum opcode_t {
65  // -----------------------------
66  // COMMANDS PC -> Arduino
67  // -----------------------------
68  OP_NOP = 0x00,
69  OP_SET_DAC = 0x10,
70  OP_SET_GPIO = 0x11,
71  OP_GET_GPIO = 0x12,
74  OP_SET_PWM = 0x25,
77  OP_START_EMS22A = 0x40, //
78  OP_STOP_EMS22A = 0x41, //
79 
80  // -----------------------------
81  // Responses Arduino -> PC
82  // -----------------------------
83  RESP_OFFSET = 0x70,
84  // -----------------------------
99 
100 
101  // error codes:
107 };
108 
109 
110 template <typename Payload>
112 {
113  const uint8_t START_FLAG;
114  const uint8_t OPCODE;
115  const uint8_t DATALEN;
116  // --------- Payload -------
117  Payload payload;
118  // -------------------------
119  uint8_t CHECKSUM;
120  const uint8_t END_FLAG;
121 
122  // Defaults:
123  TBaseFrame(uint8_t opcode) :
124  START_FLAG(FRAME_START_FLAG),
125  OPCODE(opcode),
126  DATALEN(sizeof(Payload)),
127  END_FLAG(FRAME_END_FLAG)
128  {
129  }
130 
132  {
133  CHECKSUM = calc_checksum();
134  }
135 
136  uint8_t calc_checksum() const
137  {
138  const uint8_t len = DATALEN; //reinterpret_cast<const uint8_t*>(ptr_frame)[2];
139  const uint8_t *data = reinterpret_cast<const uint8_t*>(&payload);
140  uint8_t ret =0;
141  for (unsigned int i=0;i<len;i++) ret+=*data++;
142  return ret;
143  }
144 };
145 
147 {
148 };
149 struct TFrameCMD_NOP : public TBaseFrame<TFrameCMD_NOP_payload_t>
150 {
152  {
153  }
154 };
155 
157 {
158  uint8_t dac_index;
159  uint8_t dac_value_HI, dac_value_LO;
160  uint8_t flag_enable_timeout : 1; // bitfield: 1 bit flag
162  dac_index(0), dac_value_HI(0), dac_value_LO(0),flag_enable_timeout(0)
163  {
164  }
165 };
166 struct TFrameCMD_SetDAC : public TBaseFrame<TFrameCMD_SetDAC_payload_t>
167 {
168  // Defaults:
170  {
171  }
172 };
173 
175 {
176  uint8_t pin_index;
177  uint8_t pin_value;
178 };
179 struct TFrameCMD_GPIO_output : public TBaseFrame<TFrameCMD_GPIO_output_payload_t>
180 {
181  // Defaults:
183  {
184  }
185 };
186 
188 {
189  uint8_t pin_index;
190 };
191 struct TFrameCMD_GPIO_read : public TBaseFrame<TFrameCMD_GPIO_read_payload_t>
192 {
193  // Defaults:
195  {
196  }
197 };
198 
199 
201 {
204  int8_t active_channels[8];
205  uint16_t measure_period_ms;
207 
209  measure_period_ms(200),
210  use_internal_refvolt(0)
211  {
212  for (int i=0;i<8;i++) {
213  active_channels[i]=-1;
214  }
215  }
216 };
217 struct TFrameCMD_ADC_start : public TBaseFrame<TFrameCMD_ADC_start_payload_t>
218 {
219  // Defaults:
221  {
222  }
223 };
224 
226 {
227 };
228 struct TFrameCMD_ADC_stop : public TBaseFrame<TFrameCMD_ADC_stop_payload_t>
229 {
230  // Defaults:
232  {
233  }
234 };
235 
237 {
238  uint8_t pin_index;
239  uint8_t analog_value;
240  uint8_t flag_enable_timeout : 1; // bitfield: 1 bit flag
242  pin_index(0),
243  analog_value(0),
244  flag_enable_timeout(0)
245  {
246  }
247 };
248 struct TFrameCMD_SET_PWM : public TBaseFrame<TFrameCMD_SET_PWM_payload_t>
249 {
250  // Defaults:
252  {
253  }
254 };
255 
257 {
259  uint16_t adc_data[8];
260 };
261 struct TFrame_ADC_readings : public TBaseFrame<TFrame_ADC_readings_payload_t>
262 {
263  // Defaults:
265  {
266  }
267 };
268 
269 
271 {
272  static const uint8_t NUM_ENCODERS = 2;
273 
277  int8_t encA_pin[NUM_ENCODERS], encB_pin[NUM_ENCODERS], encZ_pin[NUM_ENCODERS];
279 
281  sampling_period_ms(250)
282  {
283  for (uint8_t i=0;i<NUM_ENCODERS;i++) {
284  encA_pin[i]=encB_pin[i]=encZ_pin[i]=0;
285  }
286  }
287 };
288 struct TFrameCMD_ENCODERS_start : public TBaseFrame<TFrameCMD_ENCODERS_start_payload_t>
289 {
290  // Defaults:
292  {
293  }
294 };
295 
297 {
298 };
299 struct TFrameCMD_ENCODERS_stop : public TBaseFrame<TFrameCMD_ENCODERS_stop_payload_t>
300 {
301  // Defaults:
303  {
304  }
305 };
306 
307 
309 {
311  int32_t encoders[2];
313 };
314 struct TFrame_ENCODERS_readings : public TBaseFrame<TFrame_ENCODERS_readings_payload_t>
315 {
316  // Defaults:
318  {
319  }
320 };
321 
323 {
325  uint16_t enc_pos;
326  uint8_t enc_status;
327 };
328 struct TFrame_ENCODER_ABS_reading : public TBaseFrame<TFrame_ENCODER_ABS_reading_payload_t>
329 {
330  // Defaults:
332  {
333  }
334 };
335 
337 {
340 
342  sampling_period_ms(50)
343  {
344  ENCODER_ABS_CS=ENCODER_ABS_CLK=ENCODER_ABS_DO=0;
345  }
346 };
347 
348 struct TFrameCMD_EMS22A_start : public TBaseFrame<TFrameCMD_EMS22A_start_payload_t>
349 {
350  // Defaults:
352  {
353  }
354 };
355 
357 {
358 };
359 struct TFrameCMD_EMS22A_stop : public TBaseFrame<TFrameCMD_EMS22A_stop_payload_t>
360 {
361  // Defaults:
363  {
364  }
365 };
366 
367 
368 #if !defined(__AVR_MEGA__)
369 # pragma pack(pop)
370 #endif
uint8_t calc_checksum() const
void calc_and_update_checksum()
const uint8_t DATALEN
uint16_t enc_pos
Absolute value read from the encoder (10 bits resolution)
uint8_t analog_value
0-255 maps to 0% to 100% duty cycle
unsigned int uint32_t
const uint8_t OPCODE
const uint8_t START_FLAG
uint8_t enc_status
See EMS22A datasheet for the bit map.
GLenum GLsizei len
const uint8_t END_FLAG
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
#define FRAME_START_FLAG
int ENCODER_ABS_CS
int ENCODER_ABS_CLK
TBaseFrame(uint8_t opcode)
uint8_t use_internal_refvolt
0 or 1. Default=0
uint16_t measure_period_ms
Default = 200.
signed int int32_t
#define FRAME_END_FLAG


arduino_daq
Author(s):
autogenerated on Mon Jun 10 2019 12:46:03