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.
Dependencies: BLE_API BMP085 mbed nRF51822
Fork of nRF51822_SimpleControls by
main.cpp
00001 /* 00002 00003 Copyright (c) 2012-2014 RedBearLab 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 00009 subject to the following conditions: 00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 00011 00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 00018 */ 00019 00020 #include "mbed.h" 00021 #include "ble/BLE.h" 00022 #include "Servo.h" 00023 #include "GattCallbackParamTypes.h" 00024 #include "BMP085.h" 00025 00026 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ 00027 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ 00028 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ 00029 00030 #define TXRX_BUF_LEN 20 00031 00032 #define DIGITAL_OUT_PIN P0_17 //D7 00033 #define DIGITAL_IN_PIN P0_5 //A4 00034 #define PWM_PIN P0_16 //D6 00035 #define SERVO_PIN P0_14 //D10 00036 #define ANALOG_IN_PIN P0_1 //A0 00037 00038 BLE ble; 00039 00040 DigitalOut LED_SET(DIGITAL_OUT_PIN); 00041 DigitalIn BUTTON(DIGITAL_IN_PIN); 00042 PwmOut PWM(PWM_PIN); 00043 AnalogIn ANALOG(ANALOG_IN_PIN); 00044 Servo MYSERVO(SERVO_PIN); 00045 BMP085 myCaptor(P0_29, P0_28); 00046 Serial pc(USBTX, USBRX); 00047 00048 static uint8_t analog_enabled = 0; 00049 static uint8_t captor_enabled = 0; 00050 static uint8_t old_state = 0; 00051 00052 // The Nordic UART Service 00053 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00054 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00055 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00056 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00057 00058 00059 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00060 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00061 00062 //static uint8_t rx_buf[TXRX_BUF_LEN]; 00063 //static uint8_t rx_len=0; 00064 00065 00066 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00067 00068 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00069 00070 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00071 00072 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00073 00074 00075 00076 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00077 { 00078 pc.printf("Disconnected \r\n"); 00079 pc.printf("Restart advertising \r\n"); 00080 ble.startAdvertising(); 00081 } 00082 00083 void WrittenHandler(const GattWriteCallbackParams *Handler) 00084 { 00085 uint8_t buf[TXRX_BUF_LEN]; 00086 uint16_t bytesRead, index; 00087 00088 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 00089 { 00090 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00091 memset(txPayload, 0, TXRX_BUF_LEN); 00092 memcpy(txPayload, buf, TXRX_BUF_LEN); 00093 00094 for(index=0; index<bytesRead; index++) 00095 pc.putc(buf[index]); 00096 00097 if(buf[0] == 0x01) 00098 { 00099 00100 if(buf[1] == 0x01) 00101 while(1) 00102 { 00103 LED_SET = !LED_SET; 00104 wait(0.25); 00105 } 00106 else 00107 LED_SET = 0; 00108 } 00109 else if(buf[0] == 0x06) 00110 { 00111 if(buf[1] == 0x01) 00112 analog_enabled = 1; 00113 else 00114 analog_enabled = 0; 00115 } 00116 else if(buf[0] == 0x02) 00117 { 00118 float value = (float)buf[1]/255; 00119 PWM = value; 00120 } 00121 else if(buf[0] == 0x03) 00122 { 00123 MYSERVO.write(buf[1]); 00124 } 00125 else if(buf[0] == 0x04) 00126 { 00127 analog_enabled = 0; 00128 PWM = 0; 00129 MYSERVO.write(0); 00130 LED_SET = 0; 00131 old_state = 0; 00132 } 00133 else if(buf[0] == 0xA0) 00134 { 00135 if(buf[1] == 0x01) 00136 captor_enabled = 1; 00137 else 00138 captor_enabled = 0; 00139 } 00140 00141 } 00142 } 00143 /* 00144 void uartCB(void) 00145 { 00146 while(pc.readable()) 00147 { 00148 rx_buf[rx_len++] = pc.getc(); 00149 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') 00150 { 00151 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 00152 pc.printf("RecHandler \r\n"); 00153 pc.printf("Length: "); 00154 pc.putc(rx_len); 00155 pc.printf("\r\n"); 00156 rx_len = 0; 00157 break; 00158 } 00159 } 00160 } 00161 */ 00162 void m_status_check_handle(void) 00163 { 00164 uint8_t buf[3]; 00165 if (analog_enabled) // if analog reading enabled 00166 { 00167 // Read and send out 00168 float s = ANALOG; 00169 uint16_t value = s*1024; 00170 buf[0] = (0x0B); 00171 buf[1] = (value >> 8); 00172 buf[2] = (value); 00173 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00174 } 00175 if (captor_enabled) // if analog reading enabled 00176 { 00177 // Read and send out 00178 myCaptor.update(); 00179 float s = myCaptor.get_temperature(); 00180 uint16_t value = s; 00181 buf[0] = (0x0B); 00182 buf[1] = (value >> 8); 00183 buf[2] = (value); 00184 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00185 } 00186 // If digital in changes, report the state 00187 if (BUTTON != old_state) 00188 { 00189 old_state = BUTTON; 00190 00191 if (BUTTON == 1) 00192 { 00193 buf[0] = (0x0A); 00194 buf[1] = (0x01); 00195 buf[2] = (0x00); 00196 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00197 } 00198 else 00199 { 00200 buf[0] = (0x0A); 00201 buf[1] = (0x00); 00202 buf[2] = (0x00); 00203 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 00204 } 00205 } 00206 } 00207 00208 00209 int main(void) 00210 { 00211 Ticker ticker; 00212 ticker.attach_us(m_status_check_handle, 200000); 00213 00214 ble.init(); 00215 ble.onDisconnection(disconnectionCallback); 00216 ble.onDataWritten(WrittenHandler); 00217 00218 pc.baud(9600); 00219 pc.printf("SimpleChat Init \r\n"); 00220 00221 //pc.attach( uartCB , pc.RxIrq); 00222 00223 // setup advertising 00224 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00225 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00226 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00227 (const uint8_t *)"Mustafa", sizeof("Mustafa") - 1); 00228 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00229 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00230 // 100ms; in multiples of 0.625ms. 00231 ble.setAdvertisingInterval(160); 00232 00233 ble.addService(uartService); 00234 00235 ble.startAdvertising(); 00236 00237 pc.printf("Advertising Start \r\n"); 00238 00239 while(1) 00240 { 00241 ble.waitForEvent(); 00242 } 00243 } 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275
Generated on Wed Jul 20 2022 02:03:28 by
 1.7.2
 1.7.2 
    