mirror of
https://github.com/SFML/SFML.git
synced 2024-12-01 15:51:04 +08:00
Make Sensor::Type a scoped enumeration
This commit is contained in:
parent
3fb0ffce51
commit
eab8f426e7
@ -41,18 +41,19 @@ namespace sf::Sensor
|
|||||||
/// \brief Sensor type
|
/// \brief Sensor type
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
enum Type
|
enum class Type
|
||||||
{
|
{
|
||||||
Accelerometer, //!< Measures the raw acceleration (m/s^2)
|
Accelerometer, //!< Measures the raw acceleration (m/s^2)
|
||||||
Gyroscope, //!< Measures the raw rotation rates (degrees/s)
|
Gyroscope, //!< Measures the raw rotation rates (degrees/s)
|
||||||
Magnetometer, //!< Measures the ambient magnetic field (micro-teslas)
|
Magnetometer, //!< Measures the ambient magnetic field (micro-teslas)
|
||||||
Gravity, //!< Measures the direction and intensity of gravity, independent of device acceleration (m/s^2)
|
Gravity, //!< Measures the direction and intensity of gravity, independent of device acceleration (m/s^2)
|
||||||
UserAcceleration, //!< Measures the direction and intensity of device acceleration, independent of the gravity (m/s^2)
|
UserAcceleration, //!< Measures the direction and intensity of device acceleration, independent of the gravity (m/s^2)
|
||||||
Orientation, //!< Measures the absolute 3D orientation (degrees)
|
Orientation //!< Measures the absolute 3D orientation (degrees)
|
||||||
|
|
||||||
Count //!< Keep last -- the total number of sensor types
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||||
|
static constexpr unsigned int Count{6}; //!< The total number of sensor types
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
/// \brief Check if a sensor is available on the underlying platform
|
/// \brief Check if a sensor is available on the underlying platform
|
||||||
///
|
///
|
||||||
@ -123,16 +124,16 @@ SFML_WINDOW_API Vector3f getValue(Type sensor);
|
|||||||
///
|
///
|
||||||
/// Usage example:
|
/// Usage example:
|
||||||
/// \code
|
/// \code
|
||||||
/// if (sf::Sensor::isAvailable(sf::Sensor::Gravity))
|
/// if (sf::Sensor::isAvailable(sf::Sensor::Type::Gravity))
|
||||||
/// {
|
/// {
|
||||||
/// // gravity sensor is available
|
/// // gravity sensor is available
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// // enable the gravity sensor
|
/// // enable the gravity sensor
|
||||||
/// sf::Sensor::setEnabled(sf::Sensor::Gravity, true);
|
/// sf::Sensor::setEnabled(sf::Sensor::Type::Gravity, true);
|
||||||
///
|
///
|
||||||
/// // get the current value of gravity
|
/// // get the current value of gravity
|
||||||
/// sf::Vector3f gravity = sf::Sensor::getValue(sf::Sensor::Gravity);
|
/// sf::Vector3f gravity = sf::Sensor::getValue(sf::Sensor::Type::Gravity);
|
||||||
/// \endcode
|
/// \endcode
|
||||||
///
|
///
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
|
@ -31,6 +31,8 @@
|
|||||||
|
|
||||||
#include <android/looper.h>
|
#include <android/looper.h>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#if defined(__clang__) || defined(__GNUC__)
|
#if defined(__clang__) || defined(__GNUC__)
|
||||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||||
#endif
|
#endif
|
||||||
@ -147,7 +149,7 @@ const ASensor* SensorImpl::getDefaultSensor(Sensor::Type sensor)
|
|||||||
ASENSOR_TYPE_LINEAR_ACCELERATION,
|
ASENSOR_TYPE_LINEAR_ACCELERATION,
|
||||||
ASENSOR_TYPE_ORIENTATION};
|
ASENSOR_TYPE_ORIENTATION};
|
||||||
|
|
||||||
int type = types[sensor];
|
int type = types[static_cast<int>(sensor)];
|
||||||
|
|
||||||
// Retrieve the default sensor matching this type
|
// Retrieve the default sensor matching this type
|
||||||
return ASensorManager_getDefaultSensor(sensorManager, type);
|
return ASensorManager_getDefaultSensor(sensorManager, type);
|
||||||
@ -161,48 +163,48 @@ int SensorImpl::processSensorEvents(int /* fd */, int /* events */, void* /* sen
|
|||||||
|
|
||||||
while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0)
|
while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0)
|
||||||
{
|
{
|
||||||
unsigned int type = Sensor::Count;
|
std::optional<Sensor::Type> type;
|
||||||
Vector3f data;
|
Vector3f data;
|
||||||
|
|
||||||
switch (event.type)
|
switch (event.type)
|
||||||
{
|
{
|
||||||
case ASENSOR_TYPE_ACCELEROMETER:
|
case ASENSOR_TYPE_ACCELEROMETER:
|
||||||
type = Sensor::Accelerometer;
|
type = Sensor::Type::Accelerometer;
|
||||||
data.x = event.acceleration.x;
|
data.x = event.acceleration.x;
|
||||||
data.y = event.acceleration.y;
|
data.y = event.acceleration.y;
|
||||||
data.z = event.acceleration.z;
|
data.z = event.acceleration.z;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ASENSOR_TYPE_GYROSCOPE:
|
case ASENSOR_TYPE_GYROSCOPE:
|
||||||
type = Sensor::Gyroscope;
|
type = Sensor::Type::Gyroscope;
|
||||||
data.x = event.vector.x;
|
data.x = event.vector.x;
|
||||||
data.y = event.vector.y;
|
data.y = event.vector.y;
|
||||||
data.z = event.vector.z;
|
data.z = event.vector.z;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ASENSOR_TYPE_MAGNETIC_FIELD:
|
case ASENSOR_TYPE_MAGNETIC_FIELD:
|
||||||
type = Sensor::Magnetometer;
|
type = Sensor::Type::Magnetometer;
|
||||||
data.x = event.magnetic.x;
|
data.x = event.magnetic.x;
|
||||||
data.y = event.magnetic.y;
|
data.y = event.magnetic.y;
|
||||||
data.z = event.magnetic.z;
|
data.z = event.magnetic.z;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ASENSOR_TYPE_GRAVITY:
|
case ASENSOR_TYPE_GRAVITY:
|
||||||
type = Sensor::Gravity;
|
type = Sensor::Type::Gravity;
|
||||||
data.x = event.vector.x;
|
data.x = event.vector.x;
|
||||||
data.y = event.vector.y;
|
data.y = event.vector.y;
|
||||||
data.z = event.vector.z;
|
data.z = event.vector.z;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ASENSOR_TYPE_LINEAR_ACCELERATION:
|
case ASENSOR_TYPE_LINEAR_ACCELERATION:
|
||||||
type = Sensor::UserAcceleration;
|
type = Sensor::Type::UserAcceleration;
|
||||||
data.x = event.acceleration.x;
|
data.x = event.acceleration.x;
|
||||||
data.y = event.acceleration.y;
|
data.y = event.acceleration.y;
|
||||||
data.z = event.acceleration.z;
|
data.z = event.acceleration.z;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ASENSOR_TYPE_ORIENTATION:
|
case ASENSOR_TYPE_ORIENTATION:
|
||||||
type = Sensor::Orientation;
|
type = Sensor::Type::Orientation;
|
||||||
data.x = event.vector.x;
|
data.x = event.vector.x;
|
||||||
data.y = event.vector.y;
|
data.y = event.vector.y;
|
||||||
data.z = event.vector.z;
|
data.z = event.vector.z;
|
||||||
@ -210,10 +212,10 @@ int SensorImpl::processSensorEvents(int /* fd */, int /* events */, void* /* sen
|
|||||||
}
|
}
|
||||||
|
|
||||||
// An unknown sensor event has been detected, we don't know how to process it
|
// An unknown sensor event has been detected, we don't know how to process it
|
||||||
if (type == Sensor::Count)
|
if (!type)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
sensorData[type] = data;
|
sensorData[static_cast<int>(*type)] = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -45,17 +45,17 @@ SensorManager& SensorManager::getInstance()
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool SensorManager::isAvailable(Sensor::Type sensor)
|
bool SensorManager::isAvailable(Sensor::Type sensor)
|
||||||
{
|
{
|
||||||
return m_sensors[sensor].available;
|
return m_sensors[static_cast<int>(sensor)].available;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
void SensorManager::setEnabled(Sensor::Type sensor, bool enabled)
|
void SensorManager::setEnabled(Sensor::Type sensor, bool enabled)
|
||||||
{
|
{
|
||||||
if (m_sensors[sensor].available)
|
if (m_sensors[static_cast<int>(sensor)].available)
|
||||||
{
|
{
|
||||||
m_sensors[sensor].enabled = enabled;
|
m_sensors[static_cast<int>(sensor)].enabled = enabled;
|
||||||
m_sensors[sensor].sensor.setEnabled(enabled);
|
m_sensors[static_cast<int>(sensor)].sensor.setEnabled(enabled);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -68,14 +68,14 @@ void SensorManager::setEnabled(Sensor::Type sensor, bool enabled)
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool SensorManager::isEnabled(Sensor::Type sensor) const
|
bool SensorManager::isEnabled(Sensor::Type sensor) const
|
||||||
{
|
{
|
||||||
return m_sensors[sensor].enabled;
|
return m_sensors[static_cast<int>(sensor)].enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
Vector3f SensorManager::getValue(Sensor::Type sensor) const
|
Vector3f SensorManager::getValue(Sensor::Type sensor) const
|
||||||
{
|
{
|
||||||
return m_sensors[sensor].value;
|
return m_sensors[static_cast<int>(sensor)].value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ SensorManager::SensorManager()
|
|||||||
SensorImpl::initialize();
|
SensorImpl::initialize();
|
||||||
|
|
||||||
// Per sensor initialization
|
// Per sensor initialization
|
||||||
for (int i = 0; i < Sensor::Count; ++i)
|
for (unsigned int i = 0; i < Sensor::Count; ++i)
|
||||||
{
|
{
|
||||||
// Check which sensors are available
|
// Check which sensors are available
|
||||||
m_sensors[i].available = SensorImpl::isAvailable(static_cast<Sensor::Type>(i));
|
m_sensors[i].available = SensorImpl::isAvailable(static_cast<Sensor::Type>(i));
|
||||||
|
@ -63,18 +63,18 @@ bool SensorImpl::isAvailable(Sensor::Type sensor)
|
|||||||
{
|
{
|
||||||
switch (sensor)
|
switch (sensor)
|
||||||
{
|
{
|
||||||
case Sensor::Accelerometer:
|
case Sensor::Type::Accelerometer:
|
||||||
return [SFAppDelegate getInstance].motionManager.accelerometerAvailable;
|
return [SFAppDelegate getInstance].motionManager.accelerometerAvailable;
|
||||||
|
|
||||||
case Sensor::Gyroscope:
|
case Sensor::Type::Gyroscope:
|
||||||
return [SFAppDelegate getInstance].motionManager.gyroAvailable;
|
return [SFAppDelegate getInstance].motionManager.gyroAvailable;
|
||||||
|
|
||||||
case Sensor::Magnetometer:
|
case Sensor::Type::Magnetometer:
|
||||||
return [SFAppDelegate getInstance].motionManager.magnetometerAvailable;
|
return [SFAppDelegate getInstance].motionManager.magnetometerAvailable;
|
||||||
|
|
||||||
case Sensor::Gravity:
|
case Sensor::Type::Gravity:
|
||||||
case Sensor::UserAcceleration:
|
case Sensor::Type::UserAcceleration:
|
||||||
case Sensor::Orientation:
|
case Sensor::Type::Orientation:
|
||||||
return [SFAppDelegate getInstance].motionManager.deviceMotionAvailable;
|
return [SFAppDelegate getInstance].motionManager.deviceMotionAvailable;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -96,21 +96,21 @@ bool SensorImpl::open(Sensor::Type sensor)
|
|||||||
constexpr NSTimeInterval updateInterval = 1. / 60.;
|
constexpr NSTimeInterval updateInterval = 1. / 60.;
|
||||||
switch (sensor)
|
switch (sensor)
|
||||||
{
|
{
|
||||||
case Sensor::Accelerometer:
|
case Sensor::Type::Accelerometer:
|
||||||
[SFAppDelegate getInstance].motionManager.accelerometerUpdateInterval = updateInterval;
|
[SFAppDelegate getInstance].motionManager.accelerometerUpdateInterval = updateInterval;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Gyroscope:
|
case Sensor::Type::Gyroscope:
|
||||||
[SFAppDelegate getInstance].motionManager.gyroUpdateInterval = updateInterval;
|
[SFAppDelegate getInstance].motionManager.gyroUpdateInterval = updateInterval;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Magnetometer:
|
case Sensor::Type::Magnetometer:
|
||||||
[SFAppDelegate getInstance].motionManager.magnetometerUpdateInterval = updateInterval;
|
[SFAppDelegate getInstance].motionManager.magnetometerUpdateInterval = updateInterval;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Gravity:
|
case Sensor::Type::Gravity:
|
||||||
case Sensor::UserAcceleration:
|
case Sensor::Type::UserAcceleration:
|
||||||
case Sensor::Orientation:
|
case Sensor::Type::Orientation:
|
||||||
[SFAppDelegate getInstance].motionManager.deviceMotionUpdateInterval = updateInterval;
|
[SFAppDelegate getInstance].motionManager.deviceMotionUpdateInterval = updateInterval;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -137,35 +137,35 @@ Vector3f SensorImpl::update()
|
|||||||
|
|
||||||
switch (m_sensor)
|
switch (m_sensor)
|
||||||
{
|
{
|
||||||
case Sensor::Accelerometer:
|
case Sensor::Type::Accelerometer:
|
||||||
// Acceleration is given in G, convert to m/s^2
|
// Acceleration is given in G, convert to m/s^2
|
||||||
value.x = static_cast<float>(manager.accelerometerData.acceleration.x * 9.81);
|
value.x = static_cast<float>(manager.accelerometerData.acceleration.x * 9.81);
|
||||||
value.y = static_cast<float>(manager.accelerometerData.acceleration.y * 9.81);
|
value.y = static_cast<float>(manager.accelerometerData.acceleration.y * 9.81);
|
||||||
value.z = static_cast<float>(manager.accelerometerData.acceleration.z * 9.81);
|
value.z = static_cast<float>(manager.accelerometerData.acceleration.z * 9.81);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Gyroscope:
|
case Sensor::Type::Gyroscope:
|
||||||
// Rotation rates are given in rad/s, convert to deg/s
|
// Rotation rates are given in rad/s, convert to deg/s
|
||||||
value.x = toDegrees(static_cast<float>(manager.gyroData.rotationRate.x));
|
value.x = toDegrees(static_cast<float>(manager.gyroData.rotationRate.x));
|
||||||
value.y = toDegrees(static_cast<float>(manager.gyroData.rotationRate.y));
|
value.y = toDegrees(static_cast<float>(manager.gyroData.rotationRate.y));
|
||||||
value.z = toDegrees(static_cast<float>(manager.gyroData.rotationRate.z));
|
value.z = toDegrees(static_cast<float>(manager.gyroData.rotationRate.z));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Magnetometer:
|
case Sensor::Type::Magnetometer:
|
||||||
// Magnetic field is given in microteslas
|
// Magnetic field is given in microteslas
|
||||||
value.x = static_cast<float>(manager.magnetometerData.magneticField.x);
|
value.x = static_cast<float>(manager.magnetometerData.magneticField.x);
|
||||||
value.y = static_cast<float>(manager.magnetometerData.magneticField.y);
|
value.y = static_cast<float>(manager.magnetometerData.magneticField.y);
|
||||||
value.z = static_cast<float>(manager.magnetometerData.magneticField.z);
|
value.z = static_cast<float>(manager.magnetometerData.magneticField.z);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::UserAcceleration:
|
case Sensor::Type::UserAcceleration:
|
||||||
// User acceleration is given in G, convert to m/s^2
|
// User acceleration is given in G, convert to m/s^2
|
||||||
value.x = static_cast<float>(manager.deviceMotion.userAcceleration.x * 9.81);
|
value.x = static_cast<float>(manager.deviceMotion.userAcceleration.x * 9.81);
|
||||||
value.y = static_cast<float>(manager.deviceMotion.userAcceleration.y * 9.81);
|
value.y = static_cast<float>(manager.deviceMotion.userAcceleration.y * 9.81);
|
||||||
value.z = static_cast<float>(manager.deviceMotion.userAcceleration.z * 9.81);
|
value.z = static_cast<float>(manager.deviceMotion.userAcceleration.z * 9.81);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Orientation:
|
case Sensor::Type::Orientation:
|
||||||
// Absolute rotation (Euler) angles are given in radians, convert to degrees
|
// Absolute rotation (Euler) angles are given in radians, convert to degrees
|
||||||
value.x = toDegrees(static_cast<float>(manager.deviceMotion.attitude.yaw));
|
value.x = toDegrees(static_cast<float>(manager.deviceMotion.attitude.yaw));
|
||||||
value.y = toDegrees(static_cast<float>(manager.deviceMotion.attitude.pitch));
|
value.y = toDegrees(static_cast<float>(manager.deviceMotion.attitude.pitch));
|
||||||
@ -189,30 +189,30 @@ void SensorImpl::setEnabled(bool enabled)
|
|||||||
|
|
||||||
switch (m_sensor)
|
switch (m_sensor)
|
||||||
{
|
{
|
||||||
case Sensor::Accelerometer:
|
case Sensor::Type::Accelerometer:
|
||||||
if (enabled)
|
if (enabled)
|
||||||
[[SFAppDelegate getInstance].motionManager startAccelerometerUpdates];
|
[[SFAppDelegate getInstance].motionManager startAccelerometerUpdates];
|
||||||
else
|
else
|
||||||
[[SFAppDelegate getInstance].motionManager stopAccelerometerUpdates];
|
[[SFAppDelegate getInstance].motionManager stopAccelerometerUpdates];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Gyroscope:
|
case Sensor::Type::Gyroscope:
|
||||||
if (enabled)
|
if (enabled)
|
||||||
[[SFAppDelegate getInstance].motionManager startGyroUpdates];
|
[[SFAppDelegate getInstance].motionManager startGyroUpdates];
|
||||||
else
|
else
|
||||||
[[SFAppDelegate getInstance].motionManager stopGyroUpdates];
|
[[SFAppDelegate getInstance].motionManager stopGyroUpdates];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Magnetometer:
|
case Sensor::Type::Magnetometer:
|
||||||
if (enabled)
|
if (enabled)
|
||||||
[[SFAppDelegate getInstance].motionManager startMagnetometerUpdates];
|
[[SFAppDelegate getInstance].motionManager startMagnetometerUpdates];
|
||||||
else
|
else
|
||||||
[[SFAppDelegate getInstance].motionManager stopMagnetometerUpdates];
|
[[SFAppDelegate getInstance].motionManager stopMagnetometerUpdates];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Sensor::Gravity:
|
case Sensor::Type::Gravity:
|
||||||
case Sensor::UserAcceleration:
|
case Sensor::Type::UserAcceleration:
|
||||||
case Sensor::Orientation:
|
case Sensor::Type::Orientation:
|
||||||
// these 3 sensors all share the same implementation, so we must disable
|
// these 3 sensors all share the same implementation, so we must disable
|
||||||
// it only if the three sensors are disabled
|
// it only if the three sensors are disabled
|
||||||
if (enabled)
|
if (enabled)
|
||||||
|
Loading…
Reference in New Issue
Block a user