]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/locomotive.cpp
c7d6227cddb938e5d3acfbbb6cc2c8f564ca771f
[r2c2.git] / source / libmarklin / locomotive.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/time/timer.h>
9 #include <msp/time/units.h>
10 #include "command.h"
11 #include "constants.h"
12 #include "control.h"
13 #include "locomotive.h"
14 #include "reply.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 namespace Marklin {
20
21 Locomotive::Locomotive(const LocoType &t, Control &c, unsigned a):
22         type(t),
23         control(c),
24         addr(a),
25         speed(0),
26         reverse(false),
27         funcs(0)
28 {
29         control.add_locomotive(*this);
30
31         refresh_status();
32 }
33
34 void Locomotive::set_speed(unsigned spd)
35 {
36         spd = min(spd, 14U);
37         signal_speed_changing.emit(spd);
38
39         speed = spd;
40         send_command(false);
41
42         signal_speed_changed.emit(speed);
43 }
44
45 void Locomotive::set_reverse(bool rev)
46 {
47         if(rev==reverse)
48                 return;
49
50         if(speed)
51         {
52                 control.set_timer((500+speed*150)*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Locomotive::reverse_timeout));
53                 set_speed(0);
54         }
55         else
56         {
57                 reverse = rev;
58                 send_command(false);
59         }
60 }
61
62 void Locomotive::set_function(unsigned func, bool state)
63 {
64         if(state)
65                 funcs |= 1<<func;
66         else
67                 funcs &= ~(1<<func);
68
69         send_command(true);
70
71         signal_function_changed.emit(func, state);
72 }
73
74 void Locomotive::refresh_status()
75 {
76         unsigned char data[2];
77         data[0] = addr&0xFF;
78         data[1] = (addr>>8)&0xFF;
79         control.command(CMD_LOK_STATUS, data, 2).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply));
80 }
81
82 void Locomotive::send_command(bool setf)
83 {
84         unsigned char data[4];
85         data[0] = addr&0xFF;
86         data[1] = (addr>>8)&0xFF;
87
88         if(speed==0)
89                 data[2] = 0;
90         else if(speed==1)
91                 data[2] = 2;
92         else
93                 data[2] = (speed*19-18)/2;
94         
95         data[3] = (reverse ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
96
97         if(setf)
98         {
99                 data[3] |= 0x80;
100                 for(unsigned i=0; i<4; ++i)
101                         if((funcs>>i)&2)
102                                 data[3] |= (1<<i);
103         }
104         control.command(CMD_LOK, data, 4);
105 }
106
107 void Locomotive::status_reply(const Reply &reply)
108 {
109         if(reply.get_error()==ERR_NO_ERROR)
110         {
111                 const unsigned char *data = reply.get_data();
112
113                 if(data[0]<=1)
114                         speed = 0;
115                 else
116                         speed = data[0]*2/19+1;
117
118                 reverse = (data[1]&0x20) ? false : true;
119                 funcs = (data[1]&0xF)<<1;
120                 if(data[1]&0x10)
121                         funcs |= 1;
122
123                 for(unsigned i=0; i<5; ++i)
124                         signal_function_changed.emit(i, (funcs>>i)&1);
125         }
126 }
127
128 bool Locomotive::reverse_timeout()
129 {
130         reverse = !reverse;
131         send_command(true);
132         return false;
133 }
134
135 } // namespace Marklin