Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ This is a [buttplug.io](buttplug.io) client written in Lua.

## Example code

TODO
See `demo.lua`.

## Known issues
## Dependencies

- Does not handle multiple devices
Known to work with these versions:

- Lua 5.3
- [pollnet v0.5.1](https://github.com/probable-basilisk/pollnet/releases/tag/v0.5.1)
4 changes: 2 additions & 2 deletions buttplug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ function buttplug.send_stop_all_devices_cmd()
end

function buttplug.count_devices()
return table.getn(buttplug.devices)
return #buttplug.devices
end

function buttplug.add_device(dev)
local dev_count = table.getn(buttplug.devices)
local dev_count = #buttplug.devices

buttplug.devices[dev_count + 1] = {
index = dev["DeviceIndex"],
Expand Down
16 changes: 8 additions & 8 deletions demo.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
-- local pollnet = require("pollnet")
-- local json = require("json")
local buttplug = require("buttplug")

-- get system Sleep function
local ffi = require("ffi")

ffi.cdef[[
void Sleep(int ms);
int poll(struct pollfd *fds,unsigned long nfds,int timeout);
]]

local sleep
Expand Down Expand Up @@ -55,14 +54,15 @@ function main_loop()
end

-- Game doing other things, including running the thing
if buttplug.count_devices() > 0 then
buttplug.send_vibrate_cmd(0, { 0.2 })
sleep(500)
buttplug.send_vibrate_cmd(0, { 0 })
os.exit()
for device_index = 0, buttplug.count_devices()-1 do
buttplug.send_vibrate_cmd(device_index, { 0.2 })
end
sleep(500)
for device_index = 0, buttplug.count_devices()-1 do
buttplug.send_vibrate_cmd(device_index, { 0 })
end

sleep(500)

end
end

Expand Down
48 changes: 24 additions & 24 deletions pollnet.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
-- MIT License

-- Copyright (c) 2020 probable-basilisk

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:

-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.

-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.

--[[
pollnet bindings for luajit

Expand Down Expand Up @@ -76,14 +54,18 @@ ffi.cdef[[
struct pnctx* pollnet_init();
struct pnctx* pollnet_get_or_init_static();
void pollnet_shutdown(struct pnctx* ctx);
unsigned int pollnet_open_tcp(struct pnctx* ctx, const char* addr);
unsigned int pollnet_listen_tcp(struct pnctx* ctx, const char* addr);
unsigned int pollnet_open_ws(struct pnctx* ctx, const char* url);
unsigned int pollnet_simple_http_get(struct pnctx* ctx, const char* url);
unsigned int pollnet_simple_http_post(struct pnctx* ctx, const char* url, const char* content_type, const char* data, unsigned int datasize);
void pollnet_close(struct pnctx* ctx, unsigned int handle);
void pollnet_close_all(struct pnctx* ctx);
void pollnet_send(struct pnctx* ctx, unsigned int handle, const char* msg);
void pollnet_send_binary(struct pnctx* ctx, unsigned int handle, const char* msg, unsigned int msgsize);
unsigned int pollnet_update(struct pnctx* ctx, unsigned int handle);
int pollnet_get(struct pnctx* ctx, unsigned int handle, char* dest, unsigned int dest_size);
unsigned int pollnet_update_blocking(struct pnctx* ctx, unsigned int handle);
int pollnet_get(struct pnctx* ctx, unsigned int handle, signed char* dest, unsigned int dest_size);
int pollnet_get_error(struct pnctx* ctx, unsigned int handle, char* dest, unsigned int dest_size);
unsigned int pollnet_get_connected_client_handle(struct pnctx* ctx, unsigned int handle);
unsigned int pollnet_listen_ws(struct pnctx* ctx, const char* addr);
Expand Down Expand Up @@ -160,6 +142,10 @@ function socket_mt:open_ws(url, scratch_size)
return self:_open(scratch_size, pollnet.pollnet_open_ws, url)
end

function socket_mt:open_tcp(addr, scratch_size)
return self:_open(scratch_size, pollnet.pollnet_open_tcp, addr)
end

function socket_mt:serve_http(addr, dir, scratch_size)
self.is_http_server = true
if dir and dir ~= "" then
Expand All @@ -183,6 +169,10 @@ function socket_mt:listen_ws(addr, scratch_size)
return self:_open(scratch_size, pollnet.pollnet_listen_ws, addr)
end

function socket_mt:listen_tcp(addr, scratch_size)
return self:_open(scratch_size, pollnet.pollnet_listen_tcp, addr)
end

function socket_mt:on_connection(f)
self._on_connection = f
return self
Expand Down Expand Up @@ -274,6 +264,14 @@ local function listen_ws(addr, scratch_size)
return Socket():listen_ws(addr, scratch_size)
end

local function open_tcp(addr, scratch_size)
return Socket():open_tcp(addr, scratch_size)
end

local function listen_tcp(addr, scratch_size)
return Socket():listen_tcp(addr, scratch_size)
end

local function serve_http(addr, dir, scratch_size)
return Socket():serve_http(addr, dir, scratch_size)
end
Expand All @@ -298,10 +296,12 @@ return {
shutdown = shutdown_ctx,
open_ws = open_ws,
listen_ws = listen_ws,
open_tcp = open_tcp,
listen_tcp = listen_tcp,
serve_http = serve_http,
http_get = http_get,
http_post = http_post,
Socket = Socket,
pollnet = pollnet,
nanoid = get_nanoid,
}
}