Commit current code

This commit is contained in:
rodolpheh 2024-04-15 07:04:46 +01:00
commit a3b089ebc0
3 changed files with 194 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
# Created by https://www.toptal.com/developers/gitignore/api/zig
# Edit at https://www.toptal.com/developers/gitignore?templates=zig
### zig ###
# Zig programming language
zig-cache/
zig-out/
build/
build-*/
docgen_tmp/
# End of https://www.toptal.com/developers/gitignore/api/zig

19
build.zig Normal file
View File

@ -0,0 +1,19 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "leapc_polling",
.root_source_file = .{ .path = "src/leapc_polling.zig" },
.target = target,
.optimize = optimize,
.link_libc = true,
});
exe.addLibraryPath(.{ .path = "/usr/lib/ultraleap-hand-tracking-service" });
exe.linkSystemLibrary("LeapC");
b.installArtifact(exe);
}

162
src/leapc_polling.zig Normal file
View File

@ -0,0 +1,162 @@
const std = @import("std");
const leapc = @cImport({
@cInclude("LeapC.h");
});
const signal = @cImport({
@cInclude("signal.h");
});
const hand_fmt = "Hand with id {d}, handedness: {s}\n";
var keep_running: bool = true;
pub fn main() !void {
std.debug.print("Hello World!\n", .{});
_ = signal.signal(signal.SIGINT, &sigintHandler);
var handle: leapc.LEAP_CONNECTION = null;
var config: leapc.LEAP_CONNECTION_CONFIG = std.mem.zeroes(leapc.LEAP_CONNECTION_CONFIG);
config.size = @sizeOf(leapc.LEAP_CONNECTION_CONFIG);
// Create connection
var result: leapc.eLeapRS = leapc.LeapCreateConnection(null, &handle);
if (result != leapc.eLeapRS_Success) {
std.debug.print("LeapCreateConnection() error {d}\n", .{result});
return error.LeapCreateConnectionError;
}
defer leapc.LeapDestroyConnection(handle);
// Open connection
result = leapc.LeapOpenConnection(handle);
if (result != leapc.eLeapRS_Success) {
std.debug.print("LeapOpenConnection() error {d}\n", .{result});
return error.LeapOpenConnectionError;
}
defer leapc.LeapCloseConnection(handle);
// Set allocator
const allocator: leapc.LEAP_ALLOCATOR = .{
.allocate = allocate,
.deallocate = deallocate,
.state = null,
};
result = leapc.LeapSetAllocator(handle, &allocator);
if (result != leapc.eLeapRS_Success) {
std.debug.print("LeapSetAllocator() error {d}\n", .{result});
return error.LeapSetAllocatorError;
}
//var message: leapc.LEAP_CONNECTION_MESSAGE = std.mem.zeroes(leapc.LEAP_CONNECTION_MESSAGE);
var message: leapc.LEAP_CONNECTION_MESSAGE = std.mem.zeroInit(leapc.LEAP_CONNECTION_MESSAGE, .{});
while (keep_running) {
result = leapc.LeapPollConnection(handle, 100, &message);
if (result != leapc.eLeapRS_Success and result != leapc.eLeapRS_Timeout) {
std.debug.print("LeapPollConnection() error {d}\n", .{result});
return error.LeapPollConnectionError;
}
if (result == leapc.eLeapRS_Timeout) {
std.debug.print("TIMEOUT", .{});
continue;
}
switch (message.type) {
leapc.eLeapEventType_Connection => {
std.debug.print("Connected!\n", .{});
},
leapc.eLeapEventType_Tracking => {
const frame: *const leapc.LEAP_TRACKING_EVENT = message.unnamed_0.tracking_event;
std.debug.print("Frame id {d}, {d} hands found\n", .{ frame.tracking_frame_id, frame.nHands });
for (0..frame.nHands) |i| {
const hand: leapc.LEAP_HAND = frame.pHands[i];
std.debug.print(hand_fmt, .{ hand.id, if (hand.type == leapc.eLeapHandType_Left) "Left" else "Right" });
}
},
else => {
std.debug.print("Message unsupported for now\n", .{});
},
}
}
}
fn sigintHandler(_: c_int) callconv(.C) void {
std.debug.print("Received a sigint\n", .{});
keep_running = false;
}
fn allocate(size: u32, typeHint: leapc.eLeapAllocatorType, _: ?*anyopaque) callconv(.C) ?*anyopaque {
const allocator = std.heap.page_allocator;
switch (typeHint) {
leapc.eLeapAllocatorType_Int8 => {
const memory = allocator.alloc(i8, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_Int16 => {
const memory = allocator.alloc(i16, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_Int32 => {
const memory = allocator.alloc(i32, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_Int64 => {
const memory = allocator.alloc(i64, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_Uint8 => {
const memory = allocator.alloc(u8, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_UInt16 => {
const memory = allocator.alloc(u16, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_UInt32 => {
const memory = allocator.alloc(u32, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_UInt64 => {
const memory = allocator.alloc(u64, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_Float => {
const memory = allocator.alloc(f32, size) catch {
return null;
};
return @ptrCast(memory);
},
leapc.eLeapAllocatorType_Double => {
const memory = allocator.alloc(f64, size) catch {
return null;
};
return @ptrCast(memory);
},
else => {
return null;
},
}
}
fn deallocate(_: ?*anyopaque, _: ?*anyopaque) callconv(.C) void {
//ptr.deallocate();
}