openhmd

package module
v0.0.0-...-2f95391 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2020 License: BSL-1.0 Imports: 3 Imported by: 0

README

GoDoc Go Report Card

openhmd-go

OpenHMD API bindings for Go.

go get -u github.com/Apfel/openhmd-go

This module requires OpenHMD. Click here for instructions.

Example

This is OpenHMD's simple example, ported to Go using openhmd-go.

package main

import (
	"log"
	"os"
	"strconv"
	"time"

	openhmd "github.com/Apfel/openhmd-go"
)

func main() {
	log.Printf("openhmd-go - Simple Example")
	var id int

	if len(os.Args) < 2 || os.Args[1] == "" {
		log.Fatalln("Please provide a device ID.")
	} else {
		id, err := strconv.Atoi(os.Args[1])
		if err != nil {
			log.Fatalf("Couldn't convert '%s' to an integer.\nError: %s\n", os.Args[1], err.Error())
		}
		
		log.Printf("Using ID %d.", id)
	}

	context := openhmd.CreateContext()
	if context == nil {
		log.Fatalln("Failed to create a context.")
	}

	if count := context.Probe(); count == 0 {
		log.Fatalln("No devices found, quitting...")
	} else {
		log.Printf("Found device(s). Device count: %d\n", count)
	}

	device := context.ListOpenDevice(id)
	if device == nil || len(context.GetError()) != 0 {
		log.Fatalf("The device with ID %d couldn't be opened. Error: %s\n", id, context.GetError())
	} else {
		log.Printf("Opened device %s, vendor is %s. ID: %s\n", context.ListGetString(id, openhmd.StringValueProduct),
			context.ListGetString(id, openhmd.StringValueVendor), context.ListGetString(id, openhmd.StringValuePath))
	}

	c, width := device.GetInt(openhmd.IntValueScreenVerticalResolution, 1)
	if c != openhmd.StatusCodeOkay {
		log.Fatalf("Fetching the device's screen width failed. Status Code: %d\n", c)
	}

	c, height := device.GetInt(openhmd.IntValueScreenHorizontalResolution, 1)
	if c != openhmd.StatusCodeOkay {
		log.Fatalf("Fetching the device's screen height failed. Status Code: %d\n", c)
	}

	log.Printf("Device properties:\nResolution: %dx%d\n", width[0], height[0]) // I do know that this is rather poorly designed, but whatever

	for 1 == 1 {
		c, rot := device.GetFloat(openhmd.FloatValueRotationQuat, 4)
		if c != openhmd.StatusCodeOkay {
			log.Fatalf("Rotation - Error code %d\n", c)
		}
		c, pos := device.GetFloat(openhmd.FloatValuePositionVector, 3)
		if c != openhmd.StatusCodeOkay {
			log.Fatalf("Position - Error code %d\n", c)
		}
		log.Printf("Rotation: %f %f %f %f\nPosition: %f %f %f", rot[0], rot[1], rot[2], rot[3], pos[0], pos[1], pos[2])
		time.Sleep(time.Millisecond * 100)
		context.Update()
	}
}

Documentation

Index

Constants

View Source
const StringSize = C.OHMD_STR_SIZE

StringSize defines the maximum length of a string, including termination, in OpenHMD.

Variables

View Source
var (
	// ErrorUnknownError defines a return value that hasn't been specifically checked for.
	ErrorUnknownError = errors.New("Unknown error")

	// ErrorInvalidParameter gets returned if invalid parameters have been given.
	// Note that this is also used if too many entries are supplied, so GetFloat and GetInt, SetFloat and SetInt only return or accept 16 or less values.
	ErrorInvalidParameter = errors.New("Invalid parameter")

	// ErrorUnsupported defines either an unsupported version or action.
	ErrorUnsupported = errors.New("Unsupported version")

	// ErrorInvalidOperation defines a denied or invalid operation.
	ErrorInvalidOperation = errors.New("Invalid operation")
)

Functions

func GetString

func GetString(desc StringDescription) (string, error)

GetString fetches a string description value from OpenHMD.

func GetVersion

func GetVersion() (int, int, int)

GetVersion returns OpenHMD's version.

func RequireVersion

func RequireVersion(major, minor, patch int) bool

RequireVersion checks that the library is compatible with the required version. Returns true if the version is met.

func Sleep

func Sleep(time float64)

Sleep makes OpenHMD sleep for X seconds.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context defines the current use within an application.

func CreateContext

func CreateContext() *Context

CreateContext makes an OpenHMD context. Returns nil if the context can't allocate memory.

