summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/rotoswitch.cpp
blob: 96a76814667190fa007a7f8eca0d71ed1654dfbb (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/***************************************************************************
 *   Copyright (C) 2005 by John Myers                                      *
 *   electronerd@electronerdia.net                                         *
 *                                                                         *
 *   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 "rotoswitch.h"

#include "canvasitemparts.h"
#include "ecnode.h"
#include "libraryitem.h"
#include "switch.h"

#include <tdelocale.h>
#include <tqpainter.h>
#include <cmath>
#include <assert.h>

#include <kdebug.h>

//BEGIN class ECRotoSwitch
Item* ECRotoSwitch::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
    return new ECRotoSwitch( (ICNDocument*)itemDocument, newItem, id );
}

LibraryItem* ECRotoSwitch::libraryItem()
{
    return new LibraryItem(
                            TQString("ec/roto_switch"),
                            i18n("Rotary"),
                            i18n("Switches"),
                            "rotary.png",
                            LibraryItem::lit_component,
                            ECRotoSwitch::construct );
}


ECRotoSwitch::ECRotoSwitch( ICNDocument *icnDocument, bool newItem, const char *id )
: Component( icnDocument, newItem, id ? id : "roto_switch" ),
m_numPositions(0)
{
// 	m_name = i18n("Rotary Switch(WIP)");
	m_name = i18n("Rotary Switch");
    m_desc = i18n("Rotary Switch"); ///< \todo better description for rotoswitch
    TQPointArray pa;
    pa.makeArc( -_pinInnerRadius, -_pinInnerRadius, 2*_pinInnerRadius, 2*_pinInnerRadius , 0, 16*360 );
    setItemPoints( pa );
    //setSize( -64, -64, 128, 128 );
    
    //half the side length of the buttons
    int buttonRadius = 10;
    addButton( "go_left", TQRect( -_pinOuterRadius/3-buttonRadius, _pinOuterRadius-3*buttonRadius, 2*buttonRadius, 2*buttonRadius ), "<", false );
    addButton( "go_right", TQRect(_pinOuterRadius/3-buttonRadius, _pinOuterRadius-3*buttonRadius, 2*buttonRadius, 2*buttonRadius ), ">", false );
    
    /*Variant * v = createProperty( "button_map", Variant::Type::String );
    v->setCaption( i18n("Button Map") );
    v->setAdvanced(true);
    const TQString defButtonMap("SSSSSSSSSSSM");
    v->setValue(defButtonMap);
    */
    Variant * v = createProperty( "num_positions", Variant::Type::Int );
    v->setCaption( i18n("Number of Positions") );
    v->setAdvanced(false);
    v->setValue(6);
    v->setMinValue(3);
    m_inNode = createPin(0,height()/2,270,"in");    

    v = createProperty( "bounce", Variant::Type::Bool );
    v->setCaption("Bounce");
    v->setAdvanced(true);
    v->setValue(false);
    

    v = createProperty( "bounce_period", Variant::Type::Double );
    v->setCaption("Bounce Period");
    v->setAdvanced(true);
    v->setUnit("s");
    v->setValue(5e-3);
    

    v = createProperty( "cur_position", Variant::Type::Int );
    v->setHidden( true );
    v->setValue( 0 );
    
    //v = createProperty( "left_momentary", Variant::Type::Bool );
    //v->setCaption(i18n("Left Momentary" ) );
    //v->setValue(false);
}


ECRotoSwitch::~ECRotoSwitch()
{
}


void ECRotoSwitch::dataChanged()
{
    bool bounce = dataBool("bounce");
    int bouncePeriod_ms = int(dataDouble("bounce_period")*1e3);
    m_curPosition = dataInt( "cur_position" );
    setUpSwitches();
    if(m_positions[m_curPosition].posSwitch->state() != Switch::Closed)
    {
        m_positions[m_curPosition].posSwitch->setState(Switch::Closed);
    }
    for(int i = 0; i < m_numPositions; i++)
    {
        m_positions[i].posSwitch->setBounce( bounce, bouncePeriod_ms );
    }
}

