6 #define POLARITY PORTD2
8 #define BIT(x) (1<<(x))
17 OutputPacket packets[4];
19 uint8_t current_packet;
20 volatile uint8_t trigger_value;
21 volatile uint8_t packet_state = IDLE;
22 static uint8_t out_bit;
23 static uint8_t out_time;
24 static uint8_t out_data;
25 static uint8_t delay_time;
27 void output_init(void)
29 DDRD = (DDRD&0xF3)|0x0C;
30 PORTD &= ~BIT(ENABLE);
32 timer_start_hz(2, 80000, 1);
35 OutputPacket *output_create_packet(void)
37 while(packet_state!=IDLE) ;
39 packets[0].trigger_position = 0xFF;
43 OutputPacket *output_create_chained_packet(void)
45 uint8_t i = chain_length++;
46 packets[i].trigger_position = 0xFF;
50 void output_send_packet(void)
52 if(packet_state!=IDLE)
60 uint8_t output_get_trigger(void)
65 void output_set_power(uint8_t p)
70 PORTD &= ~BIT(ENABLE);
73 uint8_t output_is_power_on()
75 return (PORTD&BIT(ENABLE))!=0;
78 uint8_t output_command(const uint8_t *cmd_buf, uint8_t cmd_length)
80 if(cmd_buf[0]==POWER_ON || cmd_buf[0]==POWER_OFF)
85 output_set_power(cmd_buf[0]==POWER_ON);
87 else if(cmd_buf[0]==READ_POWER_STATE)
93 reply[0] = POWER_STATE;
94 reply[1] = output_is_power_on();
95 interface_send(reply, 2);
98 return INVALID_COMMAND;
103 static inline void output_tick(void)
105 if(delay_time && --delay_time)
108 if(out_time && !--out_time)
110 OutputPacket *packet = &packets[current_packet];
112 if(out_bit>=packet->length)
114 PORTD &= ~BIT(POLARITY);
115 if(--packet->repeat_count)
117 delay_time = packet->repeat_delay;
118 packet_state = READY;
122 delay_time = packet->final_delay;
123 if(++current_packet<chain_length)
124 packet_state = READY;
132 out_data = packet->data[out_bit>>3];
137 PORTD |= BIT(POLARITY);
139 PORTD &= ~BIT(POLARITY);
141 if(out_bit==packet->trigger_position)
142 trigger_value = packet->trigger_value;
144 out_time = packet->bit_duration;
150 if(packet_state==READY)
152 OutputPacket *packet = &packets[current_packet];
153 packet_state = SENDING;
155 out_time = packet->bit_duration;
156 out_data = packet->data[0];
158 PORTD |= BIT(POLARITY);
160 PORTD &= ~BIT(POLARITY);
164 TIMER_SET_CALLBACK(2, output_tick)