//* Outline for simple bidirectional communication *

//* Simple response to input *

initialize_buttons_io(); initialize_display_io(); while(forever) { uint8_t local; local = read_buttons(); write_display(local); }

//* ... and also send info out *

initialize_buttons_io(); initialize_display_io(); initialize_uart1_io(...); while(forever) { uint8_t local; local = read_buttons(); uart1_send(local); // note - this will take some time! display(local); }

//* ... and then also receive info if available *

initialize_buttons_io(); initialize_display_io(); initialize_uart1_io(...); while(forever) { uint8_t local, remote; local = read_buttons(); uart1_send(local); // Problem: we may not receive as often as we send remote = 0; // some neutral value if ( ready_RX_uart1() ) { remote = receive_uart1(); // Now process that information local = local | (remote << 4); } display(local); }

//* ... and report to the monitoring station that may or may not be listening *

initialize_buttons_io(); initialize_display_io(); initialize_uart1_io(...); initialize_uart0_io(...); while(forever) { uint8_t local; local = read_buttons(); uart1_send(local); // Problem: we may not receive as often as we send if ( ready_RX_uart1() ) { if ( noerrors_uart1() ) { uint8_t remote = receive_uart1(); // Now process that information local = local | (remote << 4); } else { receive_uart1(); } } display(local); { uint8_t reporter = count_ones_in_a_byte(local); // if it is 1 digit number then it can be really simple put_uart0( '0'+reporter ); put_uart( '\n' ); // otherwise use a buffer: char buffer[max_len+1]; itoa(buffer, reporter, 0x0A) puts_uart0(buffer); put_uart( '\n' ); } }