Программа считывает показания датчиков и управляет сервомашинками.

Dependencies:   mbed-src

Fork of NUCLEO_BLUENRG by Ostap Ostapsky

Committer:
Sergeev
Date:
Mon Aug 25 09:45:34 2014 +0000
Revision:
1:fb307cfca15c
Parent:
0:aa1e012ec210
Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ostapsky 0:aa1e012ec210 1 /******************************************************************************/
ostapsky 0:aa1e012ec210 2 /******************************************************************************/
ostapsky 0:aa1e012ec210 3 #include "mbed.h"
ostapsky 0:aa1e012ec210 4 #include "sample_service.h"
ostapsky 0:aa1e012ec210 5
ostapsky 0:aa1e012ec210 6 //Set client address here
ostapsky 0:aa1e012ec210 7 //#define CLIENT_ADDRESS {0x36, 0x1F, 0xAC, 0x29, 0x6A, 0xBC}
ostapsky 0:aa1e012ec210 8 #define CLIENT_ADDRESS {0x05, 0x25, 0xAC, 0x29, 0x6A, 0xBC};
ostapsky 0:aa1e012ec210 9
ostapsky 0:aa1e012ec210 10 volatile int connected = FALSE;
ostapsky 0:aa1e012ec210 11 volatile uint8_t set_connectable = 1;
ostapsky 0:aa1e012ec210 12 volatile uint16_t connection_handle = 0;
ostapsky 0:aa1e012ec210 13 volatile uint8_t notification_enabled = FALSE;
ostapsky 0:aa1e012ec210 14 volatile uint8_t read_finished = FALSE;
ostapsky 0:aa1e012ec210 15
ostapsky 0:aa1e012ec210 16 uint8_t *readData;
ostapsky 0:aa1e012ec210 17 uint8_t dataLength;
ostapsky 0:aa1e012ec210 18
ostapsky 0:aa1e012ec210 19 extern Serial pc;
ostapsky 0:aa1e012ec210 20 /******************************************************************************/
ostapsky 0:aa1e012ec210 21 /******************************************************************************/
ostapsky 0:aa1e012ec210 22
ostapsky 0:aa1e012ec210 23 #define COPY_UUID_128(uuid_struct, uuid_15, uuid_14, uuid_13, uuid_12, uuid_11, uuid_10, uuid_9, uuid_8, uuid_7, uuid_6, uuid_5, uuid_4, uuid_3, uuid_2, uuid_1, uuid_0) \
ostapsky 0:aa1e012ec210 24 do {\
ostapsky 0:aa1e012ec210 25 uuid_struct.uuid128[0] = uuid_0; uuid_struct.uuid128[1] = uuid_1; uuid_struct.uuid128[2] = uuid_2; uuid_struct.uuid128[3] = uuid_3; \
ostapsky 0:aa1e012ec210 26 uuid_struct.uuid128[4] = uuid_4; uuid_struct.uuid128[5] = uuid_5; uuid_struct.uuid128[6] = uuid_6; uuid_struct.uuid128[7] = uuid_7; \
ostapsky 0:aa1e012ec210 27 uuid_struct.uuid128[8] = uuid_8; uuid_struct.uuid128[9] = uuid_9; uuid_struct.uuid128[10] = uuid_10; uuid_struct.uuid128[11] = uuid_11; \
ostapsky 0:aa1e012ec210 28 uuid_struct.uuid128[12] = uuid_12; uuid_struct.uuid128[13] = uuid_13; uuid_struct.uuid128[14] = uuid_14; uuid_struct.uuid128[15] = uuid_15; \
ostapsky 0:aa1e012ec210 29 }while(0)
ostapsky 0:aa1e012ec210 30
ostapsky 0:aa1e012ec210 31
ostapsky 0:aa1e012ec210 32
ostapsky 0:aa1e012ec210 33 /******************************************************************************/
ostapsky 0:aa1e012ec210 34 /******************************************************************************/
ostapsky 0:aa1e012ec210 35 void Make_Connection(void)
ostapsky 0:aa1e012ec210 36 {
ostapsky 0:aa1e012ec210 37 tBleStatus ret;
ostapsky 0:aa1e012ec210 38
ostapsky 0:aa1e012ec210 39 //
ostapsky 0:aa1e012ec210 40 tBDAddr bdaddr = CLIENT_ADDRESS;
ostapsky 0:aa1e012ec210 41
ostapsky 0:aa1e012ec210 42 ret = aci_gap_create_connection(0x4000, 0x4000, PUBLIC_ADDR, bdaddr, PUBLIC_ADDR, 9, 9, 0, 60, 1000 , 1000);
ostapsky 0:aa1e012ec210 43
ostapsky 0:aa1e012ec210 44 if (ret != 0) {
ostapsky 0:aa1e012ec210 45 pc.printf("Error while starting connection.\n");
ostapsky 0:aa1e012ec210 46 Clock_Wait(100);
ostapsky 0:aa1e012ec210 47 }
ostapsky 0:aa1e012ec210 48
ostapsky 0:aa1e012ec210 49 }
ostapsky 0:aa1e012ec210 50 /******************************************************************************/
ostapsky 0:aa1e012ec210 51 /******************************************************************************/
ostapsky 0:aa1e012ec210 52 void sendData(uint16_t handle, uint8_t* data_buffer, uint8_t Nb_bytes)
ostapsky 0:aa1e012ec210 53 {
ostapsky 0:aa1e012ec210 54 aci_gatt_write_without_response(connection_handle, handle, Nb_bytes, data_buffer);
ostapsky 0:aa1e012ec210 55 }
ostapsky 0:aa1e012ec210 56
ostapsky 0:aa1e012ec210 57 /******************************************************************************/
ostapsky 0:aa1e012ec210 58 /******************************************************************************/
ostapsky 0:aa1e012ec210 59
ostapsky 0:aa1e012ec210 60 uint8_t *readValue(unsigned short handle, uint8_t* len)
ostapsky 0:aa1e012ec210 61 {
ostapsky 0:aa1e012ec210 62 volatile unsigned char res;
ostapsky 0:aa1e012ec210 63 static uint32_t timeout = 0;
ostapsky 0:aa1e012ec210 64
ostapsky 0:aa1e012ec210 65 res = aci_gatt_read_charac_val(connection_handle, handle);
ostapsky 0:aa1e012ec210 66
ostapsky 0:aa1e012ec210 67 timeout = 0;
ostapsky 0:aa1e012ec210 68 while ((!read_finished)&&(timeout < READ_TIMEOUT))
ostapsky 0:aa1e012ec210 69 {
ostapsky 0:aa1e012ec210 70 HCI_Process();
ostapsky 0:aa1e012ec210 71 timeout++;
ostapsky 0:aa1e012ec210 72 }
ostapsky 0:aa1e012ec210 73 if (timeout == READ_TIMEOUT)
ostapsky 0:aa1e012ec210 74 {
ostapsky 0:aa1e012ec210 75 dataLength = 0;
ostapsky 0:aa1e012ec210 76 }
ostapsky 0:aa1e012ec210 77
ostapsky 0:aa1e012ec210 78 *len = dataLength;
ostapsky 0:aa1e012ec210 79
ostapsky 0:aa1e012ec210 80 read_finished = 0;
ostapsky 0:aa1e012ec210 81 return readData;
ostapsky 0:aa1e012ec210 82
ostapsky 0:aa1e012ec210 83 }
ostapsky 0:aa1e012ec210 84
ostapsky 0:aa1e012ec210 85 /******************************************************************************/
ostapsky 0:aa1e012ec210 86 /******************************************************************************/
ostapsky 0:aa1e012ec210 87 void enableNotification()
ostapsky 0:aa1e012ec210 88 {
ostapsky 0:aa1e012ec210 89 uint8_t client_char_conf_data[] = {0x01, 0x00}; // Enable notifications
ostapsky 0:aa1e012ec210 90 struct timer t;
ostapsky 0:aa1e012ec210 91 Timer_Set(&t, CLOCK_SECOND*10);
ostapsky 0:aa1e012ec210 92
ostapsky 0:aa1e012ec210 93 while(aci_gatt_write_charac_descriptor(connection_handle, 0x0060, 2, client_char_conf_data)==BLE_STATUS_NOT_ALLOWED){
ostapsky 0:aa1e012ec210 94 /* Radio is busy */
ostapsky 0:aa1e012ec210 95 if(Timer_Expired(&t)) break;
ostapsky 0:aa1e012ec210 96 }
ostapsky 0:aa1e012ec210 97 notification_enabled = TRUE;
ostapsky 0:aa1e012ec210 98 }
ostapsky 0:aa1e012ec210 99
ostapsky 0:aa1e012ec210 100 /******************************************************************************/
ostapsky 0:aa1e012ec210 101 /******************************************************************************/
ostapsky 0:aa1e012ec210 102 void GAP_ConnectionComplete_CB(uint8_t addr[6], uint16_t handle)
ostapsky 0:aa1e012ec210 103 {
ostapsky 0:aa1e012ec210 104 connected = TRUE;
ostapsky 0:aa1e012ec210 105 connection_handle = handle;
ostapsky 0:aa1e012ec210 106
ostapsky 0:aa1e012ec210 107 }
ostapsky 0:aa1e012ec210 108
ostapsky 0:aa1e012ec210 109 /******************************************************************************/
ostapsky 0:aa1e012ec210 110 /******************************************************************************/
ostapsky 0:aa1e012ec210 111 void GAP_DisconnectionComplete_CB(void)
ostapsky 0:aa1e012ec210 112 {
ostapsky 0:aa1e012ec210 113 connected = FALSE;
ostapsky 0:aa1e012ec210 114 set_connectable = TRUE;
ostapsky 0:aa1e012ec210 115 notification_enabled = FALSE;
ostapsky 0:aa1e012ec210 116 }
ostapsky 0:aa1e012ec210 117
ostapsky 0:aa1e012ec210 118 /******************************************************************************/
ostapsky 0:aa1e012ec210 119 /******************************************************************************/
ostapsky 0:aa1e012ec210 120
ostapsky 0:aa1e012ec210 121 void GATT_Notification_CB(uint16_t attr_handle, uint8_t attr_len, uint8_t *attr_value)
ostapsky 0:aa1e012ec210 122 {
ostapsky 0:aa1e012ec210 123 //reserved
ostapsky 0:aa1e012ec210 124 }
ostapsky 0:aa1e012ec210 125
ostapsky 0:aa1e012ec210 126 /******************************************************************************/
ostapsky 0:aa1e012ec210 127 /******************************************************************************/
ostapsky 0:aa1e012ec210 128
ostapsky 0:aa1e012ec210 129 void GATT_Read_CB(uint16_t attr_handle, uint8_t attr_len, uint8_t *attr_value)
ostapsky 0:aa1e012ec210 130 {
ostapsky 0:aa1e012ec210 131 readData = attr_value;
ostapsky 0:aa1e012ec210 132 dataLength = attr_len;
ostapsky 0:aa1e012ec210 133 read_finished = 1;
ostapsky 0:aa1e012ec210 134 }
ostapsky 0:aa1e012ec210 135
ostapsky 0:aa1e012ec210 136 /******************************************************************************/
ostapsky 0:aa1e012ec210 137 /******************************************************************************/
ostapsky 0:aa1e012ec210 138
ostapsky 0:aa1e012ec210 139 void HCI_Event_CB(void *pckt)
ostapsky 0:aa1e012ec210 140 {
ostapsky 0:aa1e012ec210 141 hci_uart_pckt *hci_pckt = (hci_uart_pckt *)pckt;
ostapsky 0:aa1e012ec210 142 hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data;
ostapsky 0:aa1e012ec210 143
ostapsky 0:aa1e012ec210 144 if(hci_pckt->type != HCI_EVENT_PKT)
ostapsky 0:aa1e012ec210 145 return;
ostapsky 0:aa1e012ec210 146
ostapsky 0:aa1e012ec210 147 switch(event_pckt->evt){
ostapsky 0:aa1e012ec210 148
ostapsky 0:aa1e012ec210 149 case EVT_DISCONN_COMPLETE:
ostapsky 0:aa1e012ec210 150 {
ostapsky 0:aa1e012ec210 151 GAP_DisconnectionComplete_CB();
ostapsky 0:aa1e012ec210 152 }
ostapsky 0:aa1e012ec210 153 break;
ostapsky 0:aa1e012ec210 154
ostapsky 0:aa1e012ec210 155 case EVT_LE_META_EVENT:
ostapsky 0:aa1e012ec210 156 {
ostapsky 0:aa1e012ec210 157 evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data;
ostapsky 0:aa1e012ec210 158
ostapsky 0:aa1e012ec210 159 switch(evt->subevent){
ostapsky 0:aa1e012ec210 160 case EVT_LE_CONN_COMPLETE:
ostapsky 0:aa1e012ec210 161 {
ostapsky 0:aa1e012ec210 162 evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data;
ostapsky 0:aa1e012ec210 163 GAP_ConnectionComplete_CB(cc->peer_bdaddr, cc->handle);
ostapsky 0:aa1e012ec210 164 }
ostapsky 0:aa1e012ec210 165 break;
ostapsky 0:aa1e012ec210 166 }
ostapsky 0:aa1e012ec210 167 }
ostapsky 0:aa1e012ec210 168 break;
ostapsky 0:aa1e012ec210 169
ostapsky 0:aa1e012ec210 170 case EVT_VENDOR:
ostapsky 0:aa1e012ec210 171 {
ostapsky 0:aa1e012ec210 172 evt_blue_aci *blue_evt = (evt_blue_aci *)event_pckt->data;
ostapsky 0:aa1e012ec210 173 switch(blue_evt->ecode){
ostapsky 0:aa1e012ec210 174
ostapsky 0:aa1e012ec210 175 case EVT_BLUE_GATT_NOTIFICATION: {
ostapsky 0:aa1e012ec210 176 evt_gatt_attr_notification *evt = (evt_gatt_attr_notification*)blue_evt->data;
ostapsky 0:aa1e012ec210 177 GATT_Notification_CB(evt->attr_handle, evt->data_length - 2, evt->attr_value);
ostapsky 0:aa1e012ec210 178 }
ostapsky 0:aa1e012ec210 179 break;
ostapsky 0:aa1e012ec210 180 case EVT_BLUE_ATT_READ_RESP: {
ostapsky 0:aa1e012ec210 181 evt_gatt_procedure_complete *evt = (evt_gatt_procedure_complete*)blue_evt->data;
ostapsky 0:aa1e012ec210 182 GATT_Read_CB(evt->conn_handle, evt->data_length, evt->data);
ostapsky 0:aa1e012ec210 183
ostapsky 0:aa1e012ec210 184 }
ostapsky 0:aa1e012ec210 185 break;
ostapsky 0:aa1e012ec210 186 }
ostapsky 0:aa1e012ec210 187 }
ostapsky 0:aa1e012ec210 188 break;
ostapsky 0:aa1e012ec210 189 }
ostapsky 0:aa1e012ec210 190 }
ostapsky 0:aa1e012ec210 191 /******************************************************************************/
ostapsky 0:aa1e012ec210 192 /******************************************************************************/
ostapsky 0:aa1e012ec210 193
ostapsky 0:aa1e012ec210 194 /*****************************END OF FILE**************************************/