h4r_x52_joyext
LED and MFD Control for Saitek X52 Pro
 All Classes Files Functions Variables Enumerator Macros Pages
x52_joyext.cpp
Go to the documentation of this file.
1 /*
2  * x52_joyext Node
3  * by Christian Holl (www.rad-lab.net)
4  * License: BSD
5  *
6  * Have Fun! :-)
7  */
8 
9 /*
10  * Modified by Murilo FM (muhrix@gmail.com)
11  * 12 Dec 2013
12  *
13  */
14 
15 
17 
18 X52_JoyExt::X52_JoyExt(ros::NodeHandle &n):nh(n)
19 {
20  //bzero(updateLED,10);
21  memset(updateLED,1,10);
22  updateLED_b=false;
23  memset(updateMFD,true,3);
24 
25  updateDate=false;
26  updateTime=false;
27  memset(updateOffset,false,2);
28  updateBrightnessMFD=false;
29  updateBrightnessLED=false;
30  bzero(LED,19);
31  //memset(LED,1,19);
32 
33  bzero(Date,3);
34  bzero(Time,2);
35  bzero(Offset,2);
36  brightnessMFD=255;
37  brightnessLED=255;
38 
41 
42  //MFD content write
43  mfd_content[0]="x52_joyext Node";
44  mfd_content[1]=" Joystick under ";
45  mfd_content[2]=" ROS Control ";
46 
47 
48  int rate;
49  n.param("loop_rate", rate, 100);
50  loop_rate=new ros::Rate(rate);
51 
52  subleds=nh.subscribe("leds",1000, &X52_JoyExt::cb_leds,this);
53  submfd_text=nh.subscribe("mfd_text",1000, &X52_JoyExt::cb_mfd_text,this);
54  subdate=nh.subscribe("mfd_date",1000, &X52_JoyExt::cb_date,this);
55  subtime=nh.subscribe("mfd_time",1000, &X52_JoyExt::cb_time,this);
56  subbrightnessMFD=nh.subscribe("mfd_brightness",1000, &X52_JoyExt::cb_brighnessMFD,this);
57  subbrightnessLED=nh.subscribe("leds_brightness",1000, &X52_JoyExt::cb_brighnessLED,this);
58 
59  //Move to handling
61 }
62 
63 
65 {
66  delete loop_rate;
67 }
68 
69 void X52_JoyExt::cb_leds(const h4r_x52_joyext::x52_led_colorConstPtr &msg)
70 {
71  if(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_FIRE])
72  {
73  if( msg->color_leds[ h4r_x52_joyext::x52_led_color::LED_FIRE ] >2)
74  ROS_WARN("WRONG VALUE (%i) FOR LED FOUND! Value must be in the range of 0-2",msg->color_leds[h4r_x52_joyext::x52_led_color::LED_FIRE]);
75  LED[0]=(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_FIRE]>1);
76  updateLED[0]=true;
77  }
78  // void setLEDs(uint8_t inValue, uint8_t *red, uint8_t *green, bool *update)
79  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_A] ,&LED[X52PRO_LED_ARED-1] ,&LED[X52PRO_LED_AGREEN-1] ,&updateLED[1]);
80  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_B] ,&LED[X52PRO_LED_BRED-1] ,&LED[X52PRO_LED_BGREEN-1] ,&updateLED[2]);
81  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_D] ,&LED[X52PRO_LED_DRED-1] ,&LED[X52PRO_LED_DGREEN-1] ,&updateLED[3]);
82  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_E] ,&LED[X52PRO_LED_ERED-1] ,&LED[X52PRO_LED_EGREEN-1] ,&updateLED[4]);
83  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_T12] ,&LED[X52PRO_LED_T1RED-1],&LED[X52PRO_LED_T1GREEN-1],&updateLED[5]);
84  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_T34] ,&LED[X52PRO_LED_T2RED-1],&LED[X52PRO_LED_T2GREEN-1],&updateLED[6]);
85  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_T56] ,&LED[X52PRO_LED_T3RED-1],&LED[X52PRO_LED_T3GREEN-1],&updateLED[7]);
86  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_POV2],&LED[X52PRO_LED_CORED-1],&LED[X52PRO_LED_COGREEN-1],&updateLED[8]);
87  setLEDs(msg->color_leds[h4r_x52_joyext::x52_led_color::LED_I] ,&LED[X52PRO_LED_IRED-1] ,&LED[X52PRO_LED_IGREEN-1] ,&updateLED[9]);
88 }
89 
90 void X52_JoyExt::cb_mfd_text(const h4r_x52_joyext::x52_mfdConstPtr &msg)
91 {
92  int curPos=msg->pos+16*msg->line;
93 
94  if(msg->clearDisplay)
95  {
96  mfd_content[0]=" ";
97  mfd_content[1]=" ";
98  mfd_content[2]=" ";
99  memset(updateMFD,true,3);
100  }
101 
102  for (std::string::const_iterator it=msg->data.begin(); it != msg->data.end(); ++it)
103  {
104  char curChr=*it;
105 
106  if(curPos<3*16)
107  {
108  uint8_t curLine=curPos/16;
109  uint8_t posInLine=curPos-curLine*16;
110  if(curChr=='\n')
111  {
112  //Fill the rest of the line with spaces
113  for (curPos = 0; curPos < (curLine+1)*16; ++curPos)
114  {
115  posInLine=curPos-curLine*16;
116  mfd_content[curLine][posInLine]=curChr;
117  }
118  continue;
119  }
120  else
121  {
122  mfd_content[curLine][posInLine]=curChr;
123  updateMFD[curLine]=true;
124  curPos++;
125  }
126  }
127  else
128  {
129  ROS_WARN("MFD text exceeds display bounds!");
130  break;
131  }
132  }
133  ROS_DEBUG("Line 0: %s", mfd_content[0].c_str());
134  ROS_DEBUG("Line 1: %s", mfd_content[1].c_str());
135  ROS_DEBUG("Line 2: %s", mfd_content[2].c_str());
136 }
137 
138 void X52_JoyExt::cb_date(const h4r_x52_joyext::x52_dateConstPtr &msg)
139 {
140  Date[2]=msg->date_field_left;
141  Date[1]=msg->date_field_center;
142  Date[0]=msg->date_field_right;
143  updateDate=true;
144 }
145 
146 void X52_JoyExt::cb_time(const h4r_x52_joyext::x52_timeConstPtr &msg)
147 {
148  if(msg->set_time)
149  {
150  updateTime=true;
151  Time_24=msg->time_24;
152  Time[0]=msg->time_hours;
153  Time[1]=msg->time_minutes;
154  }
155 
156  if(msg->set_offset_0)
157  {
158  updateOffset[0]=true;
159  Offset_24[0]=msg->offset_0_24;
160  Offset_Inv[0]=msg->offset_0_inv;
161  Offset[0]=msg->offset_0;
162  }
163 
164  if(msg->set_offset_1)
165  {
166  updateOffset[1]=true;
167  Offset_24[1]=msg->offset_1_24;
168  Offset_Inv[1]=msg->offset_1_inv;
169  Offset[1]=msg->offset_1;
170  }
171 }
172 
173 void X52_JoyExt::cb_brighnessMFD(const std_msgs::UInt8ConstPtr &msg)
174 {
175  brightnessMFD=msg->data;
176  updateBrightnessMFD=true;
177 }
178 
179 void X52_JoyExt::cb_brighnessLED(const std_msgs::UInt8ConstPtr &msg)
180 {
181  brightnessLED=msg->data;
182  updateBrightnessLED=true;
183 }
184 
185 
186 #define SLEEP_AFTER_COMMAND usleep(5000)
187 
189 {
190  /*Get Joystick */
191  struct x52 *hdl=0;
192  while(hdl==0 && ros::ok())
193  {
194  ROS_INFO("Trying to find joystick...");
195  hdl=x52_init();
196  loop_rate->sleep();
197  }
198  ROS_INFO("Joystick found...");
199  x52_debug(hdl, 1);
200 
201 
202  x52_setled(hdl,0,0);
203 
204  while(ros::ok())
205  {
206  //Update Joystick LED
207  for (int lednum = 0; lednum < 10; ++lednum)
208  {
209  if(updateLED[lednum])
210  {
211  ROS_DEBUG("Setting LED: %i",lednum);
212  if(lednum != 0)
213  {
214  int led=(lednum)*2;
215  x52_setled(hdl,led,LED[led-1]);
216  x52_setled(hdl,led+1,LED[led]);
217  }
218  else
219  {
220  x52_setled(hdl,1,LED[0]);
221  }
222  updateLED[lednum]=false;
223  usleep(50);
224  }
225  }
226 
227  //Update Brightness MFD
229  {
230  x52_setbri(hdl,1,brightnessMFD);
231  updateBrightnessMFD=false;
232  usleep(50);
233  }
234 
235  //Update Brightness LED
236 
238  {
239  x52_setbri(hdl,0,brightnessLED);
240  updateBrightnessLED=false;
242  }
243 
244  //Update MFD Text
245  for (int line = 0; line < 3; ++line)
246  {
247  if(updateMFD[line])
248  {
249  x52_settext(hdl,line,(char*)mfd_content[line].c_str(),mfd_content[line].length());
250  updateMFD[line]=false;
251  }
253  }
254 
255  //Update Date
256  if(updateDate)
257  {
258  x52_setdate(hdl,Date[0],Date[1],Date[2]);
259  updateDate=false;
261  }
262 
263  //Update Time
264  if(updateTime)
265  {
266  x52_settime(hdl,Time_24,Time[0], Time[1]);
267  updateTime=false;
269  }
270 
271  //Offset Time
272  for (int o = 0; o < 2; ++ o)
273  {
274  if(updateOffset[o])
275  {
276  x52_setoffs(hdl,o,Offset_24[o],Offset_Inv[o],Offset[o]);
277  updateOffset[o]=false;
278  }
280  }
281 
282  ros::spinOnce();
283  loop_rate->sleep();
284  }
285 }
uint8_t brightnessLED
Definition: x52_joyext.h:70
ros::Subscriber subbrightnessLED
Definition: x52_joyext.h:78
uint8_t Date[3]
Definition: x52_joyext.h:63
bool updateMFD[3]
Definition: x52_joyext.h:52
uint8_t brightnessMFD
Definition: x52_joyext.h:69
ros::Subscriber submfd_text
Definition: x52_joyext.h:74
ros::Rate * loop_rate
Definition: x52_joyext.h:47
ros::Subscriber subdate
Definition: x52_joyext.h:75
bool updateLED[10]
Definition: x52_joyext.h:50
uint8_t Time_24
Definition: x52_joyext.h:64
void cb_brighnessMFD(const std_msgs::UInt8ConstPtr &msg)
Definition: x52_joyext.cpp:173
std::string mfd_content[3]
Definition: x52_joyext.h:62
#define SLEEP_AFTER_COMMAND
Definition: x52_joyext.cpp:186
bool updateBrightnessLED
Definition: x52_joyext.h:58
bool updateBrightnessMFD
Definition: x52_joyext.h:57
bool updateDate
Definition: x52_joyext.h:54
bool updateOffset[2]
Definition: x52_joyext.h:56
uint8_t Offset_Inv[2]
Definition: x52_joyext.h:68
void cb_brighnessLED(const std_msgs::UInt8ConstPtr &msg)
Definition: x52_joyext.cpp:179
X52_JoyExt(ros::NodeHandle &n)
Definition: x52_joyext.cpp:18
void setLEDs(uint8_t inValue, uint8_t *red, uint8_t *green, bool *update)
Definition: x52_joyext.h:80
uint8_t Time[2]
Definition: x52_joyext.h:65
void cb_mfd_text(const h4r_x52_joyext::x52_mfdConstPtr &msg)
Definition: x52_joyext.cpp:90
void send_to_joystick()
Definition: x52_joyext.cpp:188
void cb_date(const h4r_x52_joyext::x52_dateConstPtr &msg)
Definition: x52_joyext.cpp:138
ros::NodeHandle & nh
Definition: x52_joyext.h:46
void cb_leds(const h4r_x52_joyext::x52_led_colorConstPtr &msg)
Definition: x52_joyext.cpp:69
void cb_time(const h4r_x52_joyext::x52_timeConstPtr &msg)
Definition: x52_joyext.cpp:146
uint8_t Offset[2]
Definition: x52_joyext.h:66
virtual ~X52_JoyExt()
Definition: x52_joyext.cpp:64
ros::Subscriber subbrightnessMFD
Definition: x52_joyext.h:77
ros::Subscriber subtime
Definition: x52_joyext.h:76
uint8_t LED[19]
Definition: x52_joyext.h:61
uint8_t Offset_24[2]
Definition: x52_joyext.h:67
ros::Subscriber subleds
Definition: x52_joyext.h:73
bool updateLED_b
Definition: x52_joyext.h:51
bool updateTime
Definition: x52_joyext.h:55