]> git.tdb.fi Git - model-railway-devices.git/blob - arducontrol/output.c
c1d51488349359ae9101a9762c2837bf257aebce
[model-railway-devices.git] / arducontrol / output.c
1 #include <avr/io.h>
2 #include "interface.h"
3 #include "output.h"
4 #include "timer.h"
5
6 OutputPacket packet;
7 uint8_t out_bit;
8 uint8_t out_time;
9 uint8_t out_data;
10 uint8_t delay_time;
11
12 void output_init()
13 {
14         DDRD = (DDRD&0xF3)|0x0C;
15         PORTD &= ~0x08;
16
17         timer_start_hz(0, 80000, 1);
18 }
19
20 void clear_packet()
21 {
22         packet.ready = 0;
23         packet.sending = 0;
24         packet.done = 0;
25 }
26
27 void output_set_power(uint8_t p)
28 {
29         if(p==POWER_ON)
30                 PORTD |= 0x08;
31         else
32                 PORTD &= ~0x08;
33 }
34
35 uint8_t output_command()
36 {
37         if(cmd_buf[0]==POWER_ON || cmd_buf[0]==POWER_OFF)
38         {
39                 if(cmd_length!=1)
40                         return LENGTH_ERROR;
41
42                 output_set_power(cmd_buf[0]==POWER_ON);
43         }
44         else
45                 return INVALID_COMMAND;
46
47         return COMMAND_OK;
48 }
49
50 static inline void output_tick()
51 {
52         if(delay_time && --delay_time)
53                 return;
54
55         if(out_time && !--out_time)
56         {
57                 ++out_bit;
58                 if(out_bit>=packet.length)
59                 {
60                         PORTD &= ~0x04;
61                         if(packet.repeat_count>1)
62                         {
63                                 if(packet.repeat_count<0xFF)
64                                         --packet.repeat_count;
65                                 delay_time = packet.repeat_delay;
66                                 packet.sending = 0;
67                         }
68                         else
69                         {
70                                 delay_time = packet.final_delay;
71                                 packet.done = 1;
72                         }
73                 }
74                 else
75                 {
76                         if((out_bit&7)==0)
77                                 out_data = packet.data[out_bit>>3];
78                         else
79                                 out_data >>= 1;
80
81                         if(out_data&1)
82                                 PORTD |= 0x04;
83                         else
84                                 PORTD &= ~0x04;
85
86                         out_time = packet.bit_duration;
87                 }
88
89                 return;
90         }
91
92         if(packet.ready && !packet.sending)
93         {
94                 packet.sending = 1;
95                 out_bit = 0;
96                 out_time = packet.bit_duration;
97                 out_data = packet.data[0];
98                 if(out_data&1)
99                         PORTD |= 0x04;
100                 else
101                         PORTD &= ~0x04;
102         }
103 }
104
105 TIMER_SET_CALLBACK(0, output_tick)