]> git.tdb.fi Git - model-railway-devices.git/blob - arducontrol/motorola.c
Add railway control firmware
[model-railway-devices.git] / arducontrol / motorola.c
1 #include "packet.h"
2
3 static uint8_t motorola_speed_to_value(uint8_t speed)
4 {
5         if(speed>14)
6                 return 15;
7         else if(speed)
8                 return speed+1;
9         else
10                 return 0;
11 }
12
13 static void motorola_common_packet(uint8_t addr, uint8_t aux, uint8_t value)
14 {
15         uint8_t i;
16
17         clear_packet();
18
19         packet.bit_duration = 2;
20         packet.length = 18*8;
21
22         for(i=0; i<4; ++i)
23         {
24                 uint8_t d = addr%3;
25                 addr /= 3;
26
27                 packet.data[i*2] = (d==0 ? 0x01 : 0x7F);
28                 packet.data[i*2+1] = (d==1 ? 0x7F : 0x01);
29         }
30
31         packet.data[8] = (aux ? 0x7F : 0x01);
32         packet.data[9] = packet.data[8];
33
34         for(i=0; i<4; ++i)
35         {
36                 packet.data[10+i*2] = ((value&1) ? 0x7F : 0x01);
37                 value >>= 1;
38         }
39
40         packet.repeat_count = 2;
41         // Duration of three trits
42         packet.repeat_delay = 96;
43         packet.final_delay = 224;
44 }
45
46 static void motorola_old_packet(uint8_t addr, uint8_t aux, uint8_t value)
47 {
48         uint8_t i;
49
50         motorola_common_packet(addr, aux, value);
51
52         for(i=0; i<4; ++i)
53                 packet.data[11+i*2] = packet.data[10+i*2];
54 }
55
56 void motorola_locomotive_speed_packet(uint8_t addr, uint8_t aux, uint8_t speed)
57 {
58         motorola_old_packet(addr, aux, motorola_speed_to_value(speed));
59
60         packet.ready = 1;
61 }
62
63 void motorola_locomotive_reverse_packet(uint8_t addr, uint8_t aux)
64 {
65         motorola_old_packet(addr, aux, 1);
66
67         packet.ready = 1;
68 }
69
70 void motorola_locomotive_speed_direction_packet(uint8_t addr, uint8_t aux, uint8_t speed, uint8_t dir)
71 {
72         motorola_common_packet(addr, aux, motorola_speed_to_value(speed));
73
74         packet.data[11] = (dir ? 0x01 : 0x7F);
75         packet.data[13] = (dir ? 0x7F : 0x01);
76         packet.data[15] = (dir ? 0x01 : 0x7F);
77         packet.data[17] = 0x80-packet.data[16];
78
79         packet.ready = 1;
80 }
81
82 void motorola_locomotive_speed_function_packet(uint8_t addr, uint8_t aux, uint8_t speed, uint8_t func, uint8_t state)
83 {
84         uint8_t i;
85         uint8_t value;
86
87         value = motorola_speed_to_value(speed);
88         motorola_common_packet(addr, aux, value);
89
90         /*
91         001 -> 011
92         010 -> 100
93         011 -> 110
94         100 -> 111
95         */
96         func += 2;
97         if(func>=5)
98                 ++func;
99         if(state)
100                 func |= 8;
101         if(func==value)
102                 func = ((value&8) ? 2 : 5) | (func&8);
103
104         for(i=0; i<4; ++i)
105         {
106                 packet.data[11+i*2] = ((func&1) ? 0x7F : 0x01);
107                 func >>= 1;
108         }
109
110         packet.ready = 1;
111 }
112
113 void motorola_solenoid_packet(uint8_t addr, uint8_t output, uint8_t state)
114 {
115         uint8_t value = output;
116         if(state)
117                 value |= 8;
118
119         motorola_old_packet(addr, 0, value);
120         packet.repeat_delay >>= 1;
121         packet.bit_duration = 1;
122
123         packet.ready = 1;
124 }