Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for external bluetooth devices #535

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

sylvieee-iot
Copy link

@sylvieee-iot sylvieee-iot commented Jul 14, 2024

Add support for external haptic bluetooth devices, in this case but*plugs. This works for all sorts of bluetooth but*plugs that use the common bluetooth protocol for BPs, and is also hooked to the Lua API so that you can make per-game scripts to activate and control your device.

This is early progress code, I need some more time to work on it and help if possible. Thanks.

@sylvieee-iot
Copy link
Author

Also I need to implement some more Lua events for things like but*plug being connected, as it would make Lua scripts significantly easier to write. Now it's kind of hard to know when to start/stop rumbling the device.

@sylvieee-iot
Copy link
Author

Example of the Lua API

rupees1_address = 0x050AF88
rupees2_address = 0x05879A0

device = 0  -- Device to buzz
force = 0.4 -- Force to use
vibration_time = 120 -- Number of frames to buzz buzz for
vibration_countdown = 0 -- Number of frames until we should stop buzzing
first = true-- Is this our first time counting rupees?

previous_rupees = 0 -- How many rupees did we have in the previous frame?
current_rupees = 0  -- How many rupees do we currently have?

Buzz.initHaptics() -- Initialize haptics subsytem
Buzz.connect()     -- Connect to device
Buzz.requestDeviceList()
Buzz.startScan()

function vibe()
    vibration_countdown = vibration_time

    Buzz.getDevices()
    Buzz.sendScalar(device, force)
end

function unvibe()
    Buzz.getDevices()
    Buzz.sendScalar(device, 0.0)
end

function getRupees() return Pand.read16(rupees1_address) end

function eventHandler(e)
    if (e == Pand.Frame) then
        if (first) then
            previous_rupees = getRupees()
            first = false
            return
        end

        previous_rupees = current_rupees
        current_rupees = getRupees()

        if (current_ruppes ~= previous_rupees) then vibe() end

        -- Ongoing vibration, count down
        if (vibration_countdown > 0) then 
            vibration_countdown = vibration_countdown - 1
            -- Stop vibrating if we're done
            if (vibration_countdown == 0) then unvibe() end
        end
    end
end

I think buzz is a pretty cute name xD

@OFFTKP
Copy link
Collaborator

OFFTKP commented Jul 14, 2024

Please merge this as soon as possible

@sylvieee-iot
Copy link
Author

sylvieee-iot commented Jul 14, 2024

Please merge this as soon as possible

Yes I believe many people would love to feel the erotic sensations a toy can offer when combined with Lua scripts :3 😉

@sylvieee-iot
Copy link
Author

Can you review @OFFTKP

@@ -70,6 +70,9 @@
[submodule "third_party/capstone"]
path = third_party/capstone
url = https://github.com/capstone-engine/capstone
[submodule "third_party/open-bp-cpp/third_party/IXWebSocket"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added this as a submodule, but not open-bp-cpp. We prefer to store stuff as submodules here so that when githubs go private we don't suffer any damages. Actually wait, that would have the opposite effect. What was I saying? Oh please add open-bp-cpp as a submodule instead

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OPEN BP CPP IS HEAVILY MODIFIED AS I MADE IT GOOD WHILE IT WAS HORRIBLE BEFORE

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry but I am not going to go through 20 thousand lines of code to make sure you didn't add a backdoor (heh get it?) somewhere in the program.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dun geddit

SensorClass sensors;

public:
ExternalHapticsManager() : client("ws://127.0.0.1", 12345) {}
Copy link
Collaborator

@OFFTKP OFFTKP Jul 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't hardcode the address/port like that. More so, what if I want to receive haptic inputs from people all around the globe, do I have to modify the code myself to change the ip address my haptic device is hosted on? What is this, dwm?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds buttplug support not e-sex support bro

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what that b word is or what e-sex is, please address the review comment

ExternalHapticsManager() : client("ws://127.0.0.1", 12345) {}

void connect() {
client.connect([](const mhl::Messages message) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of const when we are copying a value over anyway?

}

void stopDevice(int index) {
if (index < devices.size()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably trigger a signed unsigned comparison warning

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't care wtf

include_directories(third_party/open-bp-cpp/include)
include_directories(third_party/open-bp-cpp/third_party/IXWebSocket/ixwebsocket)
add_subdirectory(third_party/open-bp-cpp)
target_link_libraries(AlberCore PRIVATE buttplugCpp)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library doesn't really feel as a core component of Alber to justify being linked to the core library.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't care

}

// clang-format off
static constexpr luaL_Reg functions[] = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above, also static constexpr doesn't make much sense to me anyway

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constexpr makes it a constant expresison, static guarantees that it's allocated in static storage instead of copied on the stack when the function runs. This is why any LUTs you declare in a function should be declared as static constexpr and NOT plain constexpr.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't in a function so it's gonna go in static storage anyway. However we should keep it as constexpr (even if it does literally nothing) so that we can claim we use modern C++. Also you missed a chance to use the X macros you love so much:

#define X(name) { #name, name##Thunk },

hapticsManager->sendScalar(device, value);
}

return 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number

Copy link
Author

@sylvieee-iot sylvieee-iot Jul 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir this is how Lua thunks work, you return the number of return values

hapticsManager->stopDevice(device);
}

return 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another magic number

return 0;
}

#define HAPTICS_THUNK(func) \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X macros are bad because they enable you to essentially have a very easy and portable way to generate boilerplate code and reduce duplicated code, thus leading to less source code cluttering. Wait why did I say they are bad?

return 1;
}

// clang-format off
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a tragedy

@eepykami
Copy link

buzz buzz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants