Code for the Nordic nrf51822 component of our project. For project group Alloy.

Dependencies:   BLE_API mbed nRF51822

Fork of idd_hw5_group_alloy_BLE by Doug Cook

Files at this revision

API Documentation at this revision

Comitter:
douglasc
Date:
Mon Nov 10 17:27:47 2014 +0000
Parent:
8:5818566e1e8a
Commit message:
initial commit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 5818566e1e8a -r cd8186239b8e main.cpp
--- a/main.cpp	Tue Sep 30 02:20:59 2014 +0000
+++ b/main.cpp	Mon Nov 10 17:27:47 2014 +0000
@@ -1,42 +1,33 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Interactive Device Design, Fall 2014
+ * Homework 5 - Wind and Window controller
+ * Nordic nrf51822 Board
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * Ian Shain
+ * Doug Cook
+ * Kiyana Salkeld
+ * Elizabeth Lin
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
  */
 
 #include "mbed.h"
 #include "BLEDevice.h"
-
+#include "HealthThermometerService.h"
 #include "UARTService.h"
-
-#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
-                               * it will have an impact on code-size and power consumption. */
-
-#if NEED_CONSOLE_OUTPUT
-#define DEBUG(...) { printf(__VA_ARGS__); }
-#else
-#define DEBUG(...) /* nothing */
-#endif /* #if NEED_CONSOLE_OUTPUT */
+#include <string>
 
 BLEDevice  ble;
 DigitalOut led1(LED1);
+DigitalOut led2(LED2);
 
 UARTService *uartServicePtr;
 
+Serial kl25z(P0_9, P0_11); // P0_9 (gray) -> TX, P0_11 (purple) -> RX
+
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
-    DEBUG("Disconnected!\n\r");
-    DEBUG("Restarting the advertising process\n\r");
+    kl25z.putc('h');
+    kl25z.putc('f');
     ble.startAdvertising();
 }
 
@@ -44,14 +35,28 @@
 {
     if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
         uint16_t bytesRead = params->len;
-        DEBUG("received %u bytes\n\r", bytesRead);
+        if (bytesRead <= 1) {
+            kl25z.putc('c');
+        } else {
+            kl25z.putc('d');
+        }
         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
     }
 }
 
+bool updateWindAdvertisement = false;
+
 void periodicCallback(void)
 {
     led1 = !led1;
+    updateWindAdvertisement = true;
+}
+
+bool newMessage = false;
+string measurement = "";
+void callback() {
+    newMessage = true;
+    measurement += kl25z.getc();
 }
 
 int main(void)
@@ -59,17 +64,24 @@
     led1 = 1;
     Ticker ticker;
     ticker.attach(periodicCallback, 1);
+    
+    kl25z.baud(9600);
+    kl25z.format(8,Serial::Odd,1);
+    kl25z.attach(callback);
 
-    DEBUG("Initialising the nRF51822\n\r");
+    //pc.printf("Initialising the nRF51822\n\r");
+    kl25z.putc('e');
     ble.init();
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(onDataWritten);
+    
+    HealthThermometerService thermometerService(ble, 0, HealthThermometerService::LOCATION_EAR);
 
     /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+                                     (const uint8_t *)"AlloyWindow", sizeof("AlloyWindow") - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
 
@@ -80,6 +92,14 @@
     uartServicePtr = &uartService;
 
     while (true) {
-        ble.waitForEvent();
+        if (newMessage) {
+            led2 = !led2;   // data receiver notification
+            // broadcast wind speed value
+            thermometerService.updateTemperature(atoi(measurement.c_str()));
+            measurement = "";
+            newMessage = false;
+        } else {
+            ble.waitForEvent();
+        }
     }
 }
diff -r 5818566e1e8a -r cd8186239b8e nRF51822.lib
--- a/nRF51822.lib	Tue Sep 30 02:20:59 2014 +0000
+++ b/nRF51822.lib	Mon Nov 10 17:27:47 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#936d81c963fe
+http://developer.mbed.org/users/douglasc/code/nRF51822/#530ce8db5ff5