direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rgbLed.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #include "rgbLed.h"
22 
24 {
25  // get the name of this class (this is for debugging messages)
26  className = this->staticMetaObject.className();
27 
28  // copy the pointer from the original object
29  interface1 = i;
30  mutex = m;
31 
32  // init arrays
33  for (int rgbLed=0; rgbLed<NUMBEROFRGBLEDS; rgbLed++)
34  {
35  minBrightness[rgbLed] = 0;
36  defaultBrightness[rgbLed] = 64;
37  maxBrightness[rgbLed] = 255;
38  brightness[rgbLed] = defaultBrightness[rgbLed];
39  }
40 
41  robotState = ON; // Wer're thinking positive. The robot is ON until we know nothing other. :-)
42 }
43 
44 
46 {
47 }
48 
49 
50 bool RgbLed::setBrightness(unsigned char rgbLed, unsigned char bness)
51 {
52  QString answer = "error";
53 
54 
55  if (robotState == ON)
56  {
57  // rgbLed number okay?
58  if (rgbLed > NUMBEROFRGBLEDS-1)
59  {
60  emit message(QString("<font color=\"#FF0000\">RgbLed%1 is not an allowed rgbLed numer (1-%2)! (setRgbLed)</font>").arg(rgbLed+1).arg(NUMBEROFRGBLEDS-1) );
61  return false;
62  }
63 
64 
65  // wanted rgbLed position okay?
66  if ( (bness < minBrightness[rgbLed]) || (bness > maxBrightness[rgbLed]) )
67  {
68  emit message(QString("<font color=\"#FF0000\">RGBLED%1 brightness %2 out of allowed range (%3-%4)! (setRgbLed)</font>").arg(rgbLed+1).arg(bness).arg(minBrightness[rgbLed]).arg(maxBrightness[rgbLed]));
69  return false;
70  }
71 
72 
73  // store the new rgbLed position
74  brightness[rgbLed] = bness;
75 
76  // Lock the mutex.
77  mutex->lock();
78 
79 
80  // set brightness
81  // send command to microcontroller
82  if (interface1->sendString(QString("*rgb%1%2#").arg(rgbLed + 1).arg(bness), className) == true)
83  {
84  // check if the robot answers with "ok"
85  if ( interface1->receiveString(answer, className) == true)
86  {
87  if (answer == QString("*rgb%1#").arg(rgbLed + 1))
88  {
89  // Unlock the mutex
90  mutex->unlock();
91  return true;
92  }
93  }
94  }
95 
96 
97  // Unlock the mutex.
98  mutex->unlock();
99 
100  emit message(QString("<font color=\"#FF0000\">Error setting RGBLED%1 brightness (setBrightness)</font>").arg(rgbLed+1));
101  return false;
102 
103  } // robot is ON
104 
105  // robot is OFF
106  return false;
107 }
108 
109 
110 void RgbLed::storeBrightness(int rgbLed, unsigned char type, unsigned char bness)
111 {
112  switch (type)
113  {
114  case RGBLEDMIN:
115  minBrightness[rgbLed] = bness;
116  return;
117  break;
118  case RGBLEDMAX:
119  maxBrightness[rgbLed] = bness;
120  return;
121  break;
122  case RGBLEDDEFAULT:
123  defaultBrightness[rgbLed] = bness;
124  return;
125  break;
126  case RGBLEDACTUAL:
127  brightness[rgbLed] = bness;
128  return;
129  break;
130  }
131 }
132 
133 
134 void RgbLed::init(void)
135 {
136  if (robotState == ON)
137  {
138  for (int rgbLed=0; rgbLed<NUMBEROFRGBLEDS; rgbLed++)
139  {
140  setBrightness(rgbLed, defaultBrightness[rgbLed]);
141  //emit message(QString("Init rgbLed%1 to def-pos: %2").arg(rgbLed+1).arg(rgbLedDefaultPosition[rgbLed]));
142  }
143  }
144 }
145 
146 
147 unsigned char RgbLed::getBrightness(int rgbLed, unsigned char type)
148 {
149  if ( (rgbLed < RGBLED1) || (rgbLed > (NUMBEROFRGBLEDS-1)) )
150  {
151  emit message(QString("<font color=\"#FF0000\">RgbLed%1 out of allowed range! (getRgbLedPosition)</font>").arg(rgbLed+1));
152  return 0;
153  }
154 
155 
156  switch (type)
157  {
158  case RGBLEDMIN:
159  return minBrightness[rgbLed];
160  break;
161  case RGBLEDMAX:
162  return maxBrightness[rgbLed];
163  break;
164  case RGBLEDDEFAULT:
165  return defaultBrightness[rgbLed];
166  break;
167  case RGBLEDACTUAL:
168  return brightness[rgbLed];
169  break;
170  }
171 
172  // this line is never reached
173  return 0;
174 }
175 
176 
177 void RgbLed::setRobotState(bool state)
178 {
179  // store the state within this class
180  robotState = state;
181 
182  emit message(QString("Robot state set to %1 in %2").arg(state).arg(className));
183 }