takeaki kubota / Mbed 2 deprecated sw_input_sample2

Dependencies:   BLE_API_Native_IRC mbed

Fork of sw_input_sample by takeaki kubota

Committer:
tkubotake
Date:
Mon Aug 04 11:11:06 2014 +0000
Revision:
0:62a78063f1a9
Child:
1:2c34ba518eb3
This sample was modified from BLE_Health_Thermometer_I2C.; https://mbed.org/users/ytsuboi/code/BLE_Health_Thermometer_IRC/; ; An switch input send iOS or Android device.; nRF Toolbox app > HTM enable us to check this switch input and sending.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tkubotake 0:62a78063f1a9 1 #include "mbed.h"
tkubotake 0:62a78063f1a9 2 #include "SwDigital.h"
tkubotake 0:62a78063f1a9 3 #include "nRF51822n.h"
tkubotake 0:62a78063f1a9 4
tkubotake 0:62a78063f1a9 5 nRF51822n nrf; /* BLE radio driver */
tkubotake 0:62a78063f1a9 6 SwDigital sw(p2,p3,p4,p5); // p2 : sw1 control LED1,LED2
tkubotake 0:62a78063f1a9 7 // p3 : sw2 control LED3,LED4
tkubotake 0:62a78063f1a9 8 enum{
tkubotake 0:62a78063f1a9 9 sw1,
tkubotake 0:62a78063f1a9 10 sw2,
tkubotake 0:62a78063f1a9 11 sw3,
tkubotake 0:62a78063f1a9 12 sw4
tkubotake 0:62a78063f1a9 13 };
tkubotake 0:62a78063f1a9 14
tkubotake 0:62a78063f1a9 15 /* LEDs for indication: */
tkubotake 0:62a78063f1a9 16 DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */
tkubotake 0:62a78063f1a9 17 DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */
tkubotake 0:62a78063f1a9 18
tkubotake 0:62a78063f1a9 19
tkubotake 0:62a78063f1a9 20 /* Health Thermometer Service */
tkubotake 0:62a78063f1a9 21 uint8_t thermTempPayload[5] = { 0, 0, 0, 0, 0 };
tkubotake 0:62a78063f1a9 22 GattService thermService (GattService::UUID_HEALTH_THERMOMETER_SERVICE);
tkubotake 0:62a78063f1a9 23 GattCharacteristic thermTemp (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR,
tkubotake 0:62a78063f1a9 24 5, 5, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
tkubotake 0:62a78063f1a9 25
tkubotake 0:62a78063f1a9 26 /* Battery Level Service */
tkubotake 0:62a78063f1a9 27 uint8_t batt = 100; /* Battery level */
tkubotake 0:62a78063f1a9 28 uint8_t read_batt = 0; /* Variable to hold battery level reads */
tkubotake 0:62a78063f1a9 29 GattService battService ( GattService::UUID_BATTERY_SERVICE );
tkubotake 0:62a78063f1a9 30 GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, 1, 1,
tkubotake 0:62a78063f1a9 31 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
tkubotake 0:62a78063f1a9 32 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
tkubotake 0:62a78063f1a9 33
tkubotake 0:62a78063f1a9 34
tkubotake 0:62a78063f1a9 35 /* Advertising data and parameters */
tkubotake 0:62a78063f1a9 36 GapAdvertisingData advData;
tkubotake 0:62a78063f1a9 37 GapAdvertisingData scanResponse;
tkubotake 0:62a78063f1a9 38 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
tkubotake 0:62a78063f1a9 39
tkubotake 0:62a78063f1a9 40 uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE,
tkubotake 0:62a78063f1a9 41 GattService::UUID_BATTERY_SERVICE};
tkubotake 0:62a78063f1a9 42
tkubotake 0:62a78063f1a9 43 uint32_t quick_ieee11073_from_float(float temperature);
tkubotake 0:62a78063f1a9 44 void updateServiceValues(int n);
tkubotake 0:62a78063f1a9 45
tkubotake 0:62a78063f1a9 46 /**************************************************************************/
tkubotake 0:62a78063f1a9 47 /*!
tkubotake 0:62a78063f1a9 48 @brief This custom class can be used to override any GapEvents
tkubotake 0:62a78063f1a9 49 that you are interested in handling on an application level.
tkubotake 0:62a78063f1a9 50 */
tkubotake 0:62a78063f1a9 51 /**************************************************************************/
tkubotake 0:62a78063f1a9 52 class GapEventHandler : public GapEvents
tkubotake 0:62a78063f1a9 53 {
tkubotake 0:62a78063f1a9 54 //virtual void onTimeout(void) {}
tkubotake 0:62a78063f1a9 55
tkubotake 0:62a78063f1a9 56 virtual void onConnected(void)
tkubotake 0:62a78063f1a9 57 {
tkubotake 0:62a78063f1a9 58 advertisingStateLed = 0;
tkubotake 0:62a78063f1a9 59 }
tkubotake 0:62a78063f1a9 60
tkubotake 0:62a78063f1a9 61 /* When a client device disconnects we need to start advertising again. */
tkubotake 0:62a78063f1a9 62 virtual void onDisconnected(void)
tkubotake 0:62a78063f1a9 63 {
tkubotake 0:62a78063f1a9 64 nrf.getGap().startAdvertising(advParams);
tkubotake 0:62a78063f1a9 65 advertisingStateLed = 1;
tkubotake 0:62a78063f1a9 66 }
tkubotake 0:62a78063f1a9 67 };
tkubotake 0:62a78063f1a9 68
tkubotake 0:62a78063f1a9 69 /**************************************************************************/
tkubotake 0:62a78063f1a9 70 /*!
tkubotake 0:62a78063f1a9 71 @brief Program entry point
tkubotake 0:62a78063f1a9 72 */
tkubotake 0:62a78063f1a9 73 /**************************************************************************/
tkubotake 0:62a78063f1a9 74 int main(void)
tkubotake 0:62a78063f1a9 75 {
tkubotake 0:62a78063f1a9 76
tkubotake 0:62a78063f1a9 77 /* Setup blinky led */
tkubotake 0:62a78063f1a9 78 oneSecondLed=1;
tkubotake 0:62a78063f1a9 79
tkubotake 0:62a78063f1a9 80 /* Setup an event handler for GAP events i.e. Client/Server connection events. */
tkubotake 0:62a78063f1a9 81 nrf.getGap().setEventHandler(new GapEventHandler());
tkubotake 0:62a78063f1a9 82
tkubotake 0:62a78063f1a9 83 /* Initialise the nRF51822 */
tkubotake 0:62a78063f1a9 84 nrf.init();
tkubotake 0:62a78063f1a9 85
tkubotake 0:62a78063f1a9 86 /* Make sure we get a clean start */
tkubotake 0:62a78063f1a9 87 nrf.reset();
tkubotake 0:62a78063f1a9 88
tkubotake 0:62a78063f1a9 89 /* Add BLE-Only flag and complete service list to the advertising data */
tkubotake 0:62a78063f1a9 90 advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
tkubotake 0:62a78063f1a9 91 advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
tkubotake 0:62a78063f1a9 92 (uint8_t*)uuid16_list, sizeof(uuid16_list));
tkubotake 0:62a78063f1a9 93 advData.addAppearance(GapAdvertisingData::GENERIC_THERMOMETER);
tkubotake 0:62a78063f1a9 94 nrf.getGap().setAdvertisingData(advData, scanResponse);
tkubotake 0:62a78063f1a9 95
tkubotake 0:62a78063f1a9 96 /* Health Thermometer Service */
tkubotake 0:62a78063f1a9 97 thermService.addCharacteristic(thermTemp);
tkubotake 0:62a78063f1a9 98 nrf.getGattServer().addService(thermService);
tkubotake 0:62a78063f1a9 99
tkubotake 0:62a78063f1a9 100 /* Add the Battery Level service */
tkubotake 0:62a78063f1a9 101 battService.addCharacteristic(battLevel);
tkubotake 0:62a78063f1a9 102 nrf.getGattServer().addService(battService);
tkubotake 0:62a78063f1a9 103
tkubotake 0:62a78063f1a9 104 /* Start advertising (make sure you've added all your data first) */
tkubotake 0:62a78063f1a9 105 nrf.getGap().startAdvertising(advParams);
tkubotake 0:62a78063f1a9 106 // advertisingStateLed = 1;
tkubotake 0:62a78063f1a9 107
tkubotake 0:62a78063f1a9 108 while(1)
tkubotake 0:62a78063f1a9 109 {
tkubotake 0:62a78063f1a9 110 sw.refreshEdgeData();
tkubotake 0:62a78063f1a9 111
tkubotake 0:62a78063f1a9 112 // tact action (sw level = on : led1 = on)
tkubotake 0:62a78063f1a9 113 oneSecondLed = sw.checkLevel(sw1);
tkubotake 0:62a78063f1a9 114
tkubotake 0:62a78063f1a9 115 /* Now that we're live, update the battery level & temperature characteristics */
tkubotake 0:62a78063f1a9 116 updateServiceValues(oneSecondLed.read());
tkubotake 0:62a78063f1a9 117 //wait(0.1);
tkubotake 0:62a78063f1a9 118 }
tkubotake 0:62a78063f1a9 119 }
tkubotake 0:62a78063f1a9 120
tkubotake 0:62a78063f1a9 121 /**************************************************************************/
tkubotake 0:62a78063f1a9 122 /*!
tkubotake 0:62a78063f1a9 123 @brief Ticker callback to switch advertisingStateLed state
tkubotake 0:62a78063f1a9 124 */
tkubotake 0:62a78063f1a9 125 /**************************************************************************/
tkubotake 0:62a78063f1a9 126 void updateServiceValues(int n)
tkubotake 0:62a78063f1a9 127 {
tkubotake 0:62a78063f1a9 128 /* Toggle the one second LEDs */
tkubotake 0:62a78063f1a9 129 //oneSecondLed = !oneSecondLed;
tkubotake 0:62a78063f1a9 130
tkubotake 0:62a78063f1a9 131 /* Update battery level */
tkubotake 0:62a78063f1a9 132 nrf.getGattServer().updateValue(battLevel.handle, (uint8_t*)&batt, sizeof(batt));
tkubotake 0:62a78063f1a9 133 /* Decrement the battery level. */
tkubotake 0:62a78063f1a9 134 batt <=50 ? batt=100 : batt--;;
tkubotake 0:62a78063f1a9 135
tkubotake 0:62a78063f1a9 136 /* Update the temperature. Note that we need to convert to an ieee11073 format float. */
tkubotake 0:62a78063f1a9 137 //float temperature = healthThemometer.read();
tkubotake 0:62a78063f1a9 138 float temperature = 2.1*n;
tkubotake 0:62a78063f1a9 139 uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
tkubotake 0:62a78063f1a9 140 memcpy(thermTempPayload+1, &temp_ieee11073, 4);
tkubotake 0:62a78063f1a9 141 nrf.getGattServer().updateValue(thermTemp.handle, thermTempPayload, sizeof(thermTempPayload));
tkubotake 0:62a78063f1a9 142 }
tkubotake 0:62a78063f1a9 143
tkubotake 0:62a78063f1a9 144 /**
tkubotake 0:62a78063f1a9 145 * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
tkubotake 0:62a78063f1a9 146 * @param temperature The temperature as a float.
tkubotake 0:62a78063f1a9 147 * @return The temperature in 11073-20601 FLOAT-Type format.
tkubotake 0:62a78063f1a9 148 */
tkubotake 0:62a78063f1a9 149 uint32_t quick_ieee11073_from_float(float temperature)
tkubotake 0:62a78063f1a9 150 {
tkubotake 0:62a78063f1a9 151 uint8_t exponent = 0xFF; //exponent is -1
tkubotake 0:62a78063f1a9 152 uint32_t mantissa = (uint32_t)(temperature*10);
tkubotake 0:62a78063f1a9 153
tkubotake 0:62a78063f1a9 154 return ( ((uint32_t)exponent) << 24) | mantissa;
tkubotake 0:62a78063f1a9 155 }