Make Sensor::Type a scoped enumeration

This commit is contained in:
kimci86 2023-12-10 17:59:12 +01:00 committed by Chris Thrasher
parent 3fb0ffce51
commit eab8f426e7
4 changed files with 51 additions and 48 deletions

View File

@ -41,18 +41,19 @@ namespace sf::Sensor
/// \brief Sensor type
///
////////////////////////////////////////////////////////////
enum Type
enum class Type
{
Accelerometer, //!< Measures the raw acceleration (m/s^2)
Gyroscope, //!< Measures the raw rotation rates (degrees/s)
Magnetometer, //!< Measures the ambient magnetic field (micro-teslas)
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)
Orientation, //!< Measures the absolute 3D orientation (degrees)
Count //!< Keep last -- the total number of sensor types
Orientation //!< Measures the absolute 3D orientation (degrees)
};
// 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
///
@ -123,16 +124,16 @@ SFML_WINDOW_API Vector3f getValue(Type sensor);
///
/// Usage example:
/// \code
/// if (sf::Sensor::isAvailable(sf::Sensor::Gravity))
/// if (sf::Sensor::isAvailable(sf::Sensor::Type::Gravity))
/// {
/// // gravity sensor is available
/// }
///
/// // 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
/// sf::Vector3f gravity = sf::Sensor::getValue(sf::Sensor::Gravity);
/// sf::Vector3f gravity = sf::Sensor::getValue(sf::Sensor::Type::Gravity);
/// \endcode
///
////////////////////////////////////////////////////////////

View File

