コード例 #1
0
ファイル: Compute.cpp プロジェクト: johnyu916/jarvis
    int spanVoltage(Pin* source, bool voltage){
        if (source == NULL) return 0;
        if (source->visited()) return 0;
        source->visited(true);

        Wire *wire = source->wire();
        if (!wire) {
            return 0; //have separate audit if you want to check connectivity
            cerr << "source has no wire. source: " << source->name() <<endl;
            return 20;
        }
        /*
        if (wire->visited()){
            bool wireVoltage = wire->voltage();
            if (wireVoltage != voltage){
                cerr << "inconsistent voltage found while updating voltages on wire: " << wire->info() <<endl;
                return 21;
            }
            return 0;
        }*/
        //cout <<"spanVoltage setting wire: "<< wire->info()<< " to voltage: "<<voltage<<endl;
        wire->voltage(voltage);
        list<Pin *>pins = wire->pins();
        list<Pin *>::iterator it;
        for (it = pins.begin(); it != pins.end(); it++){
            Pin *pin = (*it);
            //cout <<"spanvoltage pin name: "<<pin->name()<<endl;
            if (pin == source) continue;
            pin->visited(true);

            Element *element = pin->element();
            if (element->type() == "source"){
                Source *source = (Source *)element;
                //warning? 

            }
            else if(element->type() == "ground"){ 
                bool gv = pin->wire()->voltage();
                if (gv){
                    cerr << "error: voltage at ground is high. " << pin->wire()->info()<<endl;
                    return 24;
                }
            }
            else if (element->type() == "switch"){
                Switch *switc = (Switch *)element;
                if (!switc->isOn()) continue;
                Pin *nextPin = switc->outPin(pin);
                int rVal = spanVoltage(nextPin, voltage);
                if (rVal != 0) return rVal;
            }
            else if (element->type() == "resistor"){
                Resistor *resistor = (Resistor *)element;
                Pin *nextPin = resistor->outPin(pin);
                bool nextVoltage = voltage;
                if (resistor->isActive()) nextVoltage = false;
                int rVal = spanVoltage(nextPin, nextVoltage);
                if (rVal != 0) return rVal;
                
            }
            else if (element->type() == "bridge"){
                Bridge *bridge = (Bridge *)element;
                Pin *nextPin = bridge->outPin(pin);
                int rVal = spanVoltage(nextPin, voltage);
                if (rVal != 0) return rVal;
            }

        }
        return 0;
    }