use Serial.print.. instead of printf in Arduino examples

This commit is contained in:
Matthias Ringwald 2015-05-29 20:46:52 +02:00
parent e2e6223a19
commit 52a6864fcb
7 changed files with 180 additions and 164 deletions

View File

@ -88,20 +88,12 @@ 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
@ -590,6 +582,12 @@ BTstackManager::BTstackManager(void){
adv_data_len = pos;
att_db_util_init();
#ifdef __AVR__
// configure stdout to go via Serial
fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
stdout = &uartout;
#endif
}
void BTstackManager::setBLEAdvertisementCallback(void (*callback)(BLEAdvertisement * bleAdvertisement)){

View File

@ -19,11 +19,6 @@ 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];

View File

@ -19,15 +19,18 @@ void ancs_callback(ancs_event_t * event){
const char * attribute_name;
switch (event->type){
case ANCS_CLIENT_CONNECTED:
printf("ANCS Client: Connected\n");
Serial.println("ANCS Client: Connected");
break;
case ANCS_CLIENT_DISCONNECTED:
printf("ANCS Client: Disconnected\n");
Serial.println("ANCS Client: Disconnected");
break;
case ANCS_CLIENT_NOTIFICATION:
attribute_name = ancs_client_attribute_name_for_id(event->attribute_id);
if (!attribute_name) break;
printf("Notification: %s - %s\n", attribute_name, event->text);
Serial.print("Notification: ");
Serial.print(attribute_name);
Serial.print(" - ");
Serial.println(event->text);
break;
default:
break;
@ -36,9 +39,8 @@ void ancs_callback(ancs_event_t * event){
void setup(void){
setup_printf(9600);
printf("BTstack ANCS Client starting up...\n");
Serial.begin(9600);
Serial.println("BTstack ANCS Client starting up...");
// startup BTstack and configure log_info/log_error
BTstack.setup();

View File

@ -41,7 +41,7 @@ static timer_source_t heartbeat;
void setup(void){
setup_printf(9600);
Serial.begin(9600);
BTstack.setBLEAdvertisementCallback(advertisementCallback);
BTstack.setBLEDeviceConnectedCallback(deviceConnectedCallback);
@ -67,17 +67,20 @@ void loop(void){
sprintf(counterString, "BTstack %u\n", counter);
int result = myBLEDevice.writeCharacteristicWithoutResponse(&characteristics[charTX].characteristic, (uint8_t*) counterString, strlen(counterString) );
if (result == BLE_PERIPHERAL_OK){
printf("Wrote without response: %s\n", counterString);
Serial.print("Wrote without response: ");
Serial.println(counterString);
counter++;
}
}
}
void advertisementCallback(BLEAdvertisement *bleAdvertisement) {
printf("Device discovered: %s, RSSI: %d\n", bleAdvertisement->getBdAddr()->getAddressString(), bleAdvertisement->getRssi() );
// if (bleAdvertisement->containsService(&bleShieldServiceV2UUID) && bleAdvertisement->nameHasPrefix("BLE-Shield")) {
Serial.print("Device discovered: ");
Serial.print(bleAdvertisement->getBdAddr()->getAddressString());
Serial.print(", RSSI: %d");
Serial.println(bleAdvertisement->getRssi());
if (bleAdvertisement->containsService(&bleShieldServiceV2UUID)) {
printf("\nBLE ShieldService V2 found!\n\n");
Serial.println("\nBLE ShieldService V2 found!\n");
BTstack.bleStopScanning();
BTstack.bleConnect(bleAdvertisement, 10000); // 10 s
}
@ -86,13 +89,13 @@ void advertisementCallback(BLEAdvertisement *bleAdvertisement) {
void deviceConnectedCallback(BLEStatus status, BLEDevice *device) {
switch (status){
case BLE_STATUS_OK:
printf("Device connected!\n");
Serial.println("Device connected!");
myBLEDevice = *device;
counter = 0;
myBLEDevice.discoverGATTServices();
break;
case BLE_STATUS_CONNECTION_TIMEOUT:
printf("Error while Connecting the Peripheral\n");
Serial.println("Error while Connecting the Peripheral");
BTstack.bleStartScanning();
break;
default:
@ -101,7 +104,7 @@ void deviceConnectedCallback(BLEStatus status, BLEDevice *device) {
}
void deviceDisconnectedCallback(BLEDevice * device){
printf("Disconnected, starting over..\n");
Serial.println("Disconnected, starting over..");
sendCounter = false;
BTstack.bleStartScanning();
}
@ -109,21 +112,22 @@ void deviceDisconnectedCallback(BLEDevice * device){
void gattServiceDiscovered(BLEStatus status, BLEDevice *device, BLEService *bleService) {
switch(status){
case BLE_STATUS_OK:
printf("Service Discovered: %s\n", bleService->getUUID()->getUuidString());
Serial.print("Service Discovered: :");
Serial.println(bleService->getUUID()->getUuidString());
if (bleService->matches(&bleShieldServiceV2UUID)) {
serviceFound = true;
printf("Our service located!\n");
Serial.println("Our service located!");
myBLEService = *bleService;
}
break;
case BLE_STATUS_DONE:
printf("Service discovery finished\n");
Serial.println("Service discovery finished");
if (serviceFound) {
device->discoverCharacteristicsForService(&myBLEService);
}
break;
default:
printf("Service discovery error\n");
Serial.println("Service discovery error");
break;
}
}
@ -131,11 +135,15 @@ void gattServiceDiscovered(BLEStatus status, BLEDevice *device, BLEService *bleS
void gattCharacteristicDiscovered(BLEStatus status, BLEDevice *device, BLECharacteristic *characteristic) {
switch(status){
case BLE_STATUS_OK:
printf("Characteristic Discovered: %s, handle 0x%04x\n", characteristic->getUUID()->getUuidString(), characteristic->getCharacteristic()->value_handle);
Serial.print("Characteristic Discovered: ");
Serial.print(characteristic->getUUID()->getUuidString());
Serial.print(", handle 0x");
Serial.println(characteristic->getCharacteristic()->value_handle, HEX);
int i;
for (i=0;i<numCharacteristics;i++){
if (characteristic->matches(&characteristics[i].uuid)){
printf("\nCharacteristic '%s' found!\n", characteristics[i].name);
Serial.print("Characteristic found: ");
Serial.println(characteristics[i].name);
characteristics[i].found = 1;
characteristics[i].characteristic = *characteristic;
break;
@ -143,13 +151,14 @@ void gattCharacteristicDiscovered(BLEStatus status, BLEDevice *device, BLECharac
}
break;
case BLE_STATUS_DONE:
printf("Characteristic discovery finished, status %u.\n", status);
Serial.print("Characteristic discovery finished, status ");
Serial.println(status, HEX);
if (characteristics[charRX].found) {
device->subscribeForNotifications(&characteristics[charRX].characteristic);
}
break;
default:
printf("Characteristics discovery error\n");
Serial.println("Characteristics discovery error");
break;
}
}
@ -159,7 +168,8 @@ void gattSubscribedCallback(BLEStatus status, BLEDevice * device){
}
void gattReadCallback(BLEStatus status, BLEDevice *device, uint8_t *value, uint16_t length) {
printf("Read callback: '%s'\n", (const char *)value);
Serial.print("Read callback: ");
Serial.println((const char *)value);
device->writeCharacteristic(&characteristics[charTX].characteristic, (uint8_t*) "Hello!", 6);
}
@ -168,5 +178,6 @@ void gattWrittenCallback(BLEStatus status, BLEDevice *device){
}
void gattCharacteristicNotification(BLEDevice *device, uint16_t value_handle, uint8_t *value, uint16_t length) {
printf("Notification: '%s'\n", (const char *)value);
Serial.print("Notification: ");
Serial.println((const char *)value);
}

View File

@ -5,7 +5,7 @@
UUID uuid("E2C56DB5-DFFB-48D2-B060-D0F5A71096E0");
void setup(void){
setup_printf(9600);
Serial.begin(9600);
BTstack.iBeaconConfigure(&uuid, 4711, 2);
BTstack.setup();
BTstack.startAdvertising();

View File

@ -3,7 +3,7 @@
#include <SPI.h>
void setup(void){
setup_printf(9600);
Serial.begin(9600);
BTstack.setBLEAdvertisementCallback(advertisementCallback);
BTstack.setup();
@ -17,13 +17,23 @@ void loop(void){
void advertisementCallback(BLEAdvertisement *adv) {
if (adv->isIBeacon()) {
printf("iBeacon found %s, RSSI %d, UUID %s, MajorID %u, MinorID %u, Measured Power %0x\n",
adv->getBdAddr()->getAddressString(), adv->getRssi() ,
adv->getIBeaconUUID()->getUuidString(), adv->getIBeaconMajorID(), adv->getIBecaonMinorID(),
adv->getiBeaconMeasuredPower());
Serial.print("iBeacon found ");
Serial.print(adv->getBdAddr()->getAddressString());
Serial.print(", RSSI ");
Serial.print(adv->getRssi());
Serial.print(", UUID ");
Serial.print(adv->getIBeaconUUID()->getUuidString());
Serial.print(", MajorID ");
Serial.print(adv->getIBeaconMajorID());
Serial.print(", MinorID ");
Serial.print(adv->getIBecaonMinorID());
Serial.print(", Measured Power ");
Serial.println(adv->getiBeaconMeasuredPower());
} else {
printf("Device discovered: %s, RSSI: %d\n", adv->getBdAddr()->getAddressString(), adv->getRssi() );
Serial.print("Device discovered: ");
Serial.print(adv->getBdAddr()->getAddressString());
Serial.print(", RSSI ");
Serial.println(adv->getRssi());
}
}