ARGoS 3
A parallel, multi-engine simulator for swarm robotics
ci_leds_actuator.cpp
Go to the documentation of this file.
1
8#include "ci_leds_actuator.h"
9
10#ifdef ARGOS_WITH_LUA
11#include <argos3/core/wrappers/lua/lua_utility.h>
12#endif
13
14namespace argos {
15
16 /****************************************/
17 /****************************************/
18
19#ifdef ARGOS_WITH_LUA
20 /*
21 * This function expects the stack to have either two or four arguments.
22 * The first argument must always be the index of the LED to set.
23 * Then, in case two arguments are passed, the second argument can be the string
24 * definition of a color. In case of four arguments, the RGB values are expected.
25 */
26 int LuaLEDSetSingleColor(lua_State* pt_lua_state) {
27 /* Check parameters */
28 if(lua_gettop(pt_lua_state) != 2 && lua_gettop(pt_lua_state) != 4) {
29 return luaL_error(pt_lua_state, "robot.leds.set_single_color() expects 2 or 4 arguments");
30 }
31 luaL_checktype(pt_lua_state, 1, LUA_TNUMBER);
32 size_t unIdx = lua_tonumber(pt_lua_state, 1);
33 /* Get reference to actuator */
34 CCI_LEDsActuator* pcAct = CLuaUtility::GetDeviceInstance<CCI_LEDsActuator>(pt_lua_state, "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 LuaLEDSetAllColors(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.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 */
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
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_LEDsActuator::CreateLuaState(lua_State* pt_lua_state) {
151 CLuaUtility::OpenRobotStateTable(pt_lua_state, "leds");
152 CLuaUtility::AddToTable(pt_lua_state, "_instance", this);
153 CLuaUtility::AddToTable(pt_lua_state, "set_single_color", &LuaLEDSetSingleColor);
154 CLuaUtility::AddToTable(pt_lua_state, "set_all_colors", &LuaLEDSetAllColors);
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
virtual void CreateLuaState(lua_State *pt_lua_state)=0
Creates the Lua state for this actuator.
The basic color type.
Definition color.h:25
void SetAlpha(UInt8 un_alpha)
Sets the alpha channel of the color.
Definition color.h:117
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.
static T * GetDeviceInstance(lua_State *pt_state, const std::string &str_key)
Returns a pointer to the instance to the wanted device.
size_t GetNumLEDs() const
Returns the number of LEDs.
virtual void SetAllColors(const CColor &c_color)
Sets the color of the whole LED ring.
virtual void SetAllIntensities(UInt8 un_intensity)
Sets the intensity of all the LEDs in the ring.
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.
std::vector< CColor > TSettings