]> git.tdb.fi Git - model-railway-devices.git/blobdiff - arducontrol/interface.c
Volatile is not needed here
[model-railway-devices.git] / arducontrol / interface.c
index 014e8e98bcaa0ac5eacbeb061f1a38754e1a2ae5..a73189648669345eda9a8589bc7d56ab9e94f12d 100644 (file)
@@ -1,5 +1,7 @@
 #include <avr/io.h>
+#include "clock.h"
 #include "interface.h"
+#include "mfx.h"
 #include "monitor.h"
 #include "motorola.h"
 #include "output.h"
 static uint8_t cmd_buffer[15];
 static uint8_t cmd_length = 0;
 static uint8_t cmd_read_pos = 0;
+static uint16_t baud_rate = 9600;
+static uint16_t baud_change = 0;
+static uint32_t baud_changed_at = 0;
 
 static uint8_t dispatch_command(const uint8_t *, uint8_t);
+uint8_t interface_command(const uint8_t *, uint8_t);
 
 void interface_init(void)
 {
        DDRB |= 0x01;
        DDRD = (DDRD&0xFC)|0x02;
 
-       serial_init(9600);
+       serial_init(baud_rate);
 }
 
 void interface_check(void)
@@ -60,6 +66,21 @@ void interface_check(void)
                }
                PORTB &= ~0x01;
        }
+
+       if(baud_change)
+       {
+               if(!baud_changed_at)
+               {
+                       baud_changed_at = clock_get();
+                       serial_set_baud(baud_change);
+               }
+               else if(clock_get()-baud_changed_at>3*CLOCK_RATE)
+               {
+                       baud_change = 0;
+                       serial_set_baud(baud_rate);
+                       interface_send1(BAUD_CHANGE_FAILED);
+               }
+       }
 }
 
 static uint8_t dispatch_command(const uint8_t *cmd, uint8_t length)
@@ -75,8 +96,12 @@ static uint8_t dispatch_command(const uint8_t *cmd, uint8_t length)
        }
        else if(type==1)
                return motorola_command(cmd, length);
+       else if(type==2)
+               return mfx_command(cmd, length);
        else if(type==3)
                return s88_command(cmd, length);
+       else if(type==7)
+               return interface_command(cmd, length);
        else
                return INVALID_COMMAND;
 }
@@ -93,3 +118,31 @@ void interface_send1(uint8_t cmd)
        serial_write(0xFE);
        serial_write(cmd);
 }
+
+uint8_t interface_command(const uint8_t *cmd, uint8_t length)
+{
+       if(cmd[0]==SET_BAUD_RATE)
+       {
+               if(length!=3)
+                       return LENGTH_ERROR;
+
+               uint16_t baud = (cmd[1]<<8)|cmd[2];
+               if(baud!=baud_rate)
+               {
+                       if(baud!=baud_change)
+                       {
+                               baud_change = baud;
+                               baud_changed_at = 0;
+                       }
+                       else
+                       {
+                               baud_rate = baud_change;
+                               baud_change = 0;
+                       }
+               }
+       }
+       else
+               return INVALID_COMMAND;
+
+       return COMMAND_OK;
+}