Initial version for the 2 chip MMA8653/MAG3110 version of the BBC Microbit

Dependencies:   mbed AES BLE_API nRF51822 smallAES

Committer:
f3d
Date:
Wed Oct 02 18:52:21 2019 +0000
Revision:
4:052ba96b27f5
Parent:
2:4871b5ad7938
Initial commit for MMA8653 version of 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 4:052ba96b27f5 22 const int MMA8653_ADDRESS = (0x1d<<1);
f3d 4:052ba96b27f5 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 4:052ba96b27f5 37 // Wake the accelerometer from sleep mode by writing 1 to register number 0x2a
f3d 2:4871b5ad7938 38 char Data[8]; // Declare a buffer for data transfer
f3d 2:4871b5ad7938 39 int Status;
f3d 4:052ba96b27f5 40 Data[0]=0x2a;
f3d 4:052ba96b27f5 41 Data[1]=1;
f3d 4:052ba96b27f5 42 Status = i2c.write(MMA8653_ADDRESS,Data,2); // Write data to register
f3d 2:4871b5ad7938 43 }
f3d 2:4871b5ad7938 44
f3d 2:4871b5ad7938 45 GattAttribute::Handle_t getValueHandle() const {
f3d 2:4871b5ad7938 46 return AccelX.getValueHandle();
f3d 2:4871b5ad7938 47 }
f3d 2:4871b5ad7938 48 void updateAccelX(uint16_t newValue) {
f3d 2:4871b5ad7938 49 ble.gattServer().write(AccelX.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
f3d 2:4871b5ad7938 50 }
f3d 2:4871b5ad7938 51 void updateAccelY(uint16_t newValue) {
f3d 2:4871b5ad7938 52 ble.gattServer().write(AccelY.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
f3d 2:4871b5ad7938 53 }
f3d 2:4871b5ad7938 54 void updateAccelZ(uint16_t newValue) {
f3d 2:4871b5ad7938 55 ble.gattServer().write(AccelZ.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
f3d 2:4871b5ad7938 56 }
f3d 2:4871b5ad7938 57 void poll()
f3d 2:4871b5ad7938 58 {
f3d 2:4871b5ad7938 59 char Data[8]; // Declare a buffer for data transfer
f3d 2:4871b5ad7938 60 int Status;
f3d 2:4871b5ad7938 61 int16_t X;
f3d 4:052ba96b27f5 62 Data[0]=0x01; // Register number 1 has the X data (2 bytes)
f3d 4:052ba96b27f5 63 Status = i2c.write(MMA8653_ADDRESS,Data,1,true); // Write register number
f3d 4:052ba96b27f5 64 Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents
f3d 4:052ba96b27f5 65 X = Data[0];
f3d 4:052ba96b27f5 66 X = (X << 8) + Data[1];
f3d 4:052ba96b27f5 67 X = X >> 6; // only 10 bits of data are available
f3d 2:4871b5ad7938 68
f3d 2:4871b5ad7938 69 int16_t Y;
f3d 4:052ba96b27f5 70 Data[0]=0x03; // Register number 3 has the Y data (2 bytes)
f3d 4:052ba96b27f5 71 Status = i2c.write(MMA8653_ADDRESS,Data,1,true); // Write register number
f3d 4:052ba96b27f5 72 Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents
f3d 4:052ba96b27f5 73 Y = Data[0];
f3d 4:052ba96b27f5 74 Y = (Y << 8) + Data[1];
f3d 4:052ba96b27f5 75 Y = Y >> 6; // only 10 bits of data are available
f3d 2:4871b5ad7938 76
f3d 2:4871b5ad7938 77 int16_t Z;
f3d 4:052ba96b27f5 78 Data[0]=0x05; // Register number 1 has the Z data (2 bytes)
f3d 4:052ba96b27f5 79 Status = i2c.write(MMA8653_ADDRESS,Data,1,true); // Write register number
f3d 4:052ba96b27f5 80 Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents
f3d 4:052ba96b27f5 81 Z = Data[0];
f3d 4:052ba96b27f5 82 Z = (Z << 8) + Data[1];
f3d 4:052ba96b27f5 83 Z = Z >> 6; // only 10 bits of data are available
f3d 2:4871b5ad7938 84
f3d 2:4871b5ad7938 85 updateAccelX(X);
f3d 2:4871b5ad7938 86 updateAccelY(Y);
f3d 2:4871b5ad7938 87 updateAccelZ(Z);
f3d 2:4871b5ad7938 88
f3d 2:4871b5ad7938 89 }
f3d 2:4871b5ad7938 90 private:
f3d 2:4871b5ad7938 91 BLEDevice &ble;
f3d 2:4871b5ad7938 92 ReadOnlyGattCharacteristic<int16_t> AccelX;
f3d 2:4871b5ad7938 93 ReadOnlyGattCharacteristic<int16_t> AccelY;
f3d 2:4871b5ad7938 94 ReadOnlyGattCharacteristic<int16_t> AccelZ;
f3d 2:4871b5ad7938 95 };
f3d 2:4871b5ad7938 96
f3d 2:4871b5ad7938 97 #endif /* #ifndef __BLE_ACCEL_SERVICE_H__ */