* @date $Date$
*/
+#include "printf.h"
module TestPrintfC {
uses {
interface Boot;
}
implementation {
- #define NUM_TIMES_TO_PRINT 100
+ #define NUM_TIMES_TO_PRINT 5
uint16_t counter=0;
- uint32_t dummyVar = 345678;
+ uint8_t dummyVar1 = 123;
+ uint16_t dummyVar2 = 12345;
+ uint32_t dummyVar3 = 1234567890;
event void Boot.booted() {
call PrintfControl.start();
}
event void PrintfControl.startDone(error_t error) {
- printf("Hi my name is Kevin Klues and I am writing to you from my telos mote\n");
- printf("Here is a uint8: %hd\n", 123);
- printf("Here is a uint16: %d\n", 12345);
- printf("Here is a uint32: %ld\n", 1234567890);
+ printf("Hi I am writing to you from my TinyOS application!!\n");
+ printf("Here is a uint8: %u\n", dummyVar1);
+ printf("Here is a uint16: %u\n", dummyVar2);
+ printf("Here is a uint32: %ld\n", dummyVar3);
call PrintfFlush.flush();
}
event void PrintfControl.stopDone(error_t error) {
counter = 0;
+ call Leds.led2Toggle();
printf("This should not be printed...");
call PrintfFlush.flush();
}
}
else if(counter == NUM_TIMES_TO_PRINT) {
printf("This is a really short string...\n");
- printf("I am generating this string to have just less than 250 characters since that is the limit of the size I put on my maximum buffer when I instantiated the PrintfC component.\n");
- printf("Only part of this line should get printed because by writing this sentence, I go over my character limit that the internal Printf buffer can hold. If I were to flush before trying to write this, or increase my buffer size when I instantiate my PrintfC component to 1000, we would see this line too\n");
+ printf("I am generating this string to have just less than 250\ncharacters since that is the limit of the size I put on my\nmaximum buffer when I instantiated the PrintfC component.\n");
+ printf("Only part of this line should get printed because by writing\nthis sentence, I go over my character limit that the internal Printf buffer can hold.\n");
call PrintfFlush.flush();
}
else call PrintfControl.stop();
interface PrintfFlush;
}
uses {
- interface SplitControl as SerialControl;
- interface Leds;
+ interface SplitControl as SerialControl;
interface AMSend;
interface Packet;
}
}
implementation {
-
+
enum {
- S_STARTED,
- S_STOPPED,
- S_FLUSHING,
+ S_STARTED,
+ S_STOPPED,
+ S_FLUSHING,
};
message_t printfMsg;
void sendNext() {
printf_msg_t* m = (printf_msg_t*)call Packet.getPayload(&printfMsg, NULL);
length_to_send = (bytes_left_to_flush < sizeof(printf_msg_t)) ? bytes_left_to_flush : sizeof(printf_msg_t);
- memset(m->buffer, 0, sizeof(m->buffer));
+ memset(m->buffer, 0, sizeof(printf_msg_t));
memcpy(m->buffer, (uint8_t*)next_byte, length_to_send);
if(call AMSend.send(AM_BROADCAST_ADDR, &printfMsg, sizeof(printf_msg_t)) != SUCCESS)
post retrySend();
}
command error_t PrintfControl.start() {
- if(state == S_STOPPED)
+ if(state == S_STOPPED)
return call SerialControl.start();
return FAIL;
}
command error_t PrintfControl.stop() {
- if(state == S_STARTED)
+ if(state == S_STARTED)
return call SerialControl.stop();
return FAIL;
}
event void SerialControl.startDone(error_t error) {
- if(error != SUCCESS) {
- signal PrintfControl.startDone(error);
- return;
- }
+ if(error != SUCCESS) {
+ signal PrintfControl.startDone(error);
+ return;
+ }
#ifdef _H_atmega128hardware_H
- stdout = &atm128_stdout;
+ stdout = &atm128_stdout;
#endif
atomic {
memset(buffer, 0, sizeof(buffer));
}
event void SerialControl.stopDone(error_t error) {
- if(error != SUCCESS) {
- signal PrintfControl.stopDone(error);
- return;
- }
+ if(error != SUCCESS) {
+ signal PrintfControl.stopDone(error);
+ return;
+ }
atomic state = S_STOPPED;
signal PrintfControl.stopDone(error);
}
command error_t PrintfFlush.flush() {
- atomic {
- if(state == S_STARTED && (next_byte > buffer)) {
- state = S_FLUSHING;
- bytes_left_to_flush = next_byte - buffer;
- next_byte = buffer;
- }
- else return FAIL;
- }
- sendNext();
- return SUCCESS;
+ atomic {
+ if(state == S_STARTED && (next_byte > buffer)) {
+ state = S_FLUSHING;
+ bytes_left_to_flush = next_byte - buffer;
+ next_byte = buffer;
+ }
+ else return FAIL;
+ }
+ sendNext();
+ return SUCCESS;
}
event void AMSend.sendDone(message_t* msg, error_t error) {
- if(error == SUCCESS) {
- if(bytes_left_to_flush > 0)
- sendNext();
- else {
+ if(error == SUCCESS) {
+ if(bytes_left_to_flush > 0)
+ sendNext();
+ else {
next_byte = buffer;
bytes_left_to_flush = 0;
- length_to_send = 0;
+ length_to_send = 0;
atomic state = S_STARTED;
- signal PrintfFlush.flushDone(error);
- }
- }
- else post retrySend();
+ signal PrintfFlush.flushDone(error);
+ }
+ }
+ else post retrySend();
}
#ifdef _H_msp430hardware_h
#ifdef _H_atmega128hardware_H
int uart_putchar(char c, FILE *stream) __attribute__((noinline)) @C() @spontaneous() {
#endif
- atomic {
- if(state == S_STARTED && ((next_byte-buffer+1) < PRINTF_BUFFER_SIZE)) {
- *next_byte = c;
- next_byte++;
- return 0;
- }
- else return -1;
- }
+ atomic {
+ if(state == S_STARTED && ((next_byte-buffer) < PRINTF_BUFFER_SIZE)) {
+ *(next_byte++) = c;
+ return 0;
+ }
+ else return -1;
+ }
}
}