![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test program for magnetometer
Dependencies: mbed AES BLE_API nRF51822 smallAES
magservice.h@3:b9783107a8c4, 2019-09-30 (annotated)
- Committer:
- f3d
- Date:
- Mon Sep 30 11:59:42 2019 +0000
- Revision:
- 3:b9783107a8c4
Magnetometer for the LSM303
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 3:b9783107a8c4 | 1 | /* mbed Microcontroller Library |
f3d | 3:b9783107a8c4 | 2 | * Copyright (c) 2006-2013 ARM Limited |
f3d | 3:b9783107a8c4 | 3 | * |
f3d | 3:b9783107a8c4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
f3d | 3:b9783107a8c4 | 5 | * you may not use this file except in compliance with the License. |
f3d | 3:b9783107a8c4 | 6 | * You may obtain a copy of the License at |
f3d | 3:b9783107a8c4 | 7 | * |
f3d | 3:b9783107a8c4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
f3d | 3:b9783107a8c4 | 9 | * |
f3d | 3:b9783107a8c4 | 10 | * Unless required by applicable law or agreed to in writing, software |
f3d | 3:b9783107a8c4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
f3d | 3:b9783107a8c4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
f3d | 3:b9783107a8c4 | 13 | * See the License for the specific language governing permissions and |
f3d | 3:b9783107a8c4 | 14 | * limitations under the License. |
f3d | 3:b9783107a8c4 | 15 | */ |
f3d | 3:b9783107a8c4 | 16 | |
f3d | 3:b9783107a8c4 | 17 | #ifndef __BLE_MAG_SERVICE_H__ |
f3d | 3:b9783107a8c4 | 18 | #define __BLE_MAG_SERVICE_H__ |
f3d | 3:b9783107a8c4 | 19 | #include <mbed.h> |
f3d | 3:b9783107a8c4 | 20 | // Accelerometer : LSM303 |
f3d | 3:b9783107a8c4 | 21 | //I2C i2c(P0_30, P0_0); // SDA is on P0_30, SCL is on P0_0 |
f3d | 3:b9783107a8c4 | 22 | const int LSM303_ADDRESS = (0x19<<1); |
f3d | 3:b9783107a8c4 | 23 | //const int MMA8653_ID = 0x5a; |
f3d | 3:b9783107a8c4 | 24 | class MAGService { |
f3d | 3:b9783107a8c4 | 25 | public: |
f3d | 3:b9783107a8c4 | 26 | const static uint16_t MAG_SERVICE_UUID = 0xfff3; |
f3d | 3:b9783107a8c4 | 27 | const static uint16_t MAG_X_CHARACTERISTIC_UUID = 0x1; |
f3d | 3:b9783107a8c4 | 28 | const static uint16_t MAG_Y_CHARACTERISTIC_UUID = 0x2; |
f3d | 3:b9783107a8c4 | 29 | const static uint16_t MAG_Z_CHARACTERISTIC_UUID = 0x3; |
f3d | 3:b9783107a8c4 | 30 | |
f3d | 3:b9783107a8c4 | 31 | MAGService(BLEDevice &_ble, int16_t initialValueForMAGCharacteristic) : |
f3d | 3:b9783107a8c4 | 32 | ble(_ble), MagX(MAG_X_CHARACTERISTIC_UUID, &initialValueForMAGCharacteristic),MagY(MAG_Y_CHARACTERISTIC_UUID, &initialValueForMAGCharacteristic),MagZ(MAG_Z_CHARACTERISTIC_UUID, &initialValueForMAGCharacteristic) |
f3d | 3:b9783107a8c4 | 33 | { |
f3d | 3:b9783107a8c4 | 34 | GattCharacteristic *charTable[] = {&MagX,&MagY,&MagZ}; |
f3d | 3:b9783107a8c4 | 35 | GattService MagService(MAG_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
f3d | 3:b9783107a8c4 | 36 | ble.addService(MagService); |
f3d | 3:b9783107a8c4 | 37 | // Wake the accelerometer from sleep mode by writing disabling low power mode, setting freq to 10Hz and enabling all three axes |
f3d | 3:b9783107a8c4 | 38 | char Data[8]; // Declare a buffer for data transfer |
f3d | 3:b9783107a8c4 | 39 | int Status; |
f3d | 3:b9783107a8c4 | 40 | Data[0]=0x20; // wake up LSM303 (max speed, all accel channels) |
f3d | 3:b9783107a8c4 | 41 | Data[1]=0x77; |
f3d | 3:b9783107a8c4 | 42 | Status = i2c.write(LSM303_ADDRESS,Data,2); // Write data to register |
f3d | 3:b9783107a8c4 | 43 | |
f3d | 3:b9783107a8c4 | 44 | Data[0]=0x23; // enable high resolution mode |
f3d | 3:b9783107a8c4 | 45 | Data[1]=0x0e; |
f3d | 3:b9783107a8c4 | 46 | Status = i2c.write(LSM303_ADDRESS,Data,2); // Write data to register |
f3d | 3:b9783107a8c4 | 47 | } |
f3d | 3:b9783107a8c4 | 48 | |
f3d | 3:b9783107a8c4 | 49 | GattAttribute::Handle_t getValueHandle() const { |
f3d | 3:b9783107a8c4 | 50 | return MagX.getValueHandle(); |
f3d | 3:b9783107a8c4 | 51 | } |
f3d | 3:b9783107a8c4 | 52 | void updateMagX(uint16_t newValue) { |
f3d | 3:b9783107a8c4 | 53 | ble.gattServer().write(MagX.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t)); |
f3d | 3:b9783107a8c4 | 54 | } |
f3d | 3:b9783107a8c4 | 55 | void updateMagY(uint16_t newValue) { |
f3d | 3:b9783107a8c4 | 56 | ble.gattServer().write(MagY.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t)); |
f3d | 3:b9783107a8c4 | 57 | } |
f3d | 3:b9783107a8c4 | 58 | void updateMagZ(uint16_t newValue) { |
f3d | 3:b9783107a8c4 | 59 | ble.gattServer().write(MagZ.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t)); |
f3d | 3:b9783107a8c4 | 60 | } |
f3d | 3:b9783107a8c4 | 61 | void poll() |
f3d | 3:b9783107a8c4 | 62 | { |
f3d | 3:b9783107a8c4 | 63 | char Data[8]; // Declare a buffer for data transfer |
f3d | 3:b9783107a8c4 | 64 | int Status; |
f3d | 3:b9783107a8c4 | 65 | int16_t X; |
f3d | 3:b9783107a8c4 | 66 | |
f3d | 3:b9783107a8c4 | 67 | // Modify to suit your chip: |
f3d | 3:b9783107a8c4 | 68 | // Goal get X,Y and Z readings off magnetometer. |
f3d | 3:b9783107a8c4 | 69 | Data[0]=0x80 + 0x28; // Register number 0x28 has the X data (2 bytes) |
f3d | 3:b9783107a8c4 | 70 | Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number |
f3d | 3:b9783107a8c4 | 71 | Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents |
f3d | 3:b9783107a8c4 | 72 | X = Data[1]; |
f3d | 3:b9783107a8c4 | 73 | X = (X << 8) + Data[0]; |
f3d | 3:b9783107a8c4 | 74 | |
f3d | 3:b9783107a8c4 | 75 | int16_t Y; |
f3d | 3:b9783107a8c4 | 76 | Data[0]=0x80 + 0x2a; // Register number 0x2a has the Y data (2 bytes) |
f3d | 3:b9783107a8c4 | 77 | Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number |
f3d | 3:b9783107a8c4 | 78 | Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents |
f3d | 3:b9783107a8c4 | 79 | Y = Data[1]; |
f3d | 3:b9783107a8c4 | 80 | Y = (Y << 8) + Data[0]; |
f3d | 3:b9783107a8c4 | 81 | |
f3d | 3:b9783107a8c4 | 82 | int16_t Z; |
f3d | 3:b9783107a8c4 | 83 | Data[0]=0x80 + 0x2c; // Register number 0x2c has the Z data (2 bytes) |
f3d | 3:b9783107a8c4 | 84 | Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number |
f3d | 3:b9783107a8c4 | 85 | Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents |
f3d | 3:b9783107a8c4 | 86 | Z = Data[1]; |
f3d | 3:b9783107a8c4 | 87 | Z = (Z << 8) + Data[0]; |
f3d | 3:b9783107a8c4 | 88 | |
f3d | 3:b9783107a8c4 | 89 | updateMagX(X); |
f3d | 3:b9783107a8c4 | 90 | updateMagY(Y); |
f3d | 3:b9783107a8c4 | 91 | updateMagZ(Z); |
f3d | 3:b9783107a8c4 | 92 | |
f3d | 3:b9783107a8c4 | 93 | } |
f3d | 3:b9783107a8c4 | 94 | private: |
f3d | 3:b9783107a8c4 | 95 | BLEDevice &ble; |
f3d | 3:b9783107a8c4 | 96 | ReadOnlyGattCharacteristic<int16_t> MagX; |
f3d | 3:b9783107a8c4 | 97 | ReadOnlyGattCharacteristic<int16_t> MagY; |
f3d | 3:b9783107a8c4 | 98 | ReadOnlyGattCharacteristic<int16_t> MagZ; |
f3d | 3:b9783107a8c4 | 99 | }; |
f3d | 3:b9783107a8c4 | 100 | |
f3d | 3:b9783107a8c4 | 101 | #endif /* #ifndef __BLE_ACCEL_SERVICE_H__ */ |