BTLE example for a CJMCU-8223 (NRF51822+Lis3dh). Also includes an OLED display. Further details on ioprog.com
Dependencies: mbed BLE_API nRF51822
accelService.h@0:8fa1c7356f71, 2020-06-24 (annotated)
- Committer:
- f3d
- Date:
- Wed Jun 24 11:37:11 2020 +0000
- Revision:
- 0:8fa1c7356f71
Initial commit for CJMCU-8223 offering an LED service and an accelerometer service. Also includes an Oled display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 0:8fa1c7356f71 | 1 | /* mbed Microcontroller Library |
f3d | 0:8fa1c7356f71 | 2 | * Copyright (c) 2006-2013 ARM Limited |
f3d | 0:8fa1c7356f71 | 3 | * |
f3d | 0:8fa1c7356f71 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
f3d | 0:8fa1c7356f71 | 5 | * you may not use this file except in compliance with the License. |
f3d | 0:8fa1c7356f71 | 6 | * You may obtain a copy of the License at |
f3d | 0:8fa1c7356f71 | 7 | * |
f3d | 0:8fa1c7356f71 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
f3d | 0:8fa1c7356f71 | 9 | * |
f3d | 0:8fa1c7356f71 | 10 | * Unless required by applicable law or agreed to in writing, software |
f3d | 0:8fa1c7356f71 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
f3d | 0:8fa1c7356f71 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
f3d | 0:8fa1c7356f71 | 13 | * See the License for the specific language governing permissions and |
f3d | 0:8fa1c7356f71 | 14 | * limitations under the License. |
f3d | 0:8fa1c7356f71 | 15 | */ |
f3d | 0:8fa1c7356f71 | 16 | |
f3d | 0:8fa1c7356f71 | 17 | #ifndef __BLE_ACCEL_SERVICE_H__ |
f3d | 0:8fa1c7356f71 | 18 | #define __BLE_ACCEL_SERVICE_H__ |
f3d | 0:8fa1c7356f71 | 19 | #include <mbed.h> |
f3d | 0:8fa1c7356f71 | 20 | #include <lis3dh.h> |
f3d | 0:8fa1c7356f71 | 21 | lis3dh Lis3dh; |
f3d | 0:8fa1c7356f71 | 22 | class accelService { |
f3d | 0:8fa1c7356f71 | 23 | public: |
f3d | 0:8fa1c7356f71 | 24 | const static uint16_t ACCEL_SERVICE_UUID = 0xA012; |
f3d | 0:8fa1c7356f71 | 25 | const static uint16_t ACCEL_X_CHARACTERISTIC_UUID = 0xA013; |
f3d | 0:8fa1c7356f71 | 26 | const static uint16_t ACCEL_Y_CHARACTERISTIC_UUID = 0xA014; |
f3d | 0:8fa1c7356f71 | 27 | const static uint16_t ACCEL_Z_CHARACTERISTIC_UUID = 0xA015; |
f3d | 0:8fa1c7356f71 | 28 | |
f3d | 0:8fa1c7356f71 | 29 | accelService(BLEDevice &_ble, int16_t initialValueForACCELCharacteristic) : |
f3d | 0:8fa1c7356f71 | 30 | ble(_ble), AccelX(ACCEL_X_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic),AccelY(ACCEL_Y_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic),AccelZ(ACCEL_Z_CHARACTERISTIC_UUID, &initialValueForACCELCharacteristic) |
f3d | 0:8fa1c7356f71 | 31 | { |
f3d | 0:8fa1c7356f71 | 32 | GattCharacteristic *charTable[] = {&AccelX,&AccelY,&AccelZ}; |
f3d | 0:8fa1c7356f71 | 33 | GattService AccelService(ACCEL_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
f3d | 0:8fa1c7356f71 | 34 | ble.addService(AccelService); |
f3d | 0:8fa1c7356f71 | 35 | Lis3dh.begin(); |
f3d | 0:8fa1c7356f71 | 36 | } |
f3d | 0:8fa1c7356f71 | 37 | |
f3d | 0:8fa1c7356f71 | 38 | GattAttribute::Handle_t getValueHandle() const { |
f3d | 0:8fa1c7356f71 | 39 | return AccelX.getValueHandle(); |
f3d | 0:8fa1c7356f71 | 40 | } |
f3d | 0:8fa1c7356f71 | 41 | void updateAccelX(uint16_t newValue) { |
f3d | 0:8fa1c7356f71 | 42 | ble.gattServer().write(AccelX.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t)); |
f3d | 0:8fa1c7356f71 | 43 | } |
f3d | 0:8fa1c7356f71 | 44 | void updateAccelY(uint16_t newValue) { |
f3d | 0:8fa1c7356f71 | 45 | ble.gattServer().write(AccelY.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t)); |
f3d | 0:8fa1c7356f71 | 46 | } |
f3d | 0:8fa1c7356f71 | 47 | void updateAccelZ(uint16_t newValue) { |
f3d | 0:8fa1c7356f71 | 48 | ble.gattServer().write(AccelZ.getValueHandle(), (uint8_t *)&newValue, sizeof(uint16_t)); |
f3d | 0:8fa1c7356f71 | 49 | } |
f3d | 0:8fa1c7356f71 | 50 | void poll() |
f3d | 0:8fa1c7356f71 | 51 | { |
f3d | 0:8fa1c7356f71 | 52 | int X,Y,Z; |
f3d | 0:8fa1c7356f71 | 53 | Lis3dh.read(X,Y,Z); |
f3d | 0:8fa1c7356f71 | 54 | updateAccelX(X); |
f3d | 0:8fa1c7356f71 | 55 | updateAccelY(Y); |
f3d | 0:8fa1c7356f71 | 56 | updateAccelZ(Z); |
f3d | 0:8fa1c7356f71 | 57 | |
f3d | 0:8fa1c7356f71 | 58 | } |
f3d | 0:8fa1c7356f71 | 59 | private: |
f3d | 0:8fa1c7356f71 | 60 | BLEDevice &ble; |
f3d | 0:8fa1c7356f71 | 61 | ReadOnlyGattCharacteristic<int16_t> AccelX; |
f3d | 0:8fa1c7356f71 | 62 | ReadOnlyGattCharacteristic<int16_t> AccelY; |
f3d | 0:8fa1c7356f71 | 63 | ReadOnlyGattCharacteristic<int16_t> AccelZ; |
f3d | 0:8fa1c7356f71 | 64 | }; |
f3d | 0:8fa1c7356f71 | 65 | |
f3d | 0:8fa1c7356f71 | 66 | #endif /* #ifndef __BLE_ACCEL_SERVICE_H__ */ |