]> git.tdb.fi Git - r2c2.git/blob - source/network/train.cpp
Add a program for serial port control and associated Arduino firmware
[r2c2.git] / source / network / train.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
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 Marklin {
14
15 NetTrain::NetTrain(Client &c, const TrainInfoPacket &pkt):
16         client(c),
17         loco_type(client.get_catalogue().get_locomotive(pkt.loco_type)),
18         address(pkt.address),
19         name(pkt.name),
20         speed(0),
21         reverse(0),
22         functions(0)
23 { }
24
25 void NetTrain::set_speed(unsigned s)
26 {
27         if(s==speed)
28                 return;
29
30         TrainSpeedPacket pkt;
31         pkt.address = address;
32         pkt.speed = s;
33         pkt.reverse = reverse;
34         client.send(pkt);
35 }
36
37 void NetTrain::set_reverse(bool r)
38 {
39         if(r==reverse)
40                 return;
41
42         TrainSpeedPacket pkt;
43         pkt.address = address;
44         pkt.speed = speed;
45         pkt.reverse = r;
46         client.send(pkt);
47 }
48
49 void NetTrain::set_function(unsigned i, bool set)
50 {
51         TrainFunctionPacket pkt;
52         pkt.address = address;
53         pkt.functions = functions;
54         if(set)
55                 pkt.functions |= 1<<i;
56         else
57                 pkt.functions &= ~(1<<i);
58         client.send(pkt);
59 }
60
61 void NetTrain::set_route(const string &r)
62 {
63         if(r==route)
64                 return;
65
66         TrainRoutePacket pkt;
67         pkt.address = address;
68         pkt.route = r;
69         client.send(pkt);
70 }
71
72 void NetTrain::process_packet(const TrainSpeedPacket &pkt)
73 {
74         if(pkt.speed!=speed)
75         {
76                 speed = pkt.speed;
77                 signal_speed_changed.emit(speed);
78         }
79         if(pkt.reverse!=reverse)
80         {
81                 reverse = pkt.reverse;
82                 signal_reverse_changed.emit(reverse);
83         }
84 }
85
86 void NetTrain::process_packet(const TrainFunctionPacket &pkt)
87 {
88         unsigned old_funcs = functions;
89         functions = pkt.functions;
90         for(unsigned i=0; i<9; ++i)
91                 if(((functions^old_funcs)>>i)&1)
92                         signal_function_changed.emit(i, (functions>>i)&1);
93 }
94
95 void NetTrain::process_packet(const TrainStatusPacket &pkt)
96 {
97         status = pkt.status;
98         signal_status_changed.emit(status);
99 }
100
101 void NetTrain::process_packet(const TrainRoutePacket &pkt)
102 {
103         route = pkt.route;
104         signal_route_changed.emit(route);
105 }
106
107 } // namespace Marklin