Skip to content

Commit

Permalink
Refactor JoySensor float/double and const usage
Browse files Browse the repository at this point in the history
Consistently use float for values from SDL and double for calculated
values. Add const where possible.
  • Loading branch information
mmmaisel committed Apr 30, 2022
1 parent 2df2b28 commit 813086c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
70 changes: 37 additions & 33 deletions src/joysensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,27 +206,31 @@ JoySensorDirection JoySensor::getCurrentDirection()
return m_current_direction;
}

JoySensorType JoySensor::getType() { return m_type; }
JoySensorType JoySensor::getType() const { return m_type; }

/**
* @brief Get the assigned dead zone value.
* @return Assigned dead zone value
* @return Assigned dead zone value in degree.
*/
float JoySensor::getDeadZone()
double JoySensor::getDeadZone() const
{
return m_dead_zone * 180 / M_PI;
}

/**
* @brief Get the assigned diagonal range value.
* @return Assigned diagonal range.
* @return Assigned diagonal range in degree.
*/
float JoySensor::getDiagonalRange()
double JoySensor::getDiagonalRange() const
{
return m_diagonal_range * 180 / M_PI;
}

float JoySensor::getMaxZone()
/**
* @brief Get the assigned max zone value.
* @return Assigned max zone value in degree.
*/
double JoySensor::getMaxZone() const
{
return m_max_zone * 180 / M_PI;
}
Expand All @@ -235,21 +239,21 @@ float JoySensor::getMaxZone()
* @brief Get the value for the corresponding X axis.
* @return X axis value in m/s^2 for accelerometer or rad/s for gyroscope.
*/
float JoySensor::getXCoordinate() { return m_current_value[0]; }
float JoySensor::getXCoordinate() const { return m_current_value[0]; }

/**
* @brief Get the value for the corresponding Y axis.
* @return X axis value in m/s^2 for accelerometer or rad/s for gyroscope.
*/
float JoySensor::getYCoordinate() { return m_current_value[1]; }
float JoySensor::getYCoordinate() const { return m_current_value[1]; }

/**
* @brief Get the value for the corresponding Z axis.
* @return Z axis value in m/s^2 for accelerometer or rad/s for gyroscope.
*/
float JoySensor::getZCoordinate() { return m_current_value[2]; }
float JoySensor::getZCoordinate() const { return m_current_value[2]; }

unsigned int JoySensor::getSensorDelay() { return m_sensor_delay; }
unsigned int JoySensor::getSensorDelay() const { return m_sensor_delay; }

