KTechlab – circuit simulator for microcontrollers and electronics
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

microsettings.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /***************************************************************************
  2. * Copyright (C) 2003-2005 by David Saxton *
  3. * david@bluehaze.org *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. ***************************************************************************/
  10. #include "itemdocumentdata.h"
  11. #include "microinfo.h"
  12. #include "micropackage.h"
  13. #include "pinmapping.h"
  14. //BEGIN class VariableInfo
  15. VariableInfo::VariableInfo()
  16. {
  17. type = MicroSettings::vt_unknown;
  18. value = TQVariant(0);
  19. initAtStart = false;
  20. permanent = false;
  21. }
  22. void VariableInfo::setValue( const TQVariant & _value )
  23. {
  24. value = _value;
  25. }
  26. TQString VariableInfo::valueAsString() const
  27. {
  28. if ( value.canCast(TQVariant::String) ) return value.toString();
  29. if ( value.canCast(TQVariant::Int) ) return TQString::number(value.toInt());
  30. return "0";
  31. }
  32. //END class VariableInfo
  33. //BEGIN class PinSettings
  34. PinSettings::PinSettings()
  35. : TQObject()
  36. {
  37. m_type = PinSettings::pt_input;
  38. m_state = PinSettings::ps_off;
  39. m_id = "pin";
  40. }
  41. PinSettings::PinSettings( PinSettings::pin_type _type, PinSettings::pin_state _state, const TQString &id, const TQString &port )
  42. {
  43. m_type = _type;
  44. m_state = _state;
  45. m_id = id;
  46. m_port = port;
  47. }
  48. void PinSettings::setType( PinSettings::pin_type type )
  49. {
  50. if ( m_type == type )
  51. return;
  52. m_type = type;
  53. emit settingsChanged();
  54. }
  55. void PinSettings::setState( PinSettings::pin_state state )
  56. {
  57. if ( m_state == state)
  58. return;
  59. m_state = state;
  60. emit settingsChanged();
  61. }
  62. //END class PinSettings
  63. //BEGIN class MicroSettings
  64. MicroSettings::MicroSettings( MicroInfo * microInfo )
  65. {
  66. _microInfo = microInfo;
  67. TQStringList portNames = _microInfo->package()->portNames();
  68. const TQStringList::iterator portNamesEnd = portNames.end();
  69. for ( TQStringList::iterator it = portNames.begin(); it != portNamesEnd; ++it )
  70. {
  71. PinSettingsList portPins;
  72. TQStringList pinIDs;
  73. pinIDs = _microInfo->package()->pinIDs( PicPin::type_bidir | PicPin::type_open, *it );
  74. pinIDs.sort();
  75. const int numPins = pinIDs.size();
  76. for ( int i=0; i<numPins; i++ )
  77. {
  78. PinSettings *pinSettings = new PinSettings( PinSettings::pt_input, PinSettings::ps_off, pinIDs[i], *it );
  79. m_pinSettingsList.append(pinSettings);
  80. portPins.append(pinSettings);
  81. }
  82. m_ports[*it] = portPins;
  83. }
  84. }
  85. MicroSettings::~MicroSettings()
  86. {
  87. const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
  88. for ( PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
  89. {
  90. delete *it;
  91. }
  92. // delete m_variableList;
  93. }
  94. void MicroSettings::setPinType( const TQString &id, PinSettings::pin_type type )
  95. {
  96. PinSettings *pin = pinWithID(id);
  97. if (pin) pin->setType(type);
  98. }
  99. void MicroSettings::setPinState( const TQString &id, PinSettings::pin_state state )
  100. {
  101. PinSettings *pin = pinWithID(id);
  102. if (pin)
  103. pin->setState(state);
  104. }
  105. PinSettings* MicroSettings::pinWithID( const TQString &id )
  106. {
  107. const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
  108. for ( PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
  109. {
  110. if ( (*it)->id() == id ) return *it;
  111. }
  112. return 0L;
  113. }
  114. int MicroSettings::portState( const TQString &port )
  115. {
  116. if ( microInfo()->package()->portNames().findIndex(port) == -1 ) return -1;
  117. int pinPower = 1;
  118. int num = 0;
  119. const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
  120. for ( PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
  121. {
  122. if ( (*it)->port() == port )
  123. {
  124. if ( (*it)->state() == PinSettings::ps_on ) num += pinPower;
  125. // cout << "(*it)->state()="<<(*it)->state()<<endl;
  126. pinPower *= 2;
  127. }
  128. }
  129. return num;
  130. }
  131. int MicroSettings::portType( const TQString &port )
  132. {
  133. if ( microInfo()->package()->portNames().findIndex(port) == -1 ) return -1;
  134. int pinPower = 1;
  135. int num = 0;
  136. const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
  137. for ( PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
  138. {
  139. if ( (*it)->port() == port )
  140. {
  141. if ( (*it)->type() == PinSettings::pt_input ) num += pinPower;
  142. pinPower *= 2;
  143. }
  144. }
  145. return num;
  146. }
  147. void MicroSettings::setPortState( const TQString &port, int state )
  148. {
  149. PortList::iterator plit = m_ports.find(port);
  150. if ( plit == m_ports.end() ) return;
  151. const PinSettingsList::iterator plitEnd = plit.data().end();
  152. for ( PinSettingsList::iterator it = plit.data().begin(); it != plitEnd; ++it )
  153. {
  154. // cout << "state="<<state<<endl;
  155. (*it)->setState( (state%2 == 1) ? PinSettings::ps_on : PinSettings::ps_off );
  156. // cout << "(*it)->state()="<<(*it)->state()<<endl;
  157. state /= 2;
  158. }
  159. }
  160. void MicroSettings::setPortType( const TQString &port, int type )
  161. {
  162. PortList::iterator plit = m_ports.find(port);
  163. if ( plit == m_ports.end() ) return;
  164. const PinSettingsList::iterator plitEnd = plit.data().end();
  165. for ( PinSettingsList::iterator it = plit.data().begin(); it != plitEnd; ++it )
  166. {
  167. (*it)->setType( (type%2 == 1) ? PinSettings::pt_input : PinSettings::pt_output );
  168. type /= 2;
  169. }
  170. }
  171. MicroData MicroSettings::microData() const
  172. {
  173. MicroData data;
  174. data.id = microInfo()->id();
  175. data.pinMappings = pinMappings();
  176. const PinSettingsList::const_iterator pinListEnd = m_pinSettingsList.end();
  177. for ( PinSettingsList::const_iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it )
  178. {
  179. data.pinMap[(*it)->id()].type = (*it)->type();
  180. data.pinMap[(*it)->id()].state= (*it)->state();
  181. }
  182. const VariableMap::const_iterator variableMapEnd = m_variableMap.end();
  183. for ( VariableMap::const_iterator it = m_variableMap.begin(); it != variableMapEnd; ++it )
  184. {
  185. if ( it.data().permanent )
  186. data.variableMap[it.key()] = it.data().valueAsString();
  187. }
  188. return data;
  189. }
  190. void MicroSettings::restoreFromMicroData( const MicroData &microData )
  191. {
  192. setPinMappings( microData.pinMappings );
  193. const PinDataMap::const_iterator pinMapEnd = microData.pinMap.end();
  194. for ( PinDataMap::const_iterator it = microData.pinMap.begin(); it != pinMapEnd; ++it )
  195. {
  196. PinSettings *pin = pinWithID(it.key());
  197. if (pin)
  198. {
  199. pin->setState( it.data().state );
  200. pin->setType( it.data().type );
  201. }
  202. }
  203. const TQStringMap::const_iterator variableMapEnd = microData.variableMap.end();
  204. for ( TQStringMap::const_iterator it = microData.variableMap.begin(); it != variableMapEnd; ++it )
  205. {
  206. setVariable( it.key(), it.data(), true );
  207. }
  208. }
  209. void MicroSettings::setVariable( const TQString &name, TQVariant value, bool permanent )
  210. {
  211. if ( name.isNull() ) return;
  212. VariableMap::iterator it = m_variableMap.find(name);
  213. if ( it != m_variableMap.end() )
  214. {
  215. it.data().setValue(value);
  216. it.data().permanent = permanent;
  217. it.data().initAtStart = permanent;
  218. }
  219. else
  220. {
  221. VariableInfo info;
  222. info.setValue(value);
  223. info.permanent = permanent;
  224. info.initAtStart = permanent;
  225. m_variableMap[name] = info;
  226. }
  227. }
  228. TQStringList MicroSettings::variableNames()
  229. {
  230. TQStringList list;
  231. const VariableMap::iterator variableMapEnd = m_variableMap.end();
  232. for ( VariableMap::iterator it = m_variableMap.begin(); it != variableMapEnd; ++it )
  233. {
  234. list += it.key();
  235. }
  236. return list;
  237. }
  238. VariableInfo* MicroSettings::variableInfo( const TQString &name )
  239. {
  240. if ( name.isNull() ) return 0l;
  241. VariableMap::iterator it = m_variableMap.find(name);
  242. if ( it != m_variableMap.end() ) {
  243. return &(it.data());
  244. } else {
  245. return 0l;
  246. }
  247. }
  248. bool MicroSettings::deleteVariable( const TQString &name )
  249. {
  250. if ( name.isNull() ) return false;
  251. VariableMap::iterator it = m_variableMap.find(name);
  252. if ( it != m_variableMap.end() )
  253. {
  254. m_variableMap.erase(it);
  255. return true;
  256. } else {
  257. return false;
  258. }
  259. }
  260. void MicroSettings::removeAllVariables()
  261. {
  262. m_variableMap.clear();
  263. }
  264. PinMapping MicroSettings::pinMapping( const TQString & id ) const
  265. {
  266. return m_pinMappings[id];
  267. }
  268. void MicroSettings::setPinMappings( const PinMappingMap & pinMappings )
  269. {
  270. m_pinMappings = pinMappings;
  271. emit pinMappingsChanged();
  272. }
  273. PinMappingMap MicroSettings::pinMappings() const
  274. {
  275. return m_pinMappings;
  276. }
  277. //END class MicroSettings