dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_ir_transponder.c
Go to the documentation of this file.
1 
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18  * for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, see <http://www.gnu.org/licenses/>
22  *
23  * Additional note on redistribution: The copyright and license notices above
24  * must be maintained in each individual source file that is a derivative work
25  * of this source file; otherwise redistribution is prohibited.
26  */
27 
28 #include <pios.h>
29 
30 #if defined(PIOS_INCLUDE_IR_TRANSPONDER)
31 #include <misc_math.h>
32 
36 static void ir_invert_bits(uint8_t * data, uint8_t data_len)
37 {
38  for (int i=0; i<data_len; i++) {
39  data[i] = ~data[i];
40  }
41 }
42 
48 void pios_ir_generate_ilap_packet(uint32_t ilap_id, uint8_t * data, uint8_t data_len)
49 {
50  if (data_len < 6)
51  return;
52 
53  uint8_t crc_data[4] = {0, 0, 0, 0};
54 
55  uint8_t digit;
56 
57  // BCD encoded in reverse
58  uint32_t decimal = 10000000;
59 
60  for (int pos=3; pos>=0; pos--) {
61  digit = MIN(9, (ilap_id / decimal));
62  ilap_id -= digit * decimal;
63  crc_data[pos] |= (digit << 4) & 0xF0;
64  decimal /= 10;
65  digit = MIN(9, (ilap_id / decimal));
66  ilap_id -= digit * decimal;
67  crc_data[pos] |= digit & 0x0F;
68  decimal /= 10;
69  }
70  crc_data[3] |= 0xf0;
71 
72  uint16_t crc = PIOS_CRC16_CCITT_updateCRC(0x00, crc_data, 4);
73 
74  data[0] = crc_data[3];
75  data[1] = (crc & 0xFF00) >> 8;
76  data[2] = crc_data[2];
77  data[3] = crc_data[1];
78  data[4] = crc_data[0];
79  data[5] = (crc & 0x00FF);
80 
81  ir_invert_bits(data, 6);
82 }
83 
89 void pios_ir_generate_trackmate_packet(uint16_t trackmate_id, uint8_t * data, uint8_t data_len)
90 {
91  if (data_len < 4)
92  return;
93 
94  uint8_t crc_data[2];
95  crc_data[1] = trackmate_id & 0x00FF;
96  crc_data[0] = (trackmate_id & 0xFF00) >> 8;
97 
98  uint16_t crc = PIOS_CRC16_CCITT_updateCRC(0xFB1A, crc_data, 2);
99 
100  data[0] = (trackmate_id >> 8) & 0xFF;
101  data[1] = trackmate_id & 0xFF;
102  data[2] = (crc >> 8) & 0xFF;
103  data[3] = crc & 0xFF;
104 }
105 
106 #endif /* defined(PIOS_INCLUDE_IR_TRANSPONDER) */
union @12 crc
uint16_t PIOS_CRC16_CCITT_updateCRC(uint16_t crc, const uint8_t *data, uint32_t data_len)
Definition: pios_crc.c:235
Main PiOS header to include all the compiled in PiOS options.
void pios_ir_generate_trackmate_packet(uint16_t trackmate_id, uint8_t *data, uint8_t data_len)
uint8_t data[XFER_BYTES_PER_PACKET]
Definition: bl_messages.h:129
struct _msp_pid_item pos
Definition: msp_messages.h:100
uint8_t i
Definition: msp_messages.h:97
void pios_ir_generate_ilap_packet(uint32_t ilap_id, uint8_t *data, uint8_t data_len)
#define MIN(a, b)
Definition: misc_math.h:41