Aidan Jabbarzadeh / Mbed 2 deprecated Transmitter

Dependencies:   mbed BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers transmitter_main.cpp Source File

transmitter_main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 
00004 Serial pc(USBTX, USBRX);
00005 
00006 #define LED_RED     p22
00007 #define LED_GREEN   p21
00008 #define LED_BLUE    p23
00009 #define BUTTON_PIN  p17
00010 #define BATTERY_PIN p1
00011 
00012 DigitalOut greenLed(LED_GREEN);
00013 
00014 struct Packet {
00015     uint8_t seqNum;
00016     
00017     Packet() : seqNum(0) {}
00018 };
00019 uint8_t seqNum2 = 7;
00020 
00021 BLE ble;
00022 Packet packet;
00023 const static char DEVICE_NAME[] = "Transmitter";
00024 static volatile bool  triggerSensorPolling = false;
00025 
00026 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00027 {
00028     ble.gap().startAdvertising(); // restart advertising
00029 }
00030 
00031 void periodicCallback(void)
00032 {
00033     greenLed = !greenLed; /* Do blinky on greenLed while we're waiting for BLE events */
00034     //we can't do anything related gap() inside here!
00035     //will get error and fault handler will be triggered
00036     
00037     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
00038      * heavy-weight sensor polling from the main thread. */
00039     triggerSensorPolling = true;
00040 }
00041 
00042 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00043 {
00044     BLE &localBle          = params->ble;
00045     ble_error_t error = params->error;
00046     
00047     if (error != BLE_ERROR_NONE) {
00048         return;
00049     }
00050     
00051     /* Ensure that it is the default instance of BLE */
00052     if (localBle.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00053         return;   
00054     }
00055     
00056     localBle.gap().onDisconnection(disconnectionCallback);
00057 
00058 //    localBle.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00059 //    localBle.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00060 //    localBle.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&packet, sizeof(packet));
00061     localBle.gap().clearAdvertisingPayload();
00062     localBle.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&packet.seqNum, sizeof(packet.seqNum));
00063     pc.printf("73: payload: %x\n", localBle.gap().getAdvertisingPayload());
00064 }
00065 
00066 int main(void)
00067 {
00068     greenLed = 1;
00069     
00070     Ticker ticker;
00071     ticker.attach(periodicCallback, 5);
00072     
00073     ble.init(bleInitComplete);
00074     
00075     /* SpinWait for initialization to complete. This is necessary because the
00076      * BLE object is used in the main loop below. */
00077     while (ble.hasInitialized()  == false) {
00078          pc.printf("Initializing BLE\n");
00079     }
00080     
00081     pc.printf("Init Completed\n");
00082     
00083     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00084     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00085     ble.gap().setAdvertisingTimeout(0);
00086     ble.gap().startAdvertising();
00087     
00088     while (1) {
00089         // check for trigger from periodicCallback()
00090         if (triggerSensorPolling) {
00091             (packet.seqNum)++;
00092             pc.printf("seq_num: %d\n", packet.seqNum);
00093             ble.gap().clearAdvertisingPayload();
00094             ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&packet.seqNum, sizeof(packet.seqNum));
00095             pc.printf("47: payload: %x\n", ble.gap().getAdvertisingPayload());
00096             
00097             triggerSensorPolling = false;
00098         } 
00099         ble.waitForEvent();
00100     }
00101 }