summaryrefslogtreecommitdiffstats
path: root/src/microsettings.cpp
blob: 6790582224e6811342aff7217061897037ff68ad (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/***************************************************************************
 *   Copyright (C) 2003-2005 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   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 "itemdocumentdata.h"
#include "microinfo.h"
#include "micropackage.h"
#include "pinmapping.h"


//BEGIN class VariableInfo
VariableInfo::VariableInfo()
{
	type = MicroSettings::vt_unknown;
	value = TQVariant(0);
	initAtStart = false;
	permanent = false;
}

void VariableInfo::setValue( const TQVariant & _value )
{
	value = _value;
}

TQString VariableInfo::valueAsString() const
{
	if ( value.canCast(TQVariant::String) ) return value.toString();
	if ( value.canCast(TQVariant::Int) ) return TQString::number(value.toInt());
	return "0";
}
//END class VariableInfo



//BEGIN class PinSettings
PinSettings::PinSettings()
	: TQObject()
{
	m_type = PinSettings::pt_input;
	m_state = PinSettings::ps_off;
	m_id = "pin";
}

PinSettings::PinSettings( PinSettings::pin_type _type, PinSettings::pin_state _state, const TQString &id, const TQString &port )
{
	m_type = _type;
	m_state = _state;
	m_id = id;
	m_port = port;
}


void PinSettings::setType( PinSettings::pin_type type )
{
	if ( m_type == type )
		return;
	m_type = type;
	emit settingsChanged();
}


void PinSettings::setState( PinSettings::pin_state state )
{
	if ( m_state == state)
		return;
	m_state = state;
	emit settingsChanged();
}
//END class PinSettings



//BEGIN class MicroSettings
MicroSettings::MicroSettings( MicroInfo * microInfo )
{
	_microInfo = microInfo;
	
	TQStringList portNames = _microInfo->package()->portNames();
	const TQStringList::iterator portNamesEnd = portNames.end();
	for ( TQStringList::iterator it = portNames.begin(); it != portNamesEnd; ++it )
	{
		PinSettingsList portPins;
		TQStringList pinIDs;
		pinIDs = _microInfo->package()->pinIDs( PicPin::type_bidir | PicPin::type_open, *it );
		pinIDs.sort();
		const int numPins = pinIDs.size();
		for ( int i=0; i<numPins; i++ )
		{
			PinSettings *pinSettings = new PinSettings( PinSettings::pt_input, PinSettings::ps_off, pinIDs[i], *it ); 
			m_pinSettingsList.append(pinSettings);
			portPins.append(pinSettings);
		}
		m_ports[*it] = portPins;
	}
}

MicroSettings::~MicroSettings()
{
	const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
	for ( PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
	{
		delete *it;
	}
// 	delete m_variableList;
}

void MicroSettings::setPinType( const TQString &id, PinSettings::pin_type type )
{
	PinSettings *pin = pinWithID(id);
	if (pin) pin->setType(type);
}

void MicroSettings::setPinState( const TQString &id, PinSettings::pin_state state )
{
	PinSettings *pin = pinWithID(id);
	if (pin)
		pin->setState(state);
}


PinSettings* MicroSettings::pinWithID( const TQString &id )
{
	const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
	for ( PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
	{
		if ( (*it)->id() == id ) return *it;
	}
	return 0L;
}

int MicroSettings::portState( const TQString &port )
{
	if ( microInfo()->package()->portNames().tqfindIndex(port) == -1 ) return -1;
	
	int pinPower = 1;
	int num = 0;
	
	const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
	for ( PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
	{
		if ( (*it)->port() == port )
		{
			if ( (*it)->state() == PinSettings::ps_on ) num += pinPower;
// 			cout << "(*it)->state()="<<(*it)->state()<<endl;
			pinPower *= 2;
		}
	}
	
	return num;
}

int MicroSettings::portType( const TQString &port )
{
	if ( microInfo()->package()->portNames().tqfindIndex(port) == -1 ) return -1;
	
	int pinPower = 1;
	int num = 0;
	
	const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
	for ( PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
	{
		if ( (*it)->port() == port )
		{
			if ( (*it)->type() == PinSettings::pt_input ) num += pinPower;
			pinPower *= 2;
		}
	}
	
	return num;
}

void MicroSettings::setPortState( const TQString &port, int state )
{
	PortList::iterator plit = m_ports.tqfind(port);
	if ( plit == m_ports.end() ) return;
	
	const PinSettingsList::iterator plitEnd = plit.data().end();
	for ( PinSettingsList::iterator it = plit.data().begin(); it != plitEnd; ++it )
	{
// 		cout << "state="<<state<<endl;
		(*it)->setState( (state%2 == 1) ? PinSettings::ps_on : PinSettings::ps_off );
// 		cout << "(*it)->state()="<<(*it)->state()<<endl;
		state /= 2;
	}
}

void MicroSettings::setPortType( const TQString &port, int type )
{
	PortList::iterator plit = m_ports.tqfind(port);
	if ( plit == m_ports.end() ) return;
	
	const PinSettingsList::iterator plitEnd = plit.data().end();
	for ( PinSettingsList::iterator it = plit.data().begin(); it != plitEnd; ++it )
	{
		(*it)->setType( (type%2 == 1) ? PinSettings::pt_input : PinSettings::pt_output );
		type /= 2;
	}
}


MicroData MicroSettings::microData() const
{
	MicroData data;
	data.id = microInfo()->id();
	data.pinMappings = pinMappings();
	
	const PinSettingsList::const_iterator pinListEnd = m_pinSettingsList.end();
	for ( PinSettingsList::const_iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
	{
		data.pinMap[(*it)->id()].type = (*it)->type();
		data.pinMap[(*it)->id()].state= (*it)->state();
	}
	
	const VariableMap::const_iterator variableMapEnd = m_variableMap.end();
	for ( VariableMap::const_iterator it = m_variableMap.begin(); it != variableMapEnd; ++it )
	{
		if ( it.data().permanent )
			data.variableMap[it.key()] = it.data().valueAsString();
	} 
	
	return data;
}


void MicroSettings::restoreFromMicroData( const MicroData &microData )
{
	setPinMappings( microData.pinMappings );
	
	const PinDataMap::const_iterator pinMapEnd = microData.pinMap.end();
	for ( PinDataMap::const_iterator it = microData.pinMap.begin(); it != pinMapEnd; ++it )
	{
		PinSettings *pin = pinWithID(it.key());
		if (pin)
		{
			pin->setState( it.data().state );
			pin->setType( it.data().type );
		}
	}
	
	const TQStringMap::const_iterator variableMapEnd = microData.variableMap.end();
	for ( TQStringMap::const_iterator it = microData.variableMap.begin(); it != variableMapEnd; ++it )
	{
		setVariable( it.key(), it.data(), true );
	}
}


void MicroSettings::setVariable( const TQString &name, TQVariant value, bool permanent )
{
	if ( name.isNull() ) return;
	VariableMap::iterator it = m_variableMap.tqfind(name);
	if ( it != m_variableMap.end() )
	{
		it.data().setValue(value);
		it.data().permanent = permanent;
		it.data().initAtStart = permanent;
	}
	else
	{
		VariableInfo info;
		info.setValue(value);
		info.permanent = permanent;
		info.initAtStart = permanent;
		m_variableMap[name] = info;
	}
}


TQStringList MicroSettings::variableNames()
{
	TQStringList list;
	const VariableMap::iterator variableMapEnd = m_variableMap.end();
	for ( VariableMap::iterator it = m_variableMap.begin(); it != variableMapEnd; ++it )
	{
		list += it.key();
	}
	return list;
}


VariableInfo* MicroSettings::variableInfo( const TQString &name )
{
	if ( name.isNull() ) return 0l;
	VariableMap::iterator it = m_variableMap.tqfind(name);
	if ( it != m_variableMap.end() ) {
		return &(it.data());
	} else {
		return 0l;
	}
}


bool MicroSettings::deleteVariable( const TQString &name )
{
	if ( name.isNull() ) return false;
	VariableMap::iterator it = m_variableMap.tqfind(name);
	if ( it != m_variableMap.end() )
	{
		m_variableMap.erase(it);
		return true;
	} else {
		return false;
	}
}


void MicroSettings::removeAllVariables()
{
	m_variableMap.clear();
}


PinMapping MicroSettings::pinMapping( const TQString & id ) const
{
	return m_pinMappings[id];
}


void MicroSettings::setPinMappings( const PinMappingMap & pinMappings )
{
	m_pinMappings = pinMappings;
	emit pinMappingsChanged();
}


PinMappingMap MicroSettings::pinMappings() const
{
	return m_pinMappings;
}
//END class MicroSettings