From 9074519f752a92b74795a06927b6fb085c2fafb2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Sat, 1 Aug 2015 13:25:05 +0200 Subject: [PATCH] Implement periodic effects via SDL2 --- CMakeLists.txt | 1 + sdl2ffbconditioneffect.h | 3 +- sdl2ffbeffect.cpp | 4 +- sdl2ffbeffectfactory.cpp | 2 + sdl2ffbeffectfactory.h | 1 + sdl2ffbperiodiceffect.cpp | 101 ++++++++++++++++++++++++++++++++++++++ sdl2ffbperiodiceffect.h | 21 ++++++++ 7 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 sdl2ffbperiodiceffect.cpp create mode 100644 sdl2ffbperiodiceffect.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a18ce44..964f671 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ if (SDL2_FOUND) sdl2ffbeffect.cpp sdl2ffbconstanteffect.cpp sdl2ffbconditioneffect.cpp + sdl2ffbperiodiceffect.cpp sdl2deviceprober.cpp) endif() diff --git a/sdl2ffbconditioneffect.h b/sdl2ffbconditioneffect.h index ed2345f..178abc8 100644 --- a/sdl2ffbconditioneffect.h +++ b/sdl2ffbconditioneffect.h @@ -11,9 +11,10 @@ public: virtual SDL_HapticEffect* createFFstruct(); inline const std::shared_ptr parameters() const { return m_params; } bool setParameters(std::shared_ptr params); - bool setParameters(std::shared_ptr params); private: + bool setParameters(std::shared_ptr params); + std::shared_ptr m_params; }; diff --git a/sdl2ffbeffect.cpp b/sdl2ffbeffect.cpp index cc407a9..2e22b92 100644 --- a/sdl2ffbeffect.cpp +++ b/sdl2ffbeffect.cpp @@ -65,8 +65,8 @@ bool SDL2FFBEffect::checkGenericParameters(const std::shared_ptrdoSanityChecks) return true; - if (!checkBoundsInclusive(params->direction, 0, 35999)) { - QMessageBox::warning(nullptr, CAPTION, "Direction must be within <0; 35999)"); + if (!checkBoundsInclusive(params->direction, 0, 36000)) { + QMessageBox::warning(nullptr, CAPTION, "Direction must be within <0; 36000)"); return false; } diff --git a/sdl2ffbeffectfactory.cpp b/sdl2ffbeffectfactory.cpp index 19fc4b2..c719700 100644 --- a/sdl2ffbeffectfactory.cpp +++ b/sdl2ffbeffectfactory.cpp @@ -9,6 +9,8 @@ std::shared_ptr SDL2FFBEffectFactory::createEffect(FFBEffectTypes typ return std::shared_ptr(new SDL2FFBConditionEffect()); case FFBEffectTypes::CONSTANT: return std::shared_ptr(new SDL2FFBConstantEffect()); + case FFBEffectTypes::PERIODIC: + return std::shared_ptr(new SDL2FFBPeriodicEffect()); default: return std::shared_ptr(new FFBNullEffect()); } diff --git a/sdl2ffbeffectfactory.h b/sdl2ffbeffectfactory.h index 14453e4..998742b 100644 --- a/sdl2ffbeffectfactory.h +++ b/sdl2ffbeffectfactory.h @@ -5,6 +5,7 @@ #include "ffbnulleffect.h" #include "sdl2ffbconstanteffect.h" #include "sdl2ffbconditioneffect.h" +#include "sdl2ffbperiodiceffect.h" class SDL2FFBEffectFactory { diff --git a/sdl2ffbperiodiceffect.cpp b/sdl2ffbperiodiceffect.cpp new file mode 100644 index 0000000..5559c3a --- /dev/null +++ b/sdl2ffbperiodiceffect.cpp @@ -0,0 +1,101 @@ +#include "sdl2ffbperiodiceffect.h" +#include "globalsettings.h" +#include + +SDL2FFBPeriodicEffect::SDL2FFBPeriodicEffect() : + SDL2FFBEffect(FFBEffectTypes::PERIODIC) +{} + +SDL_HapticEffect* SDL2FFBPeriodicEffect::createFFstruct() +{ + SDL_HapticEffect* effect = SDL2FFBEffect::createFFstruct(); + + if (effect == nullptr) + return nullptr; + + switch (m_params->waveform) { + case PeriodicWaveforms::SINE: + effect->type = SDL_HAPTIC_SINE; + break; + case PeriodicWaveforms::TRIANGLE: + effect->type = SDL_HAPTIC_TRIANGLE; + break; + case PeriodicWaveforms::SAW_UP: + effect->type = SDL_HAPTIC_SAWTOOTHUP; + break; + case PeriodicWaveforms::SAW_DOWN: + effect->type = SDL_HAPTIC_SAWTOOTHDOWN; + default: + delete effect; + return nullptr; + } + + effect->periodic.direction.type = SDL_HAPTIC_POLAR; + effect->periodic.direction.dir[0] = m_params->direction; + + if (m_params->replayLength == 0) + effect->periodic.length = SDL_HAPTIC_INFINITY; + else + effect->periodic.length = m_params->replayLength; + + effect->periodic.delay = m_params->replayDelay; + + effect->periodic.period = m_params->period; + effect->periodic.magnitude = m_params->magnitude; + effect->periodic.offset = m_params->offset; + effect->periodic.phase = m_params->phase; + + return effect; +} + +bool SDL2FFBPeriodicEffect::setParameters(const std::shared_ptr params) +{ + try { + const std::shared_ptr iParams = std::dynamic_pointer_cast(params); + return setParameters(iParams); + } catch (std::bad_cast& ) { + return false; + } +} + +bool SDL2FFBPeriodicEffect::setParameters(const std::shared_ptr params) +{ + if (!GlobalSettings::GS()->doSanityChecks) + return true; + + if (!checkGenericParameters(params)) + return false; + + if (!checkEnvelopeParameters(params->attackLength, params->attackLevel, params->fadeLength, params->fadeLevel)) + return false; + + if (!checkBoundsInclusive(params->period, 0, 36000)) { + QMessageBox::warning(nullptr, CAPTION, "Period must be within <0; 36000>"); + return false; + } + + if (!checkBoundsInclusive(params->magnitude, -0x7FFF, 0x7FFF)) { + QMessageBox::warning(nullptr, CAPTION, "Magnitude must be within <-32767; 32767>"); + return false; + } + + if (!checkBoundsInclusive(params->offset, -0x7FFF, 0x7FFF)) { + QMessageBox::warning(nullptr, CAPTION, "Offset must be within <-32767; 32767>"); + return false; + } + + if (!checkBoundsInclusive(params->phase, 0, 36000)) { + QMessageBox::warning(nullptr, CAPTION, "Phase must be withing <0; 36000>"); + return false; + } + + if (params->waveform == PeriodicWaveforms::NONE || + params->waveform == PeriodicWaveforms::SQUARE) { + QMessageBox::warning(nullptr, CAPTION, "Unsupported waveform"); + return false; + } + + m_params = params; + return true; +} + diff --git a/sdl2ffbperiodiceffect.h b/sdl2ffbperiodiceffect.h new file mode 100644 index 0000000..aa15774 --- /dev/null +++ b/sdl2ffbperiodiceffect.h @@ -0,0 +1,21 @@ +#ifndef SDL2FFBPERIODIEFFECT_H +#define SDL2FFBPERIODIEFFECT_H + +#include "sdl2ffbeffect.h" +#include "ffbperiodiceffectparameters.h" + +class SDL2FFBPeriodicEffect : public SDL2FFBEffect +{ +public: + SDL2FFBPeriodicEffect(); + SDL_HapticEffect* createFFstruct(); + inline const std::shared_ptr parameters() const { return m_params; } + bool setParameters(const std::shared_ptr params); + +private: + bool setParameters(const std::shared_ptr params); + + std::shared_ptr m_params; +}; + +#endif // SDL2FFBEPERIODIFFECT_H -- 2.43.5