expose battery on hmd xdev
This commit is contained in:
@@ -182,6 +182,17 @@ struct klass
|
||||
},
|
||||
handles));
|
||||
}
|
||||
|
||||
template <typename R, typename... Args>
|
||||
auto method(const char * method, Args &&... args)
|
||||
{
|
||||
using R1 = details::type_map_t<R>;
|
||||
auto & env = jni_thread::env();
|
||||
std::string signature = "(" + details::build_type(args...) + ")" + R1::type();
|
||||
auto method_id = env.GetMethodID(self.get(), method, signature.c_str());
|
||||
assert(method_id);
|
||||
return method_id;
|
||||
}
|
||||
};
|
||||
|
||||
struct Bool
|
||||
@@ -264,6 +275,19 @@ struct object
|
||||
handles));
|
||||
}
|
||||
|
||||
template <typename R, typename... Args>
|
||||
auto call(jmethodID method_id, Args &&... args)
|
||||
{
|
||||
using R1 = details::type_map_t<R>;
|
||||
auto & env = jni_thread::env();
|
||||
assert(method_id);
|
||||
auto handles = details::handle(std::forward<Args>(args)...);
|
||||
return R(std::apply([&](auto &... t) {
|
||||
return (env.*R1::call_method)(*this, method_id, t...);
|
||||
},
|
||||
handles));
|
||||
}
|
||||
|
||||
jobject handle() const
|
||||
{
|
||||
return *this;
|
||||
@@ -280,6 +304,21 @@ struct object
|
||||
}
|
||||
};
|
||||
|
||||
template <details::string_literal Type, typename... Args>
|
||||
static object<Type> new_object(Args &&... args)
|
||||
{
|
||||
auto & env = jni_thread::env();
|
||||
auto klass = jni::klass(Type.value);
|
||||
std::string signature = "(" + details::build_type(args...) + ")V";
|
||||
auto method_id = env.GetMethodID(klass, "<init>", signature.c_str());
|
||||
assert(method_id);
|
||||
auto handles = details::handle(std::forward<Args>(args)...);
|
||||
return object<Type>(std::apply([&](auto &... t) {
|
||||
return env.NewObject(klass, method_id, t...);
|
||||
},
|
||||
handles));
|
||||
}
|
||||
|
||||
using string_t = object<"java/lang/String">;
|
||||
|
||||
struct string : public string_t
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <thread>
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include "jnipp.h"
|
||||
#endif
|
||||
|
||||
static from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time)
|
||||
{
|
||||
XrSpaceVelocity velocity{
|
||||
@@ -138,6 +142,25 @@ void scenes::stream::tracking()
|
||||
#ifdef __ANDROID__
|
||||
// Runtime may use JNI and needs the thread to be attached
|
||||
application::instance().setup_jni();
|
||||
jni::object<""> act(application::native_app()->activity->clazz);
|
||||
auto app = act.call<jni::object<"android/app/Application">>("getApplication");
|
||||
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
|
||||
|
||||
jni::string filter_jstr("android.intent.action.BATTERY_CHANGED");
|
||||
jni::string level_jstr("level");
|
||||
jni::string scale_jstr("scale");
|
||||
jni::string plugged_jstr("plugged");
|
||||
jni::Int default_jint(-1);
|
||||
|
||||
auto receiver_jobj = jni::object<"android/content/BroadcastReceiver">(NULL);
|
||||
auto filter_jobj = jni::new_object<"android/content/IntentFilter">(filter_jstr);
|
||||
|
||||
auto register_receiver = jni::klass("android/content/Context")
|
||||
.method<jni::object<"android/content/Intent">>("registerReceiver", receiver_jobj, filter_jobj);
|
||||
auto get_int_extra = jni::klass("android/content/Intent").method<jni::Int>("getIntExtra", level_jstr, default_jint);
|
||||
|
||||
XrTime next_battery_check = 0;
|
||||
const XrDuration battery_check_interval = 5'000'000'000; // 5s
|
||||
#endif
|
||||
std::vector<std::pair<device_id, XrSpace>> spaces = {
|
||||
{device_id::HEAD, application::view()},
|
||||
@@ -223,6 +246,36 @@ void scenes::stream::tracking()
|
||||
XrDuration busy_time = t.count();
|
||||
// Target: polling between 1 and 5ms, with 20% busy time
|
||||
tracking_period = std::clamp<XrDuration>(std::lerp(tracking_period, busy_time * 5, 0.2), 1'000'000, 5'000'000);
|
||||
|
||||
#ifdef __ANDROID__
|
||||
if (next_battery_check < now)
|
||||
{
|
||||
timer t2(instance);
|
||||
from_headset::battery battery{};
|
||||
auto intent = ctx.call<jni::object<"android/content/Intent">>(register_receiver, receiver_jobj, filter_jobj);
|
||||
if (intent)
|
||||
{
|
||||
auto level_jint = intent.call<jni::Int>(get_int_extra, level_jstr, default_jint);
|
||||
auto scale_jint = intent.call<jni::Int>(get_int_extra, scale_jstr, default_jint);
|
||||
|
||||
if (level_jint && level_jint.value >= 0 && scale_jint && scale_jint.value >= 0)
|
||||
{
|
||||
battery.present = true;
|
||||
battery.charge = (float)(level_jint.value) / (float)(scale_jint.value);
|
||||
}
|
||||
|
||||
auto plugged_jint = intent.call<jni::Int>(get_int_extra, plugged_jstr, default_jint);
|
||||
battery.charging = plugged_jint && plugged_jint.value > 0;
|
||||
}
|
||||
|
||||
network_session->send_stream(battery);
|
||||
|
||||
next_battery_check = now + battery_check_interval;
|
||||
XrDuration battery_dur = t2.count();
|
||||
|
||||
spdlog::info("Battery check took: {}", battery_dur);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
catch (const std::system_error & e)
|
||||
{
|
||||
|
||||
@@ -218,7 +218,14 @@ struct feedback
|
||||
uint8_t times_displayed;
|
||||
};
|
||||
|
||||
using packets = std::variant<headset_info_packet, feedback, audio_data, handshake, tracking, hand_tracking, inputs, timesync_response>;
|
||||
struct battery
|
||||
{
|
||||
float charge;
|
||||
bool present;
|
||||
bool charging;
|
||||
};
|
||||
|
||||
using packets = std::variant<headset_info_packet, feedback, audio_data, handshake, tracking, hand_tracking, inputs, timesync_response, battery>;
|
||||
} // namespace from_headset
|
||||
|
||||
namespace to_headset
|
||||
|
||||
@@ -70,6 +70,11 @@ static void wivrn_hmd_get_view_poses(xrt_device * xdev,
|
||||
xrt_fov * out_fovs,
|
||||
xrt_pose * out_poses);
|
||||
|
||||
static xrt_result_t wivrn_hmd_get_battery_status(struct xrt_device * xdev,
|
||||
bool * out_present,
|
||||
bool * out_charging,
|
||||
float * out_charge);
|
||||
|
||||
static double foveate(double a, double b, double λ, double c, double x)
|
||||
{
|
||||
// In order to save encoding, transmit and decoding time, only a portion of the image is encoded in full resolution.
|
||||
@@ -190,11 +195,13 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx,
|
||||
base->update_inputs = wivrn_hmd_update_inputs;
|
||||
base->get_tracked_pose = wivrn_hmd_get_tracked_pose;
|
||||
base->get_view_poses = wivrn_hmd_get_view_poses;
|
||||
base->get_battery_status = wivrn_hmd_get_battery_status;
|
||||
base->destroy = wivrn_hmd_destroy;
|
||||
name = XRT_DEVICE_GENERIC_HMD;
|
||||
device_type = XRT_DEVICE_TYPE_HMD;
|
||||
orientation_tracking_supported = true;
|
||||
// hand_tracking_supported = true;
|
||||
battery_status_supported = true;
|
||||
position_tracking_supported = true;
|
||||
|
||||
// Print name.
|
||||
@@ -261,6 +268,12 @@ void wivrn_hmd::update_tracking(const from_headset::tracking & tracking, const c
|
||||
views.update_tracking(tracking, offset);
|
||||
}
|
||||
|
||||
void wivrn_hmd::update_battery(const from_headset::battery & new_battery)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
battery = new_battery;
|
||||
}
|
||||
|
||||
void wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation,
|
||||
uint64_t at_timestamp_ns,
|
||||
uint32_t view_count,
|
||||
@@ -296,6 +309,19 @@ void wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation,
|
||||
}
|
||||
}
|
||||
|
||||
xrt_result_t wivrn_hmd::get_battery_status(struct xrt_device * xdev,
|
||||
bool * out_present,
|
||||
bool * out_charging,
|
||||
float * out_charge)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
*out_present = battery.present;
|
||||
*out_charging = battery.charging;
|
||||
*out_charge = battery.charge;
|
||||
|
||||
return XRT_SUCCESS;
|
||||
}
|
||||
|
||||
decltype(wivrn_hmd::foveation_parameters) wivrn_hmd::set_foveated_size(uint32_t width, uint32_t height)
|
||||
{
|
||||
assert(width % 2 == 0);
|
||||
@@ -384,3 +410,11 @@ static void wivrn_hmd_get_view_poses(xrt_device * xdev,
|
||||
{
|
||||
static_cast<wivrn_hmd *>(xdev)->get_view_poses(default_eye_relation, at_timestamp_ns, view_count, out_head_relation, out_fovs, out_poses);
|
||||
}
|
||||
|
||||
static xrt_result_t wivrn_hmd_get_battery_status(struct xrt_device * xdev,
|
||||
bool * out_present,
|
||||
bool * out_charging,
|
||||
float * out_charge)
|
||||
{
|
||||
return static_cast<wivrn_hmd *>(xdev)->get_battery_status(xdev, out_present, out_charging, out_charge);
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ class wivrn_hmd : public xrt_device
|
||||
|
||||
view_list views;
|
||||
std::array<to_headset::video_stream_description::foveation_parameter, 2> foveation_parameters{};
|
||||
from_headset::battery battery{};
|
||||
|
||||
std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx;
|
||||
|
||||
@@ -70,7 +71,12 @@ public:
|
||||
xrt_space_relation * out_head_relation,
|
||||
xrt_fov * out_fovs,
|
||||
xrt_pose * out_poses);
|
||||
xrt_result_t get_battery_status(struct xrt_device * xdev,
|
||||
bool * out_present,
|
||||
bool * out_charging,
|
||||
float * out_charge);
|
||||
|
||||
void update_battery(const from_headset::battery &);
|
||||
void update_tracking(const from_headset::tracking &, const clock_offset &);
|
||||
|
||||
decltype(foveation_parameters) set_foveated_size(uint32_t width, uint32_t height);
|
||||
|
||||
@@ -304,6 +304,11 @@ void wivrn_session::operator()(from_headset::feedback && feedback)
|
||||
dump_time("display", feedback.frame_index, o.from_headset(feedback.displayed), feedback.stream_index);
|
||||
}
|
||||
|
||||
void wivrn_session::operator()(from_headset::battery && battery)
|
||||
{
|
||||
hmd->update_battery(battery);
|
||||
}
|
||||
|
||||
void wivrn_session::operator()(audio_data && data)
|
||||
{
|
||||
if (audio_handle)
|
||||
|
||||
@@ -117,6 +117,7 @@ public:
|
||||
void operator()(from_headset::inputs &&);
|
||||
void operator()(from_headset::timesync_response &&);
|
||||
void operator()(from_headset::feedback &&);
|
||||
void operator()(from_headset::battery &&);
|
||||
void operator()(audio_data &&);
|
||||
|
||||
template <typename T>
|
||||
|
||||
Reference in New Issue
Block a user