Demo program for LSM303 based boards

Dependencies:   mbed AES BLE_API nRF51822 smallAES

Committer:
f3d
Date:
Mon Nov 02 08:48:54 2020 +0000
Revision:
8:682b575fd7b3
Parent:
6:0038b9f778a5
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
f3d 0:21352e62003f 1 /* mbed Microcontroller Library
f3d 0:21352e62003f 2 * Copyright (c) 2006-2013 ARM Limited
f3d 0:21352e62003f 3 *
f3d 0:21352e62003f 4 * Licensed under the Apache License, Version 2.0 (the "License");
f3d 0:21352e62003f 5 * you may not use this file except in compliance with the License.
f3d 0:21352e62003f 6 * You may obtain a copy of the License at
f3d 0:21352e62003f 7 *
f3d 0:21352e62003f 8 * http://www.apache.org/licenses/LICENSE-2.0
f3d 0:21352e62003f 9 *
f3d 0:21352e62003f 10 * Unless required by applicable law or agreed to in writing, software
f3d 0:21352e62003f 11 * distributed under the License is distributed on an "AS IS" BASIS,
f3d 0:21352e62003f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f3d 0:21352e62003f 13 * See the License for the specific language governing permissions and
f3d 0:21352e62003f 14 * limitations under the License.
f3d 0:21352e62003f 15 */
f3d 0:21352e62003f 16
f3d 0:21352e62003f 17 #include "mbed.h"
f3d 0:21352e62003f 18 #include "ble/BLE.h"
f3d 8:682b575fd7b3 19 #include "BLEservice.h"
f3d 8:682b575fd7b3 20 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params);
f3d 8:682b575fd7b3 21 void onBleInitError(BLE &ble, ble_error_t error);
f3d 8:682b575fd7b3 22 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params);
f3d 8:682b575fd7b3 23 Ticker ticker;
f3d 8:682b575fd7b3 24 Serial pc(P0_24, P0_25);
f3d 0:21352e62003f 25
f3d 6:0038b9f778a5 26
f3d 8:682b575fd7b3 27 const static char DEVICE_NAME[] = "Test2020";
f3d 8:682b575fd7b3 28 #define LED_SERVICE 0xA000
f3d 8:682b575fd7b3 29 #define BTN_SERVICE 0x1eee
f3d 8:682b575fd7b3 30 #define ACCEL_SERVICE 0xA012
f3d 8:682b575fd7b3 31 #define TEMP_SERVICE 0xA022
f3d 8:682b575fd7b3 32 static const uint16_t uuid16_list[] = {LED_SERVICE,BTN_SERVICE,ACCEL_SERVICE,TEMP_SERVICE};
f3d 8:682b575fd7b3 33 BLEservice * LED;
f3d 8:682b575fd7b3 34 BLEservice * Button;
f3d 8:682b575fd7b3 35 BLEservice *Accel;
f3d 8:682b575fd7b3 36 BLEservice *Temp;
f3d 8:682b575fd7b3 37 const int LSM303_MAG_ADDRESS=(0x1e << 1);
f3d 8:682b575fd7b3 38 DigitalOut col1(P0_4, 0);
f3d 8:682b575fd7b3 39 DigitalOut alivenessLED(P0_13, 0);
f3d 8:682b575fd7b3 40 DigitalOut actuatedLED(P0_14, 0);
f3d 8:682b575fd7b3 41 I2C i2c(P0_30, P0_0); // SDA is on P0_30, SCL is on P0_0
f3d 8:682b575fd7b3 42 const int LSM303_ACCEL_ADDRESS = (0x19<<1);
f3d 8:682b575fd7b3 43 void initTemp()
f3d 8:682b575fd7b3 44 {
f3d 8:682b575fd7b3 45 //init code for temp sensor
f3d 8:682b575fd7b3 46 char Data[8]; // Declare a buffer for data transfer
f3d 8:682b575fd7b3 47 int Status;
f3d 8:682b575fd7b3 48
f3d 8:682b575fd7b3 49 Data[0] =0x1f;
f3d 8:682b575fd7b3 50 Data[1] = 0b11000000;
f3d 8:682b575fd7b3 51 i2c.write(LSM303_ACCEL_ADDRESS,Data,2);
f3d 8:682b575fd7b3 52
f3d 8:682b575fd7b3 53 Data[0] = 0x23;
f3d 8:682b575fd7b3 54 Data[1] = 0b10000000;
f3d 8:682b575fd7b3 55 i2c.write(LSM303_ACCEL_ADDRESS,Data,2);
f3d 8:682b575fd7b3 56
f3d 8:682b575fd7b3 57 }
f3d 8:682b575fd7b3 58 void pollTemp()
f3d 8:682b575fd7b3 59 {
f3d 8:682b575fd7b3 60 char Data[8]; // Declare a buffer for data transfer
f3d 8:682b575fd7b3 61 int Status;
f3d 8:682b575fd7b3 62 int16_t T;
f3d 8:682b575fd7b3 63
f3d 8:682b575fd7b3 64 Data[0]=0x80+ 0x0c; // Register number 0x28 has the X data (2 bytes)
f3d 8:682b575fd7b3 65 Status = i2c.write(LSM303_ACCEL_ADDRESS,Data,1,true); // Write register number
f3d 8:682b575fd7b3 66 Status = i2c.read(LSM303_ACCEL_ADDRESS,Data,2); // Read register contents
f3d 8:682b575fd7b3 67 T = Data[1];
f3d 8:682b575fd7b3 68 T = (T << 8) + Data[0];
f3d 8:682b575fd7b3 69 T = T / 16;
f3d 8:682b575fd7b3 70 T = (20 << 3) + T;
f3d 8:682b575fd7b3 71 pc.printf("T=%d\r\n",T/4);
f3d 8:682b575fd7b3 72 Temp->writeCharacteristic(0,T/4);
f3d 8:682b575fd7b3 73 }
f3d 8:682b575fd7b3 74 void initLED()
f3d 8:682b575fd7b3 75 {
f3d 8:682b575fd7b3 76 actuatedLED = 0;
f3d 8:682b575fd7b3 77 }
f3d 8:682b575fd7b3 78 void pollLED()
f3d 8:682b575fd7b3 79 {
f3d 8:682b575fd7b3 80 uint16_t Value;
f3d 8:682b575fd7b3 81 Value = LED->readCharacteristic(0);
f3d 8:682b575fd7b3 82 actuatedLED = Value;
f3d 8:682b575fd7b3 83 }
f3d 8:682b575fd7b3 84 DigitalIn ButtonA(P0_17);
f3d 8:682b575fd7b3 85 DigitalIn ButtonB(P0_26);
f3d 8:682b575fd7b3 86 void initButton()
f3d 0:21352e62003f 87 {
f3d 6:0038b9f778a5 88
f3d 8:682b575fd7b3 89 }
f3d 8:682b575fd7b3 90 void pollButton()
f3d 8:682b575fd7b3 91 {
f3d 8:682b575fd7b3 92 uint16_t ValueA = ButtonA;
f3d 8:682b575fd7b3 93 pc.printf("Button A = %d\r\n",ValueA);
f3d 8:682b575fd7b3 94 Button->writeCharacteristic(0,ValueA);
f3d 8:682b575fd7b3 95 uint16_t ValueB = ButtonB;
f3d 8:682b575fd7b3 96 pc.printf("Button B = %d\r\n",ValueB);
f3d 8:682b575fd7b3 97 Button->writeCharacteristic(1,ValueB);
f3d 8:682b575fd7b3 98 }
f3d 8:682b575fd7b3 99
f3d 8:682b575fd7b3 100
f3d 8:682b575fd7b3 101
f3d 8:682b575fd7b3 102 void initAccel()
f3d 8:682b575fd7b3 103 {
f3d 8:682b575fd7b3 104 // Wake the accelerometer from sleep mode by writing disabling low power mode, setting freq to 10Hz and enabling all three axes
f3d 8:682b575fd7b3 105 char Data[8]; // Declare a buffer for data transfer
f3d 8:682b575fd7b3 106 int Status;
f3d 8:682b575fd7b3 107 Data[0]=0x20; // wake up LSM303 (max speed, all accel channels)
f3d 8:682b575fd7b3 108 Data[1]=0x77;
f3d 8:682b575fd7b3 109 Status = i2c.write(LSM303_ACCEL_ADDRESS,Data,2); // Write data to register
f3d 8:682b575fd7b3 110
f3d 8:682b575fd7b3 111 Data[0]=0x23; // enable high resolution mode
f3d 8:682b575fd7b3 112 Data[1]=0x0e;
f3d 8:682b575fd7b3 113 Status = i2c.write(LSM303_ACCEL_ADDRESS,Data,2); // Write data to register
f3d 8:682b575fd7b3 114
f3d 8:682b575fd7b3 115 }
f3d 8:682b575fd7b3 116 void pollAccel()
f3d 8:682b575fd7b3 117 {
f3d 8:682b575fd7b3 118 char Data[8]; // Declare a buffer for data transfer
f3d 8:682b575fd7b3 119 int Status;
f3d 8:682b575fd7b3 120 int16_t X;
f3d 8:682b575fd7b3 121 Data[0]=0x80 + 0x28; // Register number 0x28 has the X data (2 bytes)
f3d 8:682b575fd7b3 122 Status = i2c.write(LSM303_ACCEL_ADDRESS,Data,1,true); // Write register number
f3d 8:682b575fd7b3 123 Status = i2c.read(LSM303_ACCEL_ADDRESS,Data,2); // Read register contents
f3d 8:682b575fd7b3 124 X = Data[1];
f3d 8:682b575fd7b3 125 X = (X << 8) + Data[0];
f3d 8:682b575fd7b3 126
f3d 8:682b575fd7b3 127 int16_t Y;
f3d 8:682b575fd7b3 128 Data[0]=0x80 + 0x2a; // Register number 0x2a has the Y data (2 bytes)
f3d 8:682b575fd7b3 129 Status = i2c.write(LSM303_ACCEL_ADDRESS,Data,1,true); // Write register number
f3d 8:682b575fd7b3 130 Status = i2c.read(LSM303_ACCEL_ADDRESS,Data,2); // Read register contents
f3d 8:682b575fd7b3 131 Y = Data[1];
f3d 8:682b575fd7b3 132 Y = (Y << 8) + Data[0];
f3d 8:682b575fd7b3 133
f3d 8:682b575fd7b3 134 int16_t Z;
f3d 8:682b575fd7b3 135 Data[0]=0x80 + 0x2c; // Register number 0x2c has the Z data (2 bytes)
f3d 8:682b575fd7b3 136 Status = i2c.write(LSM303_ACCEL_ADDRESS,Data,1,true); // Write register number
f3d 8:682b575fd7b3 137 Status = i2c.read(LSM303_ACCEL_ADDRESS,Data,2); // Read register contents
f3d 8:682b575fd7b3 138 Z = Data[1];
f3d 8:682b575fd7b3 139 Z = (Z << 8) + Data[0];
f3d 8:682b575fd7b3 140 pc.printf("X : %d , Y %d , Z %d \r\n",X,Y,Z);
f3d 8:682b575fd7b3 141 Accel->writeCharacteristic(0,X);
f3d 8:682b575fd7b3 142 Accel->writeCharacteristic(1,Y);
f3d 8:682b575fd7b3 143 Accel->writeCharacteristic(2,Z);
f3d 8:682b575fd7b3 144 }
f3d 8:682b575fd7b3 145 void onDataWrittenCallback(const GattWriteCallbackParams *params)
f3d 8:682b575fd7b3 146 {
f3d 8:682b575fd7b3 147 pc.printf("Got writeback for %d\r\n",params->handle);
f3d 8:682b575fd7b3 148 }
f3d 8:682b575fd7b3 149 void tick()
f3d 8:682b575fd7b3 150 {
f3d 8:682b575fd7b3 151 }
f3d 8:682b575fd7b3 152 void pollIO()
f3d 8:682b575fd7b3 153 {
f3d 8:682b575fd7b3 154 pc.printf("Poll\r\n");
f3d 8:682b575fd7b3 155 LED->poll();
f3d 8:682b575fd7b3 156 Button->poll();
f3d 8:682b575fd7b3 157 Accel->poll();
f3d 8:682b575fd7b3 158 Temp->poll();
f3d 8:682b575fd7b3 159 }
f3d 8:682b575fd7b3 160 int main(void)
f3d 8:682b575fd7b3 161 {
f3d 8:682b575fd7b3 162 BLE &ble = BLE::Instance();
f3d 8:682b575fd7b3 163 ble.init(bleInitComplete);
f3d 8:682b575fd7b3 164 /* SpinWait for initialization to complete. This is necessary because the
f3d 8:682b575fd7b3 165 * BLE object is used in the main loop below. */
f3d 8:682b575fd7b3 166 while (ble.hasInitialized() == false) { /* spin loop */ }
f3d 8:682b575fd7b3 167
f3d 8:682b575fd7b3 168 pc.printf("BLE init complete\r\n");
f3d 8:682b575fd7b3 169
f3d 8:682b575fd7b3 170 uint16_t LED_char_array[]={0xabcd};
f3d 8:682b575fd7b3 171 pc.printf("Creating LED Service\r\n");
f3d 8:682b575fd7b3 172 LED = new BLEservice(ble,LED_SERVICE,1,LED_char_array,initLED,pollLED);
f3d 8:682b575fd7b3 173 LED->init();
f3d 8:682b575fd7b3 174 pc.printf("LED service ready\r\n");
f3d 8:682b575fd7b3 175
f3d 8:682b575fd7b3 176 uint16_t Button_char_array[]={0x2021,0x2022};
f3d 8:682b575fd7b3 177 pc.printf("Creating Button Service\r\n");
f3d 8:682b575fd7b3 178 Button = new BLEservice(ble,BTN_SERVICE,2,Button_char_array,initButton,pollButton,1);
f3d 8:682b575fd7b3 179 Button->init();
f3d 8:682b575fd7b3 180 pc.printf("Button service ready\r\n");
f3d 8:682b575fd7b3 181
f3d 8:682b575fd7b3 182 uint16_t Accel_char_array[]={0xA013,0xA014,0xA015};
f3d 8:682b575fd7b3 183 pc.printf("Creating Accel Service\r\n");
f3d 8:682b575fd7b3 184 Accel = new BLEservice(ble,ACCEL_SERVICE,3,Accel_char_array,initAccel,pollAccel);
f3d 8:682b575fd7b3 185 Accel->init();
f3d 8:682b575fd7b3 186 pc.printf("Accel service ready\r\n");
f3d 8:682b575fd7b3 187
f3d 8:682b575fd7b3 188 uint16_t Temp_char_array[]={0xf00d};
f3d 8:682b575fd7b3 189 pc.printf("Creating Temp Service\r\n");
f3d 8:682b575fd7b3 190 Temp = new BLEservice(ble,TEMP_SERVICE,1,Temp_char_array,initTemp,pollTemp);
f3d 8:682b575fd7b3 191 Temp->init();
f3d 8:682b575fd7b3 192 pc.printf("Temp service ready\r\n");
f3d 8:682b575fd7b3 193
f3d 8:682b575fd7b3 194 ticker.attach(tick, 1); /* wake from sleep every 100ms */
f3d 6:0038b9f778a5 195 while (1)
f3d 8:682b575fd7b3 196 { pollIO();
f3d 8:682b575fd7b3 197 ble.waitForEvent();
f3d 0:21352e62003f 198 }
f3d 0:21352e62003f 199 }
f3d 8:682b575fd7b3 200 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
f3d 8:682b575fd7b3 201 {
f3d 8:682b575fd7b3 202 BLE& ble = params->ble;
f3d 8:682b575fd7b3 203 ble_error_t error = params->error;
f3d 8:682b575fd7b3 204
f3d 8:682b575fd7b3 205 if (error != BLE_ERROR_NONE) {
f3d 8:682b575fd7b3 206 /* In case of error, forward the error handling to onBleInitError */
f3d 8:682b575fd7b3 207 onBleInitError(ble, error);
f3d 8:682b575fd7b3 208 return;
f3d 8:682b575fd7b3 209 }
f3d 8:682b575fd7b3 210
f3d 8:682b575fd7b3 211 /* Ensure that it is the default instance of BLE */
f3d 8:682b575fd7b3 212 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
f3d 8:682b575fd7b3 213 return;
f3d 8:682b575fd7b3 214 }
f3d 8:682b575fd7b3 215
f3d 8:682b575fd7b3 216 ble.gap().onDisconnection(disconnectionCallback);
f3d 8:682b575fd7b3 217 ble.gattServer().onDataWritten(onDataWrittenCallback);
f3d 8:682b575fd7b3 218 // ble.gattServer().onDataRead(onDataReadCallback); // Nordic Soft device will not call this so have to poll instead
f3d 8:682b575fd7b3 219
f3d 8:682b575fd7b3 220
f3d 8:682b575fd7b3 221 /* setup advertising */
f3d 8:682b575fd7b3 222 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
f3d 8:682b575fd7b3 223 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
f3d 8:682b575fd7b3 224 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
f3d 8:682b575fd7b3 225 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
f3d 8:682b575fd7b3 226 ble.gap().setAdvertisingInterval(500); /* 500ms. */
f3d 8:682b575fd7b3 227 pc.printf("Starting advertising\r\n");
f3d 8:682b575fd7b3 228 ble.gap().startAdvertising();
f3d 8:682b575fd7b3 229 }
f3d 8:682b575fd7b3 230 void onBleInitError(BLE &ble, ble_error_t error)
f3d 8:682b575fd7b3 231 {
f3d 8:682b575fd7b3 232 /* Initialization error handling should go here */
f3d 8:682b575fd7b3 233 pc.printf("BLE init error\r\n");
f3d 8:682b575fd7b3 234 }
f3d 8:682b575fd7b3 235 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
f3d 8:682b575fd7b3 236 {
f3d 8:682b575fd7b3 237 pc.printf("Disconnected, restarting advertising\r\n");
f3d 8:682b575fd7b3 238 BLE::Instance().gap().startAdvertising();
f3d 8:682b575fd7b3 239 }