实现lua调用c
This commit is contained in:
parent
b17b9558d3
commit
d25f4bf2b9
@ -1,6 +1,7 @@
|
||||
#include "base_form.h"
|
||||
#include "serial_port.h"
|
||||
#include "utils.h"
|
||||
#include "global.h"
|
||||
|
||||
const std::wstring BasicForm::kClassName = L"Basic";
|
||||
|
||||
@ -118,7 +119,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
mMonitorNewSelect->Close();
|
||||
}
|
||||
|
||||
|
||||
if (uMsg == WM_ADD_TCPSERVER_MONITOR) {
|
||||
TcpServerInfo* info = (TcpServerInfo*)wParam;
|
||||
TcpServerLibevent* server = (TcpServerLibevent*)lParam;
|
||||
@ -308,6 +308,26 @@ void BasicForm::InitWindow(){
|
||||
|
||||
}
|
||||
|
||||
LuaBindInterface* BasicForm::FindCurrentFormByLuaPointer(lua_State* pointer)
|
||||
{
|
||||
for (auto itr = mUartForm.begin(); itr != mUartForm.end(); itr++) {
|
||||
if (itr->second->LuaVM()->VM() == pointer) {
|
||||
return (LuaBindInterface*)itr->second;
|
||||
}
|
||||
}
|
||||
for (auto itr = mTcpClientForm.begin(); itr != mTcpClientForm.end(); itr++) {
|
||||
if (itr->second->LuaVM()->VM() == pointer) {
|
||||
return (LuaBindInterface*)itr->second;
|
||||
}
|
||||
}
|
||||
for (auto itr = mTcpServerForm.begin(); itr != mTcpServerForm.end(); itr++) {
|
||||
if (itr->second->LuaVM()->VM() == pointer) {
|
||||
return (LuaBindInterface*)itr->second;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
LRESULT BasicForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
|
||||
{
|
||||
PostQuitMessage(0L);
|
||||
|
@ -10,7 +10,6 @@
|
||||
|
||||
// base header
|
||||
#include "base/base.h"
|
||||
|
||||
// duilib
|
||||
#include "duilib/UIlib.h"
|
||||
#include "ui_components/ui_components.h"
|
||||
@ -44,6 +43,7 @@ public:
|
||||
*/
|
||||
void InitWindow() override;
|
||||
|
||||
LuaBindInterface* FindCurrentFormByLuaPointer(lua_State*);
|
||||
/**
|
||||
* 收到 WM_CLOSE 消息时该函数会被调用
|
||||
*/
|
||||
|
3
examples/proto_debuger/global.cpp
Normal file
3
examples/proto_debuger/global.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include "global.h"
|
||||
|
||||
static LuaBindInterface* gCurrentForm = nullptr;
|
12
examples/proto_debuger/global.h
Normal file
12
examples/proto_debuger/global.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
class LuaBindInterface {
|
||||
public:
|
||||
virtual void ShowDataInEdit(const char*) = 0 ;
|
||||
};
|
||||
|
||||
|
||||
class BasicForm;
|
||||
|
||||
extern BasicForm* gMainWindow;
|
@ -1,4 +1,7 @@
|
||||
#include "base_form.h"
|
||||
#include "lua_bind.h"
|
||||
#include <iostream>
|
||||
#include "global.h"
|
||||
|
||||
// 在qml界面中显示lua层处理过的数据
|
||||
int LuaShowData(lua_State* vm)
|
||||
@ -8,7 +11,12 @@ int LuaShowData(lua_State* vm)
|
||||
if (nullptr != k) {
|
||||
//todo qml显示添加
|
||||
}
|
||||
// gGlobal.ShowDataInQML(QString(k));
|
||||
std::cout << "LuaShowData" << k << std::endl;
|
||||
auto current = gMainWindow->FindCurrentFormByLuaPointer(vm);
|
||||
if (nullptr == current) {
|
||||
return -1;
|
||||
}
|
||||
current->ShowDataInEdit(k);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -57,6 +57,11 @@ int LuaDelegate::UpdateScript(std::string scr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
lua_State* LuaDelegate::VM()
|
||||
{
|
||||
return mVM;
|
||||
}
|
||||
|
||||
void LuaDelegate::PrintError(lua_State *L) {
|
||||
auto err = std::string("\nFATAL ERROR:%s\n\n") + lua_tostring(L, -1);
|
||||
std::cout << err;
|
||||
@ -67,7 +72,7 @@ int LuaDelegate::BindFunction(std::string name, lua_CFunction function)
|
||||
if ((nullptr == function) || (name == ""))
|
||||
return -1;
|
||||
this->mFunc[name] = function;
|
||||
lua_register(mVM, "serial_send", function);
|
||||
lua_register(mVM, name.c_str(), function);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@ public:
|
||||
}
|
||||
pushstack(para...);
|
||||
}
|
||||
|
||||
lua_State* VM();
|
||||
void PrintError(lua_State *L);
|
||||
int BindFunction(std::string name, lua_CFunction);
|
||||
void OnSerialData(std::string);
|
||||
|
@ -7,12 +7,15 @@
|
||||
#include <math.h>
|
||||
#include "lua_wraper.h"
|
||||
#include "loger.h"
|
||||
#include "global.h"
|
||||
|
||||
enum ThreadId
|
||||
{
|
||||
kThreadUI
|
||||
};
|
||||
|
||||
BasicForm* gMainWindow = nullptr;
|
||||
|
||||
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
|
||||
_In_opt_ HINSTANCE hPrevInstance,
|
||||
_In_ LPWSTR lpCmdLine,
|
||||
@ -45,9 +48,11 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
LPTSTR CurrentPath = new wchar_t[255];
|
||||
GetCurrentDirectory(255, CurrentPath);
|
||||
AllocConsole();
|
||||
freopen("CONOUT$", "w", stdout);
|
||||
std::wcout << CurrentPath << std::endl;
|
||||
|
||||
LuaDelegate lua;
|
||||
std::cout << std::endl;
|
||||
@ -90,13 +95,13 @@ void MainThread::Init()
|
||||
auto dpiManager = ui::DpiManager::GetInstance();
|
||||
dpiManager->SetAdaptDPI();
|
||||
// 创建一个默认带有阴影的居中窗口
|
||||
BasicForm* window = new BasicForm();
|
||||
gMainWindow = new BasicForm();
|
||||
|
||||
window->Create(NULL, BasicForm::kClassName.c_str(),
|
||||
gMainWindow->Create(NULL, BasicForm::kClassName.c_str(),
|
||||
WS_OVERLAPPEDWINDOW & WS_SIZEBOX,0,true, ui::UiRect(0,0,1024,768));
|
||||
window->CenterWindow();
|
||||
window->ShowWindow();
|
||||
window->SetInitSize(1024,768);
|
||||
gMainWindow->CenterWindow();
|
||||
gMainWindow->ShowWindow();
|
||||
gMainWindow->SetInitSize(1024,768);
|
||||
}
|
||||
|
||||
void MainThread::Cleanup()
|
||||
|
@ -163,6 +163,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="base_form.cpp" />
|
||||
<ClCompile Include="global.cpp" />
|
||||
<ClCompile Include="loger.cpp" />
|
||||
<ClCompile Include="lua_wraper.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
@ -178,6 +179,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="base_form.h" />
|
||||
<ClInclude Include="global.h" />
|
||||
<ClInclude Include="lua_bind.h" />
|
||||
<ClInclude Include="lua_wraper.h" />
|
||||
<ClInclude Include="main.h" />
|
||||
|
@ -54,6 +54,9 @@
|
||||
<ClCompile Include="lua_bind.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="global.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="main.h">
|
||||
@ -92,6 +95,9 @@
|
||||
<ClInclude Include="lua_bind.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="global.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml">
|
||||
|
39
examples/proto_debuger/script.lua
Normal file
39
examples/proto_debuger/script.lua
Normal file
@ -0,0 +1,39 @@
|
||||
require("string")
|
||||
local file = io.open("writetest.txt", "w+")
|
||||
|
||||
function OnUartData(data)
|
||||
if nil == file then
|
||||
print("open file writetest.txt fail")
|
||||
end
|
||||
file:write("OnUartData from lua " .. data)
|
||||
file:flush()
|
||||
showdata("OnUartData from lua " .. data)
|
||||
end
|
||||
|
||||
-- function OnNetworkData(addr,data,len)
|
||||
-- print(addr,data,len)
|
||||
-- print(len)
|
||||
-- print(data)
|
||||
-- -- showbuffer("recv network data: " .. data .. "\r\n")
|
||||
-- end
|
||||
|
||||
-- function OnUdpData(addr,data,len)
|
||||
-- print(addr,data,len)
|
||||
-- -- showbuffer("recv data: ",data,"\r\n")
|
||||
-- end
|
||||
|
||||
|
||||
|
||||
-- function OnNewClient(addr,port,sock)
|
||||
-- print(addr,port,sock)
|
||||
-- -- showbuffer("tcp connected: "
|
||||
-- -- ..addr.."\r\n"..port.."\r\n"..sock.."\r\n")
|
||||
-- end
|
||||
|
||||
|
||||
-- function OnClientLeave(addr,port,sock)
|
||||
-- print(addr,port,sock)
|
||||
-- -- showbuffer("tcp close: "
|
||||
-- -- ..addr.."\r\n"..port.."\r\n"..sock.."\r\n")
|
||||
-- end
|
||||
|
@ -15,6 +15,13 @@ TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, Tc
|
||||
mLua = new LuaDelegate();
|
||||
}
|
||||
|
||||
void TcpClientForm::ShowDataInEdit(const char*p) {
|
||||
if (nullptr == p) {
|
||||
std::cout << "ShowDataInEdit " << std::endl;
|
||||
}
|
||||
m_rich_edit_1->AppendText(string2wstring(p), false);
|
||||
}
|
||||
|
||||
void TcpClientForm::Init()
|
||||
{
|
||||
ui::ChildBox::Init();
|
||||
@ -52,6 +59,11 @@ TcpClientLibevent* TcpClientForm::ClientEvent()
|
||||
return mClient;
|
||||
}
|
||||
|
||||
LuaDelegate* TcpClientForm::LuaVM()
|
||||
{
|
||||
return mLua;
|
||||
}
|
||||
|
||||
void TcpClientForm::OnConnected()
|
||||
{
|
||||
std::cout << "服务器连接成功\r\n"<<__FILE__<<" " << __LINE__ << std::endl;
|
||||
@ -71,10 +83,13 @@ void TcpClientForm::OnDisConnected(std::string reason)
|
||||
void TcpClientForm::OnData(uint8_t* dat, uint64_t len)
|
||||
{
|
||||
std::cout << (char*)dat << std::endl;
|
||||
m_rich_edit_1->AppendText(string2wstring((char*)dat), false);
|
||||
}
|
||||
|
||||
void TcpClientForm::OnClose()
|
||||
{
|
||||
m_rich_edit_1->AppendText(string2wstring("Á¬½ÓÒѾ¶Ï¿ª\r\n"), false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "msgdef.h"
|
||||
|
||||
@ -15,24 +17,26 @@
|
||||
#include "tcp_client.h"
|
||||
|
||||
#include "lua_wraper.h"
|
||||
|
||||
#include "global.h"
|
||||
|
||||
using namespace std;
|
||||
class TcpClientForm :
|
||||
public ui::ChildBox,
|
||||
public TcpClientLibevent::TcpClientObserver{
|
||||
#pragma once
|
||||
public TcpClientLibevent::TcpClientObserver,
|
||||
public LuaBindInterface {
|
||||
|
||||
public:
|
||||
TcpClientForm(ui::Window* hwnd,string url, uint32_t port, TcpClientLibevent* p);
|
||||
virtual void Init() override;
|
||||
TcpClientLibevent* ClientEvent();
|
||||
LuaDelegate* LuaVM();
|
||||
|
||||
protected:
|
||||
virtual void OnConnected();
|
||||
virtual void OnDisConnected(std::string) ;
|
||||
virtual void OnData(uint8_t* dat, uint64_t len) ;
|
||||
virtual void OnClose();
|
||||
void ShowDataInEdit(const char*) override;
|
||||
|
||||
private:
|
||||
ui::Label* m_label_1;
|
||||
|
@ -103,6 +103,20 @@ void TcpServerFrom::OnDisConnected(ConnectionLibevent* cli)
|
||||
mClients.erase(cli->SocketFd());
|
||||
}
|
||||
|
||||
LuaDelegate* TcpServerFrom::LuaVM()
|
||||
{
|
||||
return mLua;
|
||||
}
|
||||
|
||||
void TcpServerFrom::ShowDataInEdit(const char* value)
|
||||
{
|
||||
if (nullptr == value) {
|
||||
std::cout << "nullp pointer in ShowDataInEdit" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TcpServerFrom::updateStatus()
|
||||
{
|
||||
wchar_t tmp[200] = L"";
|
||||
|
@ -8,9 +8,11 @@
|
||||
#include "tcp_server_libevent.h"
|
||||
#include <map>
|
||||
#include "lua_wraper.h"
|
||||
#include "global.h"
|
||||
|
||||
class TcpServerFrom :
|
||||
public ui::ChildBox
|
||||
public ui::ChildBox,
|
||||
LuaBindInterface
|
||||
{
|
||||
public:
|
||||
TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p);
|
||||
@ -18,7 +20,10 @@ public:
|
||||
virtual void Init() override;
|
||||
void OnNewConnAccept(ConnectionLibevent*);
|
||||
void OnDisConnected(ConnectionLibevent*);
|
||||
LuaDelegate* LuaVM();
|
||||
|
||||
protected:
|
||||
void ShowDataInEdit(const char*) override;
|
||||
|
||||
private:
|
||||
void updateStatus();
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "msgdef.h"
|
||||
#include "utils.h"
|
||||
#include <fstream>
|
||||
|
||||
#include "lua_bind.h"
|
||||
|
||||
UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
||||
uint32_t baurate,
|
||||
@ -23,7 +23,7 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
||||
m_runing = true;
|
||||
mLua = new LuaDelegate;
|
||||
|
||||
mLuaFile.open("D:\\project\\c++\\nim_duilib\\examples\\x64\\script.lua");
|
||||
mLuaFile.open("script.lua");
|
||||
std::string lua_script;
|
||||
if (mLuaFile.is_open()) {
|
||||
std::string s;
|
||||
@ -54,7 +54,7 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
||||
}
|
||||
}
|
||||
else {
|
||||
Sleep(300);
|
||||
Sleep(100);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -62,6 +62,14 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
|
||||
// set window so we get getwindow by GetWindow funtion
|
||||
this->SetWindow(hwnd, nullptr, false);
|
||||
}
|
||||
this->mLua->BindFunction("showdata", LuaShowData);
|
||||
}
|
||||
|
||||
void UartForm::ShowDataInEdit(const char*p) {
|
||||
if (nullptr == p) {
|
||||
std::cout << "ShowDataInEdit " << std::endl;
|
||||
}
|
||||
this->mEditRecv->AppendText(string2wstring(p), false);
|
||||
}
|
||||
|
||||
void UartForm::OnUpdateUart()
|
||||
@ -71,6 +79,11 @@ void UartForm::OnUpdateUart()
|
||||
|
||||
}
|
||||
|
||||
LuaDelegate* UartForm::LuaVM() {
|
||||
return mLua;
|
||||
}
|
||||
|
||||
|
||||
void UartForm::Init()
|
||||
{
|
||||
ui::ChildBox::Init();
|
||||
@ -125,7 +138,7 @@ void UartForm::Init()
|
||||
|
||||
auto mBtnSaveLua = static_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
||||
if (mBtnSaveLua != nullptr) {
|
||||
mBtnSaveLua->AttachClick([this](ui::EventArgs*) {
|
||||
mBtnSaveLua->AttachClick([this](ui::EventArgs*) ->bool {
|
||||
std::cout << "±£´ælua½Å±¾\r\n";
|
||||
std::string lua = wstring2string(mEditLua->GetText());
|
||||
if (0 == this->mLua->UpdateScript(lua)) {
|
||||
@ -133,9 +146,10 @@ void UartForm::Init()
|
||||
}
|
||||
else {
|
||||
MessageBox(0, L"lua½Å±¾´íÎó", L"", 0);
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
std::cout << lua.c_str() <<"\r\n";
|
||||
std::cout << lua.c_str() << "\r\n";
|
||||
return true;
|
||||
}
|
||||
);
|
||||
}
|
||||
@ -143,7 +157,7 @@ void UartForm::Init()
|
||||
|
||||
void UartForm::UpdateRecvEdit()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void UartForm::HandleMessage(ui::EventArgs& msg)
|
||||
@ -152,6 +166,5 @@ void UartForm::HandleMessage(ui::EventArgs& msg)
|
||||
printf("hello world\r\n");
|
||||
this->mEditRecv->SetText(L"123");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -15,8 +15,12 @@
|
||||
// duilib
|
||||
#include "duilib/UIlib.h"
|
||||
#include "lua_wraper.h"
|
||||
#include "global.h"
|
||||
|
||||
class UartForm : public ui::ChildBox
|
||||
|
||||
class UartForm :
|
||||
public ui::ChildBox,
|
||||
LuaBindInterface
|
||||
{
|
||||
public:
|
||||
UartForm(ui::Window* window,std::wstring name, uint32_t baurate,
|
||||
@ -25,8 +29,9 @@ public:
|
||||
uint8_t verify,
|
||||
uint8_t flow_control);
|
||||
|
||||
void ShowDataInEdit(const char*) override;
|
||||
void OnUpdateUart();
|
||||
|
||||
LuaDelegate* LuaVM();
|
||||
/// 重写父类方法,提供个性化功能,请参考父类声明
|
||||
virtual void Init() override;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user