Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@7:c6c5ba871c2d, 2016-10-21 (annotated)
- Committer:
- tsungta
- Date:
- Fri Oct 21 02:27:42 2016 +0000
- Revision:
- 7:c6c5ba871c2d
- Parent:
- 5:5f9bc8b53db9
- Child:
- 8:4653319ba675
Update to fit the usage of NQ620 platform
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Marcomissyou | 0:ef0f188a6fdd | 1 | /* mbed Microcontroller Library |
| Marcomissyou | 0:ef0f188a6fdd | 2 | * Copyright (c) 2006-2013 ARM Limited |
| Marcomissyou | 0:ef0f188a6fdd | 3 | * |
| Marcomissyou | 0:ef0f188a6fdd | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| Marcomissyou | 0:ef0f188a6fdd | 5 | * you may not use this file except in compliance with the License. |
| Marcomissyou | 0:ef0f188a6fdd | 6 | * You may obtain a copy of the License at |
| Marcomissyou | 0:ef0f188a6fdd | 7 | * |
| Marcomissyou | 0:ef0f188a6fdd | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| Marcomissyou | 0:ef0f188a6fdd | 9 | * |
| Marcomissyou | 0:ef0f188a6fdd | 10 | * Unless required by applicable law or agreed to in writing, software |
| Marcomissyou | 0:ef0f188a6fdd | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| Marcomissyou | 0:ef0f188a6fdd | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| Marcomissyou | 0:ef0f188a6fdd | 13 | * See the License for the specific language governing permissions and |
| Marcomissyou | 0:ef0f188a6fdd | 14 | * limitations under the License. |
| Marcomissyou | 0:ef0f188a6fdd | 15 | */ |
| Marcomissyou | 0:ef0f188a6fdd | 16 | |
| Marcomissyou | 0:ef0f188a6fdd | 17 | #include "mbed.h" |
| Marcomissyou | 0:ef0f188a6fdd | 18 | #include "BatteryService.h" |
| Marcomissyou | 0:ef0f188a6fdd | 19 | #include "DeviceInformationService.h" |
| Marcomissyou | 5:5f9bc8b53db9 | 20 | #include "BLE.h" |
| Marcomissyou | 0:ef0f188a6fdd | 21 | |
| Marcomissyou | 0:ef0f188a6fdd | 22 | #include "hts221.h" |
| Marcomissyou | 0:ef0f188a6fdd | 23 | #include "uvis25.h" |
| Marcomissyou | 0:ef0f188a6fdd | 24 | #include "nrf_gpio.h" |
| Marcomissyou | 0:ef0f188a6fdd | 25 | |
| Marcomissyou | 0:ef0f188a6fdd | 26 | #include "nrf51_bitfields.h" |
| Marcomissyou | 0:ef0f188a6fdd | 27 | |
| Marcomissyou | 0:ef0f188a6fdd | 28 | BLEDevice ble; |
| Marcomissyou | 1:a7ecbb0ccc61 | 29 | |
| Marcomissyou | 3:6a9082a10dc9 | 30 | DigitalOut led1(LED1); |
| Marcomissyou | 0:ef0f188a6fdd | 31 | |
| Marcomissyou | 0:ef0f188a6fdd | 32 | static const uint8_t UUID_HUMI_AND_UVI[] = {0xf5, 0x59, 0xa2, 0x49, 0xbe, 0xb1, 0x4c, 0x54, 0xa1, 0x0a, 0xc7, 0x95, 0x7e, 0x17, 0xf8, 0x67}; |
| Marcomissyou | 0:ef0f188a6fdd | 33 | uint8_t wrs_HumiUVI_payload[11] = {0, }; |
| Marcomissyou | 0:ef0f188a6fdd | 34 | uint8_t htsTempPayload[7] = {0,}; |
| Marcomissyou | 0:ef0f188a6fdd | 35 | /* Health Thermometer Service */ |
| Marcomissyou | 0:ef0f188a6fdd | 36 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ |
| Marcomissyou | 0:ef0f188a6fdd | 37 | /* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
| Marcomissyou | 0:ef0f188a6fdd | 38 | GattCharacteristic htsTemp ( GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, htsTempPayload, 6, 13, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); |
| Marcomissyou | 0:ef0f188a6fdd | 39 | GattCharacteristic wrs_HumiUVI ( UUID_HUMI_AND_UVI, wrs_HumiUVI_payload, 1, 7, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); |
| Marcomissyou | 0:ef0f188a6fdd | 40 | GattCharacteristic *htsChars[] = {&htsTemp, &wrs_HumiUVI}; |
| Marcomissyou | 0:ef0f188a6fdd | 41 | GattService htsService ( GattService::UUID_HEALTH_THERMOMETER_SERVICE, htsChars, sizeof(htsChars) / sizeof(GattCharacteristic *)); |
| Marcomissyou | 0:ef0f188a6fdd | 42 | |
| Marcomissyou | 0:ef0f188a6fdd | 43 | float tempCelsius = 25.50; |
| Marcomissyou | 0:ef0f188a6fdd | 44 | int32_t tempCelsius_ix100; |
| Marcomissyou | 0:ef0f188a6fdd | 45 | int8_t exponent = -2; |
| Marcomissyou | 0:ef0f188a6fdd | 46 | uint8_t UTC[7] = { 222, 7, 9, 11, 13, 42, 59 }; |
| Marcomissyou | 0:ef0f188a6fdd | 47 | float humi = 55; |
| Marcomissyou | 0:ef0f188a6fdd | 48 | int32_t humi_ix100; |
| Marcomissyou | 0:ef0f188a6fdd | 49 | uint8_t uvi = 8; |
| Marcomissyou | 0:ef0f188a6fdd | 50 | uint32_t counter = 0; |
| Marcomissyou | 0:ef0f188a6fdd | 51 | |
| Marcomissyou | 0:ef0f188a6fdd | 52 | const static char DEVICE_NAME[] = "WRS_d7"; |
| Marcomissyou | 0:ef0f188a6fdd | 53 | static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE, |
| Marcomissyou | 0:ef0f188a6fdd | 54 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
| Marcomissyou | 0:ef0f188a6fdd | 55 | static volatile bool triggerSensorPolling = false; |
| Marcomissyou | 0:ef0f188a6fdd | 56 | |
| Marcomissyou | 0:ef0f188a6fdd | 57 | /**************************************************************************/ |
| Marcomissyou | 0:ef0f188a6fdd | 58 | /*! |
| Marcomissyou | 0:ef0f188a6fdd | 59 | @brief This custom class can be used to override any GattServerEvents |
| Marcomissyou | 0:ef0f188a6fdd | 60 | that you are interested in handling on an application level. |
| Marcomissyou | 0:ef0f188a6fdd | 61 | */ |
| Marcomissyou | 0:ef0f188a6fdd | 62 | /**************************************************************************/ |
| Marcomissyou | 0:ef0f188a6fdd | 63 | // |
| tsungta | 7:c6c5ba871c2d | 64 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
| Marcomissyou | 0:ef0f188a6fdd | 65 | { |
| Marcomissyou | 0:ef0f188a6fdd | 66 | ble.startAdvertising(); |
| Marcomissyou | 0:ef0f188a6fdd | 67 | } |
| Marcomissyou | 0:ef0f188a6fdd | 68 | |
| Marcomissyou | 0:ef0f188a6fdd | 69 | void periodicCallback(void) |
| Marcomissyou | 0:ef0f188a6fdd | 70 | { |
| Marcomissyou | 3:6a9082a10dc9 | 71 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
| Marcomissyou | 0:ef0f188a6fdd | 72 | |
| Marcomissyou | 0:ef0f188a6fdd | 73 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
| Marcomissyou | 0:ef0f188a6fdd | 74 | * heavy-weight sensor polling from the main thread. */ |
| Marcomissyou | 0:ef0f188a6fdd | 75 | triggerSensorPolling = true; |
| Marcomissyou | 0:ef0f188a6fdd | 76 | } |
| Marcomissyou | 0:ef0f188a6fdd | 77 | /**************************************************************************/ |
| Marcomissyou | 0:ef0f188a6fdd | 78 | /*! |
| Marcomissyou | 0:ef0f188a6fdd | 79 | @brief Program entry point |
| Marcomissyou | 0:ef0f188a6fdd | 80 | */ |
| Marcomissyou | 0:ef0f188a6fdd | 81 | /**************************************************************************/ |
| Marcomissyou | 0:ef0f188a6fdd | 82 | |
| Marcomissyou | 0:ef0f188a6fdd | 83 | int main(void) |
| Marcomissyou | 0:ef0f188a6fdd | 84 | { |
| Marcomissyou | 0:ef0f188a6fdd | 85 | |
| tsungta | 7:c6c5ba871c2d | 86 | hts221_init(); |
| tsungta | 7:c6c5ba871c2d | 87 | uvis25_init(); |
| tsungta | 7:c6c5ba871c2d | 88 | |
| Marcomissyou | 0:ef0f188a6fdd | 89 | HTS221_Calib(); |
| Marcomissyou | 0:ef0f188a6fdd | 90 | |
| Marcomissyou | 0:ef0f188a6fdd | 91 | Ticker ticker; |
| Marcomissyou | 0:ef0f188a6fdd | 92 | ticker.attach(periodicCallback, 2); |
| Marcomissyou | 0:ef0f188a6fdd | 93 | |
| Marcomissyou | 0:ef0f188a6fdd | 94 | ble.init(); |
| Marcomissyou | 0:ef0f188a6fdd | 95 | ble.onDisconnection(disconnectionCallback); |
| Marcomissyou | 0:ef0f188a6fdd | 96 | |
| Marcomissyou | 0:ef0f188a6fdd | 97 | /* Setup auxiliary services. */ |
| Marcomissyou | 0:ef0f188a6fdd | 98 | BatteryService battery(ble); |
| Marcomissyou | 0:ef0f188a6fdd | 99 | DeviceInformationService deviceInfo(ble, "Delta", "NNN40_1.0", "SN", "EVB_1.0", "B230-n80-m92", "T_0128b"); |
| Marcomissyou | 0:ef0f188a6fdd | 100 | |
| Marcomissyou | 0:ef0f188a6fdd | 101 | /* Setup advertising. */ |
| Marcomissyou | 0:ef0f188a6fdd | 102 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
| Marcomissyou | 0:ef0f188a6fdd | 103 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
| Marcomissyou | 0:ef0f188a6fdd | 104 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
| Marcomissyou | 0:ef0f188a6fdd | 105 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
| Marcomissyou | 0:ef0f188a6fdd | 106 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| Marcomissyou | 0:ef0f188a6fdd | 107 | ble.setAdvertisingInterval(1600); |
| Marcomissyou | 0:ef0f188a6fdd | 108 | ble.startAdvertising(); |
| Marcomissyou | 0:ef0f188a6fdd | 109 | |
| Marcomissyou | 0:ef0f188a6fdd | 110 | ble.addService(htsService); |
| Marcomissyou | 0:ef0f188a6fdd | 111 | |
| Marcomissyou | 0:ef0f188a6fdd | 112 | while (true) { |
| Marcomissyou | 0:ef0f188a6fdd | 113 | |
| tsungta | 7:c6c5ba871c2d | 114 | if(hts221_verify_product_id()) |
| tsungta | 7:c6c5ba871c2d | 115 | HTS221_ReadTempHumi(&tempCelsius, &humi); |
| tsungta | 7:c6c5ba871c2d | 116 | else { |
| tsungta | 7:c6c5ba871c2d | 117 | //show dummy value |
| tsungta | 7:c6c5ba871c2d | 118 | tempCelsius = 100; |
| tsungta | 7:c6c5ba871c2d | 119 | humi = 100; |
| tsungta | 7:c6c5ba871c2d | 120 | } |
| tsungta | 7:c6c5ba871c2d | 121 | |
| tsungta | 7:c6c5ba871c2d | 122 | if(uvis25_verify_product_id()) |
| Marcomissyou | 3:6a9082a10dc9 | 123 | uvi = UVIS25_ReadUVI(); |
| tsungta | 7:c6c5ba871c2d | 124 | else |
| Marcomissyou | 3:6a9082a10dc9 | 125 | uvi = 100; //Environment Sensor APP will show NO UV DEVICE. |
| tsungta | 7:c6c5ba871c2d | 126 | |
| Marcomissyou | 0:ef0f188a6fdd | 127 | /* Update the Temperature measurement */ |
| Marcomissyou | 0:ef0f188a6fdd | 128 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
| Marcomissyou | 0:ef0f188a6fdd | 129 | tempCelsius_ix100 = tempCelsius * 100; |
| Marcomissyou | 0:ef0f188a6fdd | 130 | htsTempPayload[0] = 0x02; // flag |
| Marcomissyou | 0:ef0f188a6fdd | 131 | htsTempPayload[1] = tempCelsius_ix100%0xff; |
| Marcomissyou | 0:ef0f188a6fdd | 132 | htsTempPayload[2] = tempCelsius_ix100/0xff; |
| Marcomissyou | 0:ef0f188a6fdd | 133 | htsTempPayload[3] = tempCelsius_ix100/0xffff; |
| Marcomissyou | 0:ef0f188a6fdd | 134 | htsTempPayload[4] = exponent; |
| Marcomissyou | 0:ef0f188a6fdd | 135 | for (int i = 0; i < 7; i++) htsTempPayload[i+5] = UTC[i]; |
| Marcomissyou | 0:ef0f188a6fdd | 136 | ble.updateCharacteristicValue(htsTemp.getValueAttribute().getHandle(), htsTempPayload, 12); |
| Marcomissyou | 0:ef0f188a6fdd | 137 | |
| Marcomissyou | 0:ef0f188a6fdd | 138 | ble.waitForEvent(); |
| Marcomissyou | 1:a7ecbb0ccc61 | 139 | wait(1); |
| Marcomissyou | 0:ef0f188a6fdd | 140 | wrs_HumiUVI_payload[0] = 0x03; // flag |
| Marcomissyou | 0:ef0f188a6fdd | 141 | memcpy(&wrs_HumiUVI_payload[1],&humi,4); |
| Marcomissyou | 0:ef0f188a6fdd | 142 | wrs_HumiUVI_payload[5] = uvi; |
| Marcomissyou | 0:ef0f188a6fdd | 143 | |
| Marcomissyou | 0:ef0f188a6fdd | 144 | ble.updateCharacteristicValue(wrs_HumiUVI.getValueAttribute().getHandle(), wrs_HumiUVI_payload, 6); |
| Marcomissyou | 0:ef0f188a6fdd | 145 | ble.waitForEvent(); |
| Marcomissyou | 0:ef0f188a6fdd | 146 | wait(1); |
| Marcomissyou | 0:ef0f188a6fdd | 147 | |
| Marcomissyou | 0:ef0f188a6fdd | 148 | } |
| Marcomissyou | 0:ef0f188a6fdd | 149 | |
| Marcomissyou | 1:a7ecbb0ccc61 | 150 | } |