]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/centralstation.cpp
Update #includes that have been renamed, and remove stray ones
[r2c2.git] / source / libr2c2 / centralstation.cpp
index 6a8de96694f3d9ada60020ff06dc4a018a676de6..a41a494c440243c266d10379bd40236ea06fb03c 100644 (file)
@@ -1,10 +1,3 @@
-/* $Id$
-
-This file is part of R²C²
-Copyright © 2010  Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
 #include <algorithm>
 #include <msp/core/refptr.h>
 #include <msp/io/print.h>
@@ -107,6 +100,10 @@ void CentralStation::add_loco(unsigned addr, const string &proto_name, const Veh
                loco.protocol = proto;
                loco.address = addr;
 
+               const VehicleType::FunctionMap &type_funcs = type.get_functions();
+               for(VehicleType::FunctionMap::const_iterator i=type_funcs.begin(); i!=type_funcs.end(); ++i)
+                       loco.func_mask |= 1<<i->first;
+
                if(locos_synced && proto!=MFX)
                        command("create(10)");
        }
@@ -250,15 +247,6 @@ bool CentralStation::get_sensor(unsigned addr) const
 
 void CentralStation::tick()
 {
-       Time::TimeStamp t = Time::now();
-       for(SensorMap::iterator i=sensors.begin(); i!=sensors.end(); ++i)
-               if(i->second.off_timeout && t>i->second.off_timeout)
-               {
-                       i->second.state = false;
-                       i->second.off_timeout = Time::TimeStamp();
-                       signal_sensor.emit(i->first, i->second.state);
-               }
-
        while(Message msg = receive())
        {
                if(msg.footer.code)
@@ -352,7 +340,12 @@ void CentralStation::process_reply(const Message &msg)
                                        if(j!=locos.end())
                                        {
                                                command(format("request(%d, view, control, force)", i->first));
-                                               command(format("get(%d, dir, func[0])", i->first));
+                                               string cmd = format("get(%d, dir", i->first);
+                                               for(unsigned l=0; j->second.func_mask>>l; ++l)
+                                                       if((j->second.func_mask>>l)&1)
+                                                               cmd += format(", func[%d]", l);
+                                               cmd += ')';
+                                               command(cmd);
 
                                                locos.insert(LocoMap::value_type(i->first, j->second));
                                                locos.erase(j);
@@ -419,6 +412,7 @@ void CentralStation::process_reply(const Message &msg)
                {
                        s88.push_back(i->first);
                        command(format("request(%d, view)", i->first));
+                       command(format("get(%d, state)", i->first));
                }
 
                sensors_synced = true;
@@ -599,17 +593,11 @@ void CentralStation::process_object(unsigned id, const Message::AttribMap &attri
                                        unsigned addr = base*16+j+1;
                                        Sensor &sensor = sensors[addr];
                                        bool s = state&(1<<j);
-                                       if(s)
+                                       if(s!=sensor.state)
                                        {
-                                               sensor.off_timeout = Time::TimeStamp();
-                                               if(!sensor.state)
-                                               {
-                                                       sensor.state = true;
-                                                       signal_sensor.emit(addr, sensor.state);
-                                               }
+                                               sensor.state = s;
+                                               signal_sensor.emit(addr, sensor.state);
                                        }
-                                       else if(sensor.state)
-                                               sensor.off_timeout = Time::now()+700*Time::msec;
                                }
                        }
                }
@@ -721,10 +709,12 @@ CentralStation::Message CentralStation::parse_message(string::iterator &iter, co
                        if(open_bracket!=string::npos)
                        {
                                string::size_type close_bracket = attr.rfind(']');
-                               attribs[attr.substr(0, open_bracket)] = attr.substr(open_bracket+1, close_bracket-open_bracket-1);
+                               string attr_name = attr.substr(0, open_bracket);
+                               string attr_value = attr.substr(open_bracket+1, close_bracket-open_bracket-1);
+                               attribs.insert(Message::AttribMap::value_type(attr_name, attr_value));
                        }
                        else
-                               attribs[attr];
+                               attribs.insert(Message::AttribMap::value_type(attr, string()));
                }
        }
 
@@ -756,6 +746,7 @@ CentralStation::Locomotive::Locomotive():
        address(0),
        speed(0),
        reverse(false),
+       func_mask(0),
        funcs(0),
        control(false)
 { }