@ -31,6 +31,8 @@
#include <android/looper.h>
#include <optional>
#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
@ -147,7 +149,7 @@ const ASensor* SensorImpl::getDefaultSensor(Sensor::Type sensor)
ASENSOR_TYPE_LINEAR_ACCELERATION,
ASENSOR_TYPE_ORIENTATION};
int type = types[sensor];
int type = types[static_cast<int>(sensor)];
// Retrieve the default sensor matching this 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)
{
unsigned int type = Sensor::Count;
std::optional<Sensor::Type> type;
Vector3f data;
switch (event.type)
{
case ASENSOR_TYPE_ACCELEROMETER:
type = Sensor::Accelerometer;
type = Sensor::Type::Accelerometer;
data.x = event.acceleration.x;
data.y = event.acceleration.y;
data.z = event.acceleration.z;
break;
case ASENSOR_TYPE_GYROSCOPE:
type = Sensor::Gyroscope;
type = Sensor::Type::Gyroscope;
data.x = event.vector.x;
data.y = event.vector.y;
data.z = event.vector.z;
break;
case ASENSOR_TYPE_MAGNETIC_FIELD:
type = Sensor::Magnetometer;
type = Sensor::Type::Magnetometer;
data.x = event.magnetic.x;
data.y = event.magnetic.y;
data.z = event.magnetic.z;
break;
case ASENSOR_TYPE_GRAVITY:
type = Sensor::Gravity;
type = Sensor::Type::Gravity;
data.x = event.vector.x;
data.y = event.vector.y;
data.z = event.vector.z;
break;
case ASENSOR_TYPE_LINEAR_ACCELERATION:
type = Sensor::UserAcceleration;
type = Sensor::Type::UserAcceleration;
data.x = event.acceleration.x;
data.y = event.acceleration.y;
data.z = event.acceleration.z;
break;
case ASENSOR_TYPE_ORIENTATION:
type = Sensor::Orientation;
type = Sensor::Type::Orientation;
data.x = event.vector.x;
data.y = event.vector.y;
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
if (type == Sensor::Count)
if (!type)
continue;
sensorData[type] = data;
sensorData[static_cast<int>(*type)] = data;
}
return 1;

View File

@ -45,17 +45,17 @@ SensorManager& SensorManager::getInstance()
////////////////////////////////////////////////////////////
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)
{
if (m_sensors[sensor].available)
if (m_sensors[static_cast<int>(sensor)].available)
{
m_sensors[sensor].enabled = enabled;
m_sensors[sensor].sensor.setEnabled(enabled);
m_sensors[static_cast<int>(sensor)].enabled = enabled;
m_sensors[static_cast<int>(sensor)].sensor.setEnabled(enabled);
}
else
{
@ -68,14 +68,14 @@ void SensorManager::setEnabled(Sensor::Type sensor, bool enabled)
////////////////////////////////////////////////////////////
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
{
return m_sensors[sensor].value;
return m_sensors[static_cast<int>(sensor)].value;
}
@ -98,7 +98,7 @@ SensorManager::SensorManager()
SensorImpl::initialize();
// 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
m_sensors[i].available = SensorImpl::isAvailable(static_cast<Sensor::Type>(i));

View File

@ -63,18 +63,18 @@ bool SensorImpl::isAvailable(Sensor::Type sensor)
{
switch (sensor)
{
case Sensor::Accelerometer:
case Sensor::Type::Accelerometer:
return [SFAppDelegate getInstance].motionManager.accelerometerAvailable;
case Sensor::Gyroscope:
case Sensor::Type::Gyroscope:
return [SFAppDelegate getInstance].motionManager.gyroAvailable;
case Sensor::Magnetometer:
case Sensor::Type::Magnetometer:
return [SFAppDelegate getInstance].motionManager.magnetometerAvailable;
case Sensor::Gravity:
case Sensor::UserAcceleration:
case Sensor::Orientation:
case Sensor::Type::Gravity:
case Sensor::Type::UserAcceleration:
case Sensor::Type::Orientation:
return [SFAppDelegate getInstance].motionManager.deviceMotionAvailable;
default:
@ -96,21 +96,21 @@ bool SensorImpl::open(Sensor::Type sensor)
constexpr NSTimeInterval updateInterval = 1. / 60.;
switch (sensor)
{
case Sensor::Accelerometer:
case Sensor::Type::Accelerometer:
[SFAppDelegate getInstance].motionManager.accelerometerUpdateInterval = updateInterval;
break;
case Sensor::Gyroscope:
case Sensor::Type::Gyroscope:
[SFAppDelegate getInstance].motionManager.gyroUpdateInterval = updateInterval;
break;
case Sensor::Magnetometer:
case Sensor::Type::Magnetometer:
[SFAppDelegate getInstance].motionManager.magnetometerUpdateInterval = updateInterval;
break;
case Sensor::Gravity:
case Sensor::UserAcceleration:
case Sensor::Orientation:
case Sensor::Type::Gravity:
case Sensor::Type::UserAcceleration:
case Sensor::Type::Orientation:
[SFAppDelegate getInstance].motionManager.deviceMotionUpdateInterval = updateInterval;
break;
@ -137,35 +137,35 @@ Vector3f SensorImpl::update()
switch (m_sensor)
{
case Sensor::Accelerometer:
case Sensor::Type::Accelerometer:
// Acceleration is given in G, convert to m/s^2
value.x = static_cast<float>(manager.accelerometerData.acceleration.x * 9.81);
value.y = static_cast<float>(manager.accelerometerData.acceleration.y * 9.81);
value.z = static_cast<float>(manager.accelerometerData.acceleration.z * 9.81);
break;
case Sensor::Gyroscope:
case Sensor::Type::Gyroscope:
// Rotation rates are given in rad/s, convert to deg/s
value.x = toDegrees(static_cast<float>(manager.gyroData.rotationRate.x));
value.y = toDegrees(static_cast<float>(manager.gyroData.rotationRate.y));
value.z = toDegrees(static_cast<float>(manager.gyroData.rotationRate.z));
break;
case Sensor::Magnetometer:
case Sensor::Type::Magnetometer:
// Magnetic field is given in microteslas
value.x = static_cast<float>(manager.magnetometerData.magneticField.x);
value.y = static_cast<float>(manager.magnetometerData.magneticField.y);
value.z = static_cast<float>(manager.magnetometerData.magneticField.z);
break;
case Sensor::UserAcceleration:
case Sensor::Type::UserAcceleration:
// User acceleration is given in G, convert to m/s^2
value.x = static_cast<float>(manager.deviceMotion.userAcceleration.x * 9.81);
value.y = static_cast<float>(manager.deviceMotion.userAcceleration.y * 9.81);
value.z = static_cast<float>(manager.deviceMotion.userAcceleration.z * 9.81);
break;
case Sensor::Orientation:
case Sensor::Type::Orientation:
// Absolute rotation (Euler) angles are given in radians, convert to degrees
value.x = toDegrees(static_cast<float>(manager.deviceMotion.attitude.yaw));
value.y = toDegrees(static_cast<float>(manager.deviceMotion.attitude.pitch));
@ -189,30 +189,30 @@ void SensorImpl::setEnabled(bool enabled)
switch (m_sensor)
{
case Sensor::Accelerometer:
case Sensor::Type::Accelerometer:
if (enabled)
[[SFAppDelegate getInstance].motionManager startAccelerometerUpdates];
else
[[SFAppDelegate getInstance].motionManager stopAccelerometerUpdates];
break;
case Sensor::Gyroscope:
case Sensor::Type::Gyroscope:
if (enabled)
[[SFAppDelegate getInstance].motionManager startGyroUpdates];
else
[[SFAppDelegate getInstance].motionManager stopGyroUpdates];
break;
case Sensor::Magnetometer:
case Sensor::Type::Magnetometer:
if (enabled)
[[SFAppDelegate getInstance].motionManager startMagnetometerUpdates];
else
[[SFAppDelegate getInstance].motionManager stopMagnetometerUpdates];
break;
case Sensor::Gravity:
case Sensor::UserAcceleration:
case Sensor::Orientation:
case Sensor::Type::Gravity:
case Sensor::Type::UserAcceleration:
case Sensor::Type::Orientation:
// these 3 sensors all share the same implementation, so we must disable
// it only if the three sensors are disabled
if (enabled)