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

Dependencies:   mbed AES BLE_API nRF51822 smallAES

accelService.h

Committer:
f3d
Date:
2019-10-02
Revision:
4:052ba96b27f5
Parent:
2:4871b5ad7938

File content as of revision 4:052ba96b27f5:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __BLE_ACCEL_SERVICE_H__
#define __BLE_ACCEL_SERVICE_H__
#include <mbed.h>
// Accelerometer : LSM303
I2C i2c(P0_30, P0_0); // SDA is on P0_30, SCL is on P0_0
const int MMA8653_ADDRESS = (0x1d<<1); 
const int MMA8653_ID = 0x5a;
class ACCELService {
public:
    const static uint16_t ACCEL_SERVICE_UUID = 0xA012;
    const static uint16_t ACCEL_X_CHARACTERISTIC_UUID = 0xA013;
    const static uint16_t ACCEL_Y_CHARACTERISTIC_UUID = 0xA014;
    const static uint16_t ACCEL_Z_CHARACTERISTIC_UUID = 0xA015;

    ACCELService(BLEDevice &_ble, int16_t initialValueForACCELCharacteristic) :
        ble(_ble), AccelX(ACCEL_X_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic),AccelY(ACCEL_Y_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic),AccelZ(ACCEL_Z_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic)
    {
        GattCharacteristic *charTable[] = {&AccelX,&AccelY,&AccelZ};
        GattService         AccelService(ACCEL_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
        ble.addService(AccelService);
        // Wake the accelerometer from sleep mode by writing 1 to register number 0x2a    
        char Data[8]; // Declare a buffer for data transfer    
        int Status;
        Data[0]=0x2a; 
        Data[1]=1;
        Status = i2c.write(MMA8653_ADDRESS,Data,2);  // Write data to register    
    }

    GattAttribute::Handle_t getValueHandle() const {
        return AccelX.getValueHandle();
    }
    void updateAccelX(uint16_t newValue) {
        ble.gattServer().write(AccelX.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
    }
    void updateAccelY(uint16_t newValue) {
        ble.gattServer().write(AccelY.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
    }
    void updateAccelZ(uint16_t newValue) {
        ble.gattServer().write(AccelZ.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t));
    }
    void poll()
    {
        char Data[8]; // Declare a buffer for data transfer    
        int Status;
        int16_t X;
        Data[0]=0x01; // Register number 1 has the X data (2 bytes)
        Status = i2c.write(MMA8653_ADDRESS,Data,1,true);  // Write register number
        Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents
        X = Data[0];
        X = (X << 8) + Data[1];
        X = X >> 6; // only 10 bits of data are available
        
        int16_t Y;
        Data[0]=0x03; // Register number 3 has the Y data (2 bytes)
        Status = i2c.write(MMA8653_ADDRESS,Data,1,true);  // Write register number
        Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents
        Y = Data[0];
        Y = (Y << 8) + Data[1];
        Y = Y >> 6; // only 10 bits of data are available        
        
        int16_t Z;
        Data[0]=0x05; // Register number 1 has the Z data (2 bytes)
        Status = i2c.write(MMA8653_ADDRESS,Data,1,true);  // Write register number
        Status = i2c.read(MMA8653_ADDRESS,Data,2); // Read register contents
        Z = Data[0];
        Z = (Z << 8) + Data[1];
        Z = Z >> 6; // only 10 bits of data are available
        
        updateAccelX(X);
        updateAccelY(Y);
        updateAccelZ(Z);        
        
    }
private:
    BLEDevice &ble;
    ReadOnlyGattCharacteristic<int16_t>  AccelX;
    ReadOnlyGattCharacteristic<int16_t>  AccelY;
    ReadOnlyGattCharacteristic<int16_t>  AccelZ;
};

#endif /* #ifndef __BLE_ACCEL_SERVICE_H__ */