]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/locomotive.cpp
Forgot to add the new files
[r2c2.git] / source / libmarklin / locomotive.cpp
index a42d8d7820f0c4b3ce18e3c0bf7bfc19a7a6e8f4..5fe5ca5f863d07ca489cdea2bae773eb46ba9d8e 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-200 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -11,6 +11,7 @@ Distributed under the GPL
 #include "constants.h"
 #include "control.h"
 #include "locomotive.h"
+#include "locotype.h"
 #include "reply.h"
 
 using namespace std;
@@ -33,8 +34,12 @@ Locomotive::Locomotive(const LocoType &t, Control &c, unsigned a):
 
 void Locomotive::set_speed(unsigned spd)
 {
-       speed=min(spd, 14U);
+       spd = min(spd, 14U);
+       if(spd==speed)
+               return;
+       signal_speed_changing.emit(spd);
 
+       speed = spd;
        send_command(false);
 
        signal_speed_changed.emit(speed);
@@ -52,17 +57,18 @@ void Locomotive::set_reverse(bool rev)
        }
        else
        {
-               reverse=rev;
+               reverse = rev;
                send_command(false);
+               signal_reverse_changed.emit(reverse);
        }
 }
 
 void Locomotive::set_function(unsigned func, bool state)
 {
        if(state)
-               funcs|=1<<func;
+               funcs |= 1<<func;
        else
-               funcs&=~(1<<func);
+               funcs &= ~(1<<func);
 
        send_command(true);
 
@@ -72,51 +78,63 @@ void Locomotive::set_function(unsigned func, bool state)
 void Locomotive::refresh_status()
 {
        unsigned char data[2];
-       data[0]=addr&0xFF;
-       data[1]=(addr>>8)&0xFF;
+       data[0] = addr&0xFF;
+       data[1] = (addr>>8)&0xFF;
        control.command(CMD_LOK_STATUS, data, 2).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply));
 }
 
 void Locomotive::send_command(bool setf)
 {
        unsigned char data[4];
-       data[0]=addr&0xFF;
-       data[1]=(addr>>8)&0xFF;
+       data[0] = addr&0xFF;
+       data[1] = (addr>>8)&0xFF;
 
        if(speed==0)
-               data[2]=0;
+               data[2] = 0;
        else if(speed==1)
-               data[2]=2;
+               data[2] = 2;
        else
-               data[2]=(speed*19-18)/2;
+               data[2] = (speed*19-18)/2;
        
-       data[3]=(reverse ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
+       data[3] = (reverse ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
 
        if(setf)
        {
-               data[3]|=0x80;
+               data[3] |= 0x80;
                for(unsigned i=0; i<4; ++i)
                        if((funcs>>i)&2)
-                               data[3]|=(1<<i);
+                               data[3] |= (1<<i);
        }
        control.command(CMD_LOK, data, 4);
+
+       if(setf && type.get_max_function()>4)
+       {
+               if(!++data[0])
+                       ++data[1];
+               data[2] = 0;
+               data[3] = 0xA0;
+               for(unsigned i=0; i<4; ++i)
+                       if((funcs>>i)&32)
+                               data[3] |= (1<<i);
+               control.command(CMD_LOK, data, 4);
+       }
 }
 
 void Locomotive::status_reply(const Reply &reply)
 {
        if(reply.get_error()==ERR_NO_ERROR)
        {
-               const unsigned char *data=reply.get_data();
+               const unsigned char *data = reply.get_data();
 
                if(data[0]<=1)
-                       speed=0;
+                       speed = 0;
                else
-                       speed=data[0]*2/19+1;
+                       speed = data[0]*2/19+1;
 
-               reverse=(data[1]&0x20) ? false : true;
-               funcs=(data[1]&0xF)<<1;
+               reverse = (data[1]&0x20) ? false : true;
+               funcs = (data[1]&0xF)<<1;
                if(data[1]&0x10)
-                       funcs|=1;
+                       funcs |= 1;
 
                for(unsigned i=0; i<5; ++i)
                        signal_function_changed.emit(i, (funcs>>i)&1);
@@ -125,8 +143,9 @@ void Locomotive::status_reply(const Reply &reply)
 
 bool Locomotive::reverse_timeout()
 {
-       reverse=!reverse;
+       reverse = !reverse;
        send_command(true);
+       signal_reverse_changed.emit(reverse);
        return false;
 }