3 #include "ringbuffer.h"
12 #define BIT(x) (1<<(x))
14 volatile uint8_t s88_read_count = 0;
15 uint8_t s88_read_bit = 0;
16 volatile uint8_t s88_read_phase = 0;
18 RINGBUFFER(s88_buffer, 8);
19 uint8_t s88_out_index = 0;
23 DDRD = (DDRD&0x0F)|0x70;
26 timer_start_hz(0, 80000, 1);
31 // Only send one packet per check to avoid blocking
32 if(ringbuffer_fill(s88_buffer)>0)
36 reply[1] = s88_out_index++;
37 reply[2] = ringbuffer_pop(s88_buffer);
38 interface_send(reply, sizeof(reply));
42 uint8_t s88_command(const uint8_t *cmd_buf, uint8_t cmd_length)
44 if(cmd_buf[0]==S88_READ)
49 while(s88_read_count || s88_read_phase) ;
52 s88_read_phase = 0xF0;
53 s88_read_count = cmd_buf[1];
56 return INVALID_COMMAND;
61 static inline void s88_tick(void)
65 if(!s88_read_count || ringbuffer_space(s88_buffer)==0)
70 else if(s88_read_phase==1)
72 uint8_t bit = (PIND>>7)&1;
73 s88_data = (s88_data<<1)|bit;
77 ringbuffer_push(s88_buffer, s88_data);
83 else if(s88_read_phase==2)
85 else if(s88_read_phase>3)
87 if(s88_read_phase==0xF0)
89 else if(s88_read_phase==0xF4)
91 else if(s88_read_phase==0xF6)
93 else if(s88_read_phase==0xFA)
95 else if(s88_read_phase==0xFC)
97 else if(s88_read_phase==0xFF)
99 s88_data = (PIND>>7)&1;
107 s88_read_phase = (s88_read_phase+1)&3;
110 TIMER_SET_CALLBACK(0, s88_tick)