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); Serial.write((uint8_t)c);
return c; return c;
} }
static void setup_printf(int baud) {
Serial.begin(baud);
}
#else #else
static FILE uartout = {0} ; static FILE uartout = {0} ;
static int uart_putchar (char c, FILE *stream) { static int uart_putchar (char c, FILE *stream) {
Serial.write(c); Serial.write(c);
return 0; return 0;
} }
void setup_printf(int baud) {
Serial.begin(baud);
fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
stdout = &uartout;
}
#endif #endif
// HAL CPU Implementation // HAL CPU Implementation
@ -590,6 +582,12 @@ BTstackManager::BTstackManager(void){
adv_data_len = pos; adv_data_len = pos;
att_db_util_init(); 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)){ 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); 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 { class UUID {
private: private:
uint8_t uuid[16]; uint8_t uuid[16];

View File

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

View File

@ -4,25 +4,25 @@
// //
typedef struct characteristic_summary { typedef struct characteristic_summary {
UUID uuid; UUID uuid;
const char * name; const char * name;
bool found; bool found;
BLECharacteristic characteristic; BLECharacteristic characteristic;
} characteristic_summary_t; } characteristic_summary_t;
typedef enum characteristicIDs { typedef enum characteristicIDs {
charRX = 0, charRX = 0,
charTX, charTX,
charBaud, charBaud,
charBdAddr, charBdAddr,
numCharacteristics /* last one */ numCharacteristics /* last one */
} characteristicIDs_t; } characteristicIDs_t;
characteristic_summary characteristics[] = { characteristic_summary characteristics[] = {
{ UUID("f897177b-aee8-4767-8ecc-cc694fd5fcee"), "RX" }, { UUID("f897177b-aee8-4767-8ecc-cc694fd5fcee"), "RX" },
{ UUID("bf45e40a-de2a-4bc8-bba0-e5d6065f1b4b"), "TX" }, { UUID("bf45e40a-de2a-4bc8-bba0-e5d6065f1b4b"), "TX" },
{ UUID("2fbc0f31-726a-4014-b9fe-c8be0652e982"), "Baudrate" }, { UUID("2fbc0f31-726a-4014-b9fe-c8be0652e982"), "Baudrate" },
{ UUID("65c228da-bad1-4f41-b55f-3d177f4e2196"), "BD ADDR" } { UUID("65c228da-bad1-4f41-b55f-3d177f4e2196"), "BD ADDR" }
}; };
// BLE Shield Service V2 incl. used Characteristics // BLE Shield Service V2 incl. used Characteristics
@ -41,132 +41,143 @@ static timer_source_t heartbeat;
void setup(void){ void setup(void){
setup_printf(9600); Serial.begin(9600);
BTstack.setBLEAdvertisementCallback(advertisementCallback); BTstack.setBLEAdvertisementCallback(advertisementCallback);
BTstack.setBLEDeviceConnectedCallback(deviceConnectedCallback); BTstack.setBLEDeviceConnectedCallback(deviceConnectedCallback);
BTstack.setBLEDeviceDisconnectedCallback(deviceDisconnectedCallback); BTstack.setBLEDeviceDisconnectedCallback(deviceDisconnectedCallback);
BTstack.setGATTServiceDiscoveredCallback(gattServiceDiscovered); BTstack.setGATTServiceDiscoveredCallback(gattServiceDiscovered);
BTstack.setGATTCharacteristicDiscoveredCallback(gattCharacteristicDiscovered); BTstack.setGATTCharacteristicDiscoveredCallback(gattCharacteristicDiscovered);
BTstack.setGATTCharacteristicNotificationCallback(gattCharacteristicNotification); BTstack.setGATTCharacteristicNotificationCallback(gattCharacteristicNotification);
BTstack.setGATTCharacteristicReadCallback(gattReadCallback); BTstack.setGATTCharacteristicReadCallback(gattReadCallback);
BTstack.setGATTCharacteristicWrittenCallback(gattWrittenCallback); BTstack.setGATTCharacteristicWrittenCallback(gattWrittenCallback);
BTstack.setGATTCharacteristicSubscribedCallback(gattSubscribedCallback); BTstack.setGATTCharacteristicSubscribedCallback(gattSubscribedCallback);
BTstack.setup(); BTstack.setup();
BTstack.bleStartScanning(); BTstack.bleStartScanning();
} }
void loop(void){ void loop(void){
BTstack.loop(); BTstack.loop();
// send counter as fast as possible // send counter as fast as possible
if (sendCounter){ if (sendCounter){
sprintf(counterString, "BTstack %u\n", counter); sprintf(counterString, "BTstack %u\n", counter);
int result = myBLEDevice.writeCharacteristicWithoutResponse(&characteristics[charTX].characteristic, (uint8_t*) counterString, strlen(counterString) ); int result = myBLEDevice.writeCharacteristicWithoutResponse(&characteristics[charTX].characteristic, (uint8_t*) counterString, strlen(counterString) );
if (result == BLE_PERIPHERAL_OK){ if (result == BLE_PERIPHERAL_OK){
printf("Wrote without response: %s\n", counterString); Serial.print("Wrote without response: ");
counter++; Serial.println(counterString);
} counter++;
} }
}
} }
void advertisementCallback(BLEAdvertisement *bleAdvertisement) { void advertisementCallback(BLEAdvertisement *bleAdvertisement) {
printf("Device discovered: %s, RSSI: %d\n", bleAdvertisement->getBdAddr()->getAddressString(), bleAdvertisement->getRssi() ); Serial.print("Device discovered: ");
// if (bleAdvertisement->containsService(&bleShieldServiceV2UUID) && bleAdvertisement->nameHasPrefix("BLE-Shield")) { Serial.print(bleAdvertisement->getBdAddr()->getAddressString());
if (bleAdvertisement->containsService(&bleShieldServiceV2UUID)) { Serial.print(", RSSI: %d");
printf("\nBLE ShieldService V2 found!\n\n"); Serial.println(bleAdvertisement->getRssi());
BTstack.bleStopScanning(); if (bleAdvertisement->containsService(&bleShieldServiceV2UUID)) {
BTstack.bleConnect(bleAdvertisement, 10000); // 10 s Serial.println("\nBLE ShieldService V2 found!\n");
} BTstack.bleStopScanning();
BTstack.bleConnect(bleAdvertisement, 10000); // 10 s
}
} }
void deviceConnectedCallback(BLEStatus status, BLEDevice *device) { void deviceConnectedCallback(BLEStatus status, BLEDevice *device) {
switch (status){ switch (status){
case BLE_STATUS_OK: case BLE_STATUS_OK:
printf("Device connected!\n"); Serial.println("Device connected!");
myBLEDevice = *device; myBLEDevice = *device;
counter = 0; counter = 0;
myBLEDevice.discoverGATTServices(); myBLEDevice.discoverGATTServices();
break; break;
case BLE_STATUS_CONNECTION_TIMEOUT: case BLE_STATUS_CONNECTION_TIMEOUT:
printf("Error while Connecting the Peripheral\n"); Serial.println("Error while Connecting the Peripheral");
BTstack.bleStartScanning(); BTstack.bleStartScanning();
break; break;
default: default:
break; break;
} }
} }
void deviceDisconnectedCallback(BLEDevice * device){ void deviceDisconnectedCallback(BLEDevice * device){
printf("Disconnected, starting over..\n"); Serial.println("Disconnected, starting over..");
sendCounter = false; sendCounter = false;
BTstack.bleStartScanning(); BTstack.bleStartScanning();
} }
void gattServiceDiscovered(BLEStatus status, BLEDevice *device, BLEService *bleService) { void gattServiceDiscovered(BLEStatus status, BLEDevice *device, BLEService *bleService) {
switch(status){ switch(status){
case BLE_STATUS_OK: case BLE_STATUS_OK:
printf("Service Discovered: %s\n", bleService->getUUID()->getUuidString()); Serial.print("Service Discovered: :");
if (bleService->matches(&bleShieldServiceV2UUID)) { Serial.println(bleService->getUUID()->getUuidString());
serviceFound = true; if (bleService->matches(&bleShieldServiceV2UUID)) {
printf("Our service located!\n"); serviceFound = true;
myBLEService = *bleService; Serial.println("Our service located!");
} myBLEService = *bleService;
break; }
case BLE_STATUS_DONE: break;
printf("Service discovery finished\n"); case BLE_STATUS_DONE:
if (serviceFound) { Serial.println("Service discovery finished");
device->discoverCharacteristicsForService(&myBLEService); if (serviceFound) {
} device->discoverCharacteristicsForService(&myBLEService);
break; }
default: break;
printf("Service discovery error\n"); default:
break; Serial.println("Service discovery error");
} break;
}
} }
void gattCharacteristicDiscovered(BLEStatus status, BLEDevice *device, BLECharacteristic *characteristic) { void gattCharacteristicDiscovered(BLEStatus status, BLEDevice *device, BLECharacteristic *characteristic) {
switch(status){ switch(status){
case BLE_STATUS_OK: case BLE_STATUS_OK:
printf("Characteristic Discovered: %s, handle 0x%04x\n", characteristic->getUUID()->getUuidString(), characteristic->getCharacteristic()->value_handle); Serial.print("Characteristic Discovered: ");
int i; Serial.print(characteristic->getUUID()->getUuidString());
for (i=0;i<numCharacteristics;i++){ Serial.print(", handle 0x");
if (characteristic->matches(&characteristics[i].uuid)){ Serial.println(characteristic->getCharacteristic()->value_handle, HEX);
printf("\nCharacteristic '%s' found!\n", characteristics[i].name); int i;
characteristics[i].found = 1; for (i=0;i<numCharacteristics;i++){
characteristics[i].characteristic = *characteristic; if (characteristic->matches(&characteristics[i].uuid)){
break; Serial.print("Characteristic found: ");
} Serial.println(characteristics[i].name);
} characteristics[i].found = 1;
break; characteristics[i].characteristic = *characteristic;
case BLE_STATUS_DONE: break;
printf("Characteristic discovery finished, status %u.\n", status); }
if (characteristics[charRX].found) { }
device->subscribeForNotifications(&characteristics[charRX].characteristic); break;
} case BLE_STATUS_DONE:
break; Serial.print("Characteristic discovery finished, status ");
default: Serial.println(status, HEX);
printf("Characteristics discovery error\n"); if (characteristics[charRX].found) {
break; device->subscribeForNotifications(&characteristics[charRX].characteristic);
} }
break;
default:
Serial.println("Characteristics discovery error");
break;
}
} }
void gattSubscribedCallback(BLEStatus status, BLEDevice * device){ void gattSubscribedCallback(BLEStatus status, BLEDevice * device){
device->readCharacteristic(&characteristics[charBdAddr].characteristic); device->readCharacteristic(&characteristics[charBdAddr].characteristic);
} }
void gattReadCallback(BLEStatus status, BLEDevice *device, uint8_t *value, uint16_t length) { void gattReadCallback(BLEStatus status, BLEDevice *device, uint8_t *value, uint16_t length) {
printf("Read callback: '%s'\n", (const char *)value); Serial.print("Read callback: ");
device->writeCharacteristic(&characteristics[charTX].characteristic, (uint8_t*) "Hello!", 6); Serial.println((const char *)value);
device->writeCharacteristic(&characteristics[charTX].characteristic, (uint8_t*) "Hello!", 6);
} }
void gattWrittenCallback(BLEStatus status, BLEDevice *device){ void gattWrittenCallback(BLEStatus status, BLEDevice *device){
sendCounter = true; sendCounter = true;
} }
void gattCharacteristicNotification(BLEDevice *device, uint16_t value_handle, uint8_t *value, uint16_t length) { 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

@ -6,43 +6,43 @@ static uint16_t value_handle;
void setup(void){ void setup(void){
setup_printf(9600); setup_printf(9600);
// set callbacks // set callbacks
BTstack.setBLEDeviceConnectedCallback(deviceConnectedCallback); BTstack.setBLEDeviceConnectedCallback(deviceConnectedCallback);
BTstack.setBLEDeviceDisconnectedCallback(deviceDisconnectedCallback); BTstack.setBLEDeviceDisconnectedCallback(deviceDisconnectedCallback);
BTstack.setGATTCharacteristicRead(gattReadCallback); BTstack.setGATTCharacteristicRead(gattReadCallback);
BTstack.setGATTCharacteristicWrite(gattWriteCallback); BTstack.setGATTCharacteristicWrite(gattWriteCallback);
// setup GATT Database // setup GATT Database
int flags = 0; int flags = 0;
uint8_t * data = NULL; uint8_t * data = NULL;
uint16_t data_len = 0; uint16_t data_len = 0;
BTstack.addGATTService(new UUID("B8E06067-62AD-41BA-9231-206AE80AB550")); BTstack.addGATTService(new UUID("B8E06067-62AD-41BA-9231-206AE80AB550"));
value_handle = BTstack.addGATTCharacteristic(new UUID("f897177b-aee8-4767-8ecc-cc694fd5fcee"), flags, "This is a String!"); value_handle = BTstack.addGATTCharacteristic(new UUID("f897177b-aee8-4767-8ecc-cc694fd5fcee"), flags, "This is a String!");
// startup Bluetooth and activate advertisements // startup Bluetooth and activate advertisements
BTstack.setup(); BTstack.setup();
BTstack.startAdvertising(); BTstack.startAdvertising();
} }
void loop(void){ void loop(void){
BTstack.loop(); BTstack.loop();
} }
void deviceConnectedCallback(BLEStatus status, BLEDevice *device) { void deviceConnectedCallback(BLEStatus status, BLEDevice *device) {
switch (status){ switch (status){
case BLE_STATUS_OK: case BLE_STATUS_OK:
printf("Device connected!\n"); printf("Device connected!\n");
break; break;
default: default:
break; break;
} }
} }
void deviceDisconnectedCallback(BLEDevice * device){ void deviceDisconnectedCallback(BLEDevice * device){
printf("Disconnected.\n"); printf("Disconnected.\n");
} }
// ATT Client Read Callback for Dynamic Data // ATT Client Read Callback for Dynamic Data

View File

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

View File

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