Basic output of force sensor data to serial, in form "value1/n"

Dependencies:   mbed BLE_API nRF51822

Revision:
3:d37edebcc9e6
Parent:
2:697726f1d35c
Child:
5:19acba8bb777
--- a/main.cpp	Tue Nov 11 01:43:24 2014 +0000
+++ b/main.cpp	Mon Nov 17 04:55:34 2014 +0000
@@ -1,155 +1,95 @@
 #include "mbed.h"
+#include "BLEDevice.h"
+#include <string>
+#include "UARTService.h"
  
-//serial connection to PC via USB
+#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
+                               * it will have an impact on code-size and power consumption. */
 Serial pc(USBTX, USBRX);
+
+#if NEED_CONSOLE_OUTPUT
+#define DEBUG(...) { pc.printf(__VA_ARGS__); }
+#else
+#define DEBUG(...) /* nothing */
+#endif /* #if NEED_CONSOLE_OUTPUT */
+ 
+BLEDevice  ble;
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+
+DigitalOut powerSwitch(P0_0);
+ 
 AnalogIn strut1Force(P0_1);
 AnalogIn strut2Force(P0_2);
 AnalogIn strut3Force(P0_3);
-AnalogIn strut3Force(P0_4);
+AnalogIn strut4Force(P0_4);
+ 
+UARTService *uartServicePtr;
+ 
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+    DEBUG("Disconnected!\n\r");
+    DEBUG("Restarting the advertising process\n\r");
+    ble.startAdvertising();
+}
+ 
+void onDataWritten(const GattCharacteristicWriteCBParams *params)
+{
+    if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
+        uint16_t bytesRead = params->len;
+        DEBUG("received %u bytes\n\r", bytesRead);
+        DEBUG("first byte received: %u\n\r", params->data[0]);
+        if (params->data[0] == '1') {
+            led2 = 1;
+            powerSwitch = 1;
+        } else if (params->data[0] == '0') {
+            led2 = 0;
+            powerSwitch = 0;
+        }
+        char buffer[50];
+        DEBUG("%1.2f,%1.2f,%1.2f,%1.2f\n", strut1Force.read(), strut2Force.read(), strut3Force.read(), strut4Force.read());
+//        sprintf(buffer, "%1.2f,%1.2f,%1.2f,%1.2f\n", strut1Force.read(), strut2Force.read(), strut3Force.read(), strut4Force.read());
+        sprintf(buffer, "%1.2f\n", strut2Force.read());
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)buffer, sizeof(&buffer));
 
-DigitalOut light(LED1);
+    }
+}
+ 
+void periodicCallback(void)
+{
+    led1 = !led1;
+}
  
 int main(void)
 {
-pc.printf("init\n");
-    
+    led1 = 1;
+    led2 = 0;
+    powerSwitch = 0;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+ 
+    DEBUG("Initialising the nRF51822\n\r");
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
+    ble.onDataWritten(onDataWritten); // called when someone writes to me
+ 
+    /* setup advertising */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                     (const uint8_t *)"pgao", sizeof("pgao") - 1);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+ 
+    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+    ble.startAdvertising();
+ 
+    UARTService uartService(ble);
+    uartServicePtr = &uartService;
+ 
     while (true) {
-
-        pc.printf("%1.2f,%1.2f,%1.2f,%1.2f\n", strut1Force.read(), strut2Force.read(), strut3Force.read(), 0.01); //print force data to processing      
-        wait(0.005f); // wait 5ms (200Hz update rate)
+        ble.waitForEvent();
     }
 }
-/* // Sensor graphing sketch with threshold
-// This program takes ASCII-encoded strings
-// from the serial port at 9600 baud and graphs them. It expects float values in the
-// range of -1.0 to 1.0, followed by a newline, or newline and carriage return
-
-// based on Tom Igoe's Arduino sensor graphing sketch, created 20 Apr 2005
-// Adapted 16 Sep 2014 by Bjoern Hartmann for mbed
-// This example code is in the public domain.
-
-import processing.serial.*;
-import java.util.Scanner;
-
-Serial myPort;        // The serial port
-int xPos = 1;         // horizontal position of the graph
-float threshold=0.3;
-float minThreshold = 0.1;
-float max = 0;
-boolean rise = true;
-PImage bearImg;
-
-void setup () {
-  // set the window size:
-  //size(400, 300);          
-  //fullscreen
-  size(displayWidth, displayHeight);
-  // List all the available serial ports to help you find the one you need
-  println(Serial.list());
-  // Open whatever port is the one you're using.
-  myPort = new Serial(this, "/dev/tty.usbmodem1422", 9600);
-  // don't generate a serialEvent() unless you get a newline character:
-  myPort.bufferUntil('\n');
-  // set inital background:
-  redrawBG();
-  
-  bearImg = loadImage("cal_bear.png");
-}
-
-void draw () {
-
-  // everything happens in the serialEvent()
-}
-
-void keyPressed() {
-if (key == CODED) {
-    if (keyCode == UP) {
-      threshold+=0.05;
-    } else if (keyCode == DOWN) {
-      threshold-=0.05;
-    } 
-    redrawBG();  
-  }
-}
-
-// threshold 0.2 - 0.9
-
-void serialEvent (Serial myPort) {
-  // get the ASCII string:
-  
-  String inString = myPort.readStringUntil('\n');
-
-  if (inString != null) {
-    Scanner scanner = new Scanner(inString);
-    
-    // trim off any whitespace:
-    inString = trim(inString);
-    // convert to an int and map to the screen height:
-    float inFloat =0.f;
-    try{
-     inFloat = scanner.nextFloat();//float(inString);
-    } catch (Exception e) {
-      print(e);
-    }
-    
-    
-  if(rise) {  // we were last below
-    if(inFloat>threshold) { // we're above where we want to be
-      // rising edge, becoming true
-      rise = false;
-      drawHit(inFloat);
-
-    } // else // do nothing
-        
-  } else { // below == false, above threshold
-    if(inFloat < threshold) { //we drop below threshold
-      rise = true;
-      delay(500);
-      redrawBG();
-      max = 0;
-    } else  // below == false, still above threshold
-      drawHit(inFloat);
-    }
-    
-
-    // at the edge of the screen, go back to the beginning:
-  }
-}
-
-void drawHit(float inFloat) {
-        if(max < inFloat) {
-        max = inFloat;
-        float screenY = map(inFloat, 1.0, 0, 0, height);
-        
-        redrawBG();
-        // draw the line from bottom of screen to desired height
-        // choose color based on side of threshold we're on
-        float texty = map(threshold,0,1,height,0);
-        int percent = round(inFloat*100);
-        String hitText = "";
-        if(inFloat<threshold) {
-          fill(61,126,155);
-          hitText = "LAME"; 
-        } else {
-          hitText = percent + "%";
-          fill(231,76,70);
-        }
-        //println(inFloat);
-        textSize(72);
-        text(hitText,20,texty-40);
-        rect(width/2-20 , height, 40, -1*(height - screenY));
-      }
-}
-
-void redrawBG() {
-      background(236,240,241);
-      stroke(52,73,94);
-      float ty = height-map(threshold,0,1,0,height);
-      line(0,ty,width,ty);
-      fill(255,0,0);
-      stroke(0,0,0);
-      textSize(100);
-      text("Test your strength!",width/5.7,100);
-}
-*/
+ 
  
\ No newline at end of file