iCub-main
can_string.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2008 The RobotCub Consortium
5  * Author: Marco Randazzo, Marco Maggiali
6  * website: www.robotcub.org
7  * Permission is granted to copy, distribute, and/or modify this program
8  * under the terms of the GNU General Public License, version 2 or any
9  * later version published by the Free Software Foundation.
10  *
11  * A copy of the license can be found at
12  * http://www.robotcub.org/icub/license/gpl.txt
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Public License for more details
18 */
19 
20 #ifndef __can_string2__
21 #define __can_string2__
22 
23 #include "messages.h"
24 #include <yarp/os/LogStream.h>
25 #include <yarp/os/Log.h>
26 
27 template<class T>
29 {
30  enum __MAX_STRING
31  {
32  MAX_STRINGS=4,
33  };
34 
35 private:
36 
37 struct data_struct{
38  char text_buffer [256];
39  bool complete;
40  bool maybe_last_part;
41  char board_id;
42  int expected_length;
43  int current_length;
44 };
45  data_struct data[MAX_STRINGS];
46 
47 public:
51  can_string2();
52 
56  ~can_string2();
57 
64  inline int add_string(void* can_packet);
65 
73  inline char* print(int buffer_num);
74 
79  inline void clear_string(int buffer_num);
80 };
81 
82 template<class T>
84 {
85  int j=0;
86  for (j=0; j<MAX_STRINGS; j++) clear_string(j);
87 }
88 
89 template<class T>
91 {
92 }
93 
94 template<class T>
95 void can_string2<T>::clear_string(int buffer_num)
96 {
97  if (buffer_num >= MAX_STRINGS) return ;
98 
99  for (int i = 0; i < 256; i++) data[buffer_num].text_buffer[i]=0;
100  data[buffer_num].complete=false;
101  data[buffer_num].maybe_last_part=false;
102  data[buffer_num].expected_length = 0;
103  data[buffer_num].board_id=0;
104  data[buffer_num].current_length = 0;
105 
106 }
107 
108 template<class T>
109 int can_string2<T>::add_string(void* can_packet)
110 {
111  int j=0;
112  int string_id = 0;
113  int offset = 0;
114  T* t;
115 
116  t = (T*) (can_packet);
117  string_id = (t->data[1]>>4);
118  offset = (t->data[1]&0x0F);
119  data[string_id].board_id = char(t->id>>4&0xf);
120 
121  if (string_id>=MAX_STRINGS)
122  {
123  yError("msg from board %d contains an ERROR! (>MAX_STRINGS)\r\n",data[string_id ].board_id);
124  return -1;
125  }
126 
127  for (j=0 ; j<t->len-2; j++)
128  data[string_id].text_buffer[j+offset*6]=t->data[j+2];
129 
130  if (t->data[0]==ICUBCANPROTO_PER_MC_MSG__PRINT + 128)
131  {
132  data[string_id].maybe_last_part = true;
133  data[string_id].expected_length=offset*6+t->len-2;
134  }
135 
136  if (data[string_id].maybe_last_part)
137  {
138  data[string_id].current_length=ACE_OS::strlen(data[string_id].text_buffer);
139 
140  if (data[string_id].expected_length==data[string_id].current_length)
141  data[string_id].complete = true; //check me
142  }
143 /*
144  if (data[string_id].complete)
145  {
146  print(string_id);
147  clear_string(string_id);
148  }
149 */
150 /*
151  //DEBUG ONLY
152  ACE_OS::printf("%d %d\r\n",string_id,offset);
153 
154  for (j=0 ; j<50; j++)
155  ACE_OS::printf("%c",data[string_id].text_buffer[j]);
156 
157  ACE_OS::printf("\r\n");
158 
159  for (j=0 ; j<50; j++)
160  ACE_OS::printf("%d",data[string_id].text_buffer[j]);
161 
162  ACE_OS::printf("\r\n");
163 */
164  if (data[string_id].complete) return string_id;
165  else return -1;
166 }
167 
168 template<class T>
169 char* can_string2<T>::print(int buffer_num)
170 {
171  if (buffer_num>=MAX_STRINGS) return 0;
172 
173  yDebug("msg from board %d: %s \r\n",data[buffer_num].board_id , data[buffer_num].text_buffer);
174 
175  return data[buffer_num].text_buffer;
176 }
177 
178 
179 #endif
@ data
#define MAX_STRINGS
void clear_string(int buffer_num)
Resets the string buffer.
Definition: can_string.h:95
int add_string(void *can_packet)
Process a string can packet.
Definition: can_string.h:109
char * print(int buffer_num)
Prints a string buffer.
Definition: can_string.h:169
~can_string2()
Destructor.
Definition: can_string.h:90
can_string2()
Default constructor.
Definition: can_string.h:83
degrees offset
Definition: sine.m:4