summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/ecclockinput.cpp
blob: 7f1ec9c2ff4e33ad4a03ca9be33428d0aa0c07d1 (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
/***************************************************************************
 *   Copyright (C) 2003-2005 by David Saxton                               *
 *   [email protected]                                                    *
 *                                                                         *
 *   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 "ecclockinput.h"

#include "logic.h"
#include "libraryitem.h"
#include "simulator.h"

#include <klocale.h>
#include <qpainter.h>

#include <cmath>

static inline uint roundDouble( const double x )
{
	return uint(std::floor(x+0.5));
}

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

LibraryItem* ECClockInput::libraryItem()
{
	return new LibraryItem(
		QString("ec/clock_input"),
		i18n("Clock Input"),
		i18n("Logic"),
		"clockinput.png",
		LibraryItem::lit_component,
		ECClockInput::construct );
}

ECClockInput::ECClockInput( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, (id) ? id : "clock_input" )
{
	m_name = i18n("Clock Input");
	m_desc = i18n("A square-wave generator, outputing logical high/low at repeating time intervals.");
	setSize( -16, -8, 32, 16 );
	
	m_lastSetTime = 0;
	m_time = 0;
	m_high_time = 0;
	m_low_time = 0;
	m_period = 0;
	m_bSetStepCallbacks = true;
	m_pSimulator = Simulator::self();
	
	for ( unsigned i = 0; i < 1000; i++ )
	{
		ComponentCallback * ccb = new ComponentCallback( this, (VoidCallbackPtr)(&ECClockInput::stepCallback) );
		m_pComponentCallback[i] = new LinkedList<ComponentCallback>(ccb);
	}

	init1PinRight();
	m_pOut = createLogicOut( m_pPNode[0], false );
	
	createProperty( "low-time", Variant::Type::Double );
	property("low-time")->setUnit("S");
	property("low-time")->setCaption( i18n("Low Time") );
	property("low-time")->setMinValue(1.0/LOGIC_UPDATE_RATE);
	property("low-time")->setValue(0.5);
	
	createProperty( "high-time", Variant::Type::Double );
	property("high-time")->setUnit("S");
	property("high-time")->setCaption( i18n("High Time") );
	property("high-time")->setMinValue(1.0/LOGIC_UPDATE_RATE);
	property("high-time")->setValue(0.5);
	
	addDisplayText( "freq", QRect( -16, -24, 32, 14 ), "", false );
}


ECClockInput::~ECClockInput()
{
	for ( unsigned i = 0; i < 1000; i++ )
	{
		delete m_pComponentCallback[i]->data();
		delete m_pComponentCallback[i];
	}
}


void ECClockInput::dataChanged()
{
	m_high_time = roundDouble(dataDouble("high-time")*LOGIC_UPDATE_RATE);
	m_low_time = roundDouble(dataDouble("low-time")*LOGIC_UPDATE_RATE);
	m_period = m_low_time+m_high_time;
	
	const double frequency = 1./(dataDouble("high-time")+dataDouble("low-time"));
	QString display = QString::number( frequency / getMultiplier(frequency), 'g', 3 ) + getNumberMag(frequency) + "Hz";
	setDisplayText( "freq", display );
	
	bool setStepCallbacks = m_period > 100;
	if ( setStepCallbacks != m_bSetStepCallbacks )
	{
		m_bSetStepCallbacks = setStepCallbacks;
		if (setStepCallbacks)
			m_pSimulator->detachComponentCallbacks(this);
		else
			m_pSimulator->attachComponentCallback( this, (VoidCallbackPtr)(&ECClockInput::stepLogic) );
	}
	
	m_bLastStepCallbackOut = false;
	m_lastSetTime = m_pSimulator->time();
}


void ECClockInput::stepLogic()
{
	m_pOut->setHigh( m_time>m_low_time );
	
	if ( ++m_time > m_period ) {
		m_time -= int(m_time/m_period)*m_period;
	}
}


void ECClockInput::stepCallback()
{
	m_pOut->setHigh(m_bLastStepCallbackOut);
	m_bLastStepCallbackOut = !m_bLastStepCallbackOut;
}


void ECClockInput::stepNonLogic()
{
	if (!m_bSetStepCallbacks)
		return;
	
	bool addingHigh = !m_bLastStepCallbackOut;
	
	//TODO 100 number shouldn't be hard-coded
	long long lowerTime = m_pSimulator->time();
	long long upperTime = lowerTime + 100;
	
	long long upTo = m_lastSetTime;
	
	while ( upTo + (addingHigh?m_high_time:m_low_time) < upperTime )
	{
		upTo += addingHigh ? m_high_time : m_low_time;
		addingHigh = !addingHigh;
		
		long long at = upTo-lowerTime;
		if ( at >= 0 && at < 100 )
			m_pSimulator->addStepCallback( at, m_pComponentCallback[at] );
	}
	
	m_lastSetTime = upTo;
}


void ECClockInput::drawShape( QPainter &p )
{
	initPainter(p);
	
	int _x = (int)x()-10;
	int _y = (int)y()-8;
	
	p.drawRect( _x-6, _y, 32, 16 );
	
	p.drawLine( _x,		_y+8,	_x,		_y+4 );
	p.drawLine( _x,		_y+4,	_x+4,	_y+4 );
	p.drawLine( _x+4,	_y+4,	_x+4,	_y+12 );
	p.drawLine( _x+4,	_y+12,	_x+8,	_y+12 );
	p.drawLine( _x+8,	_y+12,	_x+8,	_y+4 );
	p.drawLine( _x+8,	_y+4,	_x+12,	_y+4 );
	p.drawLine( _x+12,	_y+4,	_x+12,	_y+12 );
	p.drawLine( _x+12,	_y+12,	_x+16,	_y+12 );
	p.drawLine( _x+16,	_y+12,	_x+16,	_y+4 );
	p.drawLine( _x+16,	_y+4,	_x+20,	_y+4 );
	p.drawLine( _x+20,	_y+4,	_x+20,	_y+8 );
	
	deinitPainter(p);
}