JBBoardに接続したモーター2つをRCBControllerでコントロールするテストです。
Dependencies: FatFileSystem TB6612FNG2 mbed
Fork of BTstackLE by
BLE_demo.cpp@4:3e8cd77ee303, 2013-02-10 (annotated)
- Committer:
- todotani
- Date:
- Sun Feb 10 08:44:13 2013 +0000
- Revision:
- 4:3e8cd77ee303
- Parent:
- 3:4bc438c6fc52
- Child:
- 5:5fb56e13a1f9
2013/2/10 refine
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
todotani | 2:24b33d92e086 | 1 | /* |
todotani | 2:24b33d92e086 | 2 | * Copyright (C) 2011-2012 by Matthias Ringwald |
todotani | 2:24b33d92e086 | 3 | * |
todotani | 2:24b33d92e086 | 4 | * Redistribution and use in source and binary forms, with or without |
todotani | 2:24b33d92e086 | 5 | * modification, are permitted provided that the following conditions |
todotani | 2:24b33d92e086 | 6 | * are met: |
todotani | 2:24b33d92e086 | 7 | * |
todotani | 2:24b33d92e086 | 8 | * 1. Redistributions of source code must retain the above copyright |
todotani | 2:24b33d92e086 | 9 | * notice, this list of conditions and the following disclaimer. |
todotani | 2:24b33d92e086 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
todotani | 2:24b33d92e086 | 11 | * notice, this list of conditions and the following disclaimer in the |
todotani | 2:24b33d92e086 | 12 | * documentation and/or other materials provided with the distribution. |
todotani | 2:24b33d92e086 | 13 | * 3. Neither the name of the copyright holders nor the names of |
todotani | 2:24b33d92e086 | 14 | * contributors may be used to endorse or promote products derived |
todotani | 2:24b33d92e086 | 15 | * from this software without specific prior written permission. |
todotani | 2:24b33d92e086 | 16 | * 4. This software may not be used in a commercial product |
todotani | 2:24b33d92e086 | 17 | * without an explicit license granted by the copyright holder. |
todotani | 2:24b33d92e086 | 18 | * |
todotani | 2:24b33d92e086 | 19 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS |
todotani | 2:24b33d92e086 | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
todotani | 2:24b33d92e086 | 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
todotani | 2:24b33d92e086 | 22 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS |
todotani | 2:24b33d92e086 | 23 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
todotani | 2:24b33d92e086 | 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
todotani | 2:24b33d92e086 | 25 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
todotani | 2:24b33d92e086 | 26 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
todotani | 2:24b33d92e086 | 27 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
todotani | 2:24b33d92e086 | 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
todotani | 2:24b33d92e086 | 29 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
todotani | 2:24b33d92e086 | 30 | * SUCH DAMAGE. |
todotani | 2:24b33d92e086 | 31 | * |
todotani | 2:24b33d92e086 | 32 | */ |
todotani | 2:24b33d92e086 | 33 | |
todotani | 4:3e8cd77ee303 | 34 | /* changed |
todotani | 4:3e8cd77ee303 | 35 | 2013/2/10: by todotani |
todotani | 4:3e8cd77ee303 | 36 | Modified tick event handler |
todotani | 4:3e8cd77ee303 | 37 | Modified LED control |
todotani | 4:3e8cd77ee303 | 38 | - LED 3 flush when connected |
todotani | 4:3e8cd77ee303 | 39 | - LED 2 OFF in case write attribute, ON in case read attribute |
todotani | 4:3e8cd77ee303 | 40 | - LED 1 OFF of after start of advertisement, then reflect write value to 0xFFF2 |
todotani | 4:3e8cd77ee303 | 41 | |
todotani | 4:3e8cd77ee303 | 42 | */ |
todotani | 4:3e8cd77ee303 | 43 | |
todotani | 2:24b33d92e086 | 44 | //***************************************************************************** |
todotani | 2:24b33d92e086 | 45 | // |
todotani | 2:24b33d92e086 | 46 | // att device demo |
todotani | 2:24b33d92e086 | 47 | // |
todotani | 2:24b33d92e086 | 48 | //***************************************************************************** |
todotani | 2:24b33d92e086 | 49 | |
todotani | 2:24b33d92e086 | 50 | // TODO: seperate BR/EDR from LE ACL buffers |
todotani | 2:24b33d92e086 | 51 | // TODO: move LE init into HCI |
todotani | 2:24b33d92e086 | 52 | // .. |
todotani | 2:24b33d92e086 | 53 | |
todotani | 2:24b33d92e086 | 54 | // NOTE: Supports only a single connection |
todotani | 2:24b33d92e086 | 55 | |
todotani | 1:6078e430af82 | 56 | // mbed specific |
todotani | 1:6078e430af82 | 57 | #include "mbed.h" |
todotani | 1:6078e430af82 | 58 | Serial pc(USBTX, USBRX); |
todotani | 1:6078e430af82 | 59 | DigitalOut led1(LED1), led2(LED2), led3(LED3); |
todotani | 1:6078e430af82 | 60 | |
todotani | 1:6078e430af82 | 61 | #define HEARTBEAT_PERIOD_MS 500 |
todotani | 1:6078e430af82 | 62 | |
todotani | 1:6078e430af82 | 63 | // from btstack ble_server.c |
todotani | 1:6078e430af82 | 64 | #include "global.h" |
todotani | 1:6078e430af82 | 65 | #include "debug.h" |
todotani | 1:6078e430af82 | 66 | #include "btstack/btstack.h" |
todotani | 1:6078e430af82 | 67 | #include "btstack/hci_cmds.h" |
todotani | 1:6078e430af82 | 68 | #include "btstack/run_loop.h" |
todotani | 3:4bc438c6fc52 | 69 | #include "btstack/hal_tick.h" |
todotani | 1:6078e430af82 | 70 | |
todotani | 1:6078e430af82 | 71 | #include "hci.h" |
todotani | 1:6078e430af82 | 72 | #include "l2cap.h" |
todotani | 1:6078e430af82 | 73 | #include "btstack_memory.h" |
todotani | 1:6078e430af82 | 74 | #include "remote_device_db.h" |
todotani | 1:6078e430af82 | 75 | #include "config.h" |
todotani | 1:6078e430af82 | 76 | |
todotani | 1:6078e430af82 | 77 | #include "att.h" |
todotani | 1:6078e430af82 | 78 | |
todotani | 1:6078e430af82 | 79 | hci_transport_t * hci_transport_picusb_instance(); |
todotani | 1:6078e430af82 | 80 | |
todotani | 1:6078e430af82 | 81 | static att_connection_t att_connection; |
todotani | 1:6078e430af82 | 82 | static uint16_t att_response_handle = 0; |
todotani | 1:6078e430af82 | 83 | static uint16_t att_response_size = 0; |
todotani | 1:6078e430af82 | 84 | static uint8_t att_response_buffer[28]; |
todotani | 1:6078e430af82 | 85 | |
todotani | 1:6078e430af82 | 86 | void hexdump2(void *data, int size){ |
todotani | 1:6078e430af82 | 87 | int i; |
todotani | 1:6078e430af82 | 88 | for (i=0; i<size;i++){ |
todotani | 1:6078e430af82 | 89 | log_info("%02X ", ((uint8_t *)data)[i]); |
todotani | 1:6078e430af82 | 90 | } |
todotani | 1:6078e430af82 | 91 | log_info("\n"); |
todotani | 1:6078e430af82 | 92 | } |
todotani | 1:6078e430af82 | 93 | |
todotani | 1:6078e430af82 | 94 | static void att_try_respond(void){ |
todotani | 1:6078e430af82 | 95 | if (!att_response_size) return; |
todotani | 1:6078e430af82 | 96 | if (!att_response_handle) return; |
todotani | 1:6078e430af82 | 97 | if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) return; |
todotani | 1:6078e430af82 | 98 | |
todotani | 1:6078e430af82 | 99 | // update state before sending packet |
todotani | 1:6078e430af82 | 100 | uint16_t size = att_response_size; |
todotani | 1:6078e430af82 | 101 | att_response_size = 0; |
todotani | 1:6078e430af82 | 102 | l2cap_send_connectionless(att_response_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_buffer, size); |
todotani | 1:6078e430af82 | 103 | } |
todotani | 1:6078e430af82 | 104 | |
todotani | 1:6078e430af82 | 105 | |
todotani | 1:6078e430af82 | 106 | static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ |
todotani | 1:6078e430af82 | 107 | if (packet_type != ATT_DATA_PACKET) return; |
todotani | 1:6078e430af82 | 108 | |
todotani | 1:6078e430af82 | 109 | att_response_handle = handle; |
todotani | 1:6078e430af82 | 110 | att_response_size = att_handle_request(&att_connection, packet, size, att_response_buffer); |
todotani | 1:6078e430af82 | 111 | att_try_respond(); |
todotani | 1:6078e430af82 | 112 | } |
todotani | 1:6078e430af82 | 113 | |
todotani | 1:6078e430af82 | 114 | |
todotani | 1:6078e430af82 | 115 | // enable LE, setup ADV data |
todotani | 1:6078e430af82 | 116 | static void packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ |
todotani | 1:6078e430af82 | 117 | static bd_addr_t addr; |
todotani | 3:4bc438c6fc52 | 118 | //uint8_t adv_data[] = { 02, 01, 05, 03, 02, 0xf0, 0xff }; |
todotani | 3:4bc438c6fc52 | 119 | // Data for advertisement and scan response |
todotani | 3:4bc438c6fc52 | 120 | const uint8_t adv_data[31]="\x02\x01\x05" "\x06\x09SBDBT" "\x03\x02\xf0\xff"; |
todotani | 1:6078e430af82 | 121 | switch (packet_type) { |
todotani | 1:6078e430af82 | 122 | |
todotani | 1:6078e430af82 | 123 | case HCI_EVENT_PACKET: |
todotani | 1:6078e430af82 | 124 | switch (packet[0]) { |
todotani | 1:6078e430af82 | 125 | |
todotani | 1:6078e430af82 | 126 | case BTSTACK_EVENT_STATE: |
todotani | 1:6078e430af82 | 127 | // bt stack activated, get started - set local name |
todotani | 1:6078e430af82 | 128 | if (packet[2] == HCI_STATE_WORKING) { |
todotani | 1:6078e430af82 | 129 | log_info("Working!\n"); |
todotani | 1:6078e430af82 | 130 | hci_send_cmd(&hci_read_local_supported_features); |
todotani | 1:6078e430af82 | 131 | } |
todotani | 1:6078e430af82 | 132 | break; |
todotani | 1:6078e430af82 | 133 | |
todotani | 1:6078e430af82 | 134 | case DAEMON_EVENT_HCI_PACKET_SENT: |
todotani | 1:6078e430af82 | 135 | att_try_respond(); |
todotani | 1:6078e430af82 | 136 | break; |
todotani | 1:6078e430af82 | 137 | |
todotani | 1:6078e430af82 | 138 | case HCI_EVENT_LE_META: |
todotani | 1:6078e430af82 | 139 | switch (packet[2]) { |
todotani | 1:6078e430af82 | 140 | case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: |
todotani | 1:6078e430af82 | 141 | // reset connection MTU |
todotani | 1:6078e430af82 | 142 | att_connection.mtu = 23; |
todotani | 1:6078e430af82 | 143 | break; |
todotani | 1:6078e430af82 | 144 | default: |
todotani | 1:6078e430af82 | 145 | break; |
todotani | 1:6078e430af82 | 146 | } |
todotani | 1:6078e430af82 | 147 | break; |
todotani | 1:6078e430af82 | 148 | |
todotani | 1:6078e430af82 | 149 | case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED: |
todotani | 1:6078e430af82 | 150 | if (packet[2]) { |
todotani | 1:6078e430af82 | 151 | connection_status=1; |
todotani | 1:6078e430af82 | 152 | log_info("CONNECTED\n"); |
todotani | 1:6078e430af82 | 153 | } else { |
todotani | 1:6078e430af82 | 154 | connection_status=0; |
todotani | 1:6078e430af82 | 155 | log_info("NOT CONNECTED\n"); |
todotani | 1:6078e430af82 | 156 | } |
todotani | 1:6078e430af82 | 157 | break; |
todotani | 1:6078e430af82 | 158 | |
todotani | 1:6078e430af82 | 159 | case HCI_EVENT_DISCONNECTION_COMPLETE: |
todotani | 1:6078e430af82 | 160 | att_response_handle =0; |
todotani | 1:6078e430af82 | 161 | att_response_size = 0; |
todotani | 1:6078e430af82 | 162 | |
todotani | 1:6078e430af82 | 163 | // restart advertising |
todotani | 1:6078e430af82 | 164 | hci_send_cmd(&hci_le_set_advertise_enable, 1); |
todotani | 1:6078e430af82 | 165 | break; |
todotani | 1:6078e430af82 | 166 | |
todotani | 1:6078e430af82 | 167 | case HCI_EVENT_COMMAND_COMPLETE: |
todotani | 1:6078e430af82 | 168 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)){ |
todotani | 1:6078e430af82 | 169 | bt_flip_addr(addr, &packet[6]); |
todotani | 1:6078e430af82 | 170 | log_info("BD ADDR: %s\n", bd_addr_to_str(addr)); |
todotani | 1:6078e430af82 | 171 | break; |
todotani | 1:6078e430af82 | 172 | } |
todotani | 1:6078e430af82 | 173 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ |
todotani | 1:6078e430af82 | 174 | log_info("Local supported features: %04lX%04lX\n", READ_BT_32(packet, 10), READ_BT_32(packet, 6)); |
todotani | 1:6078e430af82 | 175 | hci_send_cmd(&hci_set_event_mask, 0xffffffff, 0x20001fff); |
todotani | 1:6078e430af82 | 176 | break; |
todotani | 1:6078e430af82 | 177 | } |
todotani | 1:6078e430af82 | 178 | if (COMMAND_COMPLETE_EVENT(packet, hci_set_event_mask)){ |
todotani | 1:6078e430af82 | 179 | hci_send_cmd(&hci_write_le_host_supported, 1, 1); |
todotani | 1:6078e430af82 | 180 | break; |
todotani | 1:6078e430af82 | 181 | } |
todotani | 1:6078e430af82 | 182 | if (COMMAND_COMPLETE_EVENT(packet, hci_write_le_host_supported)){ |
todotani | 1:6078e430af82 | 183 | hci_send_cmd(&hci_le_set_event_mask, 0xffffffff, 0xffffffff); |
todotani | 1:6078e430af82 | 184 | break; |
todotani | 1:6078e430af82 | 185 | } |
todotani | 1:6078e430af82 | 186 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_event_mask)){ |
todotani | 1:6078e430af82 | 187 | hci_send_cmd(&hci_le_read_buffer_size); |
todotani | 1:6078e430af82 | 188 | break; |
todotani | 1:6078e430af82 | 189 | } |
todotani | 1:6078e430af82 | 190 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ |
todotani | 1:6078e430af82 | 191 | log_info("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); |
todotani | 1:6078e430af82 | 192 | hci_send_cmd(&hci_le_read_supported_states); |
todotani | 1:6078e430af82 | 193 | break; |
todotani | 1:6078e430af82 | 194 | } |
todotani | 1:6078e430af82 | 195 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_supported_states)){ |
todotani | 1:6078e430af82 | 196 | hci_send_cmd(&hci_le_set_advertising_parameters, 0x0400, 0x0800, 0, 0, 0, &addr, 0x07, 0); |
todotani | 1:6078e430af82 | 197 | break; |
todotani | 1:6078e430af82 | 198 | } |
todotani | 1:6078e430af82 | 199 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_parameters)){ |
todotani | 1:6078e430af82 | 200 | hci_send_cmd(&hci_le_set_advertising_data, sizeof(adv_data), adv_data); |
todotani | 1:6078e430af82 | 201 | break; |
todotani | 1:6078e430af82 | 202 | } |
todotani | 1:6078e430af82 | 203 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_data)){ |
todotani | 1:6078e430af82 | 204 | hci_send_cmd(&hci_le_set_scan_response_data, 10, adv_data); |
todotani | 1:6078e430af82 | 205 | break; |
todotani | 1:6078e430af82 | 206 | } |
todotani | 1:6078e430af82 | 207 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_scan_response_data)){ |
todotani | 1:6078e430af82 | 208 | hci_send_cmd(&hci_le_set_advertise_enable, 1); |
todotani | 1:6078e430af82 | 209 | break; |
todotani | 1:6078e430af82 | 210 | } |
todotani | 1:6078e430af82 | 211 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertise_enable)){ |
todotani | 1:6078e430af82 | 212 | hci_discoverable_control(1); |
todotani | 1:6078e430af82 | 213 | log_info("startup_state=1\n"); |
todotani | 1:6078e430af82 | 214 | startup_state=1; |
todotani | 1:6078e430af82 | 215 | led1 = 0; |
todotani | 1:6078e430af82 | 216 | break; |
todotani | 1:6078e430af82 | 217 | } |
todotani | 1:6078e430af82 | 218 | |
todotani | 1:6078e430af82 | 219 | } |
todotani | 1:6078e430af82 | 220 | } |
todotani | 1:6078e430af82 | 221 | } |
todotani | 1:6078e430af82 | 222 | |
todotani | 1:6078e430af82 | 223 | // test profile |
todotani | 1:6078e430af82 | 224 | #include "profile.h" |
todotani | 1:6078e430af82 | 225 | |
todotani | 1:6078e430af82 | 226 | static uint8_t strbuf[80]; |
todotani | 1:6078e430af82 | 227 | static uint8_t ledvalue; |
todotani | 1:6078e430af82 | 228 | |
todotani | 1:6078e430af82 | 229 | // read requests |
todotani | 1:6078e430af82 | 230 | static uint16_t att_read_callback(uint16_t handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ |
todotani | 1:6078e430af82 | 231 | uint16_t ret=0,val; |
todotani | 1:6078e430af82 | 232 | |
todotani | 1:6078e430af82 | 233 | if(buffer){ |
todotani | 1:6078e430af82 | 234 | log_info("READ Callback, handle %04x\n", handle); |
todotani | 3:4bc438c6fc52 | 235 | led2 = 1; |
todotani | 1:6078e430af82 | 236 | } |
todotani | 1:6078e430af82 | 237 | switch(handle){ |
todotani | 3:4bc438c6fc52 | 238 | // Correspond to Characteristics 0xFFF1 |
todotani | 1:6078e430af82 | 239 | case 0x000b: |
todotani | 1:6078e430af82 | 240 | ret=strlen((char*)strbuf); |
todotani | 1:6078e430af82 | 241 | if(buffer && ret<=buffer_size){ |
todotani | 1:6078e430af82 | 242 | log_info("Read text: %s\n", strbuf); |
todotani | 1:6078e430af82 | 243 | memcpy(buffer,strbuf,ret); |
todotani | 1:6078e430af82 | 244 | } |
todotani | 1:6078e430af82 | 245 | break; |
todotani | 3:4bc438c6fc52 | 246 | |
todotani | 3:4bc438c6fc52 | 247 | // Correspond to Characteristics 0xFFF2 |
todotani | 1:6078e430af82 | 248 | case 0x000d: |
todotani | 1:6078e430af82 | 249 | if(buffer && buffer_size){ |
todotani | 1:6078e430af82 | 250 | log_info("Read value: %u\n", buffer[0]); |
todotani | 1:6078e430af82 | 251 | buffer[0]=ledvalue; |
todotani | 1:6078e430af82 | 252 | } |
todotani | 1:6078e430af82 | 253 | ret=1; |
todotani | 1:6078e430af82 | 254 | break; |
todotani | 3:4bc438c6fc52 | 255 | |
todotani | 3:4bc438c6fc52 | 256 | // Correspond to Characteristics 0x00001234-0000-1000-8000-00805F9B34FB |
todotani | 1:6078e430af82 | 257 | case 0x000f: |
todotani | 1:6078e430af82 | 258 | if(buffer && buffer_size>=2){ |
todotani | 1:6078e430af82 | 259 | val=timer_counter; |
todotani | 1:6078e430af82 | 260 | log_info("Read value: %u\n", val); |
todotani | 1:6078e430af82 | 261 | buffer[0]=val&0xff; |
todotani | 1:6078e430af82 | 262 | buffer[1]=val>>8; |
todotani | 1:6078e430af82 | 263 | } |
todotani | 1:6078e430af82 | 264 | ret=2; |
todotani | 1:6078e430af82 | 265 | break; |
todotani | 1:6078e430af82 | 266 | } |
todotani | 1:6078e430af82 | 267 | return ret; |
todotani | 1:6078e430af82 | 268 | } |
todotani | 1:6078e430af82 | 269 | |
todotani | 1:6078e430af82 | 270 | // write requests |
todotani | 1:6078e430af82 | 271 | static void att_write_callback(uint16_t handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size, signature_t * signature){ |
todotani | 1:6078e430af82 | 272 | log_info("WRITE Callback, handle %04x\n", handle); |
todotani | 3:4bc438c6fc52 | 273 | led2 = 0; |
todotani | 1:6078e430af82 | 274 | switch(handle){ |
todotani | 3:4bc438c6fc52 | 275 | // Correspond to Characteristics 0xFFF1 |
todotani | 1:6078e430af82 | 276 | case 0x000b: |
todotani | 1:6078e430af82 | 277 | buffer[buffer_size]=0; |
todotani | 1:6078e430af82 | 278 | if(sizeof(strbuf)>buffer_size){ |
todotani | 1:6078e430af82 | 279 | strcpy((char*)strbuf,(char*)buffer); |
todotani | 1:6078e430af82 | 280 | } |
todotani | 1:6078e430af82 | 281 | log_info("New text: %s\n", buffer); |
todotani | 1:6078e430af82 | 282 | break; |
todotani | 3:4bc438c6fc52 | 283 | |
todotani | 3:4bc438c6fc52 | 284 | // Correspond to Characteristics 0xFFF2 |
todotani | 1:6078e430af82 | 285 | case 0x000d: |
todotani | 1:6078e430af82 | 286 | log_info("New value: %u\n", buffer[0]); |
todotani | 1:6078e430af82 | 287 | ledvalue=buffer[0]; |
todotani | 1:6078e430af82 | 288 | if (buffer[0]){ |
todotani | 1:6078e430af82 | 289 | led1 = 1; |
todotani | 1:6078e430af82 | 290 | } else { |
todotani | 1:6078e430af82 | 291 | led1 = 0; |
todotani | 1:6078e430af82 | 292 | } |
todotani | 1:6078e430af82 | 293 | break; |
todotani | 1:6078e430af82 | 294 | } |
todotani | 1:6078e430af82 | 295 | } |
todotani | 1:6078e430af82 | 296 | |
todotani | 1:6078e430af82 | 297 | |
todotani | 3:4bc438c6fc52 | 298 | void hal_tick_event(void) |
todotani | 3:4bc438c6fc52 | 299 | { |
todotani | 3:4bc438c6fc52 | 300 | timer_counter++; |
todotani | 1:6078e430af82 | 301 | |
todotani | 3:4bc438c6fc52 | 302 | if(connection_status){ |
todotani | 3:4bc438c6fc52 | 303 | led3 = !led3; |
todotani | 3:4bc438c6fc52 | 304 | }else{ |
todotani | 3:4bc438c6fc52 | 305 | led3 = 1; |
todotani | 3:4bc438c6fc52 | 306 | } |
todotani | 3:4bc438c6fc52 | 307 | } |
todotani | 1:6078e430af82 | 308 | |
todotani | 1:6078e430af82 | 309 | // main |
todotani | 1:6078e430af82 | 310 | int main(void) |
todotani | 1:6078e430af82 | 311 | { |
todotani | 1:6078e430af82 | 312 | pc.baud(115200); |
todotani | 1:6078e430af82 | 313 | log_info("%s\n", __FILE__); |
todotani | 1:6078e430af82 | 314 | |
todotani | 1:6078e430af82 | 315 | // init LEDs |
todotani | 1:6078e430af82 | 316 | led1 = led2 = led3 = 1; |
todotani | 1:6078e430af82 | 317 | |
todotani | 1:6078e430af82 | 318 | /// GET STARTED with BTstack /// |
todotani | 1:6078e430af82 | 319 | btstack_memory_init(); |
todotani | 1:6078e430af82 | 320 | run_loop_init(RUN_LOOP_EMBEDDED); |
todotani | 3:4bc438c6fc52 | 321 | hal_tick_set_handler(&hal_tick_event); |
todotani | 1:6078e430af82 | 322 | |
todotani | 1:6078e430af82 | 323 | // init HCI |
todotani | 1:6078e430af82 | 324 | // use BlueUSB |
todotani | 1:6078e430af82 | 325 | hci_transport_t* transport = hci_transport_usb_instance(); |
todotani | 1:6078e430af82 | 326 | bt_control_t * control = NULL; |
todotani | 1:6078e430af82 | 327 | hci_uart_config_t * config = NULL; |
todotani | 1:6078e430af82 | 328 | remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory; |
todotani | 1:6078e430af82 | 329 | hci_init(transport, config, control, remote_db); |
todotani | 1:6078e430af82 | 330 | |
todotani | 1:6078e430af82 | 331 | // use eHCILL |
todotani | 1:6078e430af82 | 332 | // bt_control_cc256x_enable_ehcill(1); |
todotani | 1:6078e430af82 | 333 | |
todotani | 1:6078e430af82 | 334 | // set up l2cap_le |
todotani | 1:6078e430af82 | 335 | l2cap_init(); |
todotani | 1:6078e430af82 | 336 | l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); |
todotani | 1:6078e430af82 | 337 | l2cap_register_packet_handler(packet_handler); |
todotani | 1:6078e430af82 | 338 | |
todotani | 1:6078e430af82 | 339 | // set up ATT |
todotani | 1:6078e430af82 | 340 | att_set_db(profile_data); |
todotani | 1:6078e430af82 | 341 | att_set_write_callback(att_write_callback); |
todotani | 1:6078e430af82 | 342 | att_set_read_callback(att_read_callback); |
todotani | 1:6078e430af82 | 343 | att_dump_attributes(); |
todotani | 1:6078e430af82 | 344 | att_connection.mtu = 27; |
todotani | 1:6078e430af82 | 345 | |
todotani | 1:6078e430af82 | 346 | log_info("Run...\n\r"); |
todotani | 1:6078e430af82 | 347 | |
todotani | 1:6078e430af82 | 348 | // turn on! |
todotani | 1:6078e430af82 | 349 | hci_power_control(HCI_POWER_ON); |
todotani | 1:6078e430af82 | 350 | |
todotani | 1:6078e430af82 | 351 | // go! |
todotani | 1:6078e430af82 | 352 | run_loop_execute(); |
todotani | 1:6078e430af82 | 353 | |
todotani | 1:6078e430af82 | 354 | // happy compiler! |
todotani | 1:6078e430af82 | 355 | return 0; |
todotani | 1:6078e430af82 | 356 | } |