test/testsensor.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 01 Nov 2018 12:31:45 -0400
changeset 12382 03d0bddca61b
parent 12130 24142c5073a4
child 12503 806492103856
permissions -rw-r--r--
merge fallout: Patched to compile, fixed some compiler warnings, etc.
slouken@12130
     1
/*
slouken@12130
     2
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@12130
     3
slouken@12130
     4
  This software is provided 'as-is', without any express or implied
slouken@12130
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@12130
     6
  arising from the use of this software.
slouken@12130
     7
slouken@12130
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@12130
     9
  including commercial applications, and to alter it and redistribute it
slouken@12130
    10
  freely.
slouken@12130
    11
*/
slouken@12130
    12
slouken@12130
    13
/* Simple test of the SDL sensor code */
slouken@12130
    14
slouken@12130
    15
#include "SDL.h"
slouken@12130
    16
slouken@12130
    17
static const char *GetSensorTypeString(SDL_SensorType type)
slouken@12130
    18
{
slouken@12130
    19
    static char unknown_type[64];
slouken@12130
    20
slouken@12130
    21
    switch (type)
slouken@12130
    22
    {
slouken@12130
    23
    case SDL_SENSOR_INVALID:
slouken@12130
    24
        return "SDL_SENSOR_INVALID";
slouken@12130
    25
    case SDL_SENSOR_UNKNOWN:
slouken@12130
    26
        return "SDL_SENSOR_UNKNOWN";
slouken@12130
    27
    case SDL_SENSOR_ACCEL:
slouken@12130
    28
        return "SDL_SENSOR_ACCEL";
slouken@12130
    29
    case SDL_SENSOR_GYRO:
slouken@12130
    30
        return "SDL_SENSOR_GYRO";
slouken@12130
    31
    default:
slouken@12130
    32
        SDL_snprintf(unknown_type, sizeof(unknown_type), "UNKNOWN (%d)", type);
slouken@12130
    33
        return unknown_type;
slouken@12130
    34
    }
slouken@12130
    35
}
slouken@12130
    36
slouken@12130
    37
static void HandleSensorEvent(SDL_SensorEvent *event)
slouken@12130
    38
{
slouken@12130
    39
    SDL_Sensor *sensor = SDL_SensorFromInstanceID(event->which);
slouken@12130
    40
    if (!sensor) {
slouken@12130
    41
        SDL_Log("Couldn't get sensor for sensor event\n");
slouken@12130
    42
        return;
slouken@12130
    43
    }
slouken@12130
    44
slouken@12130
    45
    switch (SDL_SensorGetType(sensor)) {
slouken@12130
    46
    case SDL_SENSOR_ACCEL:
slouken@12130
    47
        SDL_Log("Accelerometer update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
slouken@12130
    48
        break;
slouken@12130
    49
    case SDL_SENSOR_GYRO:
slouken@12130
    50
        SDL_Log("Gyro update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
slouken@12130
    51
        break;
slouken@12130
    52
    default:
slouken@12130
    53
        SDL_Log("Sensor update for sensor type %s\n", GetSensorTypeString(SDL_SensorGetType(sensor)));
slouken@12130
    54
        break;
slouken@12130
    55
    }
slouken@12130
    56
}
slouken@12130
    57
slouken@12130
    58
int
slouken@12130
    59
main(int argc, char **argv)
slouken@12130
    60
{
slouken@12130
    61
    int i;
slouken@12130
    62
    int num_sensors, num_opened;
slouken@12130
    63
slouken@12130
    64
    /* Load the SDL library */
slouken@12130
    65
    if (SDL_Init(SDL_INIT_SENSOR) < 0) {
slouken@12130
    66
        SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@12130
    67
        return (1);
slouken@12130
    68
    }
slouken@12130
    69
slouken@12130
    70
    num_sensors = SDL_NumSensors();
slouken@12130
    71
    num_opened = 0;
slouken@12130
    72
slouken@12130
    73
    SDL_Log("There are %d sensors available\n", num_sensors);
slouken@12130
    74
    for (i = 0; i < num_sensors; ++i) {
slouken@12130
    75
        SDL_Log("Sensor %d: %s, type %s, platform type %d\n",
slouken@12130
    76
            SDL_SensorGetDeviceInstanceID(i),
slouken@12130
    77
            SDL_SensorGetDeviceName(i),
slouken@12130
    78
            GetSensorTypeString(SDL_SensorGetDeviceType(i)),
slouken@12130
    79
            SDL_SensorGetDeviceNonPortableType(i));
slouken@12130
    80
slouken@12130
    81
        if (SDL_SensorGetDeviceType(i) != SDL_SENSOR_UNKNOWN) {
slouken@12130
    82
            SDL_Sensor *sensor = SDL_SensorOpen(i);
slouken@12130
    83
            if (sensor == NULL) {
slouken@12130
    84
                SDL_Log("Couldn't open sensor %d: %s\n", SDL_SensorGetDeviceInstanceID(i), SDL_GetError());
slouken@12130
    85
            } else {
slouken@12130
    86
                ++num_opened;
slouken@12130
    87
            }
slouken@12130
    88
        }
slouken@12130
    89
    }
slouken@12130
    90
    SDL_Log("Opened %d sensors\n", num_opened);
slouken@12130
    91
slouken@12130
    92
    if (num_opened > 0) {
slouken@12130
    93
        SDL_bool done = SDL_FALSE;
slouken@12130
    94
        SDL_Event event;
slouken@12130
    95
slouken@12130
    96
        SDL_CreateWindow("Sensor Test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
slouken@12130
    97
        while (!done) {
slouken@12130
    98
            while (SDL_PollEvent(&event) > 0) {
slouken@12130
    99
                switch (event.type) {
slouken@12130
   100
                case SDL_SENSORUPDATE:
slouken@12130
   101
                    HandleSensorEvent(&event.sensor);
slouken@12130
   102
                    break;
slouken@12130
   103
                case SDL_MOUSEBUTTONUP:
slouken@12130
   104
                case SDL_KEYUP:
slouken@12130
   105
                case SDL_QUIT:
slouken@12130
   106
                    done = SDL_TRUE;
slouken@12130
   107
                    break;
slouken@12130
   108
                default:
slouken@12130
   109
                    break;
slouken@12130
   110
                }
slouken@12130
   111
            }
slouken@12130
   112
        }
slouken@12130
   113
    }
slouken@12130
   114
slouken@12130
   115
    SDL_Quit();
slouken@12130
   116
    return (0);
slouken@12130
   117
}