-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaWrapper.cpp
More file actions
190 lines (139 loc) · 3.27 KB
/
LuaWrapper.cpp
File metadata and controls
190 lines (139 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* LuaWrapper.cpp
*
* Created on: 2015-12-02
* Author: 11110305
*/
#include "LuaWrapper.h"
#include "string.h"
std::vector<int> actions = std::vector<int>();
static int PushAction(lua_State * L)
{
actions.push_back(lua_tointeger(L, -1)); // Actions exécutées par le programme C++ (ResetEncoder, ResetGyro, etc. )
return 0;
}
// Commandes passees directement a lua (d'ou le static)
LuaWrapper::LuaWrapper()
{
// TODO Auto-generated constructor stub
// Creating the object, opening the libs, loading the file, making a priming run.
L = luaL_newstate();
LoadFile("AutoFrame.lua");
}
LuaWrapper::~LuaWrapper()
{
for(unsigned int i = 0; i < data.size(); i++)
{
delete data[i];
}
delete L;
}
void LuaWrapper::LoadFile(const char * file)
{
lua_settop(L, 0); // Clear the stack
luaL_openlibs(L); // Load libs
luaL_loadfile(L, file); // Load file
lua_pushcfunction(L, PushAction);
lua_setglobal(L, "PushAction"); // Set function
lua_pcall(L, 0, 0, 0); // Priming run: Exec script, but keep variables
}
void LuaWrapper::PushData(char type, const char* name, void* value, bool output)
{
WrapperData * wrapper = new WrapperData;
wrapper->type = type;
wrapper->name = name;
wrapper->WrapperValue = value;
wrapper->output = output;
data.push_back(wrapper);
}
void LuaWrapper::PushVariable(const char * name, const char * value)
{
lua_pushstring(L, value);
lua_setglobal(L, name);
}
void LuaWrapper::PushVariable(const char * name, int value)
{
lua_pushinteger(L, value);
lua_setglobal(L, name);
}
void LuaWrapper::PushVariable(const char * name, float value)
{
lua_pushnumber(L, value);
lua_setglobal(L, name);
}
void LuaWrapper::PushVariable(const char * name, bool value)
{
lua_pushboolean(L, value);
lua_setglobal(L, name);
}
std::vector<int> * LuaWrapper::GetActions()
{
return &actions; // Pour éviter des foirages de link, actions n'est pas dans le .h
}
void LuaWrapper::Update()
{
WriteAllData();
lua_getglobal(L, "update");
lua_pcall(L, 0, 0, 0);
ReadAllData();
for(unsigned int i = 0; i<actions.size(); i++)
{
std::cout << actions[i];
}
}
void LuaWrapper::WriteAllData()
{
for(unsigned int i = 0; i < data.size(); i++)
{
WriteData(data[i]->name, data[i]->WrapperValue, data[i]->type);
}
}
void LuaWrapper::WriteData(const char * name, void * value, char type)
{
switch(type)
{
case Lua_TypeInt:
lua_pushinteger(L, *((int*)value));
break;
case Lua_TypeFloat:
lua_pushnumber(L, *((float*)value));
break;
case Lua_TypeBool:
lua_pushboolean(L, *((bool*)value));
break;
case Lua_TypeString:
lua_pushstring(L, *((const char **)value));
break;
}
lua_setglobal(L, name);
}
void LuaWrapper::ReadAllData()
{
for(unsigned int i = 0; i < data.size(); i++)
{
WrapperData * variable = data[i];
if(variable->output)
{
ReadData(variable->name, variable->WrapperValue, variable->type);
}
}
}
void LuaWrapper::ReadData(const char * name, void * value, char type)
{
lua_getglobal(L, name);
switch(type)
{
case Lua_TypeInt:
*((int *)value) = lua_tointeger(L, -1);
break;
case Lua_TypeFloat:
*((float *)value) = lua_tonumber(L, -1);
break;
case Lua_TypeBool:
*((bool *)value) = lua_toboolean(L, -1);
break;
case Lua_TypeString:
// No const char * values please
break;
}
}