File indexing completed on 2024-06-16 05:17:30

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2005 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "itemdocumentdata.h"
0012 #include "microinfo.h"
0013 #include "micropackage.h"
0014 #include "pinmapping.h"
0015 
0016 // BEGIN class VariableInfo
0017 VariableInfo::VariableInfo()
0018 {
0019     type = MicroSettings::vt_unknown;
0020     value = QVariant(0);
0021     initAtStart = false;
0022     permanent = false;
0023 }
0024 
0025 void VariableInfo::setValue(const QVariant &_value)
0026 {
0027     value = _value;
0028 }
0029 
0030 QString VariableInfo::valueAsString() const
0031 {
0032     if (value.canConvert(QVariant::String))
0033         return value.toString();
0034     if (value.canConvert(QVariant::Int))
0035         return QString::number(value.toInt());
0036     return "0";
0037 }
0038 // END class VariableInfo
0039 
0040 // BEGIN class PinSettings
0041 PinSettings::PinSettings()
0042     : QObject()
0043 {
0044     m_type = PinSettings::pt_input;
0045     m_state = PinSettings::ps_off;
0046     m_id = "pin";
0047 }
0048 
0049 PinSettings::PinSettings(PinSettings::pin_type _type, PinSettings::pin_state _state, const QString &id, const QString &port)
0050 {
0051     m_type = _type;
0052     m_state = _state;
0053     m_id = id;
0054     m_port = port;
0055 }
0056 
0057 void PinSettings::setType(PinSettings::pin_type type)
0058 {
0059     if (m_type == type)
0060         return;
0061     m_type = type;
0062     emit settingsChanged();
0063 }
0064 
0065 void PinSettings::setState(PinSettings::pin_state state)
0066 {
0067     if (m_state == state)
0068         return;
0069     m_state = state;
0070     emit settingsChanged();
0071 }
0072 // END class PinSettings
0073 
0074 // BEGIN class MicroSettings
0075 MicroSettings::MicroSettings(MicroInfo *microInfo)
0076 {
0077     _microInfo = microInfo;
0078 
0079     QStringList portNames = _microInfo->package()->portNames();
0080     const QStringList::iterator portNamesEnd = portNames.end();
0081     for (QStringList::iterator it = portNames.begin(); it != portNamesEnd; ++it) {
0082         PinSettingsList portPins;
0083         QStringList pinIDs;
0084         pinIDs = _microInfo->package()->pinIDs(PicPin::type_bidir | PicPin::type_open, *it);
0085         pinIDs.sort();
0086         const int numPins = pinIDs.size();
0087         for (int i = 0; i < numPins; i++) {
0088             PinSettings *pinSettings = new PinSettings(PinSettings::pt_input, PinSettings::ps_off, pinIDs[i], *it);
0089             m_pinSettingsList.append(pinSettings);
0090             portPins.append(pinSettings);
0091         }
0092         m_ports[*it] = portPins;
0093     }
0094 }
0095 
0096 MicroSettings::~MicroSettings()
0097 {
0098     qDeleteAll(m_pinSettingsList);
0099     //  delete m_variableList;
0100 }
0101 
0102 void MicroSettings::setPinType(const QString &id, PinSettings::pin_type type)
0103 {
0104     PinSettings *pin = pinWithID(id);
0105     if (pin)
0106         pin->setType(type);
0107 }
0108 
0109 void MicroSettings::setPinState(const QString &id, PinSettings::pin_state state)
0110 {
0111     PinSettings *pin = pinWithID(id);
0112     if (pin)
0113         pin->setState(state);
0114 }
0115 
0116 PinSettings *MicroSettings::pinWithID(const QString &id)
0117 {
0118     const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
0119     for (PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it) {
0120         if ((*it)->id() == id)
0121             return *it;
0122     }
0123     return nullptr;
0124 }
0125 
0126 int MicroSettings::portState(const QString &port)
0127 {
0128     if (microInfo()->package()->portNames().indexOf(port) == -1)
0129         return -1;
0130 
0131     int pinPower = 1;
0132     int num = 0;
0133 
0134     const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
0135     for (PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it) {
0136         if ((*it)->port() == port) {
0137             if ((*it)->state() == PinSettings::ps_on)
0138                 num += pinPower;
0139             //          cout << "(*it)->state()="<<(*it)->state()<<endl;
0140             pinPower *= 2;
0141         }
0142     }
0143 
0144     return num;
0145 }
0146 
0147 int MicroSettings::portType(const QString &port)
0148 {
0149     if (microInfo()->package()->portNames().indexOf(port) == -1)
0150         return -1;
0151 
0152     int pinPower = 1;
0153     int num = 0;
0154 
0155     const PinSettingsList::iterator pinListEnd = m_pinSettingsList.end();
0156     for (PinSettingsList::iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it) {
0157         if ((*it)->port() == port) {
0158             if ((*it)->type() == PinSettings::pt_input)
0159                 num += pinPower;
0160             pinPower *= 2;
0161         }
0162     }
0163 
0164     return num;
0165 }
0166 
0167 void MicroSettings::setPortState(const QString &port, int state)
0168 {
0169     PortList::iterator plit = m_ports.find(port);
0170     if (plit == m_ports.end())
0171         return;
0172 
0173     const PinSettingsList::iterator plitEnd = plit.value().end();
0174     for (PinSettingsList::iterator it = plit.value().begin(); it != plitEnd; ++it) {
0175         //      cout << "state="<<state<<endl;
0176         (*it)->setState((state % 2 == 1) ? PinSettings::ps_on : PinSettings::ps_off);
0177         //      cout << "(*it)->state()="<<(*it)->state()<<endl;
0178         state /= 2;
0179     }
0180 }
0181 
0182 void MicroSettings::setPortType(const QString &port, int type)
0183 {
0184     PortList::iterator plit = m_ports.find(port);
0185     if (plit == m_ports.end())
0186         return;
0187 
0188     const PinSettingsList::iterator plitEnd = plit.value().end();
0189     for (PinSettingsList::iterator it = plit.value().begin(); it != plitEnd; ++it) {
0190         (*it)->setType((type % 2 == 1) ? PinSettings::pt_input : PinSettings::pt_output);
0191         type /= 2;
0192     }
0193 }
0194 
0195 MicroData MicroSettings::microData() const
0196 {
0197     MicroData data;
0198     data.id = microInfo()->id();
0199     data.pinMappings = pinMappings();
0200 
0201     const PinSettingsList::const_iterator pinListEnd = m_pinSettingsList.end();
0202     for (PinSettingsList::const_iterator it = m_pinSettingsList.begin(); it != pinListEnd; ++it) {
0203         data.pinMap[(*it)->id()].type = (*it)->type();
0204         data.pinMap[(*it)->id()].state = (*it)->state();
0205     }
0206 
0207     const VariableMap::const_iterator variableMapEnd = m_variableMap.end();
0208     for (VariableMap::const_iterator it = m_variableMap.begin(); it != variableMapEnd; ++it) {
0209         if (it.value().permanent)
0210             data.variableMap[it.key()] = it.value().valueAsString();
0211     }
0212 
0213     return data;
0214 }
0215 
0216 void MicroSettings::restoreFromMicroData(const MicroData &microData)
0217 {
0218     setPinMappings(microData.pinMappings);
0219 
0220     const PinDataMap::const_iterator pinMapEnd = microData.pinMap.end();
0221     for (PinDataMap::const_iterator it = microData.pinMap.begin(); it != pinMapEnd; ++it) {
0222         PinSettings *pin = pinWithID(it.key());
0223         if (pin) {
0224             pin->setState(it.value().state);
0225             pin->setType(it.value().type);
0226         }
0227     }
0228 
0229     const QStringMap::const_iterator variableMapEnd = microData.variableMap.end();
0230     for (QStringMap::const_iterator it = microData.variableMap.begin(); it != variableMapEnd; ++it) {
0231         setVariable(it.key(), it.value(), true);
0232     }
0233 }
0234 
0235 void MicroSettings::setVariable(const QString &name, QVariant value, bool permanent)
0236 {
0237     if (name.isNull())
0238         return;
0239     VariableMap::iterator it = m_variableMap.find(name);
0240     if (it != m_variableMap.end()) {
0241         it.value().setValue(value);
0242         it.value().permanent = permanent;
0243         it.value().initAtStart = permanent;
0244     } else {
0245         VariableInfo info;
0246         info.setValue(value);
0247         info.permanent = permanent;
0248         info.initAtStart = permanent;
0249         m_variableMap[name] = info;
0250     }
0251 }
0252 
0253 QStringList MicroSettings::variableNames()
0254 {
0255     QStringList list;
0256     const VariableMap::iterator variableMapEnd = m_variableMap.end();
0257     for (VariableMap::iterator it = m_variableMap.begin(); it != variableMapEnd; ++it) {
0258         list += it.key();
0259     }
0260     return list;
0261 }
0262 
0263 VariableInfo *MicroSettings::variableInfo(const QString &name)
0264 {
0265     if (name.isNull())
0266         return nullptr;
0267     VariableMap::iterator it = m_variableMap.find(name);
0268     if (it != m_variableMap.end()) {
0269         return &(it.value());
0270     } else {
0271         return nullptr;
0272     }
0273 }
0274 
0275 bool MicroSettings::deleteVariable(const QString &name)
0276 {
0277     if (name.isNull())
0278         return false;
0279     VariableMap::iterator it = m_variableMap.find(name);
0280     if (it != m_variableMap.end()) {
0281         m_variableMap.erase(it);
0282         return true;
0283     } else {
0284         return false;
0285     }
0286 }
0287 
0288 void MicroSettings::removeAllVariables()
0289 {
0290     m_variableMap.clear();
0291 }
0292 
0293 PinMapping MicroSettings::pinMapping(const QString &id) const
0294 {
0295     return m_pinMappings[id];
0296 }
0297 
0298 void MicroSettings::setPinMappings(const PinMappingMap &pinMappings)
0299 {
0300     m_pinMappings = pinMappings;
0301     emit pinMappingsChanged();
0302 }
0303 
0304 PinMappingMap MicroSettings::pinMappings() const
0305 {
0306     return m_pinMappings;
0307 }
0308 // END class MicroSettings
0309 
0310 #include "moc_microsettings.cpp"