diff --git a/platforms/arduino/BTstack.cpp b/platforms/arduino/BTstack.cpp index d86422b2b..21bf1b199 100644 --- a/platforms/arduino/BTstack.cpp +++ b/platforms/arduino/BTstack.cpp @@ -81,6 +81,29 @@ static void (*gattCharacteristicWrittenCallback)(BLEStatus status, BLEDevice * d static void (*gattCharacteristicSubscribedCallback)(BLEStatus status, BLEDevice * device) = NULL; static void (*gattCharacteristicUnsubscribedCallback)(BLEStatus status, BLEDevice * device) = NULL; + +// retarget printf to Serial +#ifdef ENERGIA +extern "C" int putchar(int c) { + Serial.write((uint8_t)c); + return c; +} +static void setup_printf(int baud) { + Serial.begin(baud); +} +#else +static FILE uartout = {0} ; +static int uart_putchar (char c, FILE *stream) { + Serial.write(c); + return 0; +} +void setup_printf(int baud) { + Serial.begin(baud); + fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); + stdout = &uartout; +} +#endif + // HAL CPU Implementation extern "C" void hal_cpu_disable_irqs(void){ } extern "C" void hal_cpu_enable_irqs(void) { } diff --git a/platforms/arduino/BTstack.h b/platforms/arduino/BTstack.h index 1f5eb3964..919eb41df 100644 --- a/platforms/arduino/BTstack.h +++ b/platforms/arduino/BTstack.h @@ -19,6 +19,11 @@ typedef enum BLEStatus { typedef void (*btstack_packet_handler_t) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); +/** + * @brief Setup printf to use Serial with given baud rate + */ +void setup_printf(int baud); + class UUID { private: uint8_t uuid[16]; diff --git a/platforms/arduino/examples/ANCS/ANCS.ino b/platforms/arduino/examples/ANCS/ANCS.ino index 8e054e33d..793122914 100644 --- a/platforms/arduino/examples/ANCS/ANCS.ino +++ b/platforms/arduino/examples/ANCS/ANCS.ino @@ -15,30 +15,6 @@ const uint8_t adv_data[] = { 0x11,0x15,0xD0,0x00,0x2D,0x12,0x1E,0x4B,0x0F,0xA4,0x99,0x4E,0xCE,0xB5,0x31,0xF4,0x05,0x79 }; -// retarget printf -#ifdef ENERGIA -extern "C" { - int putchar(int c) { - Serial.write((uint8_t)c); - return c; - } -} -static void setup_printf(void) { - Serial.begin(9600); -} -#else -static FILE uartout = {0} ; -static int uart_putchar (char c, FILE *stream) { - Serial.write(c); - return 0; -} -static void setup_printf(void) { - Serial.begin(9600); - fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); - stdout = &uartout; -} -#endif - static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ printf("packet_handler type %u, event 0x%02x\n", packet_type, packet[0]); ancs_client_hci_event_handler(packet_type, channel, packet, size); @@ -65,7 +41,7 @@ void ancs_callback(ancs_event_t * event){ void setup(void){ - setup_printf(); + setup_printf(9600); printf("BTstack ANCS Client starting up...\n"); diff --git a/platforms/arduino/examples/LECentral/LECentral.ino b/platforms/arduino/examples/LECentral/LECentral.ino index 4234b0250..87665ac60 100644 --- a/platforms/arduino/examples/LECentral/LECentral.ino +++ b/platforms/arduino/examples/LECentral/LECentral.ino @@ -39,18 +39,6 @@ char counterString[20]; static timer_source_t heartbeat; -// setup printf -static FILE uartout = {0} ; -static int uart_putchar (char c, FILE *stream) { - Serial.write(c); - return 0; -} -static void setup_printf(int baud) { - Serial.begin(baud); - fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); - stdout = &uartout; -} - void setup(void){ setup_printf(9600); diff --git a/platforms/arduino/examples/LEPeripheral/LEPeripheral.ino b/platforms/arduino/examples/LEPeripheral/LEPeripheral.ino index ffd333d9e..54c8354cd 100644 --- a/platforms/arduino/examples/LEPeripheral/LEPeripheral.ino +++ b/platforms/arduino/examples/LEPeripheral/LEPeripheral.ino @@ -2,18 +2,6 @@ #include #include -// setup printf -static FILE uartout = {0} ; -static int uart_putchar (char c, FILE *stream) { - Serial.write(c); - return 0; -} -static void setup_printf(int baud) { - Serial.begin(baud); - fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); - stdout = &uartout; -} - static uint16_t value_handle; void setup(void){ diff --git a/platforms/arduino/examples/iBeacon/iBeacon.ino b/platforms/arduino/examples/iBeacon/iBeacon.ino index aa11de417..b4c9e4727 100644 --- a/platforms/arduino/examples/iBeacon/iBeacon.ino +++ b/platforms/arduino/examples/iBeacon/iBeacon.ino @@ -4,32 +4,8 @@ UUID uuid("E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"); -// retarget printf -#ifdef ENERGIA -extern "C" { -int putchar(int c) { - Serial.write((uint8_t)c); - return c; -} -} -static void setup_printf(void) { - Serial.begin(9600); -} -#else -static FILE uartout = {0} ; -static int uart_putchar (char c, FILE *stream) { - Serial.write(c); - return 0; -} -static void setup_printf(void) { - Serial.begin(9600); - fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); - stdout = &uartout; -} -#endif - void setup(void){ - setup_printf(); + setup_printf(9600); BTstack.iBeaconConfigure(&uuid, 4711, 2); BTstack.setup(); BTstack.startAdvertising(); diff --git a/platforms/arduino/examples/iBeaconScanner/iBeaconScanner.ino b/platforms/arduino/examples/iBeaconScanner/iBeaconScanner.ino index 3a974c21b..539764cc2 100644 --- a/platforms/arduino/examples/iBeaconScanner/iBeaconScanner.ino +++ b/platforms/arduino/examples/iBeaconScanner/iBeaconScanner.ino @@ -2,18 +2,6 @@ #include #include -// setup printf -static FILE uartout = {0} ; -static int uart_putchar (char c, FILE *stream) { - Serial.write(c); - return 0; -} -static void setup_printf(int baud) { - Serial.begin(baud); - fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); - stdout = &uartout; -} - void setup(void){ setup_printf(9600);