func (*Context) CreateSettings

func (c *Context) CreateSettings() *DeviceSettings

CreateSettings creates a device settings instance.

func (*Context) Destroy

func (c *Context) Destroy()

Destroy removes the current OpenHMD context. Note: Your context will be removed from memory and all devices associated with the context will be closed automatically.

func (*Context) GetError

func (c *Context) GetError() string

GetError fetches the last error as a human-readable string.

func (*Context) ListGetInt

func (c *Context) ListGetInt(deviceIndex int, value IntValue) (int, error)

ListGetInt gets an integer value from the enumeration list index.

func (*Context) ListGetString

func (c *Context) ListGetString(deviceIndex int, value StringValue) string

ListGetString gets a given device description from the enumeration list index.

func (*Context) ListOpenDevice

func (c *Context) ListOpenDevice(index int) *Device

ListOpenDevice opens a device.

func (*Context) ListOpenDeviceSettings

func (c *Context) ListOpenDeviceSettings(index int, settings *DeviceSettings) *Device

ListOpenDeviceSettings opens a device with additional settings provided.

func (*Context) Probe

func (c *Context) Probe() int

Probe for devices. Returns the number of devices found on the system.

func (*Context) Update

func (c *Context) Update()

Update updates the data of the current context. This should be called periodically, to fetch the new values for the devices handled by a context.

type ControlHint

type ControlHint C.ohmd_control_hint

ControlHint defines button presses, trigger pushing and analog stick movement.

const (
	// ControlHintGeneric - Generic button pressed.
	ControlHintGeneric ControlHint = iota

	// ControlHintTrigger - Trigger pushed.
	ControlHintTrigger

	// ControlHintTriggerClick - Trigger "clicked" - defines that the Trigger has been pushed all the way in.
	ControlHintTriggerClick

	// ControlHintSqueeze - Grip button pressed.
	ControlHintSqueeze

	// ControlHintMenu - Menu button pressed.
	ControlHintMenu

	// ControlHintHome - Home button pressed.
	ControlHintHome

	// ControlHintAnalogX - Horizontal stick movement.
	ControlHintAnalogX

	// ControlHintAnalogY - Vertical stick movement.
	ControlHintAnalogY

	// ControlHintAnalogPress - Stick pressed.
	ControlHintAnalogPress

	// ControlHintButtonA - Button A pressed.
	ControlHintButtonA

	// ControlHintButtonB - Button B pressed.
	ControlHintButtonB

	// ControlHintButtonX - Button X pressed.
	ControlHintButtonX

	// ControlHintButtonY - Button Y pressed.
	ControlHintButtonY

	// ControlHintVolumePlus - Volume up button pressed.
	ControlHintVolumePlus

	// ControlHintVolumeMinus - Volume down button pressed.
	ControlHintVolumeMinus

	// ControlHintMicMute - Microphone mute button pressed.
	ControlHintMicMute
)

type ControlType

type ControlType C.ohmd_control_type

ControlType specifies digital or analog controls.

const (
	// ControlTypeDigital defines digital controls, like a button.
	ControlTypeDigital ControlType = iota

	// ControlTypeAnalog defines analog controls, like a analog stick.
	ControlTypeAnalog
)

type DataValue

type DataValue C.ohmd_data_value

DataValue defines binary data.

const (
	// DataValueDriverData defines the use of setting specific data for the internal drivers.
	DataValueDriverData DataValue = iota

	// DataValueDriverProperties defines the use of setting properties of a device internally.
	DataValueDriverProperties
)

type Device

type Device struct {
	// contains filtered or unexported fields
}

Device defines a OpenHMD device, like a Head-Mounted Display or a motion Controller.

func (*Device) Close

func (d *Device) Close() error

Close closes the current device.

func (*Device) GetFloat

func (d *Device) GetFloat(value FloatValue, length int) ([]float32, error)

GetFloat fetches (a) float value(s).

func (*Device) GetInt

func (d *Device) GetInt(value IntValue, length int) ([]int32, error)

GetInt fetches (a) int value(s).

func (*Device) SetData

func (d *Device) SetData(value DataValue, input interface{}) error

SetData sends data directly a device.

func (*Device) SetFloat

func (d *Device) SetFloat(value FloatValue, input []float32) error

SetFloat sets (a) float value(s).

type DeviceClass

type DeviceClass C.ohmd_device_class

DeviceClass defines what kind of device is being used.

const (
	// DeviceClassHMD defines a HMD.
	DeviceClassHMD DeviceClass = iota

	// DeviceClassController defines a motion controller.
	DeviceClassController

	// DeviceClassGenericTracker defines a simple tracker.
	DeviceClassGenericTracker
)

