From c486557477276a292374437c90286520195b400f Mon Sep 17 00:00:00 2001 From: maniacbug Date: Sun, 1 Jan 2012 19:11:39 -0800 Subject: [PATCH 1/2] Initialize printf and use progmem for static strings --- examples/uip_hello_world/printf.h | 37 ++++++++++++++++++++ examples/uip_hello_world/uip_hello_world.pde | 8 +++-- 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 examples/uip_hello_world/printf.h diff --git a/examples/uip_hello_world/printf.h b/examples/uip_hello_world/printf.h new file mode 100644 index 0000000..b2efd56 --- /dev/null +++ b/examples/uip_hello_world/printf.h @@ -0,0 +1,37 @@ +/* + Copyright (C) 2011 J. Coliz + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 2 as published by the Free Software Foundation. + */ + +/** + * @file printf.h + * + * Setup necessary to direct stdout to the Arduino Serial library, which + * enables 'printf' + */ + +#ifndef __PRINTF_H__ +#define __PRINTF_H__ + +#ifdef ARDUINO + +int serial_putc( char c, FILE * ) +{ + Serial.write( c ); + + return c; +} + +void printf_begin(void) +{ + fdevopen( &serial_putc, 0 ); +} + +#else +#error This example is only for use on Arduino. +#endif // ARDUINO + +#endif // __PRINTF_H__ diff --git a/examples/uip_hello_world/uip_hello_world.pde b/examples/uip_hello_world/uip_hello_world.pde index 8a864ea..f1e8c5b 100644 --- a/examples/uip_hello_world/uip_hello_world.pde +++ b/examples/uip_hello_world/uip_hello_world.pde @@ -1,6 +1,7 @@ #include #include #include +#include "printf.h" struct hello_world_state { struct psock p; @@ -85,17 +86,18 @@ void setup() { NanodeUNIO unio(NANODE_MAC_DEVICE); Serial.begin(38400); - Serial.println("UIP test"); + printf_begin(); + printf_P(PSTR(__FILE__"\r\n")); unio.read(macaddr,NANODE_MAC_ADDRESS,6); uip.init(macaddr); uip.get_mac_str(buf); Serial.println(buf); uip.wait_for_link(); - Serial.println("Link is up"); + printf_P(PSTR("Link is up\r\n")); uip.init_resolv(resolv_found); uip.start_dhcp(dhcp_status); - Serial.println("setup() done"); + printf_P(PSTR("setup() done\r\n")); } void loop() { From f986a042595bf83d3c45d2a01e68c1c8c2a16f8b Mon Sep 17 00:00:00 2001 From: maniacbug Date: Sun, 1 Jan 2012 20:03:20 -0800 Subject: [PATCH 2/2] Use progmem for logging --- NanodeUIP.cpp | 7 ++++++- NanodeUIP.h | 3 +++ examples/uip_hello_world/uip_hello_world.pde | 4 ++-- platform.cpp | 5 +++++ uip.cpp | 5 +++-- uipopt.h | 2 ++ 6 files changed, 21 insertions(+), 5 deletions(-) diff --git a/NanodeUIP.cpp b/NanodeUIP.cpp index aec8343..cac2746 100644 --- a/NanodeUIP.cpp +++ b/NanodeUIP.cpp @@ -1,5 +1,7 @@ #include "NanodeUIP.h" +#include +#include #include #include "uip.h" @@ -8,12 +10,15 @@ #include "dhcpc.h" #include "resolv.h" -void nanode_log(char *msg); void nanode_log(char *msg) { Serial.println(msg); } +void nanode_log_P(PGM_P msg) { + printf_P(PSTR("%lu: %S\r\n"),millis(),msg); +} + void dhcpc_configured(const struct dhcpc_state *s) { uip_sethostaddr(s->ipaddr); uip_setnetmask(s->netmask); diff --git a/NanodeUIP.h b/NanodeUIP.h index 5d424b7..a0f9526 100644 --- a/NanodeUIP.h +++ b/NanodeUIP.h @@ -6,11 +6,14 @@ #else #include // Arduino 0022 #endif +#include #include "uip.h" #include "timer.h" extern void resolv_conf(const uint16_t *dnsserver); +extern void nanode_log(char *msg); +extern void nanode_log_P(PGM_P msg); #define DHCP_STATUS_OK 1 #define DHCP_STATUS_DOWN 0 diff --git a/examples/uip_hello_world/uip_hello_world.pde b/examples/uip_hello_world/uip_hello_world.pde index f1e8c5b..dcb1f50 100644 --- a/examples/uip_hello_world/uip_hello_world.pde +++ b/examples/uip_hello_world/uip_hello_world.pde @@ -94,10 +94,10 @@ void setup() { uip.get_mac_str(buf); Serial.println(buf); uip.wait_for_link(); - printf_P(PSTR("Link is up\r\n")); + nanode_log_P(PSTR("Link is up")); uip.init_resolv(resolv_found); uip.start_dhcp(dhcp_status); - printf_P(PSTR("setup() done\r\n")); + nanode_log_P(PSTR("setup() done")); } void loop() { diff --git a/platform.cpp b/platform.cpp index 197c9e0..c2e328d 100644 --- a/platform.cpp +++ b/platform.cpp @@ -1,3 +1,4 @@ +#include #include "clock.h" #include "uip-conf.h" @@ -14,7 +15,11 @@ clock_time_t clock_time(void) { void nullproc(void) {} extern void nanode_log(char *msg); +extern void nanode_log_P(PGM_P msg); void uip_log(char *msg) { nanode_log(msg); } +void uip_log_P(PGM_P msg) { + nanode_log_P(msg); +} diff --git a/uip.cpp b/uip.cpp index fe39330..8e577a9 100644 --- a/uip.cpp +++ b/uip.cpp @@ -240,9 +240,10 @@ struct uip_stats uip_stat; #endif /* UIP_STATISTICS == 1 */ #if UIP_LOGGING == 1 +#include #include -void uip_log(char *msg); -#define UIP_LOG(m) uip_log(m) +void uip_log_P(PGM_P msg); +#define UIP_LOG(m) uip_log_P(PSTR(m)) #else #define UIP_LOG(m) #endif /* UIP_LOGGING == 1 */ diff --git a/uipopt.h b/uipopt.h index b1ef8e5..07343ed 100644 --- a/uipopt.h +++ b/uipopt.h @@ -67,6 +67,7 @@ #define UIP_BIG_ENDIAN 1234 #endif /* UIP_BIG_ENDIAN */ +#include #include "uip-conf.h" /*------------------------------------------------------------------------------*/ @@ -432,6 +433,7 @@ * is called by uIP whenever a log message is generated. */ void uip_log(char *msg); +void uip_log_P(PGM_P msg); /** * The link level header length.