QString JoySensor::sensorTypeName() const {
if (m_type == ACCELEROMETER)
Expand Down Expand Up @@ -302,7 +306,7 @@ double JoySensor::getDistanceFromDeadZone() const
double JoySensor::getDistanceFromDeadZone(float x, float y, float z) const
{
double distance = calculateDistance(x, y, z);
return qBound(0.0, distance - m_dead_zone, double(m_max_zone));
return qBound(0.0, distance - m_dead_zone, m_max_zone);
}

/**
Expand Down Expand Up @@ -336,9 +340,9 @@ double JoySensor::calculateXDistanceFromDeadZone(
{
double discriminant = m_dead_zone*m_dead_zone - y*y - z*z;
if(discriminant <= 0)
return std::min(abs(x), m_max_zone);
return std::min(double(abs(x)), m_max_zone);
else
return std::min(abs(x)-sqrt(discriminant), double(m_max_zone));
return std::min(double(abs(x))-sqrt(discriminant), m_max_zone);
}

/**
Expand Down Expand Up @@ -372,9 +376,9 @@ double JoySensor::calculateYDistanceFromDeadZone(
{
double discriminant = m_dead_zone*m_dead_zone - x*x - z*z;
if(discriminant <= 0)
return std::min(abs(y), m_max_zone);
return std::min(double(abs(y)), m_max_zone);
else
return std::min(abs(y)-sqrt(discriminant), double(m_max_zone));
return std::min(double(abs(y))-sqrt(discriminant), m_max_zone);
}

/**
Expand Down Expand Up @@ -408,9 +412,9 @@ double JoySensor::calculateZDistanceFromDeadZone(
{
double discriminant = m_dead_zone*m_dead_zone - x*x - y*y;
if(discriminant <= 0)
return std::min(abs(z), m_max_zone);
return std::min(double(abs(z)), m_max_zone);
else
return std::min(abs(z)-sqrt(discriminant), double(m_max_zone));
return std::min(double(abs(z))-sqrt(discriminant), m_max_zone);
}

/**
Expand Down Expand Up @@ -547,7 +551,7 @@ void JoySensor::resetCalibration()
m_calibrated = false;
}

void JoySensor::getCalibration(float* data)
void JoySensor::getCalibration(float* data) const
{
data[0] = m_calibration_value[0];
data[1] = m_calibration_value[1];
Expand Down Expand Up @@ -588,16 +592,16 @@ void JoySensor::setSensorName(QString tempName)
}
}

QString JoySensor::getSensorName() { return m_sensor_name; }
QString JoySensor::getSensorName() const { return m_sensor_name; }

bool JoySensor::isDefault()
bool JoySensor::isDefault() const
{
bool value = true;
value = value && qFuzzyCompare(double(getDeadZone()), GlobalVariables::JoySensor::DEFAULTDEADZONE);
value = value && qFuzzyCompare(getDeadZone(), GlobalVariables::JoySensor::DEFAULTDEADZONE);
if (m_type == ACCELEROMETER)
value = value && qFuzzyCompare(double(getMaxZone()), GlobalVariables::JoySensor::ACCEL_MAX);
value = value && qFuzzyCompare(getMaxZone(), GlobalVariables::JoySensor::ACCEL_MAX);
else
value = value && qFuzzyCompare(double(getMaxZone()), GlobalVariables::JoySensor::GYRO_MAX);
value = value && qFuzzyCompare(getMaxZone(), GlobalVariables::JoySensor::GYRO_MAX);

value = value && qFuzzyCompare(getDiagonalRange(), GlobalVariables::JoySensor::DEFAULTDIAGONALRANGE);
value = value && (m_sensor_delay == GlobalVariables::JoySensor::DEFAULTSENSORDELAY);
Expand All @@ -612,7 +616,7 @@ bool JoySensor::isDefault()
}
void JoySensor::setDefaultSensorName(QString tempname) { m_default_sensor_name = tempname; }

QString JoySensor::getDefaultSensorName() { return m_default_sensor_name; }
QString JoySensor::getDefaultSensorName() const { return m_default_sensor_name; }

/**
* @brief Take a XML stream and set the sensor and direction button properties
Expand Down Expand Up @@ -677,22 +681,22 @@ void JoySensor::readConfig(QXmlStreamReader *xml)
* to an XML stream.
* @param QXmlStreamWriter instance that will be used to write a profile.
*/
void JoySensor::writeConfig(QXmlStreamWriter *xml)
void JoySensor::writeConfig(QXmlStreamWriter *xml) const
{
if (!isDefault())
{
xml->writeStartElement("sensor");
xml->writeAttribute("type", QString::number(m_type));

if (!qFuzzyCompare(double(getDeadZone()), GlobalVariables::JoySensor::DEFAULTDEADZONE))
if (!qFuzzyCompare(getDeadZone(), GlobalVariables::JoySensor::DEFAULTDEADZONE))
xml->writeTextElement("deadZone", QString::number(getDeadZone()));

if (!qFuzzyCompare(double(m_max_zone), (m_type == ACCELEROMETER
if (!qFuzzyCompare(m_max_zone, (m_type == ACCELEROMETER
? GlobalVariables::JoySensor::ACCEL_MAX
: GlobalVariables::JoySensor::GYRO_MAX)))
xml->writeTextElement("maxZone", QString::number(getMaxZone()));

if (!qFuzzyCompare(double(m_diagonal_range), GlobalVariables::JoySensor::DEFAULTDIAGONALRANGE))
if (!qFuzzyCompare(m_diagonal_range, GlobalVariables::JoySensor::DEFAULTDIAGONALRANGE))
xml->writeTextElement("diagonalRange", QString::number(getDiagonalRange()));

if (m_sensor_delay > GlobalVariables::JoySensor::DEFAULTSENSORDELAY)
Expand Down Expand Up @@ -741,7 +745,7 @@ void JoySensor::reset()
resetButtons();
}

void JoySensor::setDeadZone(float value)
void JoySensor::setDeadZone(double value)
{
value = abs(value / 180 * M_PI);
if (!qFuzzyCompare(value, m_dead_zone) && (value <= m_max_zone))
Expand All @@ -752,7 +756,7 @@ void JoySensor::setDeadZone(float value)
}
}

void JoySensor::setMaxZone(float value)
void JoySensor::setMaxZone(double value)
{
value = abs(value / 180 * M_PI);
if (!qFuzzyCompare(value, m_max_zone) && (value > m_dead_zone))
Expand All @@ -767,7 +771,7 @@ void JoySensor::setMaxZone(float value)
* @brief Set the diagonal range value for a sensor.
* @param Value between 1 - 90.
*/
void JoySensor::setDiagonalRange(float value)
void JoySensor::setDiagonalRange(double value)
{
if (value < 1)
value = 1;
Expand Down Expand Up @@ -814,7 +818,7 @@ void JoySensor::delayTimerExpired()
* @param Pointer to an array of three JoySensorButton pointers in which
* the results are stored.
*/
void JoySensor::determineSensorEvent(JoySensorButton **eventbutton)
void JoySensor::determineSensorEvent(JoySensorButton **eventbutton) const
{
if (m_current_direction & SENSOR_LEFT)
eventbutton[0] = m_buttons.value(SENSOR_LEFT);
Expand Down Expand Up @@ -930,7 +934,7 @@ JoySensorDirection JoySensor::calculateAccelerometerDirection()
* There are two cases here because the spherical layers overlap if the diagonal
* angle is larger then 45 degree.
*/
JoySensorDirection JoySensor::calculateGyroscopeDirection()
JoySensorDirection JoySensor::calculateGyroscopeDirection() const
{
double distance = calculateDistance();
if (distance < m_dead_zone)
Expand Down
42 changes: 21 additions & 21 deletions src/joysensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ class JoySensor : public QObject

JoySensorDirection getCurrentDirection();

JoySensorType getType();
float getDeadZone();
float getDiagonalRange();
float getMaxZone();
float getXCoordinate();
float getYCoordinate();
float getZCoordinate();
unsigned int getSensorDelay();
JoySensorType getType() const;
double getDeadZone() const;
double getDiagonalRange() const;
double getMaxZone() const;
float getXCoordinate() const;
float getYCoordinate() const;
float getZCoordinate() const;
unsigned int getSensorDelay() const;
QString sensorTypeName() const;

void resetButtons();
Expand All @@ -83,19 +83,19 @@ class JoySensor : public QObject

bool isCalibrated() const;
void resetCalibration();
void getCalibration(float* data);
void getCalibration(float* data) const;
void setCalibration(float x0, float y0, float z0);

QHash<JoySensorDirection, JoySensorButton *> *getButtons();
JoySensorButton *getDirectionButton(JoySensorDirection direction);

QString getSensorName();
QString getSensorName() const;

bool isDefault();
bool isDefault() const;
virtual void setDefaultSensorName(QString tempname);
virtual QString getDefaultSensorName();
virtual QString getDefaultSensorName() const;
void readConfig(QXmlStreamReader *xml);
void writeConfig(QXmlStreamWriter *xml);
void writeConfig(QXmlStreamWriter *xml) const;

SetJoystick *getParentSet();

Expand All @@ -113,9 +113,9 @@ class JoySensor : public QObject

public slots:
void reset();
void setDeadZone(float value);
void setMaxZone(float value);
void setDiagonalRange(float value);
void setDeadZone(double value);
void setMaxZone(double value);
void setDiagonalRange(double value);
void setSensorName(QString tempName);
void setSensorDelay(unsigned int value);
void establishPropertyUpdatedConnection();
Expand All @@ -129,10 +129,10 @@ class JoySensor : public QObject
static const double SHOCK_TAU;

void populateButtons();
void determineSensorEvent(JoySensorButton **eventbutton);
void determineSensorEvent(JoySensorButton **eventbutton) const;
JoySensorDirection calculateSensorDirection();
JoySensorDirection calculateAccelerometerDirection();
JoySensorDirection calculateGyroscopeDirection();
JoySensorDirection calculateGyroscopeDirection() const;
void createDeskEvent(JoySensorDirection direction, bool ignoresets = false);

JoySensorType m_type;
Expand All @@ -150,9 +150,9 @@ class JoySensor : public QObject
bool m_pending_ignore_sets;
PT1 m_shock_filter;
size_t m_shock_suppress_count;
float m_dead_zone;
float m_diagonal_range;
float m_max_zone;
double m_dead_zone;
double m_diagonal_range;
double m_max_zone;
unsigned int m_sensor_delay;

QString m_sensor_name;
Expand Down

0 comments on commit 813086c

Please sign in to comment.