iCub-main
can_string_eth.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 iCub Facility - Istituto Italiano di Tecnologia
3  * Author: Davide Pollarolo
4  * website: www.robotcub.org
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16 */
17 
18 #ifndef CAN_STRING_ETH_H
19 #define CAN_STRING_ETH_H
20 
21 #include "messages.h"
22 #include <string.h>
23 #include <yarp/os/LogStream.h>
24 #include <yarp/os/Log.h>
25 #define MAX_STRINGS 4
26 
27 class CanFrame
28 {
29  private:
30  uint8_t size;
31  uint16_t id;
32  uint64_t candata;
33 
34  public:
35  CanFrame();
36  ~CanFrame();
37 
38  void setId (uint16_t id);
39  void setSize(uint8_t size);
40  void setCanData(uint64_t data);
41 
42  uint16_t getId();
43  uint8_t getSize();
44  uint64_t getData();
45 };
46 
48 {
49  this->size = 0; this->id = 0; this->candata = 0;
50 }
51 
53 {
54 }
55 
56 
57 void CanFrame::setId(uint16_t id)
58 {
59  this->id = id;
60 }
61 
62 void CanFrame::setSize(uint8_t size)
63 {
64  this->size = size;
65 }
66 
67 void CanFrame::setCanData(uint64_t data)
68 {
69  this->candata = data;
70 }
71 
72 uint16_t CanFrame::getId()
73 {
74  return this->id;
75 }
76 
78 {
79  return this->size;
80 }
81 
83 {
84  return this->candata;
85 }
86 
87 
89 {
90 private:
91 
92 struct data_struct{
93  char text_buffer [256];
94  bool complete;
95  bool maybe_last_part;
96  char board_id;
97  int expected_length;
98  int current_length;
99 };
100  data_struct data_can[MAX_STRINGS];
101 public:
105  inline can_string_eth();
106 
110  inline ~can_string_eth();
111 
118  inline int add_string(CanFrame* can_packet);
123  inline void clear_string(int buffer_num);
124 
125  inline char* get_string(int buffer_num);
126 };
127 
129 {
130  int j=0;
131  for (j=0; j<MAX_STRINGS; j++) clear_string(j);
132 }
133 
135 {
136 }
137 
139 {
140  int j=0;
141  int string_id = 0;
142  int offset = 0;
143  unsigned int id=0;
144  unsigned int size=0;
145 
146  id=can_packet->getId();
147  size=can_packet->getSize();
148  uint64_t candatabytes = can_packet->getData();
149  char *candata=(char*)&candatabytes;
150  int code = candata[0]&0xFF;
151 
152  string_id = (candata[1]&0xF0)>>4;
153  offset = (candata[1]&0x0F);
154  data_can[string_id].board_id = char(id>>4&0xf);
155 
156  //yWarning("CANPrint msg info: ID->%d, COUNTER->%d, SIZE->%d, CODE->0x%.2x", id, offset, size, code);
157 
158  if (string_id>=MAX_STRINGS)
159  {
160  yError("CANPrint msg from board %d contains an ERROR! (>MAX_STRINGS)\n",data_can[string_id ].board_id);
161  return -1;
162  }
163  for (j=0 ; j<size-2; j++)
164  data_can[string_id].text_buffer[j+offset*6]=candata[j+2];
165 
166  if (code==ICUBCANPROTO_PER_MC_MSG__PRINT + 128)
167  {
168  data_can[string_id].maybe_last_part = true;
169  data_can[string_id].expected_length=offset*6+size-2;
170  }
171 
172  if (data_can[string_id].maybe_last_part)
173  {
174  data_can[string_id].current_length=strlen(data_can[string_id].text_buffer);
175 
176  if (data_can[string_id].expected_length==data_can[string_id].current_length)
177  data_can[string_id].complete = true; //check me
178  }
179 /*
180  if (data_can[string_id].complete)
181  {
182  print(string_id);
183  clear_string(string_id);
184  }
185 */
186  if (data_can[string_id].complete) return string_id;
187  else return -1;
188 }
189 
190 void can_string_eth::clear_string(int buffer_num)
191 {
192  if (buffer_num >= MAX_STRINGS) return ;
193 
194  for (int i = 0; i < 256; i++) data_can[buffer_num].text_buffer[i]=0;
195  data_can[buffer_num].complete=false;
196  data_can[buffer_num].maybe_last_part=false;
197  data_can[buffer_num].expected_length = 0;
198  data_can[buffer_num].board_id=0;
199  data_can[buffer_num].current_length = 0;
200 
201 }
202 
203 char* can_string_eth::get_string(int buffer_num)
204 {
205  if ((buffer_num < 0) || (buffer_num > MAX_STRINGS))
206  {
207  yError("Can't get CANPrint msg from board %d (>MAX_STRINGS)\n",data_can[buffer_num].board_id);
208  return NULL;
209  }
210  return data_can[buffer_num].text_buffer;
211 }
212 
213 #endif // CAN_STRING_ETH_H
214 
@ data
#define MAX_STRINGS
void setSize(uint8_t size)
void setId(uint16_t id)
uint64_t getData()
uint8_t getSize()
void setCanData(uint64_t data)
uint16_t getId()
char * get_string(int buffer_num)
can_string_eth()
Default constructor.
int add_string(CanFrame *can_packet)
Process a string can packet.
void clear_string(int buffer_num)
Resets the string buffer.
~can_string_eth()
Destructor.
degrees offset
Definition: sine.m:4