File: rotary.cpp

package info (click to toggle)
mixxx 1.11.0~dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 96,840 kB
  • ctags: 37,182
  • sloc: cpp: 210,326; xml: 167,911; ansic: 24,880; sh: 12,876; python: 9,407; makefile: 566; php: 57
file content (122 lines) | stat: -rw-r--r-- 3,491 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/***************************************************************************
                          rotary.cpp  -  description
                             -------------------
    begin                : Tue Sep 21 2004
    copyright            : (C) 2004 by Tue Haste Andersen
    email                : haste@diku.dk
***************************************************************************/

/***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#include "rotary.h"
#include "mathstuff.h"
#include "controlobject.h"
#include <QDebug>

Rotary::Rotary()
{
    m_dCalibration = 1.;
    m_dLastValue = 0.;

    m_iFilterLength = kiRotaryFilterMaxLen;
    m_iFilterPos = 0;
    m_pFilter = new double[m_iFilterLength];
    for (int i=0; i<m_iFilterLength; ++i)
        m_pFilter[i] = 0.;
}

Rotary::~Rotary()
{
    delete [] m_pFilter;
}

/* Note: There's probably a bug in this function (or this class) somewhere.
   The filter function seems to be the cause of the "drifting" bug in the Hercules stuff.
   What happens is that filter() gets called to do some magic to a value that's returned
   from the Hercules device, and that magic adds "momentum" to it's motion (ie. it doesn't
   stop dead when you stop spinning the jog wheels.) The problem with this "magic" is that
   when herculeslinux.cpp passes the filtered value off to the wheel ControlObject (or what
   have you), the ControlObject's internal value never goes back to zero properly.
   - Albert (March 13, 2007)
 */
double Rotary::filter(double dValue)
{
    // Update filter buffer
    m_pFilter[m_iFilterPos] = dValue/m_dCalibration;
    m_iFilterPos = (m_iFilterPos+1)%m_iFilterLength;

    double dMagnitude = 0.;
    for (int i=0; i<m_iFilterLength; i++)
    {
        dMagnitude += m_pFilter[i];
    }
    dMagnitude /= (double)m_iFilterLength;
    //qDebug() << "filter in " << dValue << ", out " << dMagnitude;

    m_dLastValue = dMagnitude;

    return dMagnitude;
}

double Rotary::fillBuffer(double dValue)
{
	for (int i=0; i<m_iFilterLength; ++i)
	{
		m_pFilter[i] = dValue/m_dCalibration;
	}
	return dValue/m_dCalibration;
}

void Rotary::calibrate(double dValue)
{
    m_dCalibration += dValue;
    m_iCalibrationCount += 1;
}

void Rotary::calibrateStart()
{
    // Reset calibration data
    m_dCalibration = 0.;
    m_iCalibrationCount = 0;
}

double Rotary::calibrateEnd()
{
    m_dCalibration /= (double)m_iCalibrationCount;

    qDebug() << "Calibration " << m_dCalibration << ", count " << m_iCalibrationCount;

    return m_dCalibration;
}

void Rotary::setCalibration(double c)
{
    m_dCalibration = c;
}

double Rotary::getCalibration()
{
    return m_dCalibration;
}

void Rotary::setFilterLength(int i)
{
    if (i>kiRotaryFilterMaxLen)
        m_iFilterLength = kiRotaryFilterMaxLen;
    else if (i<1)
        m_iFilterLength = 1;
    else
        m_iFilterLength = i;
}

int Rotary::getFilterLength()
{
    return  m_iFilterLength;
}