5 static uint8_t motorola_speed_to_value(uint8_t speed)
15 static OutputPacket *motorola_common_packet(uint8_t addr, uint8_t aux, uint8_t value)
17 OutputPacket *packet = output_create_packet();
19 packet->bit_duration = 2;
20 packet->length = 18*8;
22 for(uint8_t i=0; i<4; ++i)
27 packet->data[i*2] = (d==0 ? 0x01 : 0x7F);
28 packet->data[i*2+1] = (d==1 ? 0x7F : 0x01);
31 packet->data[8] = (aux ? 0x7F : 0x01);
32 packet->data[9] = packet->data[8];
34 for(uint8_t i=0; i<4; ++i)
36 packet->data[10+i*2] = ((value&1) ? 0x7F : 0x01);
40 packet->repeat_count = 2;
41 // Duration of three trits
42 packet->repeat_delay = 96;
43 packet->final_delay = 224;
48 static OutputPacket *motorola_old_packet(uint8_t addr, uint8_t aux, uint8_t value)
50 OutputPacket *packet = motorola_common_packet(addr, aux, value);
52 for(uint8_t i=0; i<4; ++i)
53 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));
64 void motorola_locomotive_reverse_packet(uint8_t addr, uint8_t aux)
66 motorola_old_packet(addr, aux, 1);
70 void motorola_locomotive_speed_direction_packet(uint8_t addr, uint8_t aux, uint8_t speed, uint8_t dir)
72 OutputPacket *packet = motorola_common_packet(addr, aux, motorola_speed_to_value(speed));
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];
82 void motorola_locomotive_speed_function_packet(uint8_t addr, uint8_t aux, uint8_t speed, uint8_t func, uint8_t state)
84 uint8_t value = motorola_speed_to_value(speed);
85 OutputPacket *packet = motorola_common_packet(addr, aux, value);
99 func = ((value&8) ? 2 : 5) | (func&8);
101 for(uint8_t i=0; i<4; ++i)
103 packet->data[11+i*2] = ((func&1) ? 0x7F : 0x01);
107 output_send_packet();
110 void motorola_solenoid_packet(uint8_t addr, uint8_t output, uint8_t state)
112 uint8_t value = output;
116 OutputPacket *packet = motorola_old_packet(addr, 0, value);
117 packet->repeat_delay >>= 1;
118 packet->bit_duration = 1;
120 output_send_packet();
123 uint8_t motorola_command(const uint8_t *cmd_buf, uint8_t cmd_length)
125 if(cmd_buf[0]==MOTOROLA_SPEED || cmd_buf[0]==MOTOROLA_SPEED_DIRECTION || cmd_buf[0]==MOTOROLA_SPEED_FUNCTION)
130 uint8_t addr = cmd_buf[1];
132 return INVALID_VALUE;
135 return INVALID_VALUE;
136 uint8_t aux = cmd_buf[2]&0x01;
138 uint8_t func = (cmd_buf[2]&0xF0)>>4;
139 if(cmd_buf[0]==MOTOROLA_SPEED_FUNCTION)
142 return INVALID_VALUE;
144 else if(cmd_buf[2]&0xFE)
145 return INVALID_VALUE;
146 uint8_t state = cmd_buf[2]&0x02;
148 uint8_t speed = cmd_buf[3]&0x7F;
150 return INVALID_VALUE;
152 uint8_t dir = !(cmd_buf[3]&0x80);
154 if(cmd_buf[0]==MOTOROLA_SPEED)
155 motorola_locomotive_speed_packet(addr, aux, speed);
156 else if(cmd_buf[0]==MOTOROLA_SPEED_DIRECTION)
157 motorola_locomotive_speed_direction_packet(addr, aux, speed, dir);
158 else if(cmd_buf[0]==MOTOROLA_SPEED_FUNCTION)
159 motorola_locomotive_speed_function_packet(addr, aux, speed, func, state);
161 else if(cmd_buf[0]==MOTOROLA_REVERSE)
166 uint8_t addr = cmd_buf[1];
168 return INVALID_VALUE;
171 return INVALID_VALUE;
172 uint8_t aux = cmd_buf[2]&0x01;
174 motorola_locomotive_reverse_packet(addr, aux);
176 else if(cmd_buf[0]==MOTOROLA_SOLENOID)
181 uint8_t addr = cmd_buf[1];
183 return INVALID_VALUE;
186 return INVALID_VALUE;
187 uint8_t output = (cmd_buf[2]&0x70)>>4;
188 uint8_t state = cmd_buf[2]&1;
190 motorola_solenoid_packet(addr, output, state);
193 return INVALID_COMMAND;