type DeviceFlags

type DeviceFlags C.ohmd_device_flags

DeviceFlags specifies flags for a given devices.

const (
	// DeviceFlagsNullDevice defines a dummy device.
	DeviceFlagsNullDevice DeviceFlags = 1

	// DeviceFlagsPositionalTracking defines a position-tracking device.
	DeviceFlagsPositionalTracking DeviceFlags = 2

	// DeviceFlagsRotationalTracking defines a rotation-tracking device.
	DeviceFlagsRotationalTracking DeviceFlags = 4

	// DeviceFlagsLeftController defines a left-sided motion controller.
	DeviceFlagsLeftController DeviceFlags = 8

	// DeviceFlagsRightController defines a right-sided motion controller.
	DeviceFlagsRightController DeviceFlags = 16
)

type DeviceSettings

type DeviceSettings struct {
	// contains filtered or unexported fields
}

DeviceSettings stores data used for setting up the given device.

func (*DeviceSettings) Destroy

func (s *DeviceSettings) Destroy()

Destroy destroys a device settings instance.

func (*DeviceSettings) SetInt

func (s *DeviceSettings) SetInt(key IntSettings, value int) error

SetInt sets the given value for the providen key.

type FloatValue

type FloatValue C.ohmd_float_value

FloatValue defines various float32-based values, like position or rotation, and more data that needs to be accurate.

const (
	// FloatValueRotationQuat defines the absolute rotation of the device, in space, as a quaternion.
	// Valid for GetFloat.
	// Returns 4 values: X, Y, Z, W.
	FloatValueRotationQuat FloatValue = iota + 1

	// FloatValueLeftEyeGlModelViewMatrix defines a "ready to use" OpenGL style 4x4 matrix with a modelview matrix for the left eye of the HMD.
	// Valid for GetFloat.
	// Returns 16 values.
	FloatValueLeftEyeGlModelViewMatrix

	// FloatValueRightEyeGlModelViewMatrix defines a "ready to use" OpenGL style 4x4 matrix with a modelview matrix for the right eye of the HMD.
	// Valid for GetFloat.
	// Returns 16 values.
	FloatValueRightEyeGlModelViewMatrix

	// FloatValueLeftEyeGlProjectionMatrix defines a "ready to use" OpenGL style 4x4 matrix with a projection matrix for the left eye of the HMD.
	// Valid for GetFloat.
	// Returns 16 values.
	FloatValueLeftEyeGlProjectionMatrix

	// FloatValueRightEyeGlProjectionMatrix defines a "ready to use" OpenGL style 4x4 matrix with a projection matrix for the right eye of the HMD.
	// Valid for GetFloat.
	// Returns 16 values.
	FloatValueRightEyeGlProjectionMatrix

	// FloatValuePositionVector defines a 3D vector representing the absolute position of the device, in space.
	// Valid for GetFloat.
	// Returns 3 values: X, Y, Z.
	FloatValuePositionVector

	// FloatValueScreenHorizontalSize defines the width of the device's screen in metres.
	// Valid for GetFloat.
	// Returns 1 value.
	FloatValueScreenHorizontalSize

	// FloatValueScreenVerticalSize defines the height of the device's screen in metres.
	// Valid for GetFloat.
	// Returns 1 value.
	FloatValueScreenVerticalSize

	// FloatValueLensHorizontalSeparation defines the separation of the device's lenses in metres.
	// Valid for GetFloat.
	// Returns 1 value.
	FloatValueLensHorizontalSeparation

	// FloatValueLensVerticalPosition defines the vertical position of the device's lenses in metres.
	// Valid for GetFloat.
	// Returns 1 value.
	FloatValueLensVerticalPosition

	// FloatValueLeftEyeFOV defines the field of view for the left eye in degrees.
	// Valid for GetFloat.
	// Returns 1 value.
	FloatValueLeftEyeFOV

	// FloatValueLeftEyeAspectRatio defines the aspect ratio of the screen for the left eye.
	// Valid for GetFloat.
	// Returns 1 value.
	FloatValueLeftEyeAspectRatio

	// FloatValueRightEyeFOV defines the field of view for the right eye in degrees.
	// Valid for GetFloat.
	// Returns 1 value.
	FloatValueRightEyeFOV

	// FloatValueRightEyeAspectRatio defines the aspect ratio of the screen for the right eye.
	// Valid for GetFloat.
	// Returns 1 value.
	FloatValueRightEyeAspectRatio

	// FloatValueEyeIPD defines the the interpupillary distance of the user's eyes in metres.
	// Valid for GetFloat and SetFloat.
	// Returns/Accepts 1 value.
	FloatValueEyeIPD

	// FloatValueProjectionZFar defines how far the projection matrix can be drawn on the screen.
	// Valid for GetFloat and SetFloat.
	// Returns/Accepts 1 value.
	FloatValueProjectionZFar

	// FloatValueProjectionZNear defines how near the projection matrix can be drawn on the screen. This can be, for example, be used for close clipping distance.
	// Valid for GetFloat and SetFloat.
	// Returns/Accepts 1 value.
	FloatValueProjectionZNear

	// FloatValueDistortionK defines the device-specific distortion value.
	// Valid for GetFloat.
	// Returns 6 values.
	FloatValueDistortionK

	// FloatValueExternalSensorFusion defines the use of performing sensor fusion on values from the external sensors.
	// Valid for SetFloat.
	// Returns 10 values: Date + Time, X, Y, Z (gyrometer), X, Y, Z (accelerometer), X, Y, Z (magnetometer).
	FloatValueExternalSensorFusion

	// FloatValueUniversalDistortionK defines the universal shader distortion coefficients, based on the Panorama Tools model.
	// Valid for GetFloat.
	// Returns 4 values: A, B, C, D.
	FloatValueUniversalDistortionK

	// FloatValueUniversalAberrationK defines the universal shader aberration coefficients, based on Post-Warp scaling.
	// Valid for GetFloat.
	// Returns 3 values: R, G, B.
	FloatValueUniversalAberrationK

	// FloatValueControlsState defines the state of the device's controls.
	// Valid for GetFloat.
	// Returns between 1 to 16 values, based on IntValueControlsCount.
	FloatValueControlsState
)

