实现lua调用c

This commit is contained in:
zcy 2022-01-25 23:45:07 +08:00
parent b17b9558d3
commit d25f4bf2b9
17 changed files with 181 additions and 25 deletions

View File

@ -1,6 +1,7 @@
#include "base_form.h" #include "base_form.h"
#include "serial_port.h" #include "serial_port.h"
#include "utils.h" #include "utils.h"
#include "global.h"
const std::wstring BasicForm::kClassName = L"Basic"; const std::wstring BasicForm::kClassName = L"Basic";
@ -118,7 +119,6 @@ LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
mMonitorNewSelect->Close(); mMonitorNewSelect->Close();
} }
if (uMsg == WM_ADD_TCPSERVER_MONITOR) { if (uMsg == WM_ADD_TCPSERVER_MONITOR) {
TcpServerInfo* info = (TcpServerInfo*)wParam; TcpServerInfo* info = (TcpServerInfo*)wParam;
TcpServerLibevent* server = (TcpServerLibevent*)lParam; 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) LRESULT BasicForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{ {
PostQuitMessage(0L); PostQuitMessage(0L);

View File

@ -10,7 +10,6 @@
// base header // base header
#include "base/base.h" #include "base/base.h"
// duilib // duilib
#include "duilib/UIlib.h" #include "duilib/UIlib.h"
#include "ui_components/ui_components.h" #include "ui_components/ui_components.h"
@ -44,6 +43,7 @@ public:
*/ */
void InitWindow() override; void InitWindow() override;
LuaBindInterface* FindCurrentFormByLuaPointer(lua_State*);
/** /**
* WM_CLOSE * WM_CLOSE
*/ */

View File

@ -0,0 +1,3 @@
#include "global.h"
static LuaBindInterface* gCurrentForm = nullptr;

View File

@ -0,0 +1,12 @@
#pragma once
class LuaBindInterface {
public:
virtual void ShowDataInEdit(const char*) = 0 ;
};
class BasicForm;
extern BasicForm* gMainWindow;

View File

@ -1,4 +1,7 @@
#include "base_form.h"
#include "lua_bind.h" #include "lua_bind.h"
#include <iostream>
#include "global.h"
// 在qml界面中显示lua层处理过的数据 // 在qml界面中显示lua层处理过的数据
int LuaShowData(lua_State* vm) int LuaShowData(lua_State* vm)
@ -8,7 +11,12 @@ int LuaShowData(lua_State* vm)
if (nullptr != k) { if (nullptr != k) {
//todo qml显示添加 //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; return 0;
} }

View File

@ -57,6 +57,11 @@ int LuaDelegate::UpdateScript(std::string scr) {
return 0; return 0;
} }
lua_State* LuaDelegate::VM()
{
return mVM;
}
void LuaDelegate::PrintError(lua_State *L) { void LuaDelegate::PrintError(lua_State *L) {
auto err = std::string("\nFATAL ERROR:%s\n\n") + lua_tostring(L, -1); auto err = std::string("\nFATAL ERROR:%s\n\n") + lua_tostring(L, -1);
std::cout << err; std::cout << err;
@ -67,7 +72,7 @@ int LuaDelegate::BindFunction(std::string name, lua_CFunction function)
if ((nullptr == function) || (name == "")) if ((nullptr == function) || (name == ""))
return -1; return -1;
this->mFunc[name] = function; this->mFunc[name] = function;
lua_register(mVM, "serial_send", function); lua_register(mVM, name.c_str(), function);
return 0; return 0;
} }

View File

@ -92,7 +92,7 @@ public:
} }
pushstack(para...); pushstack(para...);
} }
lua_State* VM();
void PrintError(lua_State *L); void PrintError(lua_State *L);
int BindFunction(std::string name, lua_CFunction); int BindFunction(std::string name, lua_CFunction);
void OnSerialData(std::string); void OnSerialData(std::string);

View File

@ -7,12 +7,15 @@
#include <math.h> #include <math.h>
#include "lua_wraper.h" #include "lua_wraper.h"
#include "loger.h" #include "loger.h"
#include "global.h"
enum ThreadId enum ThreadId
{ {
kThreadUI kThreadUI
}; };
BasicForm* gMainWindow = nullptr;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance, _In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine, _In_ LPWSTR lpCmdLine,
@ -45,9 +48,11 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
return 0; return 0;
} }
#endif #endif
LPTSTR CurrentPath = new wchar_t[255];
GetCurrentDirectory(255, CurrentPath);
AllocConsole(); AllocConsole();
freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stdout);
std::wcout << CurrentPath << std::endl;
LuaDelegate lua; LuaDelegate lua;
std::cout << std::endl; std::cout << std::endl;
@ -90,13 +95,13 @@ void MainThread::Init()
auto dpiManager = ui::DpiManager::GetInstance(); auto dpiManager = ui::DpiManager::GetInstance();
dpiManager->SetAdaptDPI(); 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)); WS_OVERLAPPEDWINDOW & WS_SIZEBOX,0,true, ui::UiRect(0,0,1024,768));
window->CenterWindow(); gMainWindow->CenterWindow();
window->ShowWindow(); gMainWindow->ShowWindow();
window->SetInitSize(1024,768); gMainWindow->SetInitSize(1024,768);
} }
void MainThread::Cleanup() void MainThread::Cleanup()

View File

@ -163,6 +163,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="base_form.cpp" /> <ClCompile Include="base_form.cpp" />
<ClCompile Include="global.cpp" />
<ClCompile Include="loger.cpp" /> <ClCompile Include="loger.cpp" />
<ClCompile Include="lua_wraper.cpp" /> <ClCompile Include="lua_wraper.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
@ -178,6 +179,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="base_form.h" /> <ClInclude Include="base_form.h" />
<ClInclude Include="global.h" />
<ClInclude Include="lua_bind.h" /> <ClInclude Include="lua_bind.h" />
<ClInclude Include="lua_wraper.h" /> <ClInclude Include="lua_wraper.h" />
<ClInclude Include="main.h" /> <ClInclude Include="main.h" />

View File

@ -54,6 +54,9 @@
<ClCompile Include="lua_bind.cpp"> <ClCompile Include="lua_bind.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="global.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="main.h"> <ClInclude Include="main.h">
@ -92,6 +95,9 @@
<ClInclude Include="lua_bind.h"> <ClInclude Include="lua_bind.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="global.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Xml Include="..\Debug\resources\themes\default\basic\basic.xml"> <Xml Include="..\Debug\resources\themes\default\basic\basic.xml">

View 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

View File

@ -15,6 +15,13 @@ TcpClientForm::TcpClientForm(ui::Window* hwnd,std::string url, uint32_t port, Tc
mLua = new LuaDelegate(); 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() void TcpClientForm::Init()
{ {
ui::ChildBox::Init(); ui::ChildBox::Init();
@ -52,6 +59,11 @@ TcpClientLibevent* TcpClientForm::ClientEvent()
return mClient; return mClient;
} }
LuaDelegate* TcpClientForm::LuaVM()
{
return mLua;
}
void TcpClientForm::OnConnected() void TcpClientForm::OnConnected()
{ {
std::cout << "服务器连接成功\r\n"<<__FILE__<<" " << __LINE__ << std::endl; 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) void TcpClientForm::OnData(uint8_t* dat, uint64_t len)
{ {
std::cout << (char*)dat << std::endl; std::cout << (char*)dat << std::endl;
m_rich_edit_1->AppendText(string2wstring((char*)dat), false);
} }
void TcpClientForm::OnClose() void TcpClientForm::OnClose()
{ {
m_rich_edit_1->AppendText(string2wstring("Á¬½ÓÒѾ­¶Ï¿ª\r\n"), false);
} }

View File

@ -1,3 +1,5 @@
#pragma once
#include <string> #include <string>
#include "msgdef.h" #include "msgdef.h"
@ -15,24 +17,26 @@
#include "tcp_client.h" #include "tcp_client.h"
#include "lua_wraper.h" #include "lua_wraper.h"
#include "global.h"
using namespace std; using namespace std;
class TcpClientForm : class TcpClientForm :
public ui::ChildBox, public ui::ChildBox,
public TcpClientLibevent::TcpClientObserver{ public TcpClientLibevent::TcpClientObserver,
#pragma once public LuaBindInterface {
public: public:
TcpClientForm(ui::Window* hwnd,string url, uint32_t port, TcpClientLibevent* p); TcpClientForm(ui::Window* hwnd,string url, uint32_t port, TcpClientLibevent* p);
virtual void Init() override; virtual void Init() override;
TcpClientLibevent* ClientEvent(); TcpClientLibevent* ClientEvent();
LuaDelegate* LuaVM();
protected: protected:
virtual void OnConnected(); virtual void OnConnected();
virtual void OnDisConnected(std::string) ; virtual void OnDisConnected(std::string) ;
virtual void OnData(uint8_t* dat, uint64_t len) ; virtual void OnData(uint8_t* dat, uint64_t len) ;
virtual void OnClose(); virtual void OnClose();
void ShowDataInEdit(const char*) override;
private: private:
ui::Label* m_label_1; ui::Label* m_label_1;

View File

@ -103,6 +103,20 @@ void TcpServerFrom::OnDisConnected(ConnectionLibevent* cli)
mClients.erase(cli->SocketFd()); 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() void TcpServerFrom::updateStatus()
{ {
wchar_t tmp[200] = L""; wchar_t tmp[200] = L"";

View File

@ -8,9 +8,11 @@
#include "tcp_server_libevent.h" #include "tcp_server_libevent.h"
#include <map> #include <map>
#include "lua_wraper.h" #include "lua_wraper.h"
#include "global.h"
class TcpServerFrom : class TcpServerFrom :
public ui::ChildBox public ui::ChildBox,
LuaBindInterface
{ {
public: public:
TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p); TcpServerFrom(ui::Window* hwnd, string url, uint32_t port, TcpServerLibevent* p);
@ -18,7 +20,10 @@ public:
virtual void Init() override; virtual void Init() override;
void OnNewConnAccept(ConnectionLibevent*); void OnNewConnAccept(ConnectionLibevent*);
void OnDisConnected(ConnectionLibevent*); void OnDisConnected(ConnectionLibevent*);
LuaDelegate* LuaVM();
protected: protected:
void ShowDataInEdit(const char*) override;
private: private:
void updateStatus(); void updateStatus();

View File

@ -3,7 +3,7 @@
#include "msgdef.h" #include "msgdef.h"
#include "utils.h" #include "utils.h"
#include <fstream> #include <fstream>
#include "lua_bind.h"
UartForm::UartForm(ui::Window* hwnd,std::wstring name, UartForm::UartForm(ui::Window* hwnd,std::wstring name,
uint32_t baurate, uint32_t baurate,
@ -23,7 +23,7 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
m_runing = true; m_runing = true;
mLua = new LuaDelegate; mLua = new LuaDelegate;
mLuaFile.open("D:\\project\\c++\\nim_duilib\\examples\\x64\\script.lua"); mLuaFile.open("script.lua");
std::string lua_script; std::string lua_script;
if (mLuaFile.is_open()) { if (mLuaFile.is_open()) {
std::string s; std::string s;
@ -54,7 +54,7 @@ UartForm::UartForm(ui::Window* hwnd,std::wstring name,
} }
} }
else { 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 // set window so we get getwindow by GetWindow funtion
this->SetWindow(hwnd, nullptr, false); 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() void UartForm::OnUpdateUart()
@ -71,6 +79,11 @@ void UartForm::OnUpdateUart()
} }
LuaDelegate* UartForm::LuaVM() {
return mLua;
}
void UartForm::Init() void UartForm::Init()
{ {
ui::ChildBox::Init(); ui::ChildBox::Init();
@ -125,7 +138,7 @@ void UartForm::Init()
auto mBtnSaveLua = static_cast<ui::Button*>(FindSubControl(L"btn_save_lua")); auto mBtnSaveLua = static_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
if (mBtnSaveLua != nullptr) { if (mBtnSaveLua != nullptr) {
mBtnSaveLua->AttachClick([this](ui::EventArgs*) { mBtnSaveLua->AttachClick([this](ui::EventArgs*) ->bool {
std::cout << "±£´ælua½Å±¾\r\n"; std::cout << "±£´ælua½Å±¾\r\n";
std::string lua = wstring2string(mEditLua->GetText()); std::string lua = wstring2string(mEditLua->GetText());
if (0 == this->mLua->UpdateScript(lua)) { if (0 == this->mLua->UpdateScript(lua)) {
@ -133,9 +146,10 @@ void UartForm::Init()
} }
else { else {
MessageBox(0, L"lua½Å±¾´íÎó", L"", 0); 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::UpdateRecvEdit()
{ {
} }
void UartForm::HandleMessage(ui::EventArgs& msg) void UartForm::HandleMessage(ui::EventArgs& msg)
@ -152,6 +166,5 @@ void UartForm::HandleMessage(ui::EventArgs& msg)
printf("hello world\r\n"); printf("hello world\r\n");
this->mEditRecv->SetText(L"123"); this->mEditRecv->SetText(L"123");
} }
} }

View File

@ -15,8 +15,12 @@
// duilib // duilib
#include "duilib/UIlib.h" #include "duilib/UIlib.h"
#include "lua_wraper.h" #include "lua_wraper.h"
#include "global.h"
class UartForm : public ui::ChildBox
class UartForm :
public ui::ChildBox,
LuaBindInterface
{ {
public: public:
UartForm(ui::Window* window,std::wstring name, uint32_t baurate, UartForm(ui::Window* window,std::wstring name, uint32_t baurate,
@ -25,8 +29,9 @@ public:
uint8_t verify, uint8_t verify,
uint8_t flow_control); uint8_t flow_control);
void ShowDataInEdit(const char*) override;
void OnUpdateUart(); void OnUpdateUart();
LuaDelegate* LuaVM();
/// 重写父类方法,提供个性化功能,请参考父类声明 /// 重写父类方法,提供个性化功能,请参考父类声明
virtual void Init() override; virtual void Init() override;