ci_directional_leds_actuator.cpp
Go to the documentation of this file.
1 
8 
9 #ifdef ARGOS_WITH_LUA
10 #include <argos3/core/wrappers/lua/lua_utility.h>
11 #endif
12 
13 namespace argos {
14 
15  /****************************************/
16  /****************************************/
17 
18 #ifdef ARGOS_WITH_LUA
19  /*
20  * This function expects the stack to have either two or four arguments.
21  * The first argument must always be the index of the LED to set.
22  * Then, in case two arguments are passed, the second argument can be the string
23  * definition of a color. In case of four arguments, the RGB values are expected.
24  */
25  int LuaDirectionalLEDSetSingleColor(lua_State* pt_lua_state) {
26  /* Check parameters */
27  if(lua_gettop(pt_lua_state) != 2 && lua_gettop(pt_lua_state) != 4) {
28  return luaL_error(pt_lua_state, "robot.directional_leds.set_single_color() expects 2 or 4 arguments");
29  }
30  luaL_checktype(pt_lua_state, 1, LUA_TNUMBER);
31  size_t unIdx = lua_tonumber(pt_lua_state, 1);
32  /* Get reference to actuator */
33  CCI_DirectionalLEDsActuator* pcAct =
34  CLuaUtility::GetDeviceInstance<CCI_DirectionalLEDsActuator>(pt_lua_state, "directional_leds");
35  if(unIdx < 1 || unIdx > pcAct->GetNumLEDs()) {
36  return luaL_error(pt_lua_state, "passed index %d out of bounds [1,%d]", unIdx, pcAct->GetNumLEDs());
37  }
38  /* Create color buffer */
39  CColor cColor;
40  if(lua_gettop(pt_lua_state) == 2) {
41  luaL_checktype(pt_lua_state, 2, LUA_TSTRING);
42  try {
43  cColor.Set(lua_tostring(pt_lua_state, 2));
44  }
45  catch(CARGoSException& ex) {
46  return luaL_error(pt_lua_state, ex.what());
47  }
48  }
49  else {
50  luaL_checktype(pt_lua_state, 2, LUA_TNUMBER);
51  luaL_checktype(pt_lua_state, 3, LUA_TNUMBER);
52  luaL_checktype(pt_lua_state, 4, LUA_TNUMBER);
53  cColor.Set(lua_tonumber(pt_lua_state, 2),
54  lua_tonumber(pt_lua_state, 3),
55  lua_tonumber(pt_lua_state, 4));
56  }
57  /* Perform action */
58  pcAct->SetSingleColor(unIdx - 1, cColor);
59  return 0;
60  }
61 
62  /*
63  * This function expects the stack to have either one or three arguments.
64  * In case one argument is passed, it must be the string definition of a color.
65  * In case of three arguments, the RGB values are expected.
66  */
67  int LuaDirectionalLEDSetAllColors(lua_State* pt_lua_state) {
68  /* Check parameters */
69  if(lua_gettop(pt_lua_state) != 1 && lua_gettop(pt_lua_state) != 3) {
70  return luaL_error(pt_lua_state, "robot.directional_leds.set_all_colors() expects 1 or 3 arguments");
71  }
72  /* Create color buffer */
73  CColor cColor;
74  if(lua_gettop(pt_lua_state) == 1) {
75  luaL_checktype(pt_lua_state, 1, LUA_TSTRING);
76  try {
77  cColor.Set(lua_tostring(pt_lua_state, 1));
78  }
79  catch(CARGoSException& ex) {
80  return luaL_error(pt_lua_state, ex.what());
81  }
82  }
83  else {
84  luaL_checktype(pt_lua_state, 1, LUA_TNUMBER);
85  luaL_checktype(pt_lua_state, 2, LUA_TNUMBER);
86  luaL_checktype(pt_lua_state, 3, LUA_TNUMBER);
87  cColor.Set(lua_tonumber(pt_lua_state, 1),
88  lua_tonumber(pt_lua_state, 2),
89  lua_tonumber(pt_lua_state, 3));
90  }
91  /* Perform action */
92  CLuaUtility::GetDeviceInstance<CCI_DirectionalLEDsActuator>(pt_lua_state, "directional_leds")->
93  SetAllColors(cColor);
94  return 0;
95  }
96 #endif
97 
98  /****************************************/
99  /****************************************/
100 
102  return m_tSettings.size();
103  }
104 
105  /****************************************/
106  /****************************************/
107 
109  const CColor& c_color) {
110  m_tSettings[un_led_number] = c_color;
111  }
112 
113  /****************************************/
114  /****************************************/
115 
117  for(size_t i = 0; i < m_tSettings.size(); ++i) {
118  m_tSettings[i] = c_color;
119  }
120  }
121 
122  /****************************************/
123  /****************************************/
124 
125  void CCI_DirectionalLEDsActuator::SetAllColors(const std::vector<CColor>& c_colors) {
126  m_tSettings = c_colors;
127  }
128 
129  /****************************************/
130  /****************************************/
131 
133  UInt8 un_intensity) {
134  m_tSettings[un_led_number].SetAlpha(un_intensity);
135  }
136 
137  /****************************************/
138  /****************************************/
139 
141  for(size_t i = 0; i < m_tSettings.size(); ++i) {
142  m_tSettings[i].SetAlpha(un_intensity);
143  }
144  }
145 
146  /****************************************/
147  /****************************************/
148 
149 #ifdef ARGOS_WITH_LUA
150  void CCI_DirectionalLEDsActuator::CreateLuaState(lua_State* pt_lua_state) {
151  CLuaUtility::OpenRobotStateTable(pt_lua_state, "directional_leds");
152  CLuaUtility::AddToTable(pt_lua_state, "_instance", this);
153  CLuaUtility::AddToTable(pt_lua_state, "set_single_color", &LuaDirectionalLEDSetSingleColor);
154  CLuaUtility::AddToTable(pt_lua_state, "set_all_colors", &LuaDirectionalLEDSetAllColors);
155  CLuaUtility::CloseRobotStateTable(pt_lua_state);
156  }
157 #endif
158 
159  /****************************************/
160  /****************************************/
161 
162 }
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
unsigned char UInt8
8-bit unsigned integer.
Definition: datatypes.h:60
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
The basic color type.
Definition: color.h:25
static void AddToTable(lua_State *pt_state, const std::string &str_key, void *pt_data)
Adds a pointer to a chunk of data with the given string key to the table located at the top of the st...
static void OpenRobotStateTable(lua_State *pt_state, const std::string &str_key)
Opens a table in the robot state, creating it if it does not exist.
static void CloseRobotStateTable(lua_State *pt_state)
Closes a table in the robot state.
size_t GetNumLEDs() const
Returns the number of LEDs.
virtual void SetSingleColor(UInt32 un_led_number, const CColor &c_color)
Sets the color of a single LED.
virtual void SetSingleIntensity(UInt32 un_led_number, UInt8 un_intensity)
Sets the intensity of a single LED in the ring.
virtual void SetAllIntensities(UInt8 un_intensity)
Sets the intensity of all the LEDs in the ring.
virtual void SetAllColors(const CColor &c_color)
Sets the color of the whole LED ring.