inline int roundTo10(int a){return ((a/10)+(a%10<5?0:1))*10;}
void ECRotoSwitch::drawShape( TQPainter &p )
{
    initPainter(p);
    
    
    int cx = static_cast<int>(x());
    int cy =  static_cast<int>(y());

    const int rotorRadius = 5;

    
    //draw the rotor
    p.drawEllipse(cx - rotorRadius, cy-rotorRadius, 2*rotorRadius, 2*rotorRadius);
    //and its connection
    p.drawLine(cx, cy+rotorRadius, cx, cy+_pinInnerRadius);
    
    //draw the output positions
    double angleBetweenPositions = (4*M_PI/3)/(m_numPositions - 1);
    //kdDebug() << "drawShape: " << bigRadius << " " << angleBetweenPositions << endl;
    
    /// \internal \brief Round to the nearest multiple of 8    
#define round_8(a)      (((a) > 0) ? int(((a)+4)/8)*8 : int(((a)-4)/8)*8)
    for(int i = 0; i < m_numPositions ; i++)
    {
        double angle = (7*M_PI/6) - (i * angleBetweenPositions);
        int contactX = static_cast<int>(_contactRingRadius * cos(angle));
        int contactY = static_cast<int>(_contactRingRadius * sin(angle));

        p.drawEllipse(cx+contactX-_contactRadius, cy-contactY-_contactRadius, 2*_contactRadius, 2*_contactRadius);
        int pinX, pinY;
        switch(m_positions[i].pinAngle)
        {
        case 180:
            pinX = _pinInnerRadius;
            pinY = round_8(contactY);
            break;
        case 90:
            pinX = round_8(contactX);
            pinY = _pinInnerRadius;
            break;
        case 0:
            pinX = -_pinInnerRadius;
            pinY = round_8(contactY);
            break;
        default:
            assert(!"Bad pin angle");
        }
        p.drawLine(cx+contactX, cy-contactY, cx+pinX, cy-pinY);
        //kdDebug() << contactX <<", "<< contactY <<endl;
    }
#undef round_8
    //draw the connection to the selected position
    double angle = (7*M_PI/6) - (m_curPosition * angleBetweenPositions);
    int contactX = static_cast<int>(_contactRingRadius * cos(angle));
    int contactY = static_cast<int>(_contactRingRadius * sin(angle));
    int rotorX = static_cast<int>(rotorRadius * cos(angle));
    int rotorY = static_cast<int>(rotorRadius * sin(angle));
    p.drawLine(cx+rotorX, cy-rotorY, cx+contactX, cy-contactY);
    
    
    deinitPainter(p);
}

void ECRotoSwitch::buttonStateChanged( const TQString & id, bool state )
{
    SwitchPosition& curSP = m_positions[m_curPosition];
    int nextPos = m_curPosition;
    if(m_numPositions < 2)
    {
        return;
    }
    if(!state) //release
    {
        if(!curSP.isMomentary)
        {
            return;
        }
        
        if(m_curPosition == 0)
        {
            nextPos = m_curPosition + 1;
        }
        else if(m_curPosition == m_numPositions - 1)
        {
            nextPos = m_curPosition - 1;
        }
        
    }
    else //press
    {
        if(id == "go_left" && m_curPosition > 0)
        {
            nextPos = m_curPosition - 1;
        }
        else if(id == "go_right" && m_curPosition < m_numPositions - 1)
        {
            nextPos = m_curPosition + 1;
        }

    }
    if(nextPos != m_curPosition)
    {
        SwitchPosition& nextSP = m_positions[nextPos];
        
        curSP.posSwitch->setState(Switch::Open);
        nextSP.posSwitch->setState(Switch::Closed);
        
        m_curPosition = nextPos;
        
        property( "cur_position" )->setValue( m_curPosition );
    }
}


/*!
    Set up the switches according to the button_map 
 * 
 * 
 */
void ECRotoSwitch::setUpSwitches()
{
    if( dataInt("num_positions") == m_numPositions )
    {
        // number of positions didn't change, so we don't have to do anything.
        return;
    }
    //this uses the _old_ value of m_numPositions!
    for(int i=0; i<m_numPositions; i++)
    {
        SwitchPosition& sp = m_positions[i];
        TQString pinName = TQString("pin_%1").arg(i);
        removeNode(pinName);
        removeSwitch(sp.posSwitch);
    }
    
    m_numPositions = dataInt("num_positions");
    if(m_curPosition >= m_numPositions )
    {
        setActivePosition( m_numPositions - 1 );
    }
    m_positions.clear();///\todo readjust old pins        
    m_positions.reserve(m_numPositions);
    double angleBetweenPositions = (4*M_PI/3)/(m_numPositions - 1);
    //kdDebug() << "setUpSwitches: " << bigRadius << " " << angleBetweenPositions << endl;
    for( int i = 0; i < m_numPositions; i++)
    {
        double angle = (7*M_PI/6) - (i * angleBetweenPositions);
        int contactX = static_cast<int>(_contactRingRadius * cos(angle));
        int contactY = static_cast<int>(_contactRingRadius * sin(angle));
        
        SwitchPosition sp;
        if(angle > 3*M_PI/4)
        {
            sp.pinAngle = 0;
            contactX = -_pinOuterRadius;
        }
        else if(angle > M_PI/4)
        {
            sp.pinAngle = 90;
            contactY=_pinOuterRadius;
        }
        else 
        {
            sp.pinAngle = 180;
            contactX = _pinOuterRadius;
        }
        // kdDebug() << contactX <<", "<< contactY <<endl;
        
        
        sp.node = createPin(contactX,-contactY,sp.pinAngle,TQString("pin_%1").arg(i));
        sp.posSwitch = createSwitch(m_inNode, sp.node, true);
        sp.isMomentary = false;//(map[i] == 'M');
        m_positions.push_back(sp);
    }
    updateAttachedPositioning();

	// redraw ourself
	setChanged();
}

/*!
 * Set the current position to \c newPosition updating the state of the switch. 
 * \c m_curPosition must reference a valid position to switch away from
 * 
 * \param newPosition the position to switch to
 */
void ECRotoSwitch::setActivePosition(int newPosition)
{        
    SwitchPosition& curSP = m_positions[m_curPosition];
    SwitchPosition& nextSP = m_positions[newPosition];
    
    curSP.posSwitch->setState(Switch::Open);
    nextSP.posSwitch->setState(Switch::Closed);
    
    m_curPosition = newPosition;
    
    property( "cur_position" )->setValue( m_curPosition );
    
}
//END class ECRotoSwitch