This is a basic program that provides the necessary BLE service to allow communications with the UPAS
Dependencies: BLE_API mbed nRF51822 CronoDot EEPROM NCP5623BMUTBG ADS1115 BME280 Calibration_one MCP40D17 SDFileSystem LSM303 SI1145 STC3100
Fork of BLE_Button by
main.cpp@16:e066ab7e8fb3, 2015-11-03 (annotated)
- Committer:
- jelord
- Date:
- Tue Nov 03 20:33:27 2015 +0000
- Revision:
- 16:e066ab7e8fb3
- Parent:
- 15:c9c93454dd56
- Child:
- 17:077712e4e5e3
Up-to-date backup...Revert here for testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jelord | 10:66549fa08986 | 1 | //CODE BY JAKE LORD |
jelord | 10:66549fa08986 | 2 | //ALL RIGHTS RESERVED BY VOLCKENS GROUP, FORT COLLINS CO |
rgrover1 | 0:28f095301cb2 | 3 | #include "mbed.h" |
rgrover1 | 5:43a3ab27f2e4 | 4 | #include "BLE.h" |
jelord | 10:66549fa08986 | 5 | #include "UPAS_Service.h" |
jelord | 11:1058647c66e8 | 6 | #include "EEPROM.h" |
jelord | 11:1058647c66e8 | 7 | #include "CronoDot.h" |
jelord | 11:1058647c66e8 | 8 | #include "NCP5623BMUTBG.h" |
jelord | 13:b43ec7e0cc1d | 9 | |
rgrover1 | 0:28f095301cb2 | 10 | |
jelord | 11:1058647c66e8 | 11 | DigitalOut blower(p29, 0); |
jelord | 11:1058647c66e8 | 12 | DigitalOut pbKill(p18, 1); |
jelord | 11:1058647c66e8 | 13 | DigitalOut GPS_EN(p4,0); |
jelord | 11:1058647c66e8 | 14 | EEPROM E2PROM(p22, p20); |
jelord | 11:1058647c66e8 | 15 | CronoDot RTC(p22, p20); |
jelord | 12:27273e6a50b3 | 16 | NCP5623BMUTBG RGB_LED(p22, p20); |
jelord | 16:e066ab7e8fb3 | 17 | |
jelord | 16:e066ab7e8fb3 | 18 | BLE ble; |
jelord | 11:1058647c66e8 | 19 | /*EEPROM ADDRESSING: |
jelord | 11:1058647c66e8 | 20 | 0:Status bit-Unused |
jelord | 11:1058647c66e8 | 21 | 1-15:Device Name |
jelord | 11:1058647c66e8 | 22 | 16-19:Flow Rate |
jelord | 11:1058647c66e8 | 23 | 20: Data Log Interval |
jelord | 11:1058647c66e8 | 24 | 21-26: Start Time: ssmmHHddMMyy |
jelord | 11:1058647c66e8 | 25 | 27-32: Stop Time: ssmmHHddMMyy |
jelord | 11:1058647c66e8 | 26 | 33: Duty Up |
jelord | 11:1058647c66e8 | 27 | 34: Duty Down |
jelord | 11:1058647c66e8 | 28 | 35-38: Home Latitude |
jelord | 11:1058647c66e8 | 29 | 39-42: Home Longitude |
jelord | 11:1058647c66e8 | 30 | 43-46: Work Latitude |
jelord | 11:1058647c66e8 | 31 | 47-50: Work Longitude |
jelord | 11:1058647c66e8 | 32 | 51: Runready: Currently useless, should be 0 |
jelord | 11:1058647c66e8 | 33 | 52-53: Device Calibration |
jelord | 11:1058647c66e8 | 34 | 54: Consider RunReady |
jelord | 11:1058647c66e8 | 35 | 55-56: Menu Options |
jelord | 11:1058647c66e8 | 36 | 57+ Nothing*/ |
rgrover1 | 0:28f095301cb2 | 37 | |
jelord | 10:66549fa08986 | 38 | const static char DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to |
jelord | 10:66549fa08986 | 39 | static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID |
rgrover1 | 0:28f095301cb2 | 40 | |
jelord | 10:66549fa08986 | 41 | UPAS_Service *upasServicePtr; |
rgrover1 | 0:28f095301cb2 | 42 | |
jelord | 12:27273e6a50b3 | 43 | |
jelord | 12:27273e6a50b3 | 44 | //MARK: THIS IS ALL CODE NOT TAKEN FROM OTHER MENU>>>DONT ALTER!!! |
jelord | 10:66549fa08986 | 45 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect |
rgrover1 | 0:28f095301cb2 | 46 | { |
rgrover1 | 6:18d8750f39ee | 47 | ble.gap().startAdvertising(); |
rgrover1 | 0:28f095301cb2 | 48 | } |
rgrover1 | 0:28f095301cb2 | 49 | |
rgrover1 | 0:28f095301cb2 | 50 | void periodicCallback(void) |
rgrover1 | 0:28f095301cb2 | 51 | { |
jelord | 11:1058647c66e8 | 52 | // led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ |
jelord | 12:27273e6a50b3 | 53 | RTC.get_time(); |
jelord | 12:27273e6a50b3 | 54 | const uint8_t refreshPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year}; |
jelord | 12:27273e6a50b3 | 55 | ble.updateCharacteristicValue( upasServicePtr->rtcCharacteristic.getValueHandle(),refreshPassValues,6); |
jelord | 13:b43ec7e0cc1d | 56 | RGB_LED.set_led(3,0,2); |
jelord | 11:1058647c66e8 | 57 | } |
jelord | 13:b43ec7e0cc1d | 58 | |
jelord | 12:27273e6a50b3 | 59 | void writeCharacteristicCallback(const GattWriteCallbackParams *params) |
jelord | 10:66549fa08986 | 60 | { |
jelord | 12:27273e6a50b3 | 61 | uint8_t *writeData = const_cast<uint8_t*>(params->data); |
jelord | 16:e066ab7e8fb3 | 62 | RGB_LED.set_led(2,3,1); |
jelord | 10:66549fa08986 | 63 | // check to see what characteristic was written, by handle |
jelord | 12:27273e6a50b3 | 64 | if(params->handle == upasServicePtr->rtcCharacteristic.getValueHandle()) { |
jelord | 12:27273e6a50b3 | 65 | |
jelord | 12:27273e6a50b3 | 66 | //ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len); |
jelord | 12:27273e6a50b3 | 67 | |
jelord | 12:27273e6a50b3 | 68 | // E2PROM.write(0x00015, writeData+6, 12); |
jelord | 11:1058647c66e8 | 69 | RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]); |
jelord | 12:27273e6a50b3 | 70 | |
jelord | 12:27273e6a50b3 | 71 | }else if(params->handle == upasServicePtr->sampleTimeCharacteristic.getValueHandle()){ |
jelord | 12:27273e6a50b3 | 72 | |
jelord | 12:27273e6a50b3 | 73 | E2PROM.write(0x00015, writeData, 12); |
jelord | 13:b43ec7e0cc1d | 74 | |
jelord | 13:b43ec7e0cc1d | 75 | }else if(params->handle == upasServicePtr->subjectLabelCharacteristic.getValueHandle()){ |
jelord | 13:b43ec7e0cc1d | 76 | E2PROM.write(0x00001,writeData,15); |
jelord | 13:b43ec7e0cc1d | 77 | |
jelord | 15:c9c93454dd56 | 78 | }else if(params->handle == upasServicePtr->runReadyCharacteristic.getValueHandle()){ |
jelord | 13:b43ec7e0cc1d | 79 | /* Trigger twenty-four run-time mode*/ |
jelord | 13:b43ec7e0cc1d | 80 | RGB_LED.set_led(2,3,1); |
jelord | 15:c9c93454dd56 | 81 | //RunReady = 2; |
jelord | 13:b43ec7e0cc1d | 82 | |
jelord | 14:4fc1788b8ad2 | 83 | }else if(params->handle == upasServicePtr->runModeCharacteristic.getValueHandle()){ |
jelord | 13:b43ec7e0cc1d | 84 | /* Trigger demo mode*/ |
jelord | 13:b43ec7e0cc1d | 85 | RGB_LED.set_led(3,1,0); |
jelord | 14:4fc1788b8ad2 | 86 | E2PROM.write(0x00036,writeData,1); |
jelord | 10:66549fa08986 | 87 | } |
jelord | 10:66549fa08986 | 88 | } |
jelord | 10:66549fa08986 | 89 | |
rgrover1 | 0:28f095301cb2 | 90 | int main(void) |
rgrover1 | 0:28f095301cb2 | 91 | { |
jelord | 11:1058647c66e8 | 92 | // led1 = 1; |
rgrover1 | 0:28f095301cb2 | 93 | Ticker ticker; |
jelord | 13:b43ec7e0cc1d | 94 | ticker.attach(periodicCallback, 10); |
jelord | 11:1058647c66e8 | 95 | RGB_LED.set_led(1,1,1); |
jelord | 11:1058647c66e8 | 96 | RTC.get_time(); |
jelord | 12:27273e6a50b3 | 97 | uint8_t rtcPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year}; |
jelord | 12:27273e6a50b3 | 98 | uint8_t sampleTimePassValues[12] = {0,}; |
jelord | 12:27273e6a50b3 | 99 | uint8_t subjectLabelOriginal[15] = {0,}; |
jelord | 12:27273e6a50b3 | 100 | E2PROM.read(0x00015, sampleTimePassValues, 12); |
jelord | 12:27273e6a50b3 | 101 | E2PROM.read(0x00001, subjectLabelOriginal,15); |
rgrover1 | 0:28f095301cb2 | 102 | |
rgrover1 | 0:28f095301cb2 | 103 | ble.init(); |
rgrover1 | 6:18d8750f39ee | 104 | ble.gap().onDisconnection(disconnectionCallback); |
jelord | 12:27273e6a50b3 | 105 | ble.gattServer().onDataWritten(writeCharacteristicCallback); //add writeCharCallback (custom function) to whenever data is being written to device |
jelord | 12:27273e6a50b3 | 106 | UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal); //Create a GattService that is defined in UPAS_Service.h |
jelord | 10:66549fa08986 | 107 | upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service |
rgrover1 | 0:28f095301cb2 | 108 | |
jelord | 10:66549fa08986 | 109 | /* setup advertising |
jelord | 10:66549fa08986 | 110 | Following lines do the follow: |
jelord | 10:66549fa08986 | 111 | 1:Declare the device as Bluetooth Smart(Low-Energy) |
jelord | 10:66549fa08986 | 112 | 2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM |
jelord | 10:66549fa08986 | 113 | 3.Advertise the name that will be associated with the UPAS |
jelord | 10:66549fa08986 | 114 | 4.Allow the UPAS to advertise unrestricted (this might change) */ |
jelord | 10:66549fa08986 | 115 | |
rgrover1 | 6:18d8750f39ee | 116 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 6:18d8750f39ee | 117 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 6:18d8750f39ee | 118 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 6:18d8750f39ee | 119 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
jelord | 10:66549fa08986 | 120 | ble.gap().setAdvertisingInterval(160); /* 160ms. */ |
rgrover1 | 6:18d8750f39ee | 121 | ble.gap().startAdvertising(); |
jelord | 10:66549fa08986 | 122 | |
jelord | 10:66549fa08986 | 123 | //Loop keeps device in infinite loop waiting for events to occur |
jelord | 13:b43ec7e0cc1d | 124 | while (1) { |
jelord | 16:e066ab7e8fb3 | 125 | //ble.waitForEvent(); |
rgrover1 | 0:28f095301cb2 | 126 | } |
rgrover1 | 0:28f095301cb2 | 127 | } |