AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 BLE Nanoで動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo
BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するBLE Nano用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
回路図
/media/uploads/lipoyang/ministeer3.pdf
main.cpp@0:c5082e68ff72, 2015-01-29 (annotated)
- Committer:
- lipoyang
- Date:
- Thu Jan 29 23:30:56 2015 +0000
- Revision:
- 0:c5082e68ff72
- Child:
- 1:3f38c4bad274
??????????OK
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lipoyang | 0:c5082e68ff72 | 1 | /* |
lipoyang | 0:c5082e68ff72 | 2 | * Copyright (C) 2015 Bizan Nishimura (@lipoyang) |
lipoyang | 0:c5082e68ff72 | 3 | * |
lipoyang | 0:c5082e68ff72 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lipoyang | 0:c5082e68ff72 | 5 | * you may not use this file except in compliance with the License. |
lipoyang | 0:c5082e68ff72 | 6 | * You may obtain a copy of the License at |
lipoyang | 0:c5082e68ff72 | 7 | * |
lipoyang | 0:c5082e68ff72 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lipoyang | 0:c5082e68ff72 | 9 | * |
lipoyang | 0:c5082e68ff72 | 10 | * Unless required by applicable law or agreed to in writing, software |
lipoyang | 0:c5082e68ff72 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lipoyang | 0:c5082e68ff72 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lipoyang | 0:c5082e68ff72 | 13 | * See the License for the specific language governing permissions and |
lipoyang | 0:c5082e68ff72 | 14 | * limitations under the License. |
lipoyang | 0:c5082e68ff72 | 15 | */ |
lipoyang | 0:c5082e68ff72 | 16 | |
lipoyang | 0:c5082e68ff72 | 17 | #include "mbed.h" |
lipoyang | 0:c5082e68ff72 | 18 | #include "BLEDevice.h" |
lipoyang | 0:c5082e68ff72 | 19 | //#include "Servo.h" |
lipoyang | 0:c5082e68ff72 | 20 | |
lipoyang | 0:c5082e68ff72 | 21 | |
lipoyang | 0:c5082e68ff72 | 22 | //#define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
lipoyang | 0:c5082e68ff72 | 23 | //#define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
lipoyang | 0:c5082e68ff72 | 24 | //#define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
lipoyang | 0:c5082e68ff72 | 25 | |
lipoyang | 0:c5082e68ff72 | 26 | //#define TXRX_BUF_LEN 20 |
lipoyang | 0:c5082e68ff72 | 27 | |
lipoyang | 0:c5082e68ff72 | 28 | //#define DIGITAL_OUT_PIN P0_9 //TXD |
lipoyang | 0:c5082e68ff72 | 29 | //#define DIGITAL_IN_PIN P0_10 //CTS |
lipoyang | 0:c5082e68ff72 | 30 | //#define PWM_PIN P0_11 //RXD |
lipoyang | 0:c5082e68ff72 | 31 | //#define SERVO_PIN P0_8 //RTS |
lipoyang | 0:c5082e68ff72 | 32 | //#define ANALOG_IN_PIN P0_4 //P04 |
lipoyang | 0:c5082e68ff72 | 33 | |
lipoyang | 0:c5082e68ff72 | 34 | BLEDevice ble; |
lipoyang | 0:c5082e68ff72 | 35 | |
lipoyang | 0:c5082e68ff72 | 36 | // BluePropo service UUID |
lipoyang | 0:c5082e68ff72 | 37 | //static const uint16_t UUID_BLUEPROPO = 0xFFF0; |
lipoyang | 0:c5082e68ff72 | 38 | static const uint8_t UUID_BLUEPROPO[] = |
lipoyang | 0:c5082e68ff72 | 39 | { 0xc4, 0x9d, 0xfd, 0x1b, 0x86, 0x04, 0x41, 0xd2, 0x89, 0x43, 0x13, 0x6f, 0x21, 0x4d, 0xd0, 0xbf }; |
lipoyang | 0:c5082e68ff72 | 40 | |
lipoyang | 0:c5082e68ff72 | 41 | // BluePropo::Stick characteristic UUID |
lipoyang | 0:c5082e68ff72 | 42 | //static const uint16_t UUID_BLUEPROPO_STICK = 0xFFF1; |
lipoyang | 0:c5082e68ff72 | 43 | static const uint8_t UUID_BLUEPROPO_STICK[] = |
lipoyang | 0:c5082e68ff72 | 44 | { 0x74, 0x25, 0xfb, 0xa0, 0x72, 0x15, 0x41, 0x36, 0xaa, 0x3f, 0x07, 0x2a, 0xa0, 0x7d, 0x93, 0x54 }; |
lipoyang | 0:c5082e68ff72 | 45 | |
lipoyang | 0:c5082e68ff72 | 46 | // Device Name (for display) |
lipoyang | 0:c5082e68ff72 | 47 | #define DEVICE_NAME "MiniSteer BLE Nano" |
lipoyang | 0:c5082e68ff72 | 48 | |
lipoyang | 0:c5082e68ff72 | 49 | // BluePropo::Stick data structure |
lipoyang | 0:c5082e68ff72 | 50 | union StickData |
lipoyang | 0:c5082e68ff72 | 51 | { |
lipoyang | 0:c5082e68ff72 | 52 | struct { |
lipoyang | 0:c5082e68ff72 | 53 | // F(-128)<- 0 ->B(+127) |
lipoyang | 0:c5082e68ff72 | 54 | signed char fb; |
lipoyang | 0:c5082e68ff72 | 55 | // L(-128)<- 0 ->R(+127) |
lipoyang | 0:c5082e68ff72 | 56 | signed char lr; |
lipoyang | 0:c5082e68ff72 | 57 | }value; |
lipoyang | 0:c5082e68ff72 | 58 | unsigned char bytes[2]; |
lipoyang | 0:c5082e68ff72 | 59 | }; |
lipoyang | 0:c5082e68ff72 | 60 | StickData stickData; |
lipoyang | 0:c5082e68ff72 | 61 | |
lipoyang | 0:c5082e68ff72 | 62 | // buffer for BluePropo payload |
lipoyang | 0:c5082e68ff72 | 63 | uint8_t payload[10] = {0,}; |
lipoyang | 0:c5082e68ff72 | 64 | |
lipoyang | 0:c5082e68ff72 | 65 | // BluePropo::Stick characteristic |
lipoyang | 0:c5082e68ff72 | 66 | GattCharacteristic charStick (UUID_BLUEPROPO_STICK, payload, 2, 2, |
lipoyang | 0:c5082e68ff72 | 67 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
lipoyang | 0:c5082e68ff72 | 68 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
lipoyang | 0:c5082e68ff72 | 69 | // BluePropo characteristics set |
lipoyang | 0:c5082e68ff72 | 70 | GattCharacteristic *chars[] = {&charStick}; |
lipoyang | 0:c5082e68ff72 | 71 | // BluePropo service |
lipoyang | 0:c5082e68ff72 | 72 | GattService serviceBluePropo(UUID_BLUEPROPO, chars, sizeof(chars) / sizeof(GattCharacteristic *)); |
lipoyang | 0:c5082e68ff72 | 73 | |
lipoyang | 0:c5082e68ff72 | 74 | |
lipoyang | 0:c5082e68ff72 | 75 | //DigitalOut LED_SET(DIGITAL_OUT_PIN); |
lipoyang | 0:c5082e68ff72 | 76 | //DigitalIn BUTTON(DIGITAL_IN_PIN); |
lipoyang | 0:c5082e68ff72 | 77 | //PwmOut PWM(PWM_PIN); |
lipoyang | 0:c5082e68ff72 | 78 | //AnalogIn ANALOG(ANALOG_IN_PIN); |
lipoyang | 0:c5082e68ff72 | 79 | //Servo MYSERVO(SERVO_PIN); |
lipoyang | 0:c5082e68ff72 | 80 | |
lipoyang | 0:c5082e68ff72 | 81 | //Serial pc(USBTX, USBRX); |
lipoyang | 0:c5082e68ff72 | 82 | |
lipoyang | 0:c5082e68ff72 | 83 | //static uint8_t analog_enabled = 0; |
lipoyang | 0:c5082e68ff72 | 84 | //static uint8_t old_state = 0; |
lipoyang | 0:c5082e68ff72 | 85 | |
lipoyang | 0:c5082e68ff72 | 86 | // The Nordic UART Service |
lipoyang | 0:c5082e68ff72 | 87 | //static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
lipoyang | 0:c5082e68ff72 | 88 | //static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
lipoyang | 0:c5082e68ff72 | 89 | //static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
lipoyang | 0:c5082e68ff72 | 90 | //static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; |
lipoyang | 0:c5082e68ff72 | 91 | |
lipoyang | 0:c5082e68ff72 | 92 | |
lipoyang | 0:c5082e68ff72 | 93 | |
lipoyang | 0:c5082e68ff72 | 94 | //uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
lipoyang | 0:c5082e68ff72 | 95 | //uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
lipoyang | 0:c5082e68ff72 | 96 | |
lipoyang | 0:c5082e68ff72 | 97 | //static uint8_t rx_buf[TXRX_BUF_LEN]; |
lipoyang | 0:c5082e68ff72 | 98 | //static uint8_t rx_len=0; |
lipoyang | 0:c5082e68ff72 | 99 | |
lipoyang | 0:c5082e68ff72 | 100 | /* |
lipoyang | 0:c5082e68ff72 | 101 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
lipoyang | 0:c5082e68ff72 | 102 | |
lipoyang | 0:c5082e68ff72 | 103 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
lipoyang | 0:c5082e68ff72 | 104 | |
lipoyang | 0:c5082e68ff72 | 105 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
lipoyang | 0:c5082e68ff72 | 106 | |
lipoyang | 0:c5082e68ff72 | 107 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
lipoyang | 0:c5082e68ff72 | 108 | */ |
lipoyang | 0:c5082e68ff72 | 109 | |
lipoyang | 0:c5082e68ff72 | 110 | // pin asign |
lipoyang | 0:c5082e68ff72 | 111 | DigitalOut tb6612_ain1(P0_10); // AIN1: P0_10 (D2) |
lipoyang | 0:c5082e68ff72 | 112 | DigitalOut tb6612_ain2(P0_9); // AIN2: P0_9 (D1) |
lipoyang | 0:c5082e68ff72 | 113 | PwmOut tb6612_pwma(P0_11); // PWMA: P0_11 (D0) |
lipoyang | 0:c5082e68ff72 | 114 | PwmOut servo_pwm (P0_8); // SERVO: P0_8 (D3) |
lipoyang | 0:c5082e68ff72 | 115 | |
lipoyang | 0:c5082e68ff72 | 116 | // SENS-L: P0_4 (A3) |
lipoyang | 0:c5082e68ff72 | 117 | // SENS-R: P0_5 (A4) |
lipoyang | 0:c5082e68ff72 | 118 | |
lipoyang | 0:c5082e68ff72 | 119 | // DC motor driver (TB6612) |
lipoyang | 0:c5082e68ff72 | 120 | void motor (float speed) |
lipoyang | 0:c5082e68ff72 | 121 | { |
lipoyang | 0:c5082e68ff72 | 122 | if (speed > 0) { |
lipoyang | 0:c5082e68ff72 | 123 | // CW |
lipoyang | 0:c5082e68ff72 | 124 | tb6612_pwma = speed; |
lipoyang | 0:c5082e68ff72 | 125 | tb6612_ain1 = 1; |
lipoyang | 0:c5082e68ff72 | 126 | tb6612_ain2 = 0; |
lipoyang | 0:c5082e68ff72 | 127 | } else |
lipoyang | 0:c5082e68ff72 | 128 | if (speed < 0) { |
lipoyang | 0:c5082e68ff72 | 129 | // CCW |
lipoyang | 0:c5082e68ff72 | 130 | tb6612_pwma = - speed; |
lipoyang | 0:c5082e68ff72 | 131 | tb6612_ain1 = 0; |
lipoyang | 0:c5082e68ff72 | 132 | tb6612_ain2 = 1; |
lipoyang | 0:c5082e68ff72 | 133 | } else { |
lipoyang | 0:c5082e68ff72 | 134 | // stop |
lipoyang | 0:c5082e68ff72 | 135 | tb6612_pwma = 1; |
lipoyang | 0:c5082e68ff72 | 136 | tb6612_ain1 = 0; |
lipoyang | 0:c5082e68ff72 | 137 | tb6612_ain2 = 0; |
lipoyang | 0:c5082e68ff72 | 138 | // // break |
lipoyang | 0:c5082e68ff72 | 139 | // tb6612_pwma = 1; |
lipoyang | 0:c5082e68ff72 | 140 | // tb6612_ain1 = 1; |
lipoyang | 0:c5082e68ff72 | 141 | // tb6612_ain2 = 1; |
lipoyang | 0:c5082e68ff72 | 142 | } |
lipoyang | 0:c5082e68ff72 | 143 | } |
lipoyang | 0:c5082e68ff72 | 144 | |
lipoyang | 0:c5082e68ff72 | 145 | // RC servo |
lipoyang | 0:c5082e68ff72 | 146 | void servo (float deg) |
lipoyang | 0:c5082e68ff72 | 147 | { |
lipoyang | 0:c5082e68ff72 | 148 | servo_pwm.pulsewidth_us(1500 + (int)(500.0 * deg)); |
lipoyang | 0:c5082e68ff72 | 149 | } |
lipoyang | 0:c5082e68ff72 | 150 | |
lipoyang | 0:c5082e68ff72 | 151 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
lipoyang | 0:c5082e68ff72 | 152 | { |
lipoyang | 0:c5082e68ff72 | 153 | //pc.printf("Disconnected \r\n"); |
lipoyang | 0:c5082e68ff72 | 154 | //pc.printf("Restart advertising \r\n"); |
lipoyang | 0:c5082e68ff72 | 155 | ble.startAdvertising(); |
lipoyang | 0:c5082e68ff72 | 156 | } |
lipoyang | 0:c5082e68ff72 | 157 | |
lipoyang | 0:c5082e68ff72 | 158 | void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) |
lipoyang | 0:c5082e68ff72 | 159 | { |
lipoyang | 0:c5082e68ff72 | 160 | // uint8_t buf[TXRX_BUF_LEN]; |
lipoyang | 0:c5082e68ff72 | 161 | // uint16_t bytesRead; |
lipoyang | 0:c5082e68ff72 | 162 | |
lipoyang | 0:c5082e68ff72 | 163 | if (Handler->charHandle == charStick.getValueAttribute().getHandle()) |
lipoyang | 0:c5082e68ff72 | 164 | { |
lipoyang | 0:c5082e68ff72 | 165 | uint16_t bytesRead; |
lipoyang | 0:c5082e68ff72 | 166 | ble.readCharacteristicValue(charStick.getValueAttribute().getHandle(), payload, &bytesRead); |
lipoyang | 0:c5082e68ff72 | 167 | memcpy( &stickData.bytes[0], payload, sizeof(stickData)); |
lipoyang | 0:c5082e68ff72 | 168 | #if DBG |
lipoyang | 0:c5082e68ff72 | 169 | |
lipoyang | 0:c5082e68ff72 | 170 | pc.printf("DATA:%02X %02X\n\r",stickData.bytes[0],stickData.bytes[1]); |
lipoyang | 0:c5082e68ff72 | 171 | #endif |
lipoyang | 0:c5082e68ff72 | 172 | float m = (float)stickData.value.fb / 128.0; |
lipoyang | 0:c5082e68ff72 | 173 | motor(m); |
lipoyang | 0:c5082e68ff72 | 174 | float s = (float)stickData.value.lr / 128.0; |
lipoyang | 0:c5082e68ff72 | 175 | servo(s); |
lipoyang | 0:c5082e68ff72 | 176 | |
lipoyang | 0:c5082e68ff72 | 177 | /* |
lipoyang | 0:c5082e68ff72 | 178 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
lipoyang | 0:c5082e68ff72 | 179 | memset(txPayload, 0, TXRX_BUF_LEN); |
lipoyang | 0:c5082e68ff72 | 180 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
lipoyang | 0:c5082e68ff72 | 181 | |
lipoyang | 0:c5082e68ff72 | 182 | //for(index=0; index<bytesRead; index++) |
lipoyang | 0:c5082e68ff72 | 183 | //pc.putc(buf[index]); |
lipoyang | 0:c5082e68ff72 | 184 | |
lipoyang | 0:c5082e68ff72 | 185 | if(buf[0] == 0x01) |
lipoyang | 0:c5082e68ff72 | 186 | { |
lipoyang | 0:c5082e68ff72 | 187 | if(buf[1] == 0x01) |
lipoyang | 0:c5082e68ff72 | 188 | LED_SET = 1; |
lipoyang | 0:c5082e68ff72 | 189 | else |
lipoyang | 0:c5082e68ff72 | 190 | LED_SET = 0; |
lipoyang | 0:c5082e68ff72 | 191 | } |
lipoyang | 0:c5082e68ff72 | 192 | else if(buf[0] == 0xA0) |
lipoyang | 0:c5082e68ff72 | 193 | { |
lipoyang | 0:c5082e68ff72 | 194 | if(buf[1] == 0x01) |
lipoyang | 0:c5082e68ff72 | 195 | analog_enabled = 1; |
lipoyang | 0:c5082e68ff72 | 196 | else |
lipoyang | 0:c5082e68ff72 | 197 | analog_enabled = 0; |
lipoyang | 0:c5082e68ff72 | 198 | } |
lipoyang | 0:c5082e68ff72 | 199 | else if(buf[0] == 0x02) |
lipoyang | 0:c5082e68ff72 | 200 | { |
lipoyang | 0:c5082e68ff72 | 201 | float value = (float)buf[1]/255; |
lipoyang | 0:c5082e68ff72 | 202 | PWM = value; |
lipoyang | 0:c5082e68ff72 | 203 | } |
lipoyang | 0:c5082e68ff72 | 204 | else if(buf[0] == 0x03) |
lipoyang | 0:c5082e68ff72 | 205 | { |
lipoyang | 0:c5082e68ff72 | 206 | MYSERVO.write(buf[1]); |
lipoyang | 0:c5082e68ff72 | 207 | } |
lipoyang | 0:c5082e68ff72 | 208 | else if(buf[0] == 0x04) |
lipoyang | 0:c5082e68ff72 | 209 | { |
lipoyang | 0:c5082e68ff72 | 210 | analog_enabled = 0; |
lipoyang | 0:c5082e68ff72 | 211 | PWM = 0; |
lipoyang | 0:c5082e68ff72 | 212 | MYSERVO.write(0); |
lipoyang | 0:c5082e68ff72 | 213 | LED_SET = 0; |
lipoyang | 0:c5082e68ff72 | 214 | old_state = 0; |
lipoyang | 0:c5082e68ff72 | 215 | } |
lipoyang | 0:c5082e68ff72 | 216 | */ |
lipoyang | 0:c5082e68ff72 | 217 | } |
lipoyang | 0:c5082e68ff72 | 218 | } |
lipoyang | 0:c5082e68ff72 | 219 | /* |
lipoyang | 0:c5082e68ff72 | 220 | void uartCB(void) |
lipoyang | 0:c5082e68ff72 | 221 | { |
lipoyang | 0:c5082e68ff72 | 222 | while(pc.readable()) |
lipoyang | 0:c5082e68ff72 | 223 | { |
lipoyang | 0:c5082e68ff72 | 224 | rx_buf[rx_len++] = pc.getc(); |
lipoyang | 0:c5082e68ff72 | 225 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') |
lipoyang | 0:c5082e68ff72 | 226 | { |
lipoyang | 0:c5082e68ff72 | 227 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
lipoyang | 0:c5082e68ff72 | 228 | pc.printf("RecHandler \r\n"); |
lipoyang | 0:c5082e68ff72 | 229 | pc.printf("Length: "); |
lipoyang | 0:c5082e68ff72 | 230 | pc.putc(rx_len); |
lipoyang | 0:c5082e68ff72 | 231 | pc.printf("\r\n"); |
lipoyang | 0:c5082e68ff72 | 232 | rx_len = 0; |
lipoyang | 0:c5082e68ff72 | 233 | break; |
lipoyang | 0:c5082e68ff72 | 234 | } |
lipoyang | 0:c5082e68ff72 | 235 | } |
lipoyang | 0:c5082e68ff72 | 236 | } |
lipoyang | 0:c5082e68ff72 | 237 | */ |
lipoyang | 0:c5082e68ff72 | 238 | void m_status_check_handle(void) |
lipoyang | 0:c5082e68ff72 | 239 | { |
lipoyang | 0:c5082e68ff72 | 240 | /* |
lipoyang | 0:c5082e68ff72 | 241 | uint8_t buf[3]; |
lipoyang | 0:c5082e68ff72 | 242 | if (analog_enabled) // if analog reading enabled |
lipoyang | 0:c5082e68ff72 | 243 | { |
lipoyang | 0:c5082e68ff72 | 244 | // Read and send out |
lipoyang | 0:c5082e68ff72 | 245 | float s = ANALOG; |
lipoyang | 0:c5082e68ff72 | 246 | uint16_t value = s*1024; |
lipoyang | 0:c5082e68ff72 | 247 | buf[0] = (0x0B); |
lipoyang | 0:c5082e68ff72 | 248 | buf[1] = (value >> 8); |
lipoyang | 0:c5082e68ff72 | 249 | buf[2] = (value); |
lipoyang | 0:c5082e68ff72 | 250 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); |
lipoyang | 0:c5082e68ff72 | 251 | } |
lipoyang | 0:c5082e68ff72 | 252 | |
lipoyang | 0:c5082e68ff72 | 253 | // If digital in changes, report the state |
lipoyang | 0:c5082e68ff72 | 254 | if (BUTTON != old_state) |
lipoyang | 0:c5082e68ff72 | 255 | { |
lipoyang | 0:c5082e68ff72 | 256 | old_state = BUTTON; |
lipoyang | 0:c5082e68ff72 | 257 | |
lipoyang | 0:c5082e68ff72 | 258 | if (BUTTON == 1) |
lipoyang | 0:c5082e68ff72 | 259 | { |
lipoyang | 0:c5082e68ff72 | 260 | buf[0] = (0x0A); |
lipoyang | 0:c5082e68ff72 | 261 | buf[1] = (0x01); |
lipoyang | 0:c5082e68ff72 | 262 | buf[2] = (0x00); |
lipoyang | 0:c5082e68ff72 | 263 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); |
lipoyang | 0:c5082e68ff72 | 264 | } |
lipoyang | 0:c5082e68ff72 | 265 | else |
lipoyang | 0:c5082e68ff72 | 266 | { |
lipoyang | 0:c5082e68ff72 | 267 | buf[0] = (0x0A); |
lipoyang | 0:c5082e68ff72 | 268 | buf[1] = (0x00); |
lipoyang | 0:c5082e68ff72 | 269 | buf[2] = (0x00); |
lipoyang | 0:c5082e68ff72 | 270 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); |
lipoyang | 0:c5082e68ff72 | 271 | } |
lipoyang | 0:c5082e68ff72 | 272 | } |
lipoyang | 0:c5082e68ff72 | 273 | */ |
lipoyang | 0:c5082e68ff72 | 274 | } |
lipoyang | 0:c5082e68ff72 | 275 | |
lipoyang | 0:c5082e68ff72 | 276 | |
lipoyang | 0:c5082e68ff72 | 277 | int main(void) |
lipoyang | 0:c5082e68ff72 | 278 | { |
lipoyang | 0:c5082e68ff72 | 279 | // initialize servo & motor |
lipoyang | 0:c5082e68ff72 | 280 | servo_pwm.period_ms(20); |
lipoyang | 0:c5082e68ff72 | 281 | servo(0.5); |
lipoyang | 0:c5082e68ff72 | 282 | motor(0); |
lipoyang | 0:c5082e68ff72 | 283 | |
lipoyang | 0:c5082e68ff72 | 284 | Ticker ticker; |
lipoyang | 0:c5082e68ff72 | 285 | ticker.attach_us(m_status_check_handle, 200000); |
lipoyang | 0:c5082e68ff72 | 286 | |
lipoyang | 0:c5082e68ff72 | 287 | // initialize BLE |
lipoyang | 0:c5082e68ff72 | 288 | ble.init(); |
lipoyang | 0:c5082e68ff72 | 289 | ble.onDisconnection(disconnectionCallback); |
lipoyang | 0:c5082e68ff72 | 290 | ble.onDataWritten(WrittenHandler); |
lipoyang | 0:c5082e68ff72 | 291 | |
lipoyang | 0:c5082e68ff72 | 292 | //pc.baud(9600); |
lipoyang | 0:c5082e68ff72 | 293 | //pc.printf("SimpleChat Init \r\n"); |
lipoyang | 0:c5082e68ff72 | 294 | //pc.attach( uartCB , pc.RxIrq); |
lipoyang | 0:c5082e68ff72 | 295 | |
lipoyang | 0:c5082e68ff72 | 296 | // setup advertising |
lipoyang | 0:c5082e68ff72 | 297 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
lipoyang | 0:c5082e68ff72 | 298 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
lipoyang | 0:c5082e68ff72 | 299 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
lipoyang | 0:c5082e68ff72 | 300 | (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
lipoyang | 0:c5082e68ff72 | 301 | // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
lipoyang | 0:c5082e68ff72 | 302 | // (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
lipoyang | 0:c5082e68ff72 | 303 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
lipoyang | 0:c5082e68ff72 | 304 | (const uint8_t *)UUID_BLUEPROPO, sizeof(UUID_BLUEPROPO)); |
lipoyang | 0:c5082e68ff72 | 305 | ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. |
lipoyang | 0:c5082e68ff72 | 306 | ble.addService(serviceBluePropo); |
lipoyang | 0:c5082e68ff72 | 307 | ble.startAdvertising(); |
lipoyang | 0:c5082e68ff72 | 308 | //pc.printf("Advertising Start \r\n"); |
lipoyang | 0:c5082e68ff72 | 309 | |
lipoyang | 0:c5082e68ff72 | 310 | // main loop (wait for BLE event) |
lipoyang | 0:c5082e68ff72 | 311 | while(true) |
lipoyang | 0:c5082e68ff72 | 312 | { |
lipoyang | 0:c5082e68ff72 | 313 | ble.waitForEvent(); |
lipoyang | 0:c5082e68ff72 | 314 | } |
lipoyang | 0:c5082e68ff72 | 315 | } |