test/testsensor.c
changeset 12130 24142c5073a4
child 12503 806492103856
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testsensor.c	Tue Aug 21 12:11:34 2018 -0700
     1.3 @@ -0,0 +1,117 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +
    1.16 +/* Simple test of the SDL sensor code */
    1.17 +
    1.18 +#include "SDL.h"
    1.19 +
    1.20 +static const char *GetSensorTypeString(SDL_SensorType type)
    1.21 +{
    1.22 +    static char unknown_type[64];
    1.23 +
    1.24 +    switch (type)
    1.25 +    {
    1.26 +    case SDL_SENSOR_INVALID:
    1.27 +        return "SDL_SENSOR_INVALID";
    1.28 +    case SDL_SENSOR_UNKNOWN:
    1.29 +        return "SDL_SENSOR_UNKNOWN";
    1.30 +    case SDL_SENSOR_ACCEL:
    1.31 +        return "SDL_SENSOR_ACCEL";
    1.32 +    case SDL_SENSOR_GYRO:
    1.33 +        return "SDL_SENSOR_GYRO";
    1.34 +    default:
    1.35 +        SDL_snprintf(unknown_type, sizeof(unknown_type), "UNKNOWN (%d)", type);
    1.36 +        return unknown_type;
    1.37 +    }
    1.38 +}
    1.39 +
    1.40 +static void HandleSensorEvent(SDL_SensorEvent *event)
    1.41 +{
    1.42 +    SDL_Sensor *sensor = SDL_SensorFromInstanceID(event->which);
    1.43 +    if (!sensor) {
    1.44 +        SDL_Log("Couldn't get sensor for sensor event\n");
    1.45 +        return;
    1.46 +    }
    1.47 +
    1.48 +    switch (SDL_SensorGetType(sensor)) {
    1.49 +    case SDL_SENSOR_ACCEL:
    1.50 +        SDL_Log("Accelerometer update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
    1.51 +        break;
    1.52 +    case SDL_SENSOR_GYRO:
    1.53 +        SDL_Log("Gyro update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
    1.54 +        break;
    1.55 +    default:
    1.56 +        SDL_Log("Sensor update for sensor type %s\n", GetSensorTypeString(SDL_SensorGetType(sensor)));
    1.57 +        break;
    1.58 +    }
    1.59 +}
    1.60 +
    1.61 +int
    1.62 +main(int argc, char **argv)
    1.63 +{
    1.64 +    int i;
    1.65 +    int num_sensors, num_opened;
    1.66 +
    1.67 +    /* Load the SDL library */
    1.68 +    if (SDL_Init(SDL_INIT_SENSOR) < 0) {
    1.69 +        SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError());
    1.70 +        return (1);
    1.71 +    }
    1.72 +
    1.73 +    num_sensors = SDL_NumSensors();
    1.74 +    num_opened = 0;
    1.75 +
    1.76 +    SDL_Log("There are %d sensors available\n", num_sensors);
    1.77 +    for (i = 0; i < num_sensors; ++i) {
    1.78 +        SDL_Log("Sensor %d: %s, type %s, platform type %d\n",
    1.79 +            SDL_SensorGetDeviceInstanceID(i),
    1.80 +            SDL_SensorGetDeviceName(i),
    1.81 +            GetSensorTypeString(SDL_SensorGetDeviceType(i)),
    1.82 +            SDL_SensorGetDeviceNonPortableType(i));
    1.83 +
    1.84 +        if (SDL_SensorGetDeviceType(i) != SDL_SENSOR_UNKNOWN) {
    1.85 +            SDL_Sensor *sensor = SDL_SensorOpen(i);
    1.86 +            if (sensor == NULL) {
    1.87 +                SDL_Log("Couldn't open sensor %d: %s\n", SDL_SensorGetDeviceInstanceID(i), SDL_GetError());
    1.88 +            } else {
    1.89 +                ++num_opened;
    1.90 +            }
    1.91 +        }
    1.92 +    }
    1.93 +    SDL_Log("Opened %d sensors\n", num_opened);
    1.94 +
    1.95 +    if (num_opened > 0) {
    1.96 +        SDL_bool done = SDL_FALSE;
    1.97 +        SDL_Event event;
    1.98 +
    1.99 +        SDL_CreateWindow("Sensor Test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
   1.100 +        while (!done) {
   1.101 +            while (SDL_PollEvent(&event) > 0) {
   1.102 +                switch (event.type) {
   1.103 +                case SDL_SENSORUPDATE:
   1.104 +                    HandleSensorEvent(&event.sensor);
   1.105 +                    break;
   1.106 +                case SDL_MOUSEBUTTONUP:
   1.107 +                case SDL_KEYUP:
   1.108 +                case SDL_QUIT:
   1.109 +                    done = SDL_TRUE;
   1.110 +                    break;
   1.111 +                default:
   1.112 +                    break;
   1.113 +                }
   1.114 +            }
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    SDL_Quit();
   1.119 +    return (0);
   1.120 +}