Refactor pingpair_test to remove all serial io from within the irq handler, as necessitated by Arduino 1.0
This commit is contained in:
@@ -230,7 +230,7 @@ void setup(void)
|
||||
// Dump the configuration of the rf unit for debugging
|
||||
//
|
||||
|
||||
radio.printDetails();
|
||||
//radio.printDetails();
|
||||
|
||||
//
|
||||
// Attach interrupt handler to interrupt #0 (using pin 2)
|
||||
@@ -238,8 +238,23 @@ void setup(void)
|
||||
//
|
||||
|
||||
attachInterrupt(0, check_radio, FALLING);
|
||||
|
||||
if ( role == role_receiver )
|
||||
printf("\n\r+OK ");
|
||||
}
|
||||
|
||||
//
|
||||
// Print buffer
|
||||
//
|
||||
// Printing from the interrupt handler is a bad idea, so we print from there
|
||||
// to this intermediate buffer
|
||||
//
|
||||
|
||||
char prbuf[1000];
|
||||
char *prbuf_end = prbuf + sizeof(prbuf);
|
||||
char *prbuf_in = prbuf;
|
||||
char *prbuf_out = prbuf;
|
||||
|
||||
//
|
||||
// Loop
|
||||
//
|
||||
@@ -274,7 +289,7 @@ void loop(void)
|
||||
delay(interval);
|
||||
|
||||
// Timeout if we have not received anything back ever
|
||||
if ( ! last_message_count && millis() > interval * 10 )
|
||||
if ( ! last_message_count && millis() > interval * 100 )
|
||||
{
|
||||
printf("No responses received. Are interrupts connected??\n\r");
|
||||
done = true;
|
||||
@@ -285,6 +300,17 @@ void loop(void)
|
||||
// Receiver role: Does nothing! All the work is in IRQ
|
||||
//
|
||||
|
||||
//
|
||||
// Spew print buffer
|
||||
//
|
||||
|
||||
size_t write_length = prbuf_in - prbuf_out;
|
||||
if ( write_length )
|
||||
{
|
||||
Serial.write(reinterpret_cast<uint8_t*>(prbuf_out),write_length);
|
||||
prbuf_out += write_length;
|
||||
}
|
||||
|
||||
//
|
||||
// Stop the test if we're done and report results
|
||||
//
|
||||
@@ -299,8 +325,6 @@ void loop(void)
|
||||
printf("FAIL\n\r\n\r");
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
}
|
||||
|
||||
void check_radio(void)
|
||||
@@ -313,10 +337,10 @@ void check_radio(void)
|
||||
if ( tx )
|
||||
{
|
||||
if ( role == role_sender )
|
||||
printf("Send:OK ");
|
||||
prbuf_in += sprintf(prbuf_in,"Send:OK ");
|
||||
|
||||
if ( role == role_receiver )
|
||||
printf("Ack Payload:Sent\n\r");
|
||||
prbuf_in += sprintf(prbuf_in,"Ack Payload:Sent\n\r");
|
||||
}
|
||||
|
||||
// Have we failed to transmit?
|
||||
@@ -324,14 +348,14 @@ void check_radio(void)
|
||||
{
|
||||
if ( role == role_sender )
|
||||
{
|
||||
printf("Send:Failed ");
|
||||
prbuf_in += sprintf(prbuf_in,"Send:Failed ");
|
||||
|
||||
// log status of this line
|
||||
one_failed();
|
||||
}
|
||||
|
||||
if ( role == role_receiver )
|
||||
printf("Ack Payload:Failed\n\r");
|
||||
prbuf_in += sprintf(prbuf_in,"Ack Payload:Failed\n\r");
|
||||
}
|
||||
|
||||
// Transmitter can power down for now, because
|
||||
@@ -346,19 +370,19 @@ void check_radio(void)
|
||||
if ( role == role_sender )
|
||||
{
|
||||
radio.read(&message_count,sizeof(message_count));
|
||||
printf("Ack:%lu ",message_count);
|
||||
prbuf_in += sprintf(prbuf_in,"Ack:%lu ",message_count);
|
||||
|
||||
// is this ack what we were expecting? to account
|
||||
// for failures, we simply want to make sure we get a
|
||||
// DIFFERENT ack every time.
|
||||
if ( ( message_count != last_message_count ) || ( configuration=='3' && message_count == 16 ) )
|
||||
{
|
||||
printf("OK ");
|
||||
prbuf_in += sprintf(prbuf_in,"OK ");
|
||||
one_ok();
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("FAILED ");
|
||||
prbuf_in += sprintf(prbuf_in,"FAILED ");
|
||||
one_failed();
|
||||
}
|
||||
last_message_count = message_count;
|
||||
@@ -382,7 +406,7 @@ void check_radio(void)
|
||||
receive_payload[len] = 0;
|
||||
|
||||
// Spew it
|
||||
printf("Got payload size=%i value=%s strlen=%u\n\r",len,receive_payload,strlen(receive_payload));
|
||||
prbuf_in += sprintf(prbuf_in,"Recv size=%i val=%s len=%u\n\r",len,receive_payload,strlen(receive_payload));
|
||||
|
||||
// Add an ack packet for the next time around.
|
||||
// Here we will report back how many bytes we got this time.
|
||||
|
||||
Reference in New Issue
Block a user