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:
Wed Aug 06 03:47:34 2014 +0000
Revision:
3:df03d6154360
Parent:
2:b0e9b12b67e4
Child:
4:05466501b5cb
4 tact switch input and advertise on BLE stable.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tkubotake 0:62a78063f1a9 1 #include "mbed.h"
tkubotake 0:62a78063f1a9 2 #include "nRF51822n.h"
tkubotake 0:62a78063f1a9 3
tkubotake 1:2c34ba518eb3 4 #define DEVICE_NAME "Smart Button"
tkubotake 1:2c34ba518eb3 5
tkubotake 2:b0e9b12b67e4 6 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
tkubotake 2:b0e9b12b67e4 7 * it will have an impact on code-size and power consumption. */
tkubotake 2:b0e9b12b67e4 8
tkubotake 2:b0e9b12b67e4 9 #if NEED_CONSOLE_OUTPUT
tkubotake 2:b0e9b12b67e4 10 Serial pc(USBTX, USBRX);
tkubotake 2:b0e9b12b67e4 11 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
tkubotake 2:b0e9b12b67e4 12 #else
tkubotake 2:b0e9b12b67e4 13 #define DEBUG(...) /* nothing */
tkubotake 2:b0e9b12b67e4 14 #endif /* #if NEED_CONSOLE_OUTPUT */
tkubotake 2:b0e9b12b67e4 15
tkubotake 0:62a78063f1a9 16 nRF51822n nrf; /* BLE radio driver */
tkubotake 2:b0e9b12b67e4 17 DigitalIn sw1(p2);
tkubotake 2:b0e9b12b67e4 18 DigitalIn sw2(p3);
tkubotake 2:b0e9b12b67e4 19 DigitalIn sw3(p4);
tkubotake 2:b0e9b12b67e4 20 DigitalIn sw4(p5);
tkubotake 0:62a78063f1a9 21
tkubotake 1:2c34ba518eb3 22 enum {
tkubotake 1:2c34ba518eb3 23 UUID_SWITCH_STATUS_SERVICE = 0xFFE0,
tkubotake 1:2c34ba518eb3 24 UUID_SWITCH_STATUS_CHAR = 0xFFE1
tkubotake 1:2c34ba518eb3 25 };
tkubotake 1:2c34ba518eb3 26
tkubotake 0:62a78063f1a9 27 /* LEDs for indication: */
tkubotake 0:62a78063f1a9 28 DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */
tkubotake 0:62a78063f1a9 29 DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */
tkubotake 0:62a78063f1a9 30
tkubotake 1:2c34ba518eb3 31 /* Switch Status Service */
tkubotake 1:2c34ba518eb3 32 uint8_t btns_in = 0;
tkubotake 1:2c34ba518eb3 33 GattService switchsService(UUID_SWITCH_STATUS_SERVICE);
tkubotake 1:2c34ba518eb3 34 GattCharacteristic switchsStatus( UUID_SWITCH_STATUS_CHAR, 1, 1,
tkubotake 1:2c34ba518eb3 35 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
tkubotake 1:2c34ba518eb3 36 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
tkubotake 0:62a78063f1a9 37
tkubotake 0:62a78063f1a9 38 /* Health Thermometer Service */
tkubotake 2:b0e9b12b67e4 39 /*
tkubotake 0:62a78063f1a9 40 uint8_t thermTempPayload[5] = { 0, 0, 0, 0, 0 };
tkubotake 0:62a78063f1a9 41 GattService thermService (GattService::UUID_HEALTH_THERMOMETER_SERVICE);
tkubotake 0:62a78063f1a9 42 GattCharacteristic thermTemp (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR,
tkubotake 0:62a78063f1a9 43 5, 5, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
tkubotake 2:b0e9b12b67e4 44 */
tkubotake 0:62a78063f1a9 45
tkubotake 0:62a78063f1a9 46 /* Battery Level Service */
tkubotake 0:62a78063f1a9 47 uint8_t batt = 100; /* Battery level */
tkubotake 0:62a78063f1a9 48 uint8_t read_batt = 0; /* Variable to hold battery level reads */
tkubotake 0:62a78063f1a9 49 GattService battService ( GattService::UUID_BATTERY_SERVICE );
tkubotake 0:62a78063f1a9 50 GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, 1, 1,
tkubotake 0:62a78063f1a9 51 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
tkubotake 0:62a78063f1a9 52 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
tkubotake 0:62a78063f1a9 53
tkubotake 0:62a78063f1a9 54
tkubotake 0:62a78063f1a9 55 /* Advertising data and parameters */
tkubotake 0:62a78063f1a9 56 GapAdvertisingData advData;
tkubotake 0:62a78063f1a9 57 GapAdvertisingData scanResponse;
tkubotake 0:62a78063f1a9 58 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
tkubotake 0:62a78063f1a9 59
tkubotake 1:2c34ba518eb3 60 uint16_t uuid16_list[] = {UUID_SWITCH_STATUS_SERVICE,
tkubotake 0:62a78063f1a9 61 GattService::UUID_BATTERY_SERVICE};
tkubotake 0:62a78063f1a9 62
tkubotake 2:b0e9b12b67e4 63 void updateServiceValues(uint8_t n);
tkubotake 0:62a78063f1a9 64
tkubotake 0:62a78063f1a9 65 /**************************************************************************/
tkubotake 0:62a78063f1a9 66 /*!
tkubotake 0:62a78063f1a9 67 @brief This custom class can be used to override any GapEvents
tkubotake 0:62a78063f1a9 68 that you are interested in handling on an application level.
tkubotake 0:62a78063f1a9 69 */
tkubotake 0:62a78063f1a9 70 /**************************************************************************/
tkubotake 0:62a78063f1a9 71 class GapEventHandler : public GapEvents
tkubotake 0:62a78063f1a9 72 {
tkubotake 0:62a78063f1a9 73 //virtual void onTimeout(void) {}
tkubotake 0:62a78063f1a9 74
tkubotake 0:62a78063f1a9 75 virtual void onConnected(void)
tkubotake 0:62a78063f1a9 76 {
tkubotake 0:62a78063f1a9 77 advertisingStateLed = 0;
tkubotake 0:62a78063f1a9 78 }
tkubotake 0:62a78063f1a9 79
tkubotake 0:62a78063f1a9 80 /* When a client device disconnects we need to start advertising again. */
tkubotake 0:62a78063f1a9 81 virtual void onDisconnected(void)
tkubotake 0:62a78063f1a9 82 {
tkubotake 0:62a78063f1a9 83 nrf.getGap().startAdvertising(advParams);
tkubotake 0:62a78063f1a9 84 advertisingStateLed = 1;
tkubotake 0:62a78063f1a9 85 }
tkubotake 0:62a78063f1a9 86 };
tkubotake 0:62a78063f1a9 87
tkubotake 0:62a78063f1a9 88 /**************************************************************************/
tkubotake 0:62a78063f1a9 89 /*!
tkubotake 0:62a78063f1a9 90 @brief Program entry point
tkubotake 0:62a78063f1a9 91 */
tkubotake 0:62a78063f1a9 92 /**************************************************************************/
tkubotake 0:62a78063f1a9 93 int main(void)
tkubotake 0:62a78063f1a9 94 {
tkubotake 2:b0e9b12b67e4 95 DEBUG("main process >>>\n\r");
tkubotake 0:62a78063f1a9 96
tkubotake 0:62a78063f1a9 97 /* Setup blinky led */
tkubotake 2:b0e9b12b67e4 98 oneSecondLed=0;
tkubotake 0:62a78063f1a9 99
tkubotake 0:62a78063f1a9 100 /* Setup an event handler for GAP events i.e. Client/Server connection events. */
tkubotake 0:62a78063f1a9 101 nrf.getGap().setEventHandler(new GapEventHandler());
tkubotake 0:62a78063f1a9 102
tkubotake 0:62a78063f1a9 103 /* Initialise the nRF51822 */
tkubotake 0:62a78063f1a9 104 nrf.init();
tkubotake 0:62a78063f1a9 105
tkubotake 0:62a78063f1a9 106 /* Make sure we get a clean start */
tkubotake 0:62a78063f1a9 107 nrf.reset();
tkubotake 0:62a78063f1a9 108
tkubotake 0:62a78063f1a9 109 /* Add BLE-Only flag and complete service list to the advertising data */
tkubotake 0:62a78063f1a9 110 advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
tkubotake 0:62a78063f1a9 111 advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
tkubotake 0:62a78063f1a9 112 (uint8_t*)uuid16_list, sizeof(uuid16_list));
tkubotake 1:2c34ba518eb3 113
tkubotake 1:2c34ba518eb3 114 advData.addData(GapAdvertisingData::COMPLETE_LOCAL_NAME,
tkubotake 1:2c34ba518eb3 115 (uint8_t*)DEVICE_NAME,
tkubotake 1:2c34ba518eb3 116 strlen(DEVICE_NAME)
tkubotake 1:2c34ba518eb3 117 );
tkubotake 1:2c34ba518eb3 118
tkubotake 0:62a78063f1a9 119 advData.addAppearance(GapAdvertisingData::GENERIC_THERMOMETER);
tkubotake 0:62a78063f1a9 120 nrf.getGap().setAdvertisingData(advData, scanResponse);
tkubotake 0:62a78063f1a9 121
tkubotake 1:2c34ba518eb3 122 /* Switchs Status Service */
tkubotake 1:2c34ba518eb3 123 switchsService.addCharacteristic(switchsStatus);
tkubotake 1:2c34ba518eb3 124 nrf.getGattServer().addService(switchsService);
tkubotake 1:2c34ba518eb3 125
tkubotake 0:62a78063f1a9 126 /* Health Thermometer Service */
tkubotake 2:b0e9b12b67e4 127 // thermService.addCharacteristic(thermTemp);
tkubotake 2:b0e9b12b67e4 128 // nrf.getGattServer().addService(thermService);
tkubotake 0:62a78063f1a9 129
tkubotake 0:62a78063f1a9 130 /* Add the Battery Level service */
tkubotake 0:62a78063f1a9 131 battService.addCharacteristic(battLevel);
tkubotake 0:62a78063f1a9 132 nrf.getGattServer().addService(battService);
tkubotake 0:62a78063f1a9 133
tkubotake 0:62a78063f1a9 134 /* Start advertising (make sure you've added all your data first) */
tkubotake 0:62a78063f1a9 135 nrf.getGap().startAdvertising(advParams);
tkubotake 0:62a78063f1a9 136 // advertisingStateLed = 1;
tkubotake 0:62a78063f1a9 137
tkubotake 2:b0e9b12b67e4 138 uint8_t n_current = 0;
tkubotake 2:b0e9b12b67e4 139 int n_sw1,n_sw2,n_sw3,n_sw4;
tkubotake 2:b0e9b12b67e4 140
tkubotake 2:b0e9b12b67e4 141
tkubotake 0:62a78063f1a9 142 while(1)
tkubotake 0:62a78063f1a9 143 {
tkubotake 2:b0e9b12b67e4 144 n_sw1 = sw1.read();
tkubotake 2:b0e9b12b67e4 145 n_sw2 = sw2.read();
tkubotake 2:b0e9b12b67e4 146 n_sw3 = sw3.read();
tkubotake 2:b0e9b12b67e4 147 n_sw4 = sw4.read();
tkubotake 2:b0e9b12b67e4 148
tkubotake 2:b0e9b12b67e4 149 oneSecondLed = (n_sw1 < 1) ? 1 : 0;
tkubotake 2:b0e9b12b67e4 150
tkubotake 2:b0e9b12b67e4 151 uint8_t n = 0;
tkubotake 2:b0e9b12b67e4 152 if(n_sw1 < 1) n += 0x01;
tkubotake 2:b0e9b12b67e4 153 if(n_sw2 < 1) n += 0x02;
tkubotake 2:b0e9b12b67e4 154 if(n_sw3 < 1) n += 0x04;
tkubotake 2:b0e9b12b67e4 155 if(n_sw4 < 1) n += 0x08;
tkubotake 2:b0e9b12b67e4 156
tkubotake 0:62a78063f1a9 157 /* Now that we're live, update the battery level & temperature characteristics */
tkubotake 2:b0e9b12b67e4 158 //updateServiceValues(oneSecondLed.read());
tkubotake 2:b0e9b12b67e4 159 //DEBUG("adv sw1 %d >>>\n\r",n_sw1);
tkubotake 2:b0e9b12b67e4 160 if(n_current != n){
tkubotake 2:b0e9b12b67e4 161 DEBUG("adv %d %d >>>\n\r",n,n_current);
tkubotake 2:b0e9b12b67e4 162 updateServiceValues(n);
tkubotake 2:b0e9b12b67e4 163 n_current = n;
tkubotake 2:b0e9b12b67e4 164 }
tkubotake 0:62a78063f1a9 165 }
tkubotake 0:62a78063f1a9 166 }
tkubotake 0:62a78063f1a9 167
tkubotake 0:62a78063f1a9 168 /**************************************************************************/
tkubotake 0:62a78063f1a9 169 /*!
tkubotake 0:62a78063f1a9 170 @brief Ticker callback to switch advertisingStateLed state
tkubotake 0:62a78063f1a9 171 */
tkubotake 0:62a78063f1a9 172 /**************************************************************************/
tkubotake 2:b0e9b12b67e4 173 void updateServiceValues(uint8_t n)
tkubotake 0:62a78063f1a9 174 {
tkubotake 0:62a78063f1a9 175
tkubotake 1:2c34ba518eb3 176 /* Update switches status */
tkubotake 1:2c34ba518eb3 177 btns_in = n;
tkubotake 1:2c34ba518eb3 178 nrf.getGattServer().updateValue(switchsStatus.handle, (uint8_t*)&btns_in, sizeof(btns_in) );
tkubotake 1:2c34ba518eb3 179
tkubotake 0:62a78063f1a9 180 /* Update battery level */
tkubotake 1:2c34ba518eb3 181 // nrf.getGattServer().updateValue(battLevel.handle, (uint8_t*)&batt, sizeof(batt));
tkubotake 0:62a78063f1a9 182 /* Decrement the battery level. */
tkubotake 1:2c34ba518eb3 183 // batt <=50 ? batt=100 : batt--;;
tkubotake 0:62a78063f1a9 184
tkubotake 0:62a78063f1a9 185 /* Update the temperature. Note that we need to convert to an ieee11073 format float. */
tkubotake 0:62a78063f1a9 186 //float temperature = healthThemometer.read();
tkubotake 1:2c34ba518eb3 187 /*
tkubotake 0:62a78063f1a9 188 float temperature = 2.1*n;
tkubotake 0:62a78063f1a9 189 uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
tkubotake 0:62a78063f1a9 190 memcpy(thermTempPayload+1, &temp_ieee11073, 4);
tkubotake 0:62a78063f1a9 191 nrf.getGattServer().updateValue(thermTemp.handle, thermTempPayload, sizeof(thermTempPayload));
tkubotake 1:2c34ba518eb3 192 */
tkubotake 0:62a78063f1a9 193 }