Test program for magnetometer

Dependencies:   mbed AES BLE_API nRF51822 smallAES

Committer:
f3d
Date:
Mon Sep 30 10:38:33 2019 +0000
Revision:
2:4871b5ad7938
Code for the LSM303 version of the BBC Microbit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
f3d 2:4871b5ad7938 1 /* mbed Microcontroller Library
f3d 2:4871b5ad7938 2 * Copyright (c) 2006-2013 ARM Limited
f3d 2:4871b5ad7938 3 *
f3d 2:4871b5ad7938 4 * Licensed under the Apache License, Version 2.0 (the "License");
f3d 2:4871b5ad7938 5 * you may not use this file except in compliance with the License.
f3d 2:4871b5ad7938 6 * You may obtain a copy of the License at
f3d 2:4871b5ad7938 7 *
f3d 2:4871b5ad7938 8 * http://www.apache.org/licenses/LICENSE-2.0
f3d 2:4871b5ad7938 9 *
f3d 2:4871b5ad7938 10 * Unless required by applicable law or agreed to in writing, software
f3d 2:4871b5ad7938 11 * distributed under the License is distributed on an "AS IS" BASIS,
f3d 2:4871b5ad7938 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f3d 2:4871b5ad7938 13 * See the License for the specific language governing permissions and
f3d 2:4871b5ad7938 14 * limitations under the License.
f3d 2:4871b5ad7938 15 */
f3d 2:4871b5ad7938 16
f3d 2:4871b5ad7938 17 #ifndef __BLE_ACCEL_SERVICE_H__
f3d 2:4871b5ad7938 18 #define __BLE_ACCEL_SERVICE_H__
f3d 2:4871b5ad7938 19 #include <mbed.h>
f3d 2:4871b5ad7938 20 // Accelerometer : LSM303
f3d 2:4871b5ad7938 21 I2C i2c(P0_30, P0_0); // SDA is on P0_30, SCL is on P0_0
f3d 2:4871b5ad7938 22 const int LSM303_ADDRESS = (0x19<<1);
f3d 2:4871b5ad7938 23 //const int MMA8653_ID = 0x5a;
f3d 2:4871b5ad7938 24 class ACCELService {
f3d 2:4871b5ad7938 25 public:
f3d 2:4871b5ad7938 26 const static uint16_t ACCEL_SERVICE_UUID = 0xA012;
f3d 2:4871b5ad7938 27 const static uint16_t ACCEL_X_CHARACTERISTIC_UUID = 0xA013;
f3d 2:4871b5ad7938 28 const static uint16_t ACCEL_Y_CHARACTERISTIC_UUID = 0xA014;
f3d 2:4871b5ad7938 29 const static uint16_t ACCEL_Z_CHARACTERISTIC_UUID = 0xA015;
f3d 2:4871b5ad7938 30
f3d 2:4871b5ad7938 31 ACCELService(BLEDevice &_ble, int16_t initialValueForACCELCharacteristic) :
f3d 2:4871b5ad7938 32 ble(_ble), AccelX(ACCEL_X_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic),AccelY(ACCEL_Y_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic),AccelZ(ACCEL_Z_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic)
f3d 2:4871b5ad7938 33 {
f3d 2:4871b5ad7938 34 GattCharacteristic *charTable[] = {&AccelX,&AccelY,&AccelZ};
f3d 2:4871b5ad7938 35 GattService AccelService(ACCEL_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
f3d 2:4871b5ad7938 36 ble.addService(AccelService);
f3d 2:4871b5ad7938 37 // Wake the accelerometer from sleep mode by writing disabling low power mode, setting freq to 10Hz and enabling all three axes
f3d 2:4871b5ad7938 38 char Data[8]; // Declare a buffer for data transfer
f3d 2:4871b5ad7938 39 int Status;
f3d 2:4871b5ad7938 40 Data[0]=0x20; // wake up LSM303 (max speed, all accel channels)
f3d 2:4871b5ad7938 41 Data[1]=0x77;
f3d 2:4871b5ad7938 42 Status = i2c.write(LSM303_ADDRESS,Data,2); // Write data to register
f3d 2:4871b5ad7938 43
f3d 2:4871b5ad7938 44 Data[0]=0x23; // enable high resolution mode
f3d 2:4871b5ad7938 45 Data[1]=0x0e;
f3d 2:4871b5ad7938 46 Status = i2c.write(LSM303_ADDRESS,Data,2); // Write data to register
f3d 2:4871b5ad7938 47 }
f3d 2:4871b5ad7938 48
f3d 2:4871b5ad7938 49 GattAttribute::Handle_t getValueHandle() const {
f3d 2:4871b5ad7938 50 return AccelX.getValueHandle();
f3d 2:4871b5ad7938 51 }
f3d 2:4871b5ad7938 52 void updateAccelX(uint16_t newValue) {
f3d 2:4871b5ad7938 53 ble.gattServer().write(AccelX.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
f3d 2:4871b5ad7938 54 }
f3d 2:4871b5ad7938 55 void updateAccelY(uint16_t newValue) {
f3d 2:4871b5ad7938 56 ble.gattServer().write(AccelY.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
f3d 2:4871b5ad7938 57 }
f3d 2:4871b5ad7938 58 void updateAccelZ(uint16_t newValue) {
f3d 2:4871b5ad7938 59 ble.gattServer().write(AccelZ.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
f3d 2:4871b5ad7938 60 }
f3d 2:4871b5ad7938 61 void poll()
f3d 2:4871b5ad7938 62 {
f3d 2:4871b5ad7938 63 char Data[8]; // Declare a buffer for data transfer
f3d 2:4871b5ad7938 64 int Status;
f3d 2:4871b5ad7938 65 int16_t X;
f3d 2:4871b5ad7938 66
f3d 2:4871b5ad7938 67
f3d 2:4871b5ad7938 68 Data[0]=0x80 + 0x28; // Register number 0x28 has the X data (2 bytes)
f3d 2:4871b5ad7938 69 Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number
f3d 2:4871b5ad7938 70 Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents
f3d 2:4871b5ad7938 71 X = Data[1];
f3d 2:4871b5ad7938 72 X = (X << 8) + Data[0];
f3d 2:4871b5ad7938 73
f3d 2:4871b5ad7938 74 int16_t Y;
f3d 2:4871b5ad7938 75 Data[0]=0x80 + 0x2a; // Register number 0x2a has the Y data (2 bytes)
f3d 2:4871b5ad7938 76 Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number
f3d 2:4871b5ad7938 77 Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents
f3d 2:4871b5ad7938 78 Y = Data[1];
f3d 2:4871b5ad7938 79 Y = (Y << 8) + Data[0];
f3d 2:4871b5ad7938 80
f3d 2:4871b5ad7938 81 int16_t Z;
f3d 2:4871b5ad7938 82 Data[0]=0x80 + 0x2c; // Register number 0x2c has the Z data (2 bytes)
f3d 2:4871b5ad7938 83 Status = i2c.write(LSM303_ADDRESS,Data,1,true); // Write register number
f3d 2:4871b5ad7938 84 Status = i2c.read(LSM303_ADDRESS,Data,2); // Read register contents
f3d 2:4871b5ad7938 85 Z = Data[1];
f3d 2:4871b5ad7938 86 Z = (Z << 8) + Data[0];
f3d 2:4871b5ad7938 87
f3d 2:4871b5ad7938 88 updateAccelX(X);
f3d 2:4871b5ad7938 89 updateAccelY(Y);
f3d 2:4871b5ad7938 90 updateAccelZ(Z);
f3d 2:4871b5ad7938 91
f3d 2:4871b5ad7938 92 }
f3d 2:4871b5ad7938 93 private:
f3d 2:4871b5ad7938 94 BLEDevice &ble;
f3d 2:4871b5ad7938 95 ReadOnlyGattCharacteristic<int16_t> AccelX;
f3d 2:4871b5ad7938 96 ReadOnlyGattCharacteristic<int16_t> AccelY;
f3d 2:4871b5ad7938 97 ReadOnlyGattCharacteristic<int16_t> AccelZ;
f3d 2:4871b5ad7938 98 };
f3d 2:4871b5ad7938 99
f3d 2:4871b5ad7938 100 #endif /* #ifndef __BLE_ACCEL_SERVICE_H__ */