]> git.tdb.fi Git - r2c2.git/blob - source/network/train.cpp
Emit signals from loco status command only when all data has been gathered
[r2c2.git] / source / network / train.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2009  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include "client.h"
9 #include "train.h"
10
11 using namespace std;
12
13 namespace R2C2 {
14
15 NetTrain::NetTrain(Client &c, const TrainInfoPacket &pkt):
16         client(c),
17         loco_type(client.get_catalogue().get_vehicle(pkt.loco_type)),
18         address(pkt.address),
19         name(pkt.name),
20         functions(0)
21 { }
22
23 void NetTrain::set_control(const string &c, float v)
24 {
25         if(v==controls[c])
26                 return;
27
28         TrainControlPacket pkt;
29         pkt.address = address;
30         pkt.control = c;
31         pkt.value = v;
32         client.send(pkt);
33 }
34
35 float NetTrain::get_control(const string &c) const
36 {
37         map<string, float>::const_iterator i = controls.find(c);
38         return i!=controls.end() ? i->second : 0;
39 }
40
41 void NetTrain::set_function(unsigned i, bool set)
42 {
43         TrainFunctionPacket pkt;
44         pkt.address = address;
45         pkt.functions = functions;
46         if(set)
47                 pkt.functions |= 1<<i;
48         else
49                 pkt.functions &= ~(1<<i);
50         client.send(pkt);
51 }
52
53 void NetTrain::set_route(const string &r)
54 {
55         if(r==route)
56                 return;
57
58         TrainRoutePacket pkt;
59         pkt.address = address;
60         pkt.route = r;
61         client.send(pkt);
62 }
63
64 void NetTrain::process_packet(const TrainControlPacket &pkt)
65 {
66         controls[pkt.control] = pkt.value;
67         signal_control_changed.emit(pkt.control, pkt.value);
68 }
69
70 void NetTrain::process_packet(const TrainFunctionPacket &pkt)
71 {
72         unsigned old_funcs = functions;
73         functions = pkt.functions;
74         for(unsigned i=0; i<9; ++i)
75                 if(((functions^old_funcs)>>i)&1)
76                         signal_function_changed.emit(i, (functions>>i)&1);
77 }
78
79 void NetTrain::process_packet(const TrainStatusPacket &pkt)
80 {
81         status = pkt.status;
82         signal_status_changed.emit(status);
83 }
84
85 void NetTrain::process_packet(const TrainRoutePacket &pkt)
86 {
87         route = pkt.route;
88         signal_route_changed.emit(route);
89 }
90
91 } // namespace R2C2