Skip to content

Commit

Permalink
[udev] Protect motor states with a mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
garbear committed Aug 30, 2016
1 parent 5ab8d4a commit 7362313
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
40 changes: 29 additions & 11 deletions src/api/udev/JoystickUdev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,21 @@ void CJoystickUdev::Deinitialize(void)

void CJoystickUdev::ProcessEvents(void)
{
uint32_t oldStrength = static_cast<uint32_t>(m_previousMotors[MOTOR_STRONG]) +
static_cast<uint32_t>(m_previousMotors[MOTOR_WEAK]);
uint32_t newStrength = static_cast<uint32_t>(m_motors[MOTOR_STRONG]) +
static_cast<uint32_t>(m_motors[MOTOR_WEAK]);
using namespace P8PLATFORM;

std::array<uint16_t, MOTOR_COUNT> motors;
std::array<uint16_t, MOTOR_COUNT> previousMotors;

{
CLockObject lock(m_mutex);
motors = m_motors;
previousMotors = m_previousMotors;
}

uint32_t oldStrength = static_cast<uint32_t>(previousMotors[MOTOR_STRONG]) +
static_cast<uint32_t>(previousMotors[MOTOR_WEAK]);
uint32_t newStrength = static_cast<uint32_t>(motors[MOTOR_STRONG]) +
static_cast<uint32_t>(motors[MOTOR_WEAK]);

bool bWasPlaying = (oldStrength > 0);
bool bIsPlaying = (newStrength > 0);
Expand All @@ -117,7 +128,7 @@ void CJoystickUdev::ProcessEvents(void)
}
else if (!bWasPlaying && bIsPlaying)
{
UpdateMotorState();
UpdateMotorState(motors);

// Play effect
Play(true);
Expand All @@ -130,10 +141,13 @@ void CJoystickUdev::ProcessEvents(void)
else
{
if (oldStrength != newStrength)
UpdateMotorState();
UpdateMotorState(motors);
}

m_previousMotors = m_motors;
{
CLockObject lock(m_mutex);
m_previousMotors = motors;
}
}

void CJoystickUdev::Play(bool bPlayStop)
Expand All @@ -148,16 +162,16 @@ void CJoystickUdev::Play(bool bPlayStop)
esyslog("[udev]: Failed to play rumble effect on \"%s\"", Name().c_str());
}

void CJoystickUdev::UpdateMotorState()
void CJoystickUdev::UpdateMotorState(const std::array<uint16_t, MOTOR_COUNT>& motors)
{
struct ff_effect e = { };

int old_effect = m_has_set_ff ? m_effect : -1;

e.type = FF_RUMBLE;
e.id = old_effect;
e.u.rumble.strong_magnitude = m_motors[MOTOR_STRONG];
e.u.rumble.weak_magnitude = m_motors[MOTOR_WEAK];
e.u.rumble.strong_magnitude = motors[MOTOR_STRONG];
e.u.rumble.weak_magnitude = motors[MOTOR_WEAK];

if (ioctl(m_fd, EVIOCSFF, &e) < 0)
{
Expand Down Expand Up @@ -332,7 +346,9 @@ bool CJoystickUdev::GetProperties()

bool CJoystickUdev::SetMotor(unsigned int motorIndex, float magnitude)
{
if (!m_bInitialized)
using namespace P8PLATFORM;

if (!m_bInitialized)
return false;

if (motorIndex >= MotorCount() || magnitude < 0.0f)
Expand All @@ -343,6 +359,8 @@ bool CJoystickUdev::SetMotor(unsigned int motorIndex, float magnitude)

uint16_t strength = std::min(0xffff, static_cast<int>(magnitude * 0xffff));

CLockObject lock(m_mutex);

m_motors[motorIndex] = strength;

return true;
Expand Down
5 changes: 4 additions & 1 deletion src/api/udev/JoystickUdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

#include "api/Joystick.h"

#include "p8-platform/threads/mutex.h"

#include <array>
#include <linux/input.h>
#include <sys/types.h>
Expand Down Expand Up @@ -74,7 +76,7 @@ namespace JOYSTICK
bool SetMotor(unsigned int motorIndex, float magnitude);

private:
void UpdateMotorState();
void UpdateMotorState(const std::array<uint16_t, MOTOR_COUNT>& motors);
void Play(bool bPlayStop);

struct Axis
Expand All @@ -100,5 +102,6 @@ namespace JOYSTICK
std::map<unsigned int, Axis> m_axes_bind; // Maps keycodes -> axis and axis info
std::array<uint16_t, MOTOR_COUNT> m_motors;
std::array<uint16_t, MOTOR_COUNT> m_previousMotors;
P8PLATFORM::CMutex m_mutex;
};
}

0 comments on commit 7362313

Please sign in to comment.