JBBoardに接続したモーター2つをRCBControllerでコントロールするテストです。
Dependencies: FatFileSystem TB6612FNG2 mbed
Fork of BTstackLE by
BLE_demo.cpp@7:3ed1e36587d4, 2014-05-12 (annotated)
- Committer:
- jksoft
- Date:
- Mon May 12 14:24:35 2014 +0000
- Revision:
- 7:3ed1e36587d4
- Parent:
- 6:cf06ba884429
First edition
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 | 6:cf06ba884429 | 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 | 5:5fb56e13a1f9 | 34 | /* Change history |
todotani | 6:cf06ba884429 | 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 | 5:5fb56e13a1f9 | 41 | 2013/2/11: by todotani |
todotani | 5:5fb56e13a1f9 | 42 | Changed serial pc.baud (115200 to 921600) to avoid imcomplete connection issue |
todotani | 5:5fb56e13a1f9 | 43 | Disable btstack debug-log |
todotani | 6:cf06ba884429 | 44 | 2013/2/20: by todotani |
todotani | 6:cf06ba884429 | 45 | Change tick timer interval 250ms to 1ms |
todotani | 6:cf06ba884429 | 46 | Change Attribute 0xFFF1 as read DigitalIn p5 of mbed |
todotani | 4:3e8cd77ee303 | 47 | */ |
todotani | 4:3e8cd77ee303 | 48 | |
todotani | 2:24b33d92e086 | 49 | //***************************************************************************** |
todotani | 2:24b33d92e086 | 50 | // |
todotani | 2:24b33d92e086 | 51 | // att device demo |
todotani | 2:24b33d92e086 | 52 | // |
todotani | 2:24b33d92e086 | 53 | //***************************************************************************** |
todotani | 2:24b33d92e086 | 54 | |
todotani | 2:24b33d92e086 | 55 | // TODO: seperate BR/EDR from LE ACL buffers |
todotani | 2:24b33d92e086 | 56 | // TODO: move LE init into HCI |
todotani | 2:24b33d92e086 | 57 | // .. |
todotani | 2:24b33d92e086 | 58 | |
todotani | 2:24b33d92e086 | 59 | // NOTE: Supports only a single connection |
todotani | 2:24b33d92e086 | 60 | |
todotani | 1:6078e430af82 | 61 | // mbed specific |
todotani | 1:6078e430af82 | 62 | #include "mbed.h" |
todotani | 1:6078e430af82 | 63 | Serial pc(USBTX, USBRX); |
jksoft | 7:3ed1e36587d4 | 64 | DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);; |
todotani | 6:cf06ba884429 | 65 | DigitalIn sw1(p5); |
todotani | 1:6078e430af82 | 66 | |
todotani | 1:6078e430af82 | 67 | // from btstack ble_server.c |
todotani | 1:6078e430af82 | 68 | #include "global.h" |
todotani | 1:6078e430af82 | 69 | #include "debug.h" |
todotani | 1:6078e430af82 | 70 | #include "btstack/btstack.h" |
todotani | 1:6078e430af82 | 71 | #include "btstack/hci_cmds.h" |
todotani | 1:6078e430af82 | 72 | #include "btstack/run_loop.h" |
todotani | 3:4bc438c6fc52 | 73 | #include "btstack/hal_tick.h" |
todotani | 1:6078e430af82 | 74 | |
todotani | 1:6078e430af82 | 75 | #include "hci.h" |
todotani | 1:6078e430af82 | 76 | #include "l2cap.h" |
todotani | 1:6078e430af82 | 77 | #include "btstack_memory.h" |
todotani | 1:6078e430af82 | 78 | #include "remote_device_db.h" |
todotani | 1:6078e430af82 | 79 | #include "config.h" |
todotani | 1:6078e430af82 | 80 | |
todotani | 1:6078e430af82 | 81 | #include "att.h" |
jksoft | 7:3ed1e36587d4 | 82 | #include "TB6612.h" |
jksoft | 7:3ed1e36587d4 | 83 | |
jksoft | 7:3ed1e36587d4 | 84 | TB6612 MOTOR_A(p21,p19,p20); // PWM IN1 IN2 |
jksoft | 7:3ed1e36587d4 | 85 | TB6612 MOTOR_B(p22,p29,p30); // PWM IN1 IN2 |
todotani | 1:6078e430af82 | 86 | |
todotani | 1:6078e430af82 | 87 | hci_transport_t * hci_transport_picusb_instance(); |
todotani | 1:6078e430af82 | 88 | |
todotani | 1:6078e430af82 | 89 | static att_connection_t att_connection; |
todotani | 1:6078e430af82 | 90 | static uint16_t att_response_handle = 0; |
todotani | 1:6078e430af82 | 91 | static uint16_t att_response_size = 0; |
todotani | 1:6078e430af82 | 92 | static uint8_t att_response_buffer[28]; |
todotani | 1:6078e430af82 | 93 | |
todotani | 6:cf06ba884429 | 94 | static uint8_t switch_new_state = 1; |
todotani | 6:cf06ba884429 | 95 | static uint8_t switch_old_state = 1; |
todotani | 6:cf06ba884429 | 96 | |
todotani | 6:cf06ba884429 | 97 | void hexdump2(void *data, int size) |
todotani | 6:cf06ba884429 | 98 | { |
todotani | 1:6078e430af82 | 99 | int i; |
todotani | 6:cf06ba884429 | 100 | for (i=0; i<size; i++) { |
todotani | 1:6078e430af82 | 101 | log_info("%02X ", ((uint8_t *)data)[i]); |
todotani | 1:6078e430af82 | 102 | } |
todotani | 1:6078e430af82 | 103 | log_info("\n"); |
todotani | 1:6078e430af82 | 104 | } |
todotani | 1:6078e430af82 | 105 | |
todotani | 6:cf06ba884429 | 106 | static void att_try_respond(void) |
todotani | 6:cf06ba884429 | 107 | { |
todotani | 1:6078e430af82 | 108 | if (!att_response_size) return; |
todotani | 1:6078e430af82 | 109 | if (!att_response_handle) return; |
todotani | 1:6078e430af82 | 110 | if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) return; |
todotani | 6:cf06ba884429 | 111 | |
todotani | 1:6078e430af82 | 112 | // update state before sending packet |
todotani | 1:6078e430af82 | 113 | uint16_t size = att_response_size; |
todotani | 1:6078e430af82 | 114 | att_response_size = 0; |
todotani | 1:6078e430af82 | 115 | l2cap_send_connectionless(att_response_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_buffer, size); |
todotani | 1:6078e430af82 | 116 | } |
todotani | 1:6078e430af82 | 117 | |
todotani | 1:6078e430af82 | 118 | |
todotani | 6:cf06ba884429 | 119 | static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) |
todotani | 6:cf06ba884429 | 120 | { |
todotani | 1:6078e430af82 | 121 | if (packet_type != ATT_DATA_PACKET) return; |
todotani | 6:cf06ba884429 | 122 | |
todotani | 1:6078e430af82 | 123 | att_response_handle = handle; |
todotani | 1:6078e430af82 | 124 | att_response_size = att_handle_request(&att_connection, packet, size, att_response_buffer); |
todotani | 1:6078e430af82 | 125 | att_try_respond(); |
todotani | 1:6078e430af82 | 126 | } |
todotani | 1:6078e430af82 | 127 | |
todotani | 1:6078e430af82 | 128 | |
todotani | 1:6078e430af82 | 129 | // enable LE, setup ADV data |
todotani | 6:cf06ba884429 | 130 | static void packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) |
todotani | 6:cf06ba884429 | 131 | { |
todotani | 1:6078e430af82 | 132 | static bd_addr_t addr; |
todotani | 3:4bc438c6fc52 | 133 | // Data for advertisement and scan response |
todotani | 6:cf06ba884429 | 134 | const uint8_t adv_data[31]="\x02\x01\x05" "\x05\x09mbed" "\x03\x02\xf0\xff"; |
todotani | 1:6078e430af82 | 135 | switch (packet_type) { |
todotani | 6:cf06ba884429 | 136 | |
todotani | 1:6078e430af82 | 137 | case HCI_EVENT_PACKET: |
todotani | 1:6078e430af82 | 138 | switch (packet[0]) { |
todotani | 6:cf06ba884429 | 139 | |
todotani | 1:6078e430af82 | 140 | case BTSTACK_EVENT_STATE: |
todotani | 1:6078e430af82 | 141 | // bt stack activated, get started - set local name |
todotani | 1:6078e430af82 | 142 | if (packet[2] == HCI_STATE_WORKING) { |
todotani | 6:cf06ba884429 | 143 | log_info("Working!\n"); |
todotani | 1:6078e430af82 | 144 | hci_send_cmd(&hci_read_local_supported_features); |
todotani | 1:6078e430af82 | 145 | } |
todotani | 1:6078e430af82 | 146 | break; |
todotani | 6:cf06ba884429 | 147 | |
todotani | 1:6078e430af82 | 148 | case DAEMON_EVENT_HCI_PACKET_SENT: |
todotani | 1:6078e430af82 | 149 | att_try_respond(); |
todotani | 1:6078e430af82 | 150 | break; |
todotani | 6:cf06ba884429 | 151 | |
todotani | 1:6078e430af82 | 152 | case HCI_EVENT_LE_META: |
todotani | 1:6078e430af82 | 153 | switch (packet[2]) { |
todotani | 1:6078e430af82 | 154 | case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: |
todotani | 1:6078e430af82 | 155 | // reset connection MTU |
todotani | 1:6078e430af82 | 156 | att_connection.mtu = 23; |
todotani | 1:6078e430af82 | 157 | break; |
todotani | 1:6078e430af82 | 158 | default: |
todotani | 1:6078e430af82 | 159 | break; |
todotani | 1:6078e430af82 | 160 | } |
todotani | 1:6078e430af82 | 161 | break; |
todotani | 1:6078e430af82 | 162 | |
todotani | 1:6078e430af82 | 163 | case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED: |
todotani | 1:6078e430af82 | 164 | if (packet[2]) { |
todotani | 1:6078e430af82 | 165 | connection_status=1; |
todotani | 1:6078e430af82 | 166 | log_info("CONNECTED\n"); |
todotani | 1:6078e430af82 | 167 | } else { |
todotani | 1:6078e430af82 | 168 | connection_status=0; |
todotani | 1:6078e430af82 | 169 | log_info("NOT CONNECTED\n"); |
todotani | 1:6078e430af82 | 170 | } |
todotani | 1:6078e430af82 | 171 | break; |
todotani | 6:cf06ba884429 | 172 | |
todotani | 1:6078e430af82 | 173 | case HCI_EVENT_DISCONNECTION_COMPLETE: |
todotani | 1:6078e430af82 | 174 | att_response_handle =0; |
todotani | 1:6078e430af82 | 175 | att_response_size = 0; |
todotani | 6:cf06ba884429 | 176 | |
todotani | 1:6078e430af82 | 177 | // restart advertising |
todotani | 1:6078e430af82 | 178 | hci_send_cmd(&hci_le_set_advertise_enable, 1); |
todotani | 1:6078e430af82 | 179 | break; |
todotani | 6:cf06ba884429 | 180 | |
todotani | 1:6078e430af82 | 181 | case HCI_EVENT_COMMAND_COMPLETE: |
todotani | 6:cf06ba884429 | 182 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { |
todotani | 1:6078e430af82 | 183 | bt_flip_addr(addr, &packet[6]); |
todotani | 1:6078e430af82 | 184 | log_info("BD ADDR: %s\n", bd_addr_to_str(addr)); |
todotani | 1:6078e430af82 | 185 | break; |
todotani | 1:6078e430af82 | 186 | } |
todotani | 6:cf06ba884429 | 187 | if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)) { |
todotani | 1:6078e430af82 | 188 | log_info("Local supported features: %04lX%04lX\n", READ_BT_32(packet, 10), READ_BT_32(packet, 6)); |
todotani | 1:6078e430af82 | 189 | hci_send_cmd(&hci_set_event_mask, 0xffffffff, 0x20001fff); |
todotani | 1:6078e430af82 | 190 | break; |
todotani | 1:6078e430af82 | 191 | } |
todotani | 6:cf06ba884429 | 192 | if (COMMAND_COMPLETE_EVENT(packet, hci_set_event_mask)) { |
todotani | 1:6078e430af82 | 193 | hci_send_cmd(&hci_write_le_host_supported, 1, 1); |
todotani | 1:6078e430af82 | 194 | break; |
todotani | 1:6078e430af82 | 195 | } |
todotani | 6:cf06ba884429 | 196 | if (COMMAND_COMPLETE_EVENT(packet, hci_write_le_host_supported)) { |
todotani | 1:6078e430af82 | 197 | hci_send_cmd(&hci_le_set_event_mask, 0xffffffff, 0xffffffff); |
todotani | 1:6078e430af82 | 198 | break; |
todotani | 1:6078e430af82 | 199 | } |
todotani | 6:cf06ba884429 | 200 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_event_mask)) { |
todotani | 1:6078e430af82 | 201 | hci_send_cmd(&hci_le_read_buffer_size); |
todotani | 1:6078e430af82 | 202 | break; |
todotani | 1:6078e430af82 | 203 | } |
todotani | 6:cf06ba884429 | 204 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)) { |
todotani | 1:6078e430af82 | 205 | log_info("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); |
todotani | 6:cf06ba884429 | 206 | hci_send_cmd(&hci_le_read_supported_states); |
todotani | 6:cf06ba884429 | 207 | break; |
todotani | 1:6078e430af82 | 208 | } |
todotani | 6:cf06ba884429 | 209 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_supported_states)) { |
todotani | 6:cf06ba884429 | 210 | hci_send_cmd(&hci_le_set_advertising_parameters, 0x0400, 0x0800, 0, 0, 0, &addr, 0x07, 0); |
todotani | 6:cf06ba884429 | 211 | break; |
todotani | 1:6078e430af82 | 212 | } |
todotani | 6:cf06ba884429 | 213 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_parameters)) { |
todotani | 6:cf06ba884429 | 214 | hci_send_cmd(&hci_le_set_advertising_data, sizeof(adv_data), adv_data); |
todotani | 6:cf06ba884429 | 215 | break; |
todotani | 1:6078e430af82 | 216 | } |
todotani | 6:cf06ba884429 | 217 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertising_data)) { |
todotani | 6:cf06ba884429 | 218 | hci_send_cmd(&hci_le_set_scan_response_data, 10, adv_data); |
todotani | 6:cf06ba884429 | 219 | break; |
todotani | 1:6078e430af82 | 220 | } |
todotani | 6:cf06ba884429 | 221 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_scan_response_data)) { |
todotani | 6:cf06ba884429 | 222 | hci_send_cmd(&hci_le_set_advertise_enable, 1); |
todotani | 6:cf06ba884429 | 223 | break; |
todotani | 1:6078e430af82 | 224 | } |
todotani | 6:cf06ba884429 | 225 | if (COMMAND_COMPLETE_EVENT(packet, hci_le_set_advertise_enable)) { |
todotani | 1:6078e430af82 | 226 | hci_discoverable_control(1); |
todotani | 1:6078e430af82 | 227 | log_info("startup_state=1\n"); |
todotani | 1:6078e430af82 | 228 | startup_state=1; |
todotani | 1:6078e430af82 | 229 | led1 = 0; |
todotani | 1:6078e430af82 | 230 | break; |
todotani | 1:6078e430af82 | 231 | } |
todotani | 6:cf06ba884429 | 232 | |
todotani | 1:6078e430af82 | 233 | } |
todotani | 1:6078e430af82 | 234 | } |
todotani | 1:6078e430af82 | 235 | } |
todotani | 1:6078e430af82 | 236 | |
todotani | 1:6078e430af82 | 237 | // test profile |
todotani | 1:6078e430af82 | 238 | #include "profile.h" |
todotani | 1:6078e430af82 | 239 | |
todotani | 1:6078e430af82 | 240 | static uint8_t strbuf[80]; |
todotani | 1:6078e430af82 | 241 | static uint8_t ledvalue; |
todotani | 1:6078e430af82 | 242 | |
todotani | 1:6078e430af82 | 243 | // read requests |
todotani | 6:cf06ba884429 | 244 | static uint16_t att_read_callback(uint16_t handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size) |
todotani | 6:cf06ba884429 | 245 | { |
todotani | 1:6078e430af82 | 246 | uint16_t ret=0,val; |
todotani | 1:6078e430af82 | 247 | |
todotani | 6:cf06ba884429 | 248 | if(buffer) { |
todotani | 1:6078e430af82 | 249 | log_info("READ Callback, handle %04x\n", handle); |
todotani | 3:4bc438c6fc52 | 250 | led2 = 1; |
todotani | 1:6078e430af82 | 251 | } |
todotani | 6:cf06ba884429 | 252 | switch(handle) { |
todotani | 6:cf06ba884429 | 253 | // Correspond to Characteristics 0xFFF1 |
todotani | 6:cf06ba884429 | 254 | case 0x000b: |
jksoft | 7:3ed1e36587d4 | 255 | #if 0 |
todotani | 6:cf06ba884429 | 256 | if(buffer && ret<=buffer_size) { |
todotani | 6:cf06ba884429 | 257 | buffer[0] = sw1.read(); |
todotani | 6:cf06ba884429 | 258 | log_info("Read value: %u\n", buffer[0]); |
todotani | 1:6078e430af82 | 259 | } |
todotani | 6:cf06ba884429 | 260 | ret = 1; |
jksoft | 7:3ed1e36587d4 | 261 | #else |
jksoft | 7:3ed1e36587d4 | 262 | ret = 0; |
jksoft | 7:3ed1e36587d4 | 263 | #endif |
todotani | 1:6078e430af82 | 264 | break; |
todotani | 3:4bc438c6fc52 | 265 | |
todotani | 6:cf06ba884429 | 266 | // Correspond to Characteristics 0xFFF2 |
todotani | 1:6078e430af82 | 267 | case 0x000d: |
todotani | 6:cf06ba884429 | 268 | if(buffer && buffer_size) { |
todotani | 6:cf06ba884429 | 269 | ledvalue = led1; |
todotani | 6:cf06ba884429 | 270 | buffer[0] = ledvalue; |
todotani | 1:6078e430af82 | 271 | log_info("Read value: %u\n", buffer[0]); |
todotani | 1:6078e430af82 | 272 | } |
todotani | 1:6078e430af82 | 273 | ret=1; |
todotani | 1:6078e430af82 | 274 | break; |
todotani | 3:4bc438c6fc52 | 275 | |
todotani | 6:cf06ba884429 | 276 | // Correspond to Characteristics 0x00001234-0000-1000-8000-00805F9B34FB |
todotani | 1:6078e430af82 | 277 | case 0x000f: |
todotani | 6:cf06ba884429 | 278 | if(buffer && buffer_size>=2) { |
todotani | 1:6078e430af82 | 279 | val=timer_counter; |
todotani | 1:6078e430af82 | 280 | log_info("Read value: %u\n", val); |
todotani | 1:6078e430af82 | 281 | buffer[0]=val&0xff; |
todotani | 1:6078e430af82 | 282 | buffer[1]=val>>8; |
todotani | 1:6078e430af82 | 283 | } |
todotani | 1:6078e430af82 | 284 | ret=2; |
todotani | 1:6078e430af82 | 285 | break; |
todotani | 1:6078e430af82 | 286 | } |
todotani | 1:6078e430af82 | 287 | return ret; |
todotani | 1:6078e430af82 | 288 | } |
todotani | 1:6078e430af82 | 289 | |
todotani | 1:6078e430af82 | 290 | // write requests |
todotani | 6:cf06ba884429 | 291 | 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 | 6:cf06ba884429 | 292 | { |
todotani | 1:6078e430af82 | 293 | log_info("WRITE Callback, handle %04x\n", handle); |
todotani | 3:4bc438c6fc52 | 294 | led2 = 0; |
todotani | 6:cf06ba884429 | 295 | switch(handle) { |
todotani | 6:cf06ba884429 | 296 | // Correspond to Characteristics 0xFFF1 |
todotani | 1:6078e430af82 | 297 | case 0x000b: |
jksoft | 7:3ed1e36587d4 | 298 | uint16_t game_pad; |
jksoft | 7:3ed1e36587d4 | 299 | game_pad = (buffer[0] << 8 ) | buffer[1]; |
jksoft | 7:3ed1e36587d4 | 300 | //log_info("No action\n"); |
jksoft | 7:3ed1e36587d4 | 301 | switch(game_pad) |
jksoft | 7:3ed1e36587d4 | 302 | { |
jksoft | 7:3ed1e36587d4 | 303 | case 0x0001: |
jksoft | 7:3ed1e36587d4 | 304 | MOTOR_A = 30; |
jksoft | 7:3ed1e36587d4 | 305 | MOTOR_B = 30; |
jksoft | 7:3ed1e36587d4 | 306 | break; |
jksoft | 7:3ed1e36587d4 | 307 | case 0x0002: |
jksoft | 7:3ed1e36587d4 | 308 | MOTOR_A = -30; |
jksoft | 7:3ed1e36587d4 | 309 | MOTOR_B = -30; |
jksoft | 7:3ed1e36587d4 | 310 | break; |
jksoft | 7:3ed1e36587d4 | 311 | case 0x0004: |
jksoft | 7:3ed1e36587d4 | 312 | MOTOR_A = 30; |
jksoft | 7:3ed1e36587d4 | 313 | MOTOR_B = -30; |
jksoft | 7:3ed1e36587d4 | 314 | break; |
jksoft | 7:3ed1e36587d4 | 315 | case 0x0008: |
jksoft | 7:3ed1e36587d4 | 316 | MOTOR_A = -30; |
jksoft | 7:3ed1e36587d4 | 317 | MOTOR_B = 30; |
jksoft | 7:3ed1e36587d4 | 318 | break; |
jksoft | 7:3ed1e36587d4 | 319 | case 0x0100: |
jksoft | 7:3ed1e36587d4 | 320 | led4 = !led4; |
jksoft | 7:3ed1e36587d4 | 321 | int x = (int)buffer[6] - 128; |
jksoft | 7:3ed1e36587d4 | 322 | if( (x > 5)||(x < -5) ) |
jksoft | 7:3ed1e36587d4 | 323 | { |
jksoft | 7:3ed1e36587d4 | 324 | x = x > 100 ? 100 : x; |
jksoft | 7:3ed1e36587d4 | 325 | x = x < -100 ? -100 : x; |
jksoft | 7:3ed1e36587d4 | 326 | MOTOR_A = x > 0 ? 100 - x : 100; |
jksoft | 7:3ed1e36587d4 | 327 | MOTOR_B = x < 0 ? 100 + x : 100; |
jksoft | 7:3ed1e36587d4 | 328 | } |
jksoft | 7:3ed1e36587d4 | 329 | default: |
jksoft | 7:3ed1e36587d4 | 330 | MOTOR_A = 0; |
jksoft | 7:3ed1e36587d4 | 331 | MOTOR_B = 0; |
jksoft | 7:3ed1e36587d4 | 332 | break; |
jksoft | 7:3ed1e36587d4 | 333 | |
jksoft | 7:3ed1e36587d4 | 334 | } |
todotani | 1:6078e430af82 | 335 | break; |
todotani | 6:cf06ba884429 | 336 | |
todotani | 6:cf06ba884429 | 337 | // Correspond to Characteristics 0xFFF2 |
todotani | 1:6078e430af82 | 338 | case 0x000d: |
todotani | 1:6078e430af82 | 339 | log_info("New value: %u\n", buffer[0]); |
todotani | 6:cf06ba884429 | 340 | ledvalue = buffer[0]; |
todotani | 6:cf06ba884429 | 341 | led1 = ledvalue; |
todotani | 1:6078e430af82 | 342 | break; |
todotani | 1:6078e430af82 | 343 | } |
todotani | 1:6078e430af82 | 344 | } |
todotani | 1:6078e430af82 | 345 | |
todotani | 1:6078e430af82 | 346 | |
todotani | 3:4bc438c6fc52 | 347 | void hal_tick_event(void) |
todotani | 3:4bc438c6fc52 | 348 | { |
todotani | 3:4bc438c6fc52 | 349 | timer_counter++; |
todotani | 1:6078e430af82 | 350 | |
todotani | 6:cf06ba884429 | 351 | if(connection_status) { |
todotani | 6:cf06ba884429 | 352 | if(timer_counter % 250 == 0) { |
todotani | 6:cf06ba884429 | 353 | led3 = !led3; |
todotani | 6:cf06ba884429 | 354 | // Under testing |
todotani | 6:cf06ba884429 | 355 | // switch_new_state = (uint8_t)sw1.read(); |
todotani | 6:cf06ba884429 | 356 | // if (switch_new_state != switch_old_state) { |
todotani | 6:cf06ba884429 | 357 | // log_info("Fire notification\n"); |
todotani | 6:cf06ba884429 | 358 | // switch_old_state = switch_new_state; |
todotani | 6:cf06ba884429 | 359 | // att_prepare_handle_value_notification(&att_connection, 0x000b, &switch_new_state, 1, att_response_buffer); |
todotani | 6:cf06ba884429 | 360 | } |
todotani | 6:cf06ba884429 | 361 | } else { |
todotani | 3:4bc438c6fc52 | 362 | led3 = 1; |
todotani | 3:4bc438c6fc52 | 363 | } |
todotani | 3:4bc438c6fc52 | 364 | } |
todotani | 1:6078e430af82 | 365 | |
todotani | 1:6078e430af82 | 366 | // main |
todotani | 1:6078e430af82 | 367 | int main(void) |
todotani | 1:6078e430af82 | 368 | { |
todotani | 5:5fb56e13a1f9 | 369 | pc.baud(921600); |
todotani | 6:cf06ba884429 | 370 | //pc.baud(230400); |
todotani | 1:6078e430af82 | 371 | log_info("%s\n", __FILE__); |
todotani | 1:6078e430af82 | 372 | |
todotani | 1:6078e430af82 | 373 | // init LEDs |
todotani | 1:6078e430af82 | 374 | led1 = led2 = led3 = 1; |
todotani | 6:cf06ba884429 | 375 | ledvalue = led1; |
todotani | 6:cf06ba884429 | 376 | |
todotani | 6:cf06ba884429 | 377 | /// GET STARTED with BTstack /// |
todotani | 1:6078e430af82 | 378 | btstack_memory_init(); |
todotani | 1:6078e430af82 | 379 | run_loop_init(RUN_LOOP_EMBEDDED); |
todotani | 3:4bc438c6fc52 | 380 | hal_tick_set_handler(&hal_tick_event); |
todotani | 6:cf06ba884429 | 381 | |
todotani | 1:6078e430af82 | 382 | // init HCI |
todotani | 1:6078e430af82 | 383 | // use BlueUSB |
todotani | 1:6078e430af82 | 384 | hci_transport_t* transport = hci_transport_usb_instance(); |
todotani | 1:6078e430af82 | 385 | bt_control_t * control = NULL; |
todotani | 1:6078e430af82 | 386 | hci_uart_config_t * config = NULL; |
todotani | 1:6078e430af82 | 387 | remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_memory; |
todotani | 1:6078e430af82 | 388 | hci_init(transport, config, control, remote_db); |
todotani | 6:cf06ba884429 | 389 | |
todotani | 1:6078e430af82 | 390 | // use eHCILL |
todotani | 1:6078e430af82 | 391 | // bt_control_cc256x_enable_ehcill(1); |
todotani | 6:cf06ba884429 | 392 | |
todotani | 1:6078e430af82 | 393 | // set up l2cap_le |
todotani | 1:6078e430af82 | 394 | l2cap_init(); |
todotani | 1:6078e430af82 | 395 | l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); |
todotani | 1:6078e430af82 | 396 | l2cap_register_packet_handler(packet_handler); |
todotani | 6:cf06ba884429 | 397 | |
todotani | 1:6078e430af82 | 398 | // set up ATT |
todotani | 1:6078e430af82 | 399 | att_set_db(profile_data); |
todotani | 1:6078e430af82 | 400 | att_set_write_callback(att_write_callback); |
todotani | 1:6078e430af82 | 401 | att_set_read_callback(att_read_callback); |
todotani | 1:6078e430af82 | 402 | att_dump_attributes(); |
todotani | 1:6078e430af82 | 403 | att_connection.mtu = 27; |
todotani | 6:cf06ba884429 | 404 | |
todotani | 6:cf06ba884429 | 405 | log_info("Run...\n\n"); |
todotani | 1:6078e430af82 | 406 | |
todotani | 1:6078e430af82 | 407 | // turn on! |
todotani | 1:6078e430af82 | 408 | hci_power_control(HCI_POWER_ON); |
todotani | 1:6078e430af82 | 409 | |
todotani | 1:6078e430af82 | 410 | // go! |
todotani | 6:cf06ba884429 | 411 | run_loop_execute(); |
todotani | 6:cf06ba884429 | 412 | |
todotani | 1:6078e430af82 | 413 | // happy compiler! |
todotani | 1:6078e430af82 | 414 | return 0; |
todotani | 1:6078e430af82 | 415 | } |