nrf52 nrf51 with same code

Dependencies:   CCS811

Fork of MtConnect04S_MtSense02 by MtM+

Committer:
johnathanlyu
Date:
Fri Apr 27 09:52:19 2018 +0000
Revision:
2:f3768a226561
Parent:
0:4d76c0bc0f78
add 52 support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnathanlyu 0:4d76c0bc0f78 1 #ifndef AIRQUALITYSERVICE_H
johnathanlyu 0:4d76c0bc0f78 2 #define AIRQUALITYSERVICE_H
johnathanlyu 0:4d76c0bc0f78 3
johnathanlyu 0:4d76c0bc0f78 4 #include "ble/BLE.h"
johnathanlyu 0:4d76c0bc0f78 5
johnathanlyu 0:4d76c0bc0f78 6 class AirQualityService {
johnathanlyu 0:4d76c0bc0f78 7 public:
johnathanlyu 0:4d76c0bc0f78 8 typedef uint16_t CO2Type_t;
johnathanlyu 0:4d76c0bc0f78 9 typedef uint16_t TVOCType_t;
johnathanlyu 0:4d76c0bc0f78 10 AirQualityService(BLE& _ble, UUID co2CID, UUID tvocCID) :
johnathanlyu 0:4d76c0bc0f78 11 ble(_ble),
johnathanlyu 0:4d76c0bc0f78 12 co2Characteristic (co2CID, &co2, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
johnathanlyu 0:4d76c0bc0f78 13 tvocCharacteristic(tvocCID, &tvoc, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
johnathanlyu 0:4d76c0bc0f78 14
johnathanlyu 0:4d76c0bc0f78 15 uint8_t AQSID[16] = { '@', 'M', 'T', 'M',
johnathanlyu 0:4d76c0bc0f78 16 'C', 'O', '2', 'A',
johnathanlyu 0:4d76c0bc0f78 17 'N', 'D', 'T', 'V',
johnathanlyu 0:4d76c0bc0f78 18 'O', 'C', '@', '@',
johnathanlyu 0:4d76c0bc0f78 19 };
johnathanlyu 0:4d76c0bc0f78 20
johnathanlyu 0:4d76c0bc0f78 21 static bool serviceAdded = false; /* We should only ever need to add the information service once. */
johnathanlyu 0:4d76c0bc0f78 22 UUID customUUID(AQSID);
johnathanlyu 0:4d76c0bc0f78 23
johnathanlyu 0:4d76c0bc0f78 24 if (serviceAdded) {
johnathanlyu 0:4d76c0bc0f78 25 return;
johnathanlyu 0:4d76c0bc0f78 26 }
johnathanlyu 0:4d76c0bc0f78 27
johnathanlyu 0:4d76c0bc0f78 28 GattCharacteristic *charTable[] = { &co2Characteristic, &tvocCharacteristic };
johnathanlyu 0:4d76c0bc0f78 29
johnathanlyu 0:4d76c0bc0f78 30 GattService customAQService(customUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
johnathanlyu 0:4d76c0bc0f78 31
johnathanlyu 0:4d76c0bc0f78 32 ble.gattServer().addService(customAQService);
johnathanlyu 0:4d76c0bc0f78 33 serviceAdded = true;
johnathanlyu 0:4d76c0bc0f78 34
johnathanlyu 0:4d76c0bc0f78 35 }
johnathanlyu 0:4d76c0bc0f78 36
johnathanlyu 0:4d76c0bc0f78 37 void updateCO2(uint16_t co2ppm) {
johnathanlyu 0:4d76c0bc0f78 38 co2 = (CO2Type_t) co2ppm;
johnathanlyu 0:4d76c0bc0f78 39 ble.gattServer().write(co2Characteristic.getValueHandle(), (uint8_t *) &co2, sizeof(CO2Type_t));
johnathanlyu 0:4d76c0bc0f78 40 }
johnathanlyu 0:4d76c0bc0f78 41
johnathanlyu 0:4d76c0bc0f78 42 void updateTVOC(uint16_t tvocppb) {
johnathanlyu 0:4d76c0bc0f78 43 tvoc = (TVOCType_t) tvocppb;
johnathanlyu 0:4d76c0bc0f78 44 ble.gattServer().write(tvocCharacteristic.getValueHandle(), (uint8_t *) &tvoc, sizeof(TVOCType_t));
johnathanlyu 0:4d76c0bc0f78 45 }
johnathanlyu 0:4d76c0bc0f78 46
johnathanlyu 0:4d76c0bc0f78 47 private:
johnathanlyu 0:4d76c0bc0f78 48 BLE& ble;
johnathanlyu 0:4d76c0bc0f78 49 CO2Type_t co2;
johnathanlyu 0:4d76c0bc0f78 50 TVOCType_t tvoc;
johnathanlyu 0:4d76c0bc0f78 51 ReadOnlyGattCharacteristic<CO2Type_t> co2Characteristic;
johnathanlyu 0:4d76c0bc0f78 52 ReadOnlyGattCharacteristic<TVOCType_t> tvocCharacteristic;
johnathanlyu 0:4d76c0bc0f78 53 };
johnathanlyu 0:4d76c0bc0f78 54
johnathanlyu 0:4d76c0bc0f78 55 #endif