type IntSettings

type IntSettings C.ohmd_int_settings

IntSettings can be used for integer-based settings for the device.

const (
	// IntSettingsIDsAutomaticUpdate allows OpenHMD to create background threads for automatic updating.
	// If this is set to 0, Update needs to be called at least 10 times per second.
	IntSettingsIDsAutomaticUpdate IntSettings = iota
)

type IntValue

type IntValue C.ohmd_int_value

IntValue carries information that can be single- or double-digit long, and don't need to be accurate, like FloatValue. These can be, for example, the button count, and so on.

const (
	// IntValueScreenHorizontalResolution defines the horizontal resolution of the device's screen.
	// Valid for GetInt.
	// Returns 1 value.
	IntValueScreenHorizontalResolution IntValue = iota

	// IntValueScreenVerticalResolution defines the vertical resolution of the device's screen.
	// Valid for GetInt.
	// Returns 1 value.
	IntValueScreenVerticalResolution

	// IntValueDeviceClass returns the device's class, refer to DeviceClass for more information.
	// Valid For GetInt and ListGetInt.
	// Returns 1 value.
	IntValueDeviceClass

	// IntValueDeviceFlags returns the device's flags, refer to DeviceFlags for more information.
	// Valid For GetInt and ListGetInt.
	// Returns 1 value.
	IntValueDeviceFlags

	// IntValueControlsCount returns the count of controls for the device.
	// Valid for GetInt.
	// Returns 1 value.
	IntValueControlsCount

	// IntValueControlsHints returns which buttons are supported.
	// Valid for GetInt.
	// Returns between 1 to 16 values, based on IntValueControlsCount.
	IntValueControlsHints

	// IntValueControlsTypes returns whether a control entry is analog or digital.
	// Valid for GetInt.
	// Returns between 1 to 16 values, based on IntValueControlsCount.
	IntValueControlsTypes
)

type StringDescription

type StringDescription C.ohmd_string_description

StringDescription returns information about the used shaders.

const (
	StringDescriptionGlslDisortionVertSrc StringDescription = iota
	StringDescriptionGlslDisortionFragSRC
	StringDescriptionGsls330DisortionVertSrc
	StringDescriptionGsls330DisortionFragSrc
	StringDescriptionGslsEsDisortionVertSrc
	StringDescriptionGslsEsDisortionFragSrc
)

A collection of string descriptions, returning Shader-related data.

type StringValue

type StringValue C.ohmd_string_value

StringValue can be used for identifying the hardware itself.

const (
	// StringValueVendor returns the name of the product's vendor.
	StringValueVendor StringValue = iota + 1

	//StringValueProduct returns the name of the product itself.
	StringValueProduct

	// StringValuePath returns the internal path of the device.
	StringValuePath
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL