Switch branches/tags
Nothing to show
Find file Copy path
25c098f Nov 30, 2018
1 contributor

Users who have contributed to this file

84 lines (71 sloc) 2.95 KB
// FOVE Data Example
// This shows how to fetch and output data from the FOVE service in a console program
#include "FoveAPI.h"
#include "Util.h"
#include <chrono>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <thread>
// Use std namespace for convenience
using namespace std;
// Helper function to check error responses from the FOVE API
bool checkError(const Fove::ErrorCode errorCode)
// Check for error
switch (errorCode) {
case Fove::ErrorCode::None:
return true;
case Fove::ErrorCode::Connect_NotConnected:
// This happens when the service is off, or we haven't finished connecting to it
cerr << "Not connected to service" << endl;
case Fove::ErrorCode::Data_NoUpdate:
// This happens after we connect to the service, but before the first frame of data comes in
cerr << "No update" << endl;
// Less common errors are simply logged with their numeric value
cerr << "Error #" << EnumToUnderlyingValue(errorCode) << endl;
return false;
int main() try {
// Create the Headset object, taking the capabilities we need in our program
// Different capabilities may enable different hardware or software, so use only the capabilities that are needed
Fove::Headset headset = Fove::Headset::create(Fove::ClientCapabilities::Gaze).getValue();
// Loop indefinitely
while (true) {
// Wait for the next eye frame
// The current thread will sleep until a new frame comes in
// This allows us to capture data at the full frame rate of eye tracking and not use too much CPU
const Fove::Result<> waitResult = headset.waitForNextEyeFrame();
if (!checkError(waitResult.getError())) {
// Sleep for a second in the event of failure
// If the wait function fails, it might have returned immediately, and we may eat up 100% of a CPU core if we don't sleep manually
this_thread::sleep_for(chrono::seconds { 1 }); // Can use 1s in C++14 and later
continue; // Skip getting the gaze vectors
// Fetch the left gaze vector
// You can swap this out with other IFVRHeadset functions to get other types of data
const Fove::Result<Fove::Stereo<Fove::GazeVector>> gaze = headset.getGazeVectors();
if (checkError(gaze.getError())) {
// If there was no error, we are allowed to access the other members of the struct
cout << "Gaze vectors: L(" << fixed << setprecision(3)
<< setw(5) << gaze.getValue().l.vector.x << ", "
<< setw(5) << gaze.getValue().l.vector.y << ", "
<< setw(5) << gaze.getValue().l.vector.z << ") R("
<< setw(5) << gaze.getValue().r.vector.x << ", "
<< setw(5) << gaze.getValue().r.vector.y << ", "
<< setw(5) << gaze.getValue().r.vector.z << ')' << endl;
} catch (const exception& e) {
// If an exception is thrown for any reason, log it and exit
// The FOVE api is designed not to throw externally, but the standard library can
cerr << "Error: " << e.what() << endl;