# gearvr-controller
Use a Samsung Gear VR Controller (model ET-YO324) from a computer. The controller speaks
Bluetooth LE with its own protocol, so no operating system treats it as a gamepad. This
repository reads it directly:
- **`gearvr-controller`** is a Rust library: packet decoding, orientation tracking, a
simulated controller, and a Bluetooth connection that reconnects by itself.
- **`gearvr-demo`** is a terminal app built on the library. It shows the controller turning
in 3D, the touchpad, the buttons and live sensor traces.
- **`gearvr-viewer`** is a graphical app built on the library, drawn with
[wgpu](https://wgpu.rs). The controller turns in 3D and paints where you point it. It
runs as a desktop window and, built for WebAssembly, in a web page.
- **`web/`** holds two browser pages: a readout of the controller's sensors over Web
Bluetooth, and the page the graphical app runs in.
**Try it in your browser**, with nothing to install:
[the graphical demo](https://bagobor.github.io/gearvr-controller/viewer.html) and
[the sensor readout](https://bagobor.github.io/gearvr-controller/). Both run a simulated
controller if you do not have one. Connecting a real one needs Chrome or Edge on a
computer.

The terminal demo, here showing the built-in simulated controller. The picture is drawn
from the program's own output.
## What has been tested
- The browser demo has been used with a real controller, in Chrome on macOS.
- The Rust packet decoder is checked against a packet captured from that controller.
- The orientation filter, the simulator, the terminal view and the graphical demo's scene
are covered by tests.
- The graphical demo has been run on macOS with the simulated controller only: as a
desktop window, and as a web page in Chrome. The web page's Bluetooth code is copied from
the browser demo's, but that copy has not yet been run with a real controller.
- **The Rust Bluetooth connection has not yet been run against a real controller.** It
compiles and follows the same steps as the browser demo. Treat it as untested until you
have seen it connect.
## Run the terminal demo
You need [Rust](https://rustup.rs). No controller is needed to try it:
```bash
cargo run -p gearvr-demo -- --simulate
```
With a controller, press any button on it to wake it, then:
```bash
cargo run -p gearvr-demo
```
`q` quits. `r` makes the direction the controller points the new "straight ahead"; the Home
button on the controller does the same. Add `--plain` to print readings as lines of text
instead of the full-screen view.
On macOS the terminal you run this in needs the Bluetooth permission. macOS asks the first
time; you can also turn it on in System Settings → Privacy & Security → Bluetooth. On Linux
the program talks to BlueZ, which needs `libdbus-1-dev` and `pkg-config` to build.
## Run the graphical demo

The controller sits inside a dome and you paint on it by pointing. Try it without a
controller:
```bash
cargo run --release -p gearvr-viewer -- --simulate
```
With a controller, press any button on it to wake it, then:
```bash
cargo run --release -p gearvr-viewer
```
| Trigger | Paint while held |
| Touchpad click | Next colour |
| Volume up, volume down | Bigger brush, smaller brush |
| Back, held for a moment | Wipe the drawing |
| Home | Make the direction it points the new "straight ahead" |
In the window, `r` recenters, `c` wipes the drawing, and `q` or Esc quits. The window's
title shows the connection, the battery and the brush. The Bluetooth permission note above
applies here too.
### In a web page
The same app builds for the browser. It draws with WebGPU, which current Chrome, Edge and
Safari have. Open it at <https://bagobor.github.io/gearvr-controller/viewer.html>.
The page starts with the simulated controller. **Connect controller** switches to a real
one; that needs Web Bluetooth, so Chrome or Edge on a computer. The page's JavaScript holds
the Bluetooth connection and hands each packet to the Rust code, which is the same scene
the desktop window shows.
## Run the browser demo
Open <https://bagobor.github.io/gearvr-controller/> and choose **Connect controller**, or
**Run simulation** to see it without one. Connecting needs Web Bluetooth, so Chrome or Edge
on a computer.
To build and serve either page from your own copy, see
[Run the web pages from your own copy](https://github.com/bagobor/gearvr-controller/blob/main/docs/development.md#run-the-web-pages-from-your-own-copy).
## Use the library
```toml
[dependencies]
gearvr-controller = "0.1"
tokio = { version = "1", features = ["macros", "rt"] }
```
The crate is on [crates.io](https://crates.io/crates/gearvr-controller) and its
documentation is on [docs.rs](https://docs.rs/gearvr-controller). To follow `main` instead
of a release, depend on the repository:
`gearvr-controller = { git = "https://github.com/bagobor/gearvr-controller" }`.
```rust
use gearvr_controller::{ble, Orientation};
#[tokio::main(flavor = "current_thread")]
async fn main() {
let mut orientation = Orientation::new();
let mut events = ble::events(); // finds the controller, reconnects when it sleeps
while let Some(event) = events.recv().await {
match event {
ble::Event::Packet(packet) => {
orientation.update(&packet);
let e = orientation.euler();
println!(
"yaw {:.0}° touch {:?} buttons {:?}",
e.yaw.to_degrees(),
packet.touch,
packet.buttons
);
}
other => println!("{other:?}"),
}
}
}
```
`ble::Controller` gives direct control if you would rather manage the connection yourself.
Decoding and orientation tracking have no dependencies. To use them with your own Bluetooth
code, or on a platform btleplug does not cover, turn the default feature off:
```toml
gearvr-controller = { version = "0.1", default-features = false }
```
```rust
use gearvr_controller::{Orientation, Packet};
fn on_notification(bytes: &[u8], orientation: &mut Orientation) {
if let Ok(packet) = Packet::parse(bytes) {
orientation.update(&packet);
}
}
```
`sim::Simulator` produces the same bytes a controller sends, so you can build and test
without the hardware.
Without the feature the crate also builds for WebAssembly. `Event`, the type the Bluetooth
connection reports with, stays available, so another transport can report the same way.
The graphical demo's web page does exactly that.
The controller's protocol (the Bluetooth service, the commands and the layout of the
60-byte data packet) is described in
[How the controller talks](https://github.com/bagobor/gearvr-controller/blob/main/docs/protocol.md).
How the library is put together, and how data gets from the controller to your program,
is drawn in
[Architecture](https://github.com/bagobor/gearvr-controller/blob/main/docs/architecture.md).
## Things to know
- **It falls asleep.** Left still for about a minute, the controller drops the connection.
The keep-alive command does not prevent this. Press any button to wake it; the library
and both demos reconnect by themselves.
- **One connection at a time.** If a phone, a browser tab or another program holds the
controller, nothing else will find it.
- **Heading drifts.** Pitch and roll are anchored by gravity, but nothing anchors the
heading, so it wanders slowly. Recenter when it matters. The magnetometer could fix this
but its readings are uncalibrated, so it is not used.
- **The model's handedness is assumed.** The axis directions the library uses match other
open implementations and a captured packet, but have not been checked by eye against the
real controller. If the picture turns the wrong way, that is where to look.
## Development
How to check a change, run the web pages from your own copy, redraw the pictures on this
page, and how the library and the demo pages get published is in
[Development](https://github.com/bagobor/gearvr-controller/blob/main/docs/development.md).
## Credits
The protocol was worked out by others. This project leans on
[jsyang/gearvr-controller-webbluetooth](https://github.com/jsyang/gearvr-controller-webbluetooth)
and [uutzinger/gearVRC](https://github.com/uutzinger/gearVRC).
## License
MIT. See [LICENSE](https://github.com/bagobor/gearvr-controller/blob/main/LICENSE).