Program to read degrees using HMC5883L

Dependencies:   DOOR_BLE_API HMC5883L mbed mbed nRF51822

Committer:
brunohorta
Date:
Thu May 31 20:01:21 2018 +0000
Revision:
1:b867d2573588
Parent:
0:f99eedea5d17
compass

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brunohorta 0:f99eedea5d17 1 /* mbed Microcontroller Library
brunohorta 0:f99eedea5d17 2 * Copyright (c) 2006-2013 ARM Limited
brunohorta 0:f99eedea5d17 3 *
brunohorta 0:f99eedea5d17 4 * Licensed under the Apache License, Version 2.0 (the "License");
brunohorta 0:f99eedea5d17 5 * you may not use this file except in compliance with the License.
brunohorta 0:f99eedea5d17 6 * You may obtain a copy of the License at
brunohorta 0:f99eedea5d17 7 *
brunohorta 0:f99eedea5d17 8 * http://www.apache.org/licenses/LICENSE-2.0
brunohorta 0:f99eedea5d17 9 *
brunohorta 0:f99eedea5d17 10 * Unless required by applicable law or agreed to in writing, software
brunohorta 0:f99eedea5d17 11 * distributed under the License is distributed on an "AS IS" BASIS,
brunohorta 0:f99eedea5d17 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
brunohorta 0:f99eedea5d17 13 * See the License for the specific language governing permissions and
brunohorta 0:f99eedea5d17 14 * limitations under the License.
brunohorta 0:f99eedea5d17 15 */
brunohorta 0:f99eedea5d17 16
brunohorta 0:f99eedea5d17 17 #include "mbed.h"
brunohorta 0:f99eedea5d17 18 #include "ble/BLE.h"
brunohorta 0:f99eedea5d17 19 #include "nrf_error.h"
brunohorta 0:f99eedea5d17 20
brunohorta 0:f99eedea5d17 21 //BEACON
brunohorta 0:f99eedea5d17 22 #include "ble/services/iBeacon.h"
brunohorta 0:f99eedea5d17 23 #include "HMC5883L.h"
brunohorta 0:f99eedea5d17 24 #include "ble/services/UARTService.h"
brunohorta 0:f99eedea5d17 25 #include "ble/services/iBeacon.h"
brunohorta 0:f99eedea5d17 26 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
brunohorta 0:f99eedea5d17 27 * it will have an impact on code-size and power consumption. */
brunohorta 0:f99eedea5d17 28
brunohorta 0:f99eedea5d17 29 #if NEED_CONSOLE_OUTPUT
brunohorta 0:f99eedea5d17 30 #define DEBUG(...) { printf(__VA_ARGS__); }
brunohorta 0:f99eedea5d17 31 #else
brunohorta 0:f99eedea5d17 32 #define DEBUG(...) /* nothing */
brunohorta 0:f99eedea5d17 33 #endif /* #if NEED_CONSOLE_OUTPUT */
brunohorta 0:f99eedea5d17 34
brunohorta 0:f99eedea5d17 35 BLE ble;
brunohorta 0:f99eedea5d17 36 int countState = 0;
brunohorta 0:f99eedea5d17 37 DigitalOut led1(LED1);
brunohorta 0:f99eedea5d17 38 HMC5883L hmc5883l;
brunohorta 0:f99eedea5d17 39 UARTService *uartServicePtr;
brunohorta 0:f99eedea5d17 40 bool beaconMode = false;
brunohorta 0:f99eedea5d17 41 //BEACON
brunohorta 0:f99eedea5d17 42 GapAdvertisingData adv_data = GapAdvertisingData();
brunohorta 0:f99eedea5d17 43
brunohorta 0:f99eedea5d17 44 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
brunohorta 0:f99eedea5d17 45 {
brunohorta 0:f99eedea5d17 46 DEBUG("Disconnected!\n\r");
brunohorta 0:f99eedea5d17 47 DEBUG("Restarting the advertising process\n\r");
brunohorta 0:f99eedea5d17 48 ble.startAdvertising();
brunohorta 0:f99eedea5d17 49 }
brunohorta 0:f99eedea5d17 50
brunohorta 0:f99eedea5d17 51 float start = 0;
brunohorta 0:f99eedea5d17 52
brunohorta 0:f99eedea5d17 53 InterruptIn buttonReset(p6);
brunohorta 0:f99eedea5d17 54
brunohorta 0:f99eedea5d17 55 void resetDeg(){
brunohorta 0:f99eedea5d17 56 start = 0;
brunohorta 0:f99eedea5d17 57 countState++;
brunohorta 0:f99eedea5d17 58 if(countState >= 3){
brunohorta 0:f99eedea5d17 59 beaconMode = !beaconMode;
brunohorta 0:f99eedea5d17 60 }
brunohorta 0:f99eedea5d17 61
brunohorta 0:f99eedea5d17 62 }
brunohorta 0:f99eedea5d17 63
brunohorta 0:f99eedea5d17 64 void periodicCallback(void){
brunohorta 0:f99eedea5d17 65
brunohorta 0:f99eedea5d17 66 //float magData[3];
brunohorta 0:f99eedea5d17 67 // hmc5883l.readMagData(magData);
brunohorta 0:f99eedea5d17 68 //float x = magData[0];
brunohorta 0:f99eedea5d17 69 //float y = magData[1];
brunohorta 0:f99eedea5d17 70 //float z = magData[2];
brunohorta 0:f99eedea5d17 71 //char temp[200];
brunohorta 0:f99eedea5d17 72
brunohorta 0:f99eedea5d17 73 //snprintf(temp, 20, "%d:%d:%d", (int)x,(int)y,(int)z);
brunohorta 0:f99eedea5d17 74 char temp[20];
brunohorta 0:f99eedea5d17 75
brunohorta 0:f99eedea5d17 76 float g = hmc5883l.getHeading();
brunohorta 0:f99eedea5d17 77
brunohorta 0:f99eedea5d17 78 if(start == 0){
brunohorta 0:f99eedea5d17 79 start = g;
brunohorta 0:f99eedea5d17 80 }
brunohorta 0:f99eedea5d17 81 float d = g - start;
brunohorta 0:f99eedea5d17 82 //POSITIVE
brunohorta 0:f99eedea5d17 83 if( d < 0){
brunohorta 0:f99eedea5d17 84 d = d + 360;
brunohorta 0:f99eedea5d17 85 }
brunohorta 0:f99eedea5d17 86 if( d > start){
brunohorta 0:f99eedea5d17 87 d = 0;
brunohorta 0:f99eedea5d17 88 }
brunohorta 0:f99eedea5d17 89 //NEGATIVE
brunohorta 0:f99eedea5d17 90 snprintf(temp, 20, "%g deg", d);
brunohorta 0:f99eedea5d17 91
brunohorta 0:f99eedea5d17 92 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(uint8_t *)temp, strlen(temp));
brunohorta 0:f99eedea5d17 93
brunohorta 0:f99eedea5d17 94 }
brunohorta 0:f99eedea5d17 95
brunohorta 0:f99eedea5d17 96 //BEACON
brunohorta 0:f99eedea5d17 97 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
brunohorta 0:f99eedea5d17 98 {
brunohorta 0:f99eedea5d17 99 BLE &ble = params->ble;
brunohorta 0:f99eedea5d17 100 ble_error_t error = params->error;
brunohorta 0:f99eedea5d17 101
brunohorta 0:f99eedea5d17 102 if (error != BLE_ERROR_NONE) {
brunohorta 0:f99eedea5d17 103 return;
brunohorta 0:f99eedea5d17 104 }
brunohorta 0:f99eedea5d17 105
brunohorta 0:f99eedea5d17 106 /**
brunohorta 0:f99eedea5d17 107 * The Beacon payload has the following composition:
brunohorta 0:f99eedea5d17 108 * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
brunohorta 0:f99eedea5d17 109 * Major/Minor = 0x1122 / 0x3344
brunohorta 0:f99eedea5d17 110 * Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB)
brunohorta 0:f99eedea5d17 111 *
brunohorta 0:f99eedea5d17 112 * Note: please remember to calibrate your beacons TX Power for more accurate results.
brunohorta 0:f99eedea5d17 113 */
brunohorta 0:f99eedea5d17 114 const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
brunohorta 0:f99eedea5d17 115 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61};
brunohorta 0:f99eedea5d17 116 uint16_t majorNumber = 1122;
brunohorta 0:f99eedea5d17 117 uint16_t minorNumber = 3344;
brunohorta 0:f99eedea5d17 118 uint16_t txPower = 0xC8;
brunohorta 0:f99eedea5d17 119 iBeacon *ibeacon = new iBeacon(ble, uuid, majorNumber, minorNumber, txPower);
brunohorta 0:f99eedea5d17 120
brunohorta 0:f99eedea5d17 121 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
brunohorta 0:f99eedea5d17 122
brunohorta 0:f99eedea5d17 123 ble.gap().startAdvertising();
brunohorta 0:f99eedea5d17 124 }
brunohorta 0:f99eedea5d17 125
brunohorta 0:f99eedea5d17 126 //BEACON
brunohorta 0:f99eedea5d17 127 void updateData(){
brunohorta 0:f99eedea5d17 128 adv_data = ble.getAdvertisingData();
brunohorta 0:f99eedea5d17 129 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, "ALO",3);
brunohorta 0:f99eedea5d17 130 ble.setAdvertisingData(adv_data);
brunohorta 0:f99eedea5d17 131 }
brunohorta 0:f99eedea5d17 132
brunohorta 0:f99eedea5d17 133
brunohorta 0:f99eedea5d17 134 int main(void){
brunohorta 0:f99eedea5d17 135
brunohorta 0:f99eedea5d17 136 if(beaconMode){
brunohorta 0:f99eedea5d17 137 ble.init(bleInitComplete);
brunohorta 0:f99eedea5d17 138
brunohorta 0:f99eedea5d17 139 /* SpinWait for initialization to complete. This is necessary because the
brunohorta 0:f99eedea5d17 140 * BLE object is used in the main loop below. */
brunohorta 0:f99eedea5d17 141 while (!ble.hasInitialized()) { /* spin loop */ }
brunohorta 0:f99eedea5d17 142
brunohorta 0:f99eedea5d17 143 while (true) {
brunohorta 0:f99eedea5d17 144 ble.waitForEvent(); // allows or low power operation
brunohorta 0:f99eedea5d17 145 }
brunohorta 0:f99eedea5d17 146 }else{
brunohorta 0:f99eedea5d17 147 buttonReset.fall(&resetDeg);
brunohorta 0:f99eedea5d17 148 hmc5883l.init();
brunohorta 0:f99eedea5d17 149 hmc5883l.setScale(COMPASS_SCALE_130);
brunohorta 0:f99eedea5d17 150 hmc5883l.setOrientation(COMPASS_VERTICAL_X_EAST);
brunohorta 0:f99eedea5d17 151
brunohorta 0:f99eedea5d17 152 Ticker ticker;
brunohorta 0:f99eedea5d17 153 ticker.attach(periodicCallback, 1);
brunohorta 0:f99eedea5d17 154
brunohorta 0:f99eedea5d17 155 DEBUG("Initialising the nRF51822\n\r");
brunohorta 0:f99eedea5d17 156 ble.init();
brunohorta 0:f99eedea5d17 157 ble.onDisconnection(disconnectionCallback);
brunohorta 0:f99eedea5d17 158
brunohorta 0:f99eedea5d17 159
brunohorta 0:f99eedea5d17 160 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
brunohorta 0:f99eedea5d17 161 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
brunohorta 0:f99eedea5d17 162 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
brunohorta 0:f99eedea5d17 163 (const uint8_t *)"DOOR BLE", sizeof("DOOR BLE") - 1);
brunohorta 0:f99eedea5d17 164 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
brunohorta 0:f99eedea5d17 165 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
brunohorta 0:f99eedea5d17 166
brunohorta 0:f99eedea5d17 167 ble.setAdvertisingInterval(1000);
brunohorta 0:f99eedea5d17 168 ble.startAdvertising();
brunohorta 0:f99eedea5d17 169
brunohorta 0:f99eedea5d17 170 UARTService uartService(ble);
brunohorta 0:f99eedea5d17 171 uartServicePtr = &uartService;
brunohorta 0:f99eedea5d17 172
brunohorta 0:f99eedea5d17 173 while (true) {
brunohorta 0:f99eedea5d17 174 ble.waitForEvent();
brunohorta 0:f99eedea5d17 175 }
brunohorta 0:f99eedea5d17 176
brunohorta 0:f99eedea5d17 177 /* SpinWait for initialization to complete. This is necessary because the
brunohorta 0:f99eedea5d17 178 * BLE object is used in the main loop below. */
brunohorta 0:f99eedea5d17 179 /** UARTService uartService(ble);
brunohorta 0:f99eedea5d17 180 uartServicePtr = &uartService;
brunohorta 0:f99eedea5d17 181 while (!ble.hasInitialized()) { }
brunohorta 0:f99eedea5d17 182
brunohorta 0:f99eedea5d17 183 while (true) {
brunohorta 0:f99eedea5d17 184 ble.waitForEvent(); // allows or low power operation
brunohorta 0:f99eedea5d17 185 }*/
brunohorta 0:f99eedea5d17 186 }
brunohorta 0:f99eedea5d17 187 }