From eab5fd3a44a63459cf2660e591ff30bf512fcb84 Mon Sep 17 00:00:00 2001 From: sylvieee-iot Date: Wed, 8 May 2024 22:47:41 +0300 Subject: [PATCH] I NEED SLEEP --- CMakeLists.txt | 2 +- include/external_haptics_manager.hpp | 51 +++++++++++++++ src/lua.cpp | 93 ++++++++++++++++++++++++++++ src/panda_sdl/main.cpp | 33 ---------- 4 files changed, 145 insertions(+), 34 deletions(-) create mode 100644 include/external_haptics_manager.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cb445b15..d6390646 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -241,7 +241,7 @@ set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/termcolor.hpp include/PICA/dynapica/shader_rec_emitter_arm64.hpp include/scheduler.hpp include/applets/error_applet.hpp include/audio/dsp_core.hpp include/audio/null_core.hpp include/audio/teakra_core.hpp include/audio/miniaudio_device.hpp include/ring_buffer.hpp include/bitfield.hpp include/audio/dsp_shared_mem.hpp - include/audio/hle_core.hpp include/capstone.hpp include/audio/aac.hpp + include/audio/hle_core.hpp include/capstone.hpp include/audio/aac.hpp include/external_haptics_manager.hpp ) cmrc_add_resource_library( diff --git a/include/external_haptics_manager.hpp b/include/external_haptics_manager.hpp new file mode 100644 index 00000000..da96606b --- /dev/null +++ b/include/external_haptics_manager.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "buttplugclient.h" + +class ExternalHapticsManager { + Client client; + std::vector devices; + SensorClass sensors; + + public: + ExternalHapticsManager() : client("ws://127.0.0.1", 12345) {} + + void connect() { + client.connect([](const mhl::Messages message) { + switch (message.messageType) { + case mhl::MessageTypes::DeviceList: std::printf("Device List callback\n"); break; + case mhl::MessageTypes::DeviceAdded: std::printf("Device List callback\n"); break; + case mhl::MessageTypes::ServerInfo: std::printf("Server info callback\n"); break; + case mhl::MessageTypes::DeviceRemoved: std::printf("Device Removed callback\n"); break; + case mhl::MessageTypes::SensorReading: std::printf("Sensor reading callback\n"); break; + default: std::printf("Unknown message"); + } + }); + } + + void requestDeviceList() { client.requestDeviceList(); } + void startScan() { client.startScan(); } + void stopScan() { client.stopScan(); } + void stopAllDevices() { client.stopAllDevices(); } + + void getDevices() { devices = client.getDevices(); } + void getSensors() { sensors = client.getSensors(); } + + void sendScalar(int index, double value) { + if (index < devices.size()) { + client.sendScalar(devices[index], value); + } + } + + void stopDevice(int index) { + if (index < devices.size()) { + client.stopDevice(devices[index]); + } + } +}; \ No newline at end of file diff --git a/src/lua.cpp b/src/lua.cpp index 6a16ab5b..c5b86624 100644 --- a/src/lua.cpp +++ b/src/lua.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "capstone.hpp" #include "emulator.hpp" @@ -11,6 +12,8 @@ extern "C" { #include "luv.h" } + +#include "external_haptics_manager.hpp" #endif void LuaManager::initialize() { @@ -242,6 +245,80 @@ static int disassembleTeakThunk(lua_State* L) { return 1; } +#ifndef __ANDROID__ +// Haptics functions + +namespace Haptics { + std::unique_ptr hapticsManager = nullptr; + + static int initHapticsThunk(lua_State* L) { + if (hapticsManager == nullptr) { + hapticsManager.reset(new ExternalHapticsManager()); + } + + return 0; + } + +#define HAPTICS_THUNK(func) \ + static int func##Thunk(lua_State* L) { \ + if (hapticsManager != nullptr) { \ + hapticsManager->func(); \ + } \ + \ + return 0; \ + } + + HAPTICS_THUNK(startScan) + HAPTICS_THUNK(stopScan) + HAPTICS_THUNK(connect) + HAPTICS_THUNK(requestDeviceList) + HAPTICS_THUNK(getDevices) + HAPTICS_THUNK(getSensors) + HAPTICS_THUNK(stopAllDevices) + #undef HAPTICS_THUNK + + static int sendScalarThunk(lua_State* L) { + const int device = (int)lua_tonumber(L, 1); + const auto value = lua_tonumber(L, 2); + + if (hapticsManager != nullptr) { + hapticsManager->sendScalar(device, value); + } + + return 2; + } + + static int stopDeviceThunk(lua_State* L) { + const int device = (int)lua_tonumber(L, 1); + + if (hapticsManager != nullptr) { + hapticsManager->stopDevice(device); + } + + return 1; + } + + // clang-format off + static constexpr luaL_Reg functions[] = { + { "initHaptics", initHapticsThunk }, + { "startScan", startScanThunk }, + { "stopScan", stopScanThunk }, + { "connect", connectThunk }, + { "requestDeviceList", requestDeviceListThunk }, + { "getDevices", getDevicesThunk }, + { "getSensors", getSensorsThunk }, + { "stopAllDevices", stopAllDevicesThunk }, + { "sendScalar", sendScalarThunk }, + { "stopDevice", stopDeviceThunk }, + }; + // clang-format on + + void registerFunctions(lua_State* L) { + luaL_register(L, "HAPTICS", functions); + } +} // namespace Haptics +#endif + // clang-format off static constexpr luaL_Reg functions[] = { { "__read8", read8Thunk }, @@ -310,6 +387,20 @@ void LuaManager::initializeThunks() { ButtonLeft = __ButtonLeft, ButtonRight= __ButtonRight, } + + Buzz = { + initHaptics = function() HAPTICS.initHaptics() end, + startScan = function() HAPTICS.startScan() end, + stopScan = function() HAPTICS.stopScan() end, + connect = function() HAPTICS.connect() end, + requestDeviceList = function() HAPTICS.requestDeviceList() end, + getDevices = function() HAPTICS.getDevices() end, + getSensors = function() HAPTICS.getSensors() end, + stopAllDevices = function() HAPTICS.stopAllDevices() end, + + sendScalar = function(device, value) HAPTICS.sendScalar(device, value) end, + stopDevice = function(device) HAPTICS.stopDevice(device) end, + } )"; auto addIntConstant = [&](T x, const char* name) { @@ -318,6 +409,8 @@ void LuaManager::initializeThunks() { }; luaL_register(L, "GLOBALS", functions); + Haptics::registerFunctions(L); + // Add values for event enum addIntConstant(LuaEvent::Frame, "__Frame"); diff --git a/src/panda_sdl/main.cpp b/src/panda_sdl/main.cpp index 560b33ac..ef354c61 100644 --- a/src/panda_sdl/main.cpp +++ b/src/panda_sdl/main.cpp @@ -1,40 +1,7 @@ #include "panda_sdl/frontend_sdl.hpp" -#include -#include -#include -#include -#include "buttplugclient.h" - -void callbackFunction(const mhl::Messages msg) { - switch (msg.messageType) { - case mhl::MessageTypes::DeviceList: printf("Device List callback\n"); break; - case mhl::MessageTypes::DeviceAdded: printf("Device List callback\n"); break; - case mhl::MessageTypes::ServerInfo: printf("Server info callback\n"); break; - case mhl::MessageTypes::DeviceRemoved: printf("Device Removed callback\n"); break; - case mhl::MessageTypes::SensorReading: printf("Sensor reading callback\n"); break; - default: printf("Unknown message"); - } -} - int main(int argc, char *argv[]) { FrontendSDL app; - std::string url = "ws://127.0.0.1"; - Client client(url, 12345, "test.txt"); - client.connect(callbackFunction); - client.requestDeviceList(); - - while (1) { - client.startScan(); - - std::vector myDevices = client.getDevices(); - if (myDevices.size() > 0) { - client.sendScalar(myDevices[0], 0.5); - } - - std::this_thread::sleep_for(std::chrono::milliseconds(2000)); - client.stopScan(); - } if (argc > 1) { auto romPath = std::filesystem::current_path() / argv[1];