NAzT
1/28/2016 - 5:27 PM

step-counter.ino

step-counter.ino

/*
  Copyright (c) 2015 Intel Corporation. All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
  1301 USA
*/


#include "CurieImu.h"
#include <CurieBle.h>

const int ledPin = 13; // set ledPin to use on-board LED
BLEPeripheral blePeripheral; // create peripheral instance

boolean stepEventsEnabeled = true;   // whether you're polling or using events
long lastStepCount = 0;              // step count on previous polling check
boolean blinkState = false;

BLEService bleService("19B10001-E8F2-537E-4F6C-D104768A1214"); // create service
// create ble characteristic and allow remote device to read and write
BLECharCharacteristic bleChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify);

void setupBlePeripheral() {
  // set the local name peripheral advertises
  blePeripheral.setDeviceName("Step Counter - Nat");
  blePeripheral.setLocalName("#cmmakerparty");
  // set the UUID for the service this peripheral advertises
  blePeripheral.setAdvertisedServiceUuid(bleService.uuid());

  // add service and characteristic
  blePeripheral.addAttribute(bleService);
  blePeripheral.addAttribute(bleChar);

  // assign event handlers for connected, disconnected to peripheral
  blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);

  // assign event handlers for characteristic
  bleChar.setEventHandler(BLEWritten, bleCharacteristicWritten);
  // set an initial value for the characteristic
  bleChar.setValue(0);

  // advertise the service
  blePeripheral.begin();
  Serial.println(("Bluetooth device active, waiting for connections..."));
}

static void eventCallback(void) {
  if (CurieImu.getIntStepStatus())
    updateStepCount();
}

void setUpImuSensor() {
  // intialize the sensor:
  CurieImu.initialize();
  // turn on step detection mode:
  CurieImu.setStepDetectionMode(BMI160_STEP_MODE_NORMAL);
  // enable step counting:
  CurieImu.setStepCountEnabled(true);

  // attach the eventCallback function as the
  // step event handler:
  CurieImu.attachInterrupt(eventCallback);
  CurieImu.setIntStepEnabled(true);        // turn on step detection
  CurieImu.setIntEnabled(true);            // enable interrupts

  Serial.println("IMU initialisation complete, waiting for events...");
}

void setup() {
  Serial.begin(9600);
  delay(500);
  Serial.println("OK");
  pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
  setUpImuSensor();
  setupBlePeripheral();
}

static void updateStepCount() {
  // get the step count:
  int stepCount = CurieImu.getStepCount();

  // if the step count has changed, print it:
  if (stepCount != lastStepCount) {
    Serial.print("Step count: ");
    Serial.println(stepCount);
    // save the current count for comparison next check:
    lastStepCount = stepCount;
  }
}

void process() {
  static uint32_t prev = millis();
  if (millis() - prev > 1000) {
    prev = millis();
    bleChar.setValue(lastStepCount);
  }
}

void loop() {
  // poll peripheral
  blePeripheral.poll();
  process();
}

void blePeripheralConnectHandler(BLECentral& central) {
  // central connected event handler
  Serial.print("Connected event, central: ");
  Serial.println(central.address());

}

void blePeripheralDisconnectHandler(BLECentral& central) {
  // central disconnected event handler
  Serial.print("Disconnected event, central: ");
  Serial.println(central.address());
}

void bleCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
  // central wrote new value to characteristic, update LED
}