5 static uint8_t motorola_speed_to_value(uint8_t speed)
15 static void motorola_common_packet(uint8_t addr, uint8_t aux, uint8_t value)
21 packet.bit_duration = 2;
29 packet.data[i*2] = (d==0 ? 0x01 : 0x7F);
30 packet.data[i*2+1] = (d==1 ? 0x7F : 0x01);
33 packet.data[8] = (aux ? 0x7F : 0x01);
34 packet.data[9] = packet.data[8];
38 packet.data[10+i*2] = ((value&1) ? 0x7F : 0x01);
42 packet.repeat_count = 2;
43 // Duration of three trits
44 packet.repeat_delay = 96;
45 packet.final_delay = 224;
48 static void motorola_old_packet(uint8_t addr, uint8_t aux, uint8_t value)
52 motorola_common_packet(addr, aux, value);
55 packet.data[11+i*2] = packet.data[10+i*2];
58 void motorola_locomotive_speed_packet(uint8_t addr, uint8_t aux, uint8_t speed)
60 motorola_old_packet(addr, aux, motorola_speed_to_value(speed));
65 void motorola_locomotive_reverse_packet(uint8_t addr, uint8_t aux)
67 motorola_old_packet(addr, aux, 1);
72 void motorola_locomotive_speed_direction_packet(uint8_t addr, uint8_t aux, uint8_t speed, uint8_t dir)
74 motorola_common_packet(addr, aux, motorola_speed_to_value(speed));
76 packet.data[11] = (dir ? 0x01 : 0x7F);
77 packet.data[13] = (dir ? 0x7F : 0x01);
78 packet.data[15] = (dir ? 0x01 : 0x7F);
79 packet.data[17] = 0x80-packet.data[16];
84 void motorola_locomotive_speed_function_packet(uint8_t addr, uint8_t aux, uint8_t speed, uint8_t func, uint8_t state)
89 value = motorola_speed_to_value(speed);
90 motorola_common_packet(addr, aux, value);
104 func = ((value&8) ? 2 : 5) | (func&8);
108 packet.data[11+i*2] = ((func&1) ? 0x7F : 0x01);
115 void motorola_solenoid_packet(uint8_t addr, uint8_t output, uint8_t state)
117 uint8_t value = output;
121 motorola_old_packet(addr, 0, value);
122 packet.repeat_delay >>= 1;
123 packet.bit_duration = 1;
128 uint8_t motorola_command(const uint8_t *cmd_buf, uint8_t cmd_length)
130 if(cmd_buf[0]==MOTOROLA_SPEED || cmd_buf[0]==MOTOROLA_SPEED_DIRECTION || cmd_buf[0]==MOTOROLA_SPEED_FUNCTION)
135 uint8_t addr = cmd_buf[1];
137 return INVALID_VALUE;
140 return INVALID_VALUE;
141 uint8_t aux = cmd_buf[2]&0x01;
143 uint8_t func = (cmd_buf[2]&0xF0)>>4;
144 if(cmd_buf[0]==MOTOROLA_SPEED_FUNCTION)
147 return INVALID_VALUE;
149 else if(cmd_buf[2]&0xFE)
150 return INVALID_VALUE;
151 uint8_t state = cmd_buf[2]&0x02;
153 uint8_t speed = cmd_buf[3]&0x7F;
155 return INVALID_VALUE;
157 uint8_t dir = !(cmd_buf[3]&0x80);
159 while(packet.ready && !packet.done) ;
161 if(cmd_buf[0]==MOTOROLA_SPEED)
162 motorola_locomotive_speed_packet(addr, aux, speed);
163 else if(cmd_buf[0]==MOTOROLA_SPEED_DIRECTION)
164 motorola_locomotive_speed_direction_packet(addr, aux, speed, dir);
165 else if(cmd_buf[0]==MOTOROLA_SPEED_FUNCTION)
166 motorola_locomotive_speed_function_packet(addr, aux, speed, func, state);
168 else if(cmd_buf[0]==MOTOROLA_REVERSE)
173 uint8_t addr = cmd_buf[1];
175 return INVALID_VALUE;
178 return INVALID_VALUE;
179 uint8_t aux = cmd_buf[2]&0x01;
181 while(packet.ready && !packet.done) ;
183 motorola_locomotive_reverse_packet(addr, aux);
185 else if(cmd_buf[0]==MOTOROLA_SOLENOID)
190 uint8_t addr = cmd_buf[1];
192 return INVALID_VALUE;
195 return INVALID_VALUE;
196 uint8_t output = (cmd_buf[2]&0x70)>>4;
197 uint8_t state = cmd_buf[2]&1;
199 while(packet.ready && !packet.done) ;
201 motorola_solenoid_packet(addr, output, state);
204 return INVALID_COMMAND;