mbed HRM1017にSBBLE/konashiを移植する実験(途中)
Dependencies: BLE_API mbed nRF51822
Fork of BLE_TEST_konashi by
Konashi/SBBLEと完全互換ではないので注意!!
ライブラリ類をUpdateするとコンパイル出来なくなります。インポートした物をそのまま使って下さい。
Characteristicを増やすと挙動がおかしくなる不具合があります。
- onDataWrittenコールバック関数が呼ばれなくなる。
- onDisconnectionコールバック関数の中の"ble.startAdvertising()"でハングする。
PIO入出力と次の3つのうちの2つまでの組み合わせなら動作するのを確認しました。
main.cppの20行目付近で選択できます。
- PWM
- I2C
- UART
IOの構成を変更したときは、iPhone/iPadのBluetoothをOFF->ONしてキャッシュをクリアする必要があります。
IOのピン割り付けはソースコードを見てください。
動作確認にはiOSのSBBLEアプリが使えます。
https://itunes.apple.com/jp/app/sbble/id788610934?mt=8
main.cpp@12:dd866bd5eaf9, 2016-04-19 (annotated)
- Committer:
- mbed_tw_hoehoe
- Date:
- Tue Apr 19 16:22:00 2016 +0000
- Revision:
- 12:dd866bd5eaf9
- Parent:
- 11:c25480277877
2016/04/20 ?????BLE API????; TY51822r3 ??Build?????;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:e910d9bb040f | 1 | /* mbed Microcontroller Library |
yihui | 0:e910d9bb040f | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 0:e910d9bb040f | 3 | * |
yihui | 0:e910d9bb040f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 0:e910d9bb040f | 5 | * you may not use this file except in compliance with the License. |
yihui | 0:e910d9bb040f | 6 | * You may obtain a copy of the License at |
yihui | 0:e910d9bb040f | 7 | * |
yihui | 0:e910d9bb040f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 0:e910d9bb040f | 9 | * |
yihui | 0:e910d9bb040f | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 0:e910d9bb040f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 0:e910d9bb040f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 0:e910d9bb040f | 13 | * See the License for the specific language governing permissions and |
yihui | 0:e910d9bb040f | 14 | * limitations under the License. |
yihui | 0:e910d9bb040f | 15 | */ |
yihui | 0:e910d9bb040f | 16 | |
yihui | 0:e910d9bb040f | 17 | #include "mbed.h" |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 18 | #include "BLE.h" |
yihui | 0:e910d9bb040f | 19 | |
robo8080 | 11:c25480277877 | 20 | //Debug用 |
robo8080 | 11:c25480277877 | 21 | #define USE_KONASHI_PWM 0 |
robo8080 | 11:c25480277877 | 22 | #define USE_KONASHI_I2C 1 |
robo8080 | 11:c25480277877 | 23 | #define USE_KONASHI_ANALOG 0 //未実装 |
robo8080 | 11:c25480277877 | 24 | #define USE_KONASHI_UART 1 |
robo8080 | 11:c25480277877 | 25 | |
robo8080 | 8:a62b8f7d5dcf | 26 | const static char DEVICE_NAME[] = "mbed HRM1017"; |
robo8080 | 5:61109bce11fe | 27 | |
robo8080 | 5:61109bce11fe | 28 | static const uint16_t KONASHI_SERVICE_UUID = 0xFF00; |
robo8080 | 5:61109bce11fe | 29 | |
robo8080 | 5:61109bce11fe | 30 | static const uint16_t KONASHI_PIO_SETTING_UUID = 0x3000; |
robo8080 | 5:61109bce11fe | 31 | static const uint16_t KONASHI_PIO_PULLUP_UUID = 0x3001; |
robo8080 | 5:61109bce11fe | 32 | static const uint16_t KONASHI_PIO_OUTPUT_UUID = 0x3002; |
robo8080 | 5:61109bce11fe | 33 | static const uint16_t KONASHI_PIO_INPUT_NOTIFICATION_UUID = 0x3003; |
robo8080 | 5:61109bce11fe | 34 | |
robo8080 | 5:61109bce11fe | 35 | static const uint16_t KONASHI_PWM_CONFIG_UUID = 0x3004; |
robo8080 | 5:61109bce11fe | 36 | static const uint16_t KONASHI_PWM_PARAM_UUID = 0x3005; |
robo8080 | 5:61109bce11fe | 37 | static const uint16_t KONASHI_PWM_DUTY_UUID = 0x3006; |
robo8080 | 5:61109bce11fe | 38 | |
robo8080 | 5:61109bce11fe | 39 | static const uint16_t KONASHI_ANALOG_DRIVE_UUID = 0x3007; |
robo8080 | 5:61109bce11fe | 40 | static const uint16_t KONASHI_ANALOG_READ0_UUID = 0x3008; |
robo8080 | 5:61109bce11fe | 41 | static const uint16_t KONASHI_ANALOG_READ1_UUID = 0x3009; |
robo8080 | 5:61109bce11fe | 42 | static const uint16_t KONASHI_ANALOG_READ2_UUID = 0x300A; |
yihui | 0:e910d9bb040f | 43 | |
robo8080 | 5:61109bce11fe | 44 | static const uint16_t KONASHI_I2C_CONFIG_UUID = 0x300B; |
robo8080 | 5:61109bce11fe | 45 | static const uint16_t KONASHI_I2C_START_STOP_UUID = 0x300C; |
robo8080 | 5:61109bce11fe | 46 | static const uint16_t KONASHI_I2C_WRITE_UUID = 0x300D; |
robo8080 | 5:61109bce11fe | 47 | static const uint16_t KONASHI_I2C_READ_PARAM_UIUD = 0x300E; |
robo8080 | 5:61109bce11fe | 48 | static const uint16_t KONASHI_I2C_READ_UUID = 0x300F; |
robo8080 | 5:61109bce11fe | 49 | |
robo8080 | 5:61109bce11fe | 50 | static const uint16_t KONASHI_UART_CONFIG_UUID = 0x3010; |
robo8080 | 5:61109bce11fe | 51 | static const uint16_t KONASHI_UART_BAUDRATE_UUID = 0x3011; |
robo8080 | 5:61109bce11fe | 52 | static const uint16_t KONASHI_UART_TX_UUID = 0x3012; |
robo8080 | 5:61109bce11fe | 53 | static const uint16_t KONASHI_UART_RX_NOTIFICATION_UUID = 0x3013; |
robo8080 | 5:61109bce11fe | 54 | |
robo8080 | 5:61109bce11fe | 55 | static const uint16_t KONASHI_HARDWARE_RESET_UUID = 0x3014; |
robo8080 | 5:61109bce11fe | 56 | static const uint16_t KONASHI_HARDWARE_LOW_BAT_NOTIFICATION_UUID = 0x3015; |
robo8080 | 5:61109bce11fe | 57 | |
robo8080 | 5:61109bce11fe | 58 | uint16_t uuid16_list[] = { KONASHI_SERVICE_UUID }; |
robo8080 | 5:61109bce11fe | 59 | |
robo8080 | 11:c25480277877 | 60 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
Rohit Grover |
2:e060367b9024 | 61 | * it will have an impact on code-size and power consumption. */ |
yihui | 0:e910d9bb040f | 62 | |
Rohit Grover |
2:e060367b9024 | 63 | #if NEED_CONSOLE_OUTPUT |
robo8080 | 11:c25480277877 | 64 | //Serial pc(USBTX, USBRX); |
Rohit Grover |
2:e060367b9024 | 65 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
yihui | 0:e910d9bb040f | 66 | #else |
Rohit Grover |
2:e060367b9024 | 67 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
2:e060367b9024 | 68 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
yihui | 0:e910d9bb040f | 69 | |
robo8080 | 11:c25480277877 | 70 | Serial pc(USBTX, USBRX); |
Rohit Grover |
2:e060367b9024 | 71 | BLEDevice ble; |
Rohit Grover |
2:e060367b9024 | 72 | DigitalOut led1(LED1); |
robo8080 | 6:9c9630cb454b | 73 | DigitalOut led2(LED2); |
robo8080 | 6:9c9630cb454b | 74 | |
robo8080 | 9:a6ae6f8c95b7 | 75 | //BusOut ioOut(P0_0,P0_1,P0_2,P0_3,P0_4,P0_5,P0_6,P0_7); |
robo8080 | 9:a6ae6f8c95b7 | 76 | BusOut ioOut(P0_5, P0_6, P0_7, P0_1, P0_0, P0_30 ,P0_29 ,P0_28); |
robo8080 | 9:a6ae6f8c95b7 | 77 | //BusIn ioIn(P0_10,P0_12,P0_13,P0_14,P0_15,P0_17, P0_16, P0_21); |
robo8080 | 9:a6ae6f8c95b7 | 78 | BusIn ioIn(P0_10,P0_12,P0_13,P0_14,P0_15,P0_17, P0_16, P0_21); |
robo8080 | 5:61109bce11fe | 79 | uint8_t pioSetting[20] = {0,}; |
robo8080 | 5:61109bce11fe | 80 | uint8_t pioPullup[20] = {0,}; |
robo8080 | 5:61109bce11fe | 81 | uint8_t pioOutput[20] = {0,}; |
robo8080 | 5:61109bce11fe | 82 | uint8_t pioInput[20] = {0,}; |
robo8080 | 5:61109bce11fe | 83 | |
robo8080 | 5:61109bce11fe | 84 | uint8_t pwmConfig[20] = {0,}; |
robo8080 | 5:61109bce11fe | 85 | uint8_t pwmPeriod[20] = {0,}; |
robo8080 | 5:61109bce11fe | 86 | uint8_t pwmDuty[20] = {0,}; |
robo8080 | 6:9c9630cb454b | 87 | static uint32_t pwm_period[3] = {20000,20000,20000}; |
robo8080 | 6:9c9630cb454b | 88 | static uint32_t pwm_duty[3] = {0,0,0}; |
robo8080 | 6:9c9630cb454b | 89 | static uint8_t pwm_config = 0; |
robo8080 | 9:a6ae6f8c95b7 | 90 | //PwmOut pwm[3] = {P0_29, P0_28, P0_25,}; |
robo8080 | 9:a6ae6f8c95b7 | 91 | PwmOut pwm[3] = {P0_23, P0_24, P0_25,}; |
robo8080 | 5:61109bce11fe | 92 | |
robo8080 | 5:61109bce11fe | 93 | uint8_t analogDrive[20] = {0,}; |
robo8080 | 5:61109bce11fe | 94 | uint8_t analogRead0[20] = {0,}; |
robo8080 | 5:61109bce11fe | 95 | uint8_t analogRead1[20] = {0,}; |
robo8080 | 5:61109bce11fe | 96 | uint8_t analogRead2[20] = {0,}; |
robo8080 | 9:a6ae6f8c95b7 | 97 | AnalogIn analog[3] = {P0_2, P0_3,P0_4}; |
robo8080 | 5:61109bce11fe | 98 | |
robo8080 | 9:a6ae6f8c95b7 | 99 | I2C i2c(P0_22,P0_20); |
robo8080 | 9:a6ae6f8c95b7 | 100 | static uint8_t i2c_config = 0; |
robo8080 | 9:a6ae6f8c95b7 | 101 | static uint8_t i2c_recvbuf[20]; |
robo8080 | 9:a6ae6f8c95b7 | 102 | static uint8_t i2c_recvlen = 0; |
robo8080 | 10:1f4cfd4dd6c8 | 103 | static bool i2c_repeated = false; |
robo8080 | 5:61109bce11fe | 104 | uint8_t i2cConfig[20] = {0,}; |
robo8080 | 5:61109bce11fe | 105 | uint8_t i2cStartStop[20] = {0,}; |
robo8080 | 5:61109bce11fe | 106 | uint8_t i2cWrite[20] = {0,}; |
robo8080 | 5:61109bce11fe | 107 | uint8_t i2cReadParam[20] = {0,}; |
robo8080 | 5:61109bce11fe | 108 | uint8_t i2cRead[20] = {0,}; |
robo8080 | 5:61109bce11fe | 109 | |
robo8080 | 9:a6ae6f8c95b7 | 110 | #define UART_DEFAULT_BAUDRATE 115200 |
robo8080 | 9:a6ae6f8c95b7 | 111 | static uint16_t uart_baudrate=UART_DEFAULT_BAUDRATE/240; |
robo8080 | 5:61109bce11fe | 112 | uint8_t uartConfig[20] = {0,}; |
robo8080 | 5:61109bce11fe | 113 | uint8_t uartBaudrate[20] = {0,}; |
robo8080 | 5:61109bce11fe | 114 | uint8_t uartTx[20] = {0,}; |
robo8080 | 5:61109bce11fe | 115 | uint8_t uartRx[20] = {0,}; |
robo8080 | 5:61109bce11fe | 116 | |
robo8080 | 5:61109bce11fe | 117 | uint8_t hardwareRest[20] = {0,}; |
robo8080 | 5:61109bce11fe | 118 | uint8_t hardwareLowBat[20] = {0,}; |
robo8080 | 5:61109bce11fe | 119 | |
robo8080 | 5:61109bce11fe | 120 | GattCharacteristic pioSettingCharacteristic (KONASHI_PIO_SETTING_UUID, pioSetting, 1, 1, |
robo8080 | 5:61109bce11fe | 121 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 122 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 123 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 124 | GattCharacteristic pioPullupCharacteristic (KONASHI_PIO_PULLUP_UUID, pioPullup, 1, 1, |
robo8080 | 5:61109bce11fe | 125 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 126 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 127 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 128 | GattCharacteristic pioOutputCharacteristic (KONASHI_PIO_OUTPUT_UUID, pioOutput, 1, 1, |
robo8080 | 5:61109bce11fe | 129 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 130 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 131 | GattCharacteristic pioInputCharacteristic (KONASHI_PIO_INPUT_NOTIFICATION_UUID, pioInput, 1, 1, |
robo8080 | 5:61109bce11fe | 132 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 133 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
robo8080 | 5:61109bce11fe | 134 | |
robo8080 | 5:61109bce11fe | 135 | GattCharacteristic pwmConfigCharacteristic (KONASHI_PWM_CONFIG_UUID, pwmConfig, 1, 1, |
robo8080 | 5:61109bce11fe | 136 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 137 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 138 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 139 | GattCharacteristic pwmPeriodCharacteristic (KONASHI_PWM_PARAM_UUID, pwmPeriod, 5, 5, |
robo8080 | 5:61109bce11fe | 140 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 141 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 142 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 143 | GattCharacteristic pwmDutyCharacteristic (KONASHI_PWM_DUTY_UUID, pwmDuty, 5, 5, |
robo8080 | 5:61109bce11fe | 144 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 145 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 146 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 147 | |
robo8080 | 5:61109bce11fe | 148 | GattCharacteristic analogDriveCharacteristic (KONASHI_ANALOG_DRIVE_UUID, analogDrive, 1, 1, |
robo8080 | 5:61109bce11fe | 149 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 150 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 151 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 152 | GattCharacteristic analogRead0Characteristic (KONASHI_ANALOG_READ0_UUID, analogRead0, 2, 2, |
robo8080 | 5:61109bce11fe | 153 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 154 | GattCharacteristic analogRead1Characteristic (KONASHI_ANALOG_READ1_UUID, analogRead1, 2, 2, |
robo8080 | 5:61109bce11fe | 155 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 156 | GattCharacteristic analogRead2Characteristic (KONASHI_ANALOG_READ2_UUID, analogRead2, 2, 2, |
robo8080 | 5:61109bce11fe | 157 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 158 | |
robo8080 | 5:61109bce11fe | 159 | GattCharacteristic i2cConfigCharacteristic (KONASHI_I2C_CONFIG_UUID, i2cConfig, 1, 1, |
robo8080 | 5:61109bce11fe | 160 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 161 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 162 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 163 | GattCharacteristic i2cStartStopCharacteristic (KONASHI_I2C_START_STOP_UUID, i2cStartStop, 1, 1, |
robo8080 | 5:61109bce11fe | 164 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 165 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 166 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 167 | GattCharacteristic i2cWriteCharacteristic (KONASHI_I2C_WRITE_UUID, i2cWrite, 3, 20, |
robo8080 | 5:61109bce11fe | 168 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 169 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 170 | GattCharacteristic i2cReadParamCharacteristic (KONASHI_I2C_READ_PARAM_UIUD, i2cReadParam, 2, 2, |
robo8080 | 5:61109bce11fe | 171 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 172 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 173 | GattCharacteristic i2cReadCharacteristic (KONASHI_I2C_READ_UUID, i2cRead, 0, 20, |
robo8080 | 5:61109bce11fe | 174 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 175 | |
robo8080 | 5:61109bce11fe | 176 | GattCharacteristic uartConfigCharacteristic (KONASHI_UART_CONFIG_UUID, uartConfig, 1, 1, |
robo8080 | 5:61109bce11fe | 177 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 178 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 179 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 180 | GattCharacteristic uartBaudrateCharacteristic (KONASHI_UART_BAUDRATE_UUID, uartBaudrate, 1, 1, |
robo8080 | 5:61109bce11fe | 181 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 182 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 183 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 184 | GattCharacteristic uartTxCharacteristic (KONASHI_UART_TX_UUID, uartTx, 1, 1, |
robo8080 | 5:61109bce11fe | 185 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 186 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 187 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 188 | GattCharacteristic uartRxCharacteristic (KONASHI_UART_RX_NOTIFICATION_UUID, uartRx, 0, 20, |
robo8080 | 5:61109bce11fe | 189 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
Rohit Grover |
2:e060367b9024 | 190 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
robo8080 | 5:61109bce11fe | 191 | |
robo8080 | 5:61109bce11fe | 192 | GattCharacteristic hardwareRestCharacteristic (KONASHI_HARDWARE_RESET_UUID, hardwareRest, 1, 1, |
robo8080 | 5:61109bce11fe | 193 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
robo8080 | 5:61109bce11fe | 194 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
robo8080 | 5:61109bce11fe | 195 | GattCharacteristic hardwareLowBatCharacteristic (KONASHI_HARDWARE_LOW_BAT_NOTIFICATION_UUID, hardwareLowBat, 1, 1, |
robo8080 | 5:61109bce11fe | 196 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
robo8080 | 5:61109bce11fe | 197 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
robo8080 | 5:61109bce11fe | 198 | |
robo8080 | 6:9c9630cb454b | 199 | GattCharacteristic *konashiChars[] = {// &pioSettingCharacteristic, |
robo8080 | 6:9c9630cb454b | 200 | // &pioPullupCharacteristic, |
robo8080 | 5:61109bce11fe | 201 | &pioOutputCharacteristic, |
robo8080 | 6:9c9630cb454b | 202 | &pioInputCharacteristic, |
robo8080 | 11:c25480277877 | 203 | #if USE_KONASHI_PWM |
robo8080 | 5:61109bce11fe | 204 | &pwmConfigCharacteristic, |
robo8080 | 5:61109bce11fe | 205 | &pwmPeriodCharacteristic, |
robo8080 | 5:61109bce11fe | 206 | &pwmDutyCharacteristic, |
robo8080 | 11:c25480277877 | 207 | #endif |
robo8080 | 11:c25480277877 | 208 | #if USE_KONASHI_ANALOG |
robo8080 | 11:c25480277877 | 209 | &analogDriveCharacteristic, |
robo8080 | 5:61109bce11fe | 210 | &analogRead0Characteristic, |
robo8080 | 5:61109bce11fe | 211 | &analogRead1Characteristic, |
robo8080 | 11:c25480277877 | 212 | &analogRead2Characteristic, |
robo8080 | 11:c25480277877 | 213 | #endif |
robo8080 | 11:c25480277877 | 214 | #if USE_KONASHI_I2C |
robo8080 | 5:61109bce11fe | 215 | &i2cConfigCharacteristic, |
robo8080 | 5:61109bce11fe | 216 | &i2cStartStopCharacteristic, |
robo8080 | 5:61109bce11fe | 217 | &i2cWriteCharacteristic, |
robo8080 | 5:61109bce11fe | 218 | &i2cReadParamCharacteristic, |
robo8080 | 5:61109bce11fe | 219 | &i2cReadCharacteristic, |
robo8080 | 11:c25480277877 | 220 | #endif |
robo8080 | 11:c25480277877 | 221 | #if USE_KONASHI_UART |
robo8080 | 11:c25480277877 | 222 | &uartConfigCharacteristic, |
robo8080 | 6:9c9630cb454b | 223 | &uartBaudrateCharacteristic, |
robo8080 | 5:61109bce11fe | 224 | &uartTxCharacteristic, |
robo8080 | 5:61109bce11fe | 225 | &uartRxCharacteristic, |
robo8080 | 11:c25480277877 | 226 | #endif |
robo8080 | 11:c25480277877 | 227 | /* &hardwareRestCharacteristic, |
robo8080 | 5:61109bce11fe | 228 | &hardwareLowBatCharacteristic,*/ }; |
robo8080 | 5:61109bce11fe | 229 | GattService konashiService(KONASHI_SERVICE_UUID, konashiChars, sizeof(konashiChars) / sizeof(GattCharacteristic *)); |
robo8080 | 5:61109bce11fe | 230 | |
robo8080 | 5:61109bce11fe | 231 | // SYSTEM |
robo8080 | 5:61109bce11fe | 232 | static char systemId = 'A'; |
robo8080 | 5:61109bce11fe | 233 | GattCharacteristic systemID(GattCharacteristic::UUID_SYSTEM_ID_CHAR, (uint8_t *)systemId, sizeof(systemId), sizeof(systemId), |
robo8080 | 5:61109bce11fe | 234 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 235 | // MODEL |
robo8080 | 5:61109bce11fe | 236 | static char model[31] = "mbed HRM1017"; |
robo8080 | 5:61109bce11fe | 237 | GattCharacteristic modelID(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)model, strlen(model), strlen(model), |
robo8080 | 5:61109bce11fe | 238 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 239 | // Firmware |
robo8080 | 5:61109bce11fe | 240 | static char fwversion[31] = "1.0"; |
robo8080 | 5:61109bce11fe | 241 | GattCharacteristic fwChars(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)fwversion, strlen(fwversion), strlen(fwversion), |
robo8080 | 5:61109bce11fe | 242 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 243 | // Software |
robo8080 | 5:61109bce11fe | 244 | static char swversion[31] = "1.0"; |
robo8080 | 5:61109bce11fe | 245 | GattCharacteristic swChars(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR, (uint8_t *)swversion, strlen(swversion), strlen(swversion), |
robo8080 | 5:61109bce11fe | 246 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 247 | // Hardware |
robo8080 | 5:61109bce11fe | 248 | static char hwversion[31] = "1.0"; |
robo8080 | 5:61109bce11fe | 249 | GattCharacteristic hwChars(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, (uint8_t *)hwversion, strlen(hwversion), strlen(hwversion), |
robo8080 | 5:61109bce11fe | 250 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 251 | // Manufacturer |
robo8080 | 5:61109bce11fe | 252 | static char vendor[31] = "Test Company Inc."; |
robo8080 | 5:61109bce11fe | 253 | GattCharacteristic vendorChars(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)vendor, strlen(vendor), strlen(vendor), |
robo8080 | 5:61109bce11fe | 254 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 255 | // Serial number |
robo8080 | 5:61109bce11fe | 256 | static char serial[31] = "1234567890"; |
robo8080 | 5:61109bce11fe | 257 | GattCharacteristic serialChars(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)serial, strlen(serial), strlen(serial), |
robo8080 | 5:61109bce11fe | 258 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
robo8080 | 5:61109bce11fe | 259 | |
robo8080 | 5:61109bce11fe | 260 | //GattCharacteristic *informationChars[] = {&systemID, &modelID, &serialChars, &fwChars, &hwChars, &swChars, &vendorChars, }; |
robo8080 | 5:61109bce11fe | 261 | GattCharacteristic *informationChars[] = {&modelID, &fwChars, &hwChars, &swChars, &vendorChars, }; |
robo8080 | 5:61109bce11fe | 262 | GattService informationService(GattService::UUID_DEVICE_INFORMATION_SERVICE, informationChars, sizeof(informationChars) / sizeof(GattCharacteristic *)); |
robo8080 | 5:61109bce11fe | 263 | |
robo8080 | 5:61109bce11fe | 264 | |
robo8080 | 5:61109bce11fe | 265 | static uint8_t batteryLevel = 100; |
robo8080 | 5:61109bce11fe | 266 | GattCharacteristic batteryPercentage(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, (uint8_t *)batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), |
robo8080 | 5:61109bce11fe | 267 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
robo8080 | 5:61109bce11fe | 268 | GattCharacteristic *batteryChars[] = {&batteryPercentage }; |
robo8080 | 5:61109bce11fe | 269 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *)); |
robo8080 | 5:61109bce11fe | 270 | |
robo8080 | 10:1f4cfd4dd6c8 | 271 | static Gap::ConnectionParams_t connectionParams; |
robo8080 | 10:1f4cfd4dd6c8 | 272 | |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 273 | void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) |
robo8080 | 5:61109bce11fe | 274 | { |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 275 | //DEBUG("connected. Got handle %u\r\n", handle); |
robo8080 | 6:9c9630cb454b | 276 | led2 = 0; |
robo8080 | 11:c25480277877 | 277 | /* |
robo8080 | 10:1f4cfd4dd6c8 | 278 | connectionParams.slaveLatency = 1; |
robo8080 | 10:1f4cfd4dd6c8 | 279 | if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { |
robo8080 | 10:1f4cfd4dd6c8 | 280 | DEBUG("failed to update connection paramter\r\n"); |
robo8080 | 11:c25480277877 | 281 | }*/ |
robo8080 | 5:61109bce11fe | 282 | } |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 283 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *reason) |
yihui | 0:e910d9bb040f | 284 | { |
robo8080 | 10:1f4cfd4dd6c8 | 285 | DEBUG("Disconnected handle %u!\n\r", handle); |
Rohit Grover |
2:e060367b9024 | 286 | DEBUG("Restarting the advertising process\n\r"); |
robo8080 | 6:9c9630cb454b | 287 | led2 = 1; |
Rohit Grover |
2:e060367b9024 | 288 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 289 | } |
robo8080 | 6:9c9630cb454b | 290 | static void set_pwm_duty(int ch) |
robo8080 | 6:9c9630cb454b | 291 | { |
robo8080 | 6:9c9630cb454b | 292 | // uint32_t period; |
robo8080 | 6:9c9630cb454b | 293 | uint32_t duty; |
robo8080 | 6:9c9630cb454b | 294 | unsigned bitmask = 1; |
yihui | 0:e910d9bb040f | 295 | |
robo8080 | 6:9c9630cb454b | 296 | if(ch>=3){ |
robo8080 | 6:9c9630cb454b | 297 | return; |
robo8080 | 6:9c9630cb454b | 298 | } |
robo8080 | 6:9c9630cb454b | 299 | duty=pwm_duty[ch]; |
robo8080 | 6:9c9630cb454b | 300 | |
robo8080 | 6:9c9630cb454b | 301 | if(pwm_config & (bitmask << ch)) |
robo8080 | 6:9c9630cb454b | 302 | { |
robo8080 | 6:9c9630cb454b | 303 | pwm[ch].pulsewidth_us(duty); |
robo8080 | 6:9c9630cb454b | 304 | } |
robo8080 | 6:9c9630cb454b | 305 | else |
robo8080 | 6:9c9630cb454b | 306 | { |
robo8080 | 6:9c9630cb454b | 307 | pwm[ch].pulsewidth_us(0); |
robo8080 | 6:9c9630cb454b | 308 | } |
robo8080 | 6:9c9630cb454b | 309 | } |
robo8080 | 6:9c9630cb454b | 310 | static void set_pwm_period(int ch) |
robo8080 | 6:9c9630cb454b | 311 | { |
robo8080 | 6:9c9630cb454b | 312 | uint32_t period,duty; |
robo8080 | 6:9c9630cb454b | 313 | |
robo8080 | 6:9c9630cb454b | 314 | if(ch>=3){ |
robo8080 | 6:9c9630cb454b | 315 | return; |
robo8080 | 6:9c9630cb454b | 316 | } |
robo8080 | 6:9c9630cb454b | 317 | period=pwm_period[ch]; |
robo8080 | 6:9c9630cb454b | 318 | duty=pwm_duty[ch]; |
robo8080 | 6:9c9630cb454b | 319 | pwm[ch].period_us(period); |
robo8080 | 6:9c9630cb454b | 320 | pwm[ch].pulsewidth_us(duty); |
robo8080 | 6:9c9630cb454b | 321 | } |
robo8080 | 6:9c9630cb454b | 322 | static void set_pwm_config(uint8_t config) |
robo8080 | 6:9c9630cb454b | 323 | { |
robo8080 | 6:9c9630cb454b | 324 | pwm_config = config; |
robo8080 | 6:9c9630cb454b | 325 | for(int i = 0; i < 3 ; i++) |
robo8080 | 6:9c9630cb454b | 326 | { |
robo8080 | 6:9c9630cb454b | 327 | set_pwm_duty(i); |
robo8080 | 6:9c9630cb454b | 328 | } |
robo8080 | 6:9c9630cb454b | 329 | |
robo8080 | 6:9c9630cb454b | 330 | } |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 331 | void onDataWritten(const GattWriteCallbackParams *params) |
yihui | 0:e910d9bb040f | 332 | { |
robo8080 | 5:61109bce11fe | 333 | DEBUG("onDataWritten\n\r"); |
robo8080 | 11:c25480277877 | 334 | DEBUG("params->len = %d\n\r",params->len); |
robo8080 | 11:c25480277877 | 335 | uint16_t bytesRead = params->len; |
robo8080 | 11:c25480277877 | 336 | /*if (charHandle == pioSettingCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 10:1f4cfd4dd6c8 | 337 | DEBUG("pioSettingCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 338 | } else if (charHandle == pioPullupCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 10:1f4cfd4dd6c8 | 339 | DEBUG("pioPullupCharacteristic!\n\r"); |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 340 | } else */ if (params->handle == pioOutputCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 5:61109bce11fe | 341 | DEBUG("pioOutputCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 342 | // uint8_t getPioOut[10]; |
robo8080 | 11:c25480277877 | 343 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 344 | // ble.readCharacteristicValue(pioOutputCharacteristic.getValueAttribute().getHandle(), getPioOut, &bytesRead); |
robo8080 | 11:c25480277877 | 345 | // DEBUG("DATA: %d %d\n\r",getPioOut[0],pioOutput[0]); |
robo8080 | 11:c25480277877 | 346 | DEBUG("DATA: %d %d\n\r",params->data[0],pioOutput[0]); |
robo8080 | 11:c25480277877 | 347 | if(params->data[0]!=pioOutput[0]) { |
robo8080 | 11:c25480277877 | 348 | pioOutput[0]=params->data[0]; |
robo8080 | 11:c25480277877 | 349 | ioOut = pioOutput[0]; |
robo8080 | 5:61109bce11fe | 350 | } |
robo8080 | 11:c25480277877 | 351 | #if USE_KONASHI_PWM |
robo8080 | 11:c25480277877 | 352 | } else if (charHandle == pwmConfigCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 5:61109bce11fe | 353 | DEBUG("pwmConfigCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 354 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 355 | // ble.readCharacteristicValue(pwmConfigCharacteristic.getValueAttribute().getHandle(), pwmConfig, &bytesRead); |
robo8080 | 11:c25480277877 | 356 | pwmConfig[0]=params->data[0]; |
robo8080 | 6:9c9630cb454b | 357 | set_pwm_config(pwmConfig[0]); |
robo8080 | 11:c25480277877 | 358 | } else if (charHandle == pwmPeriodCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 5:61109bce11fe | 359 | DEBUG("pwmPeriodCharacteristic!\n\r"); |
robo8080 | 6:9c9630cb454b | 360 | uint32_t l; |
robo8080 | 11:c25480277877 | 361 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 362 | // ble.readCharacteristicValue(pwmPeriodCharacteristic.getValueAttribute().getHandle(), pwmPeriod, &bytesRead); |
robo8080 | 11:c25480277877 | 363 | if (bytesRead < sizeof(pwmPeriod)) { |
robo8080 | 11:c25480277877 | 364 | memcpy(pwmPeriod, params->data, bytesRead); |
robo8080 | 11:c25480277877 | 365 | } |
robo8080 | 6:9c9630cb454b | 366 | int i=pwmPeriod[0]; |
robo8080 | 6:9c9630cb454b | 367 | l =((uint32_t)pwmPeriod[1])<<24; |
robo8080 | 6:9c9630cb454b | 368 | l|=((uint32_t)pwmPeriod[2])<<16; |
robo8080 | 6:9c9630cb454b | 369 | l|=((uint32_t)pwmPeriod[3])<< 8; |
robo8080 | 6:9c9630cb454b | 370 | l|=((uint32_t)pwmPeriod[4])<< 0; |
robo8080 | 6:9c9630cb454b | 371 | if(i<3){ |
robo8080 | 6:9c9630cb454b | 372 | pwmPeriod[i]=l; |
robo8080 | 6:9c9630cb454b | 373 | set_pwm_period(i); |
robo8080 | 6:9c9630cb454b | 374 | } |
robo8080 | 11:c25480277877 | 375 | } else if (charHandle == pwmDutyCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 5:61109bce11fe | 376 | DEBUG("pwmDutyCharacteristic!\n\r"); |
robo8080 | 6:9c9630cb454b | 377 | uint32_t l; |
robo8080 | 11:c25480277877 | 378 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 379 | // ble.readCharacteristicValue(pwmDutyCharacteristic.getValueAttribute().getHandle(), pwmDuty, &bytesRead); |
robo8080 | 11:c25480277877 | 380 | if (bytesRead < sizeof(pwmDuty)) { |
robo8080 | 11:c25480277877 | 381 | memcpy(pwmDuty, params->data, bytesRead); |
robo8080 | 11:c25480277877 | 382 | } |
robo8080 | 6:9c9630cb454b | 383 | int i=pwmDuty[0]; |
robo8080 | 6:9c9630cb454b | 384 | l =((uint32_t)pwmDuty[1])<<24; |
robo8080 | 6:9c9630cb454b | 385 | l|=((uint32_t)pwmDuty[2])<<16; |
robo8080 | 6:9c9630cb454b | 386 | l|=((uint32_t)pwmDuty[3])<< 8; |
robo8080 | 6:9c9630cb454b | 387 | l|=((uint32_t)pwmDuty[4])<< 0; |
robo8080 | 6:9c9630cb454b | 388 | if(i<3){ |
robo8080 | 6:9c9630cb454b | 389 | pwm_duty[i]=l; |
robo8080 | 6:9c9630cb454b | 390 | set_pwm_duty(i); |
robo8080 | 6:9c9630cb454b | 391 | } |
robo8080 | 11:c25480277877 | 392 | #endif //USE_KONASHI_PWM |
robo8080 | 11:c25480277877 | 393 | /* } else if (charHandle == analogDriveCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 10:1f4cfd4dd6c8 | 394 | DEBUG("analogDriveCharacteristic!\n\r");*/ |
robo8080 | 11:c25480277877 | 395 | #if USE_KONASHI_I2C |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 396 | } else if (params->handle == i2cConfigCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 5:61109bce11fe | 397 | DEBUG("i2cConfigCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 398 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 399 | // ble.readCharacteristicValue(i2cConfigCharacteristic.getValueAttribute().getHandle(), i2cConfig, &bytesRead); |
robo8080 | 9:a6ae6f8c95b7 | 400 | //write 0:OFF 1:100kHz 2:400kHz |
robo8080 | 9:a6ae6f8c95b7 | 401 | if(bytesRead){ |
robo8080 | 11:c25480277877 | 402 | i2cConfig[0] = params->data[0]; |
robo8080 | 9:a6ae6f8c95b7 | 403 | i2c_config=i2cConfig[0]; |
robo8080 | 9:a6ae6f8c95b7 | 404 | if(i2cConfig[0] == 1){ |
robo8080 | 9:a6ae6f8c95b7 | 405 | i2c.frequency(100000); |
robo8080 | 9:a6ae6f8c95b7 | 406 | } else if(i2cConfig[0] == 2){ |
robo8080 | 9:a6ae6f8c95b7 | 407 | i2c.frequency(400000); |
robo8080 | 9:a6ae6f8c95b7 | 408 | } |
robo8080 | 9:a6ae6f8c95b7 | 409 | } |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 410 | } else if (params->handle == i2cStartStopCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 9:a6ae6f8c95b7 | 411 | DEBUG("i2cStartStopCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 412 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 413 | // ble.readCharacteristicValue(i2cStartStopCharacteristic.getValueAttribute().getHandle(), i2cStartStop, &bytesRead); |
robo8080 | 9:a6ae6f8c95b7 | 414 | //write 0:stop 1:start condition 2:repeated start condition |
robo8080 | 9:a6ae6f8c95b7 | 415 | if(bytesRead){ |
robo8080 | 11:c25480277877 | 416 | i2cStartStop[0] = params->data[0]; |
robo8080 | 9:a6ae6f8c95b7 | 417 | if(i2cStartStop[0] == 0) { |
robo8080 | 10:1f4cfd4dd6c8 | 418 | i2c_repeated = false; |
robo8080 | 10:1f4cfd4dd6c8 | 419 | // i2c.stop(); |
robo8080 | 10:1f4cfd4dd6c8 | 420 | } else if(i2cStartStop[0] == 1) { |
robo8080 | 10:1f4cfd4dd6c8 | 421 | i2c_repeated = false; |
robo8080 | 10:1f4cfd4dd6c8 | 422 | // i2c.start(); |
robo8080 | 10:1f4cfd4dd6c8 | 423 | } else if(i2cStartStop[0] == 2) { |
robo8080 | 10:1f4cfd4dd6c8 | 424 | i2c_repeated = true; |
robo8080 | 9:a6ae6f8c95b7 | 425 | } else { |
robo8080 | 10:1f4cfd4dd6c8 | 426 | i2c_repeated = false; |
robo8080 | 9:a6ae6f8c95b7 | 427 | } |
robo8080 | 9:a6ae6f8c95b7 | 428 | } |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 429 | } else if (params->handle == i2cWriteCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 9:a6ae6f8c95b7 | 430 | DEBUG("i2cWriteCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 431 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 432 | // ble.readCharacteristicValue(i2cWriteCharacteristic.getValueAttribute().getHandle(), i2cWrite, &bytesRead); |
robo8080 | 11:c25480277877 | 433 | if (bytesRead < sizeof(i2cWrite)) { |
robo8080 | 11:c25480277877 | 434 | memcpy(i2cWrite, params->data, bytesRead); |
robo8080 | 11:c25480277877 | 435 | } |
robo8080 | 9:a6ae6f8c95b7 | 436 | //write +0:write length(from next byte) +1:(addr<<1)|(R:1/W:0) +2~:Write bytes |
robo8080 | 10:1f4cfd4dd6c8 | 437 | DEBUG("DATA: %d %d %d\n\r",i2cWrite[0],i2cWrite[1],i2cWrite[2]); |
robo8080 | 9:a6ae6f8c95b7 | 438 | if(bytesRead>=3){ |
robo8080 | 10:1f4cfd4dd6c8 | 439 | i2c.write((int)(i2cWrite[1]),(char *)&i2cWrite[2],(int)i2cWrite[0],i2c_repeated); |
robo8080 | 9:a6ae6f8c95b7 | 440 | } |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 441 | } else if (params->handle == i2cReadParamCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 5:61109bce11fe | 442 | DEBUG("i2cReadParamCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 443 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 444 | // ble.readCharacteristicValue(i2cReadParamCharacteristic.getValueAttribute().getHandle(), i2cReadParam, &bytesRead); |
robo8080 | 11:c25480277877 | 445 | if (bytesRead < sizeof(i2cReadParam)) { |
robo8080 | 11:c25480277877 | 446 | memcpy(i2cReadParam, params->data, bytesRead); |
robo8080 | 11:c25480277877 | 447 | } |
robo8080 | 9:a6ae6f8c95b7 | 448 | //write +0:read length(from next byte) +1:(addr<<1)|(R:1/W:0) |
robo8080 | 9:a6ae6f8c95b7 | 449 | if(bytesRead>=2){ |
robo8080 | 9:a6ae6f8c95b7 | 450 | i2c_recvlen=i2cReadParam[0]; |
robo8080 | 10:1f4cfd4dd6c8 | 451 | if(20>=i2c_recvlen){ |
robo8080 | 10:1f4cfd4dd6c8 | 452 | i2c.read((int)(i2cReadParam[1]),(char *)&i2c_recvbuf[0],(int)i2cReadParam[0],i2c_repeated); |
robo8080 | 10:1f4cfd4dd6c8 | 453 | for(int i=0;i<i2c_recvlen;i++){ |
robo8080 | 10:1f4cfd4dd6c8 | 454 | i2cRead[i]=i2c_recvbuf[i]; |
robo8080 | 10:1f4cfd4dd6c8 | 455 | } |
robo8080 | 11:c25480277877 | 456 | ble.updateCharacteristicValue(i2cReadCharacteristic.getValueAttribute().getHandle(), (uint8_t*)&i2cRead, i2c_recvlen); |
robo8080 | 9:a6ae6f8c95b7 | 457 | } |
robo8080 | 9:a6ae6f8c95b7 | 458 | } |
robo8080 | 11:c25480277877 | 459 | // ble.updateCharacteristicValue(i2cReadCharacteristic.getValueAttribute().getHandle(), (uint8_t*)&i2cRead, i2c_recvlen); |
robo8080 | 11:c25480277877 | 460 | #endif //USE_KONASHI_I2C |
robo8080 | 11:c25480277877 | 461 | #if USE_KONASHI_UART |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 462 | } else if (params->handle == uartConfigCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 5:61109bce11fe | 463 | DEBUG("uartConfigCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 464 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 465 | // ble.readCharacteristicValue(uartConfigCharacteristic.getValueAttribute().getHandle(), uartConfig, &bytesRead); |
robo8080 | 11:c25480277877 | 466 | if (bytesRead < sizeof(uartConfig)) { |
robo8080 | 11:c25480277877 | 467 | memcpy(uartConfig, params->data, bytesRead); |
robo8080 | 11:c25480277877 | 468 | } |
robo8080 | 9:a6ae6f8c95b7 | 469 | if(bytesRead>0){ |
robo8080 | 9:a6ae6f8c95b7 | 470 | if(uartConfig[0]) { |
robo8080 | 9:a6ae6f8c95b7 | 471 | pc.baud(uart_baudrate*240); |
robo8080 | 9:a6ae6f8c95b7 | 472 | } |
robo8080 | 9:a6ae6f8c95b7 | 473 | } |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 474 | } else if (params->handle == uartBaudrateCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 5:61109bce11fe | 475 | DEBUG("uartBaudrateCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 476 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 477 | // ble.readCharacteristicValue(uartBaudrateCharacteristic.getValueAttribute().getHandle(), uartBaudrate, &bytesRead); |
robo8080 | 11:c25480277877 | 478 | if (bytesRead < sizeof(uartBaudrate)) { |
robo8080 | 11:c25480277877 | 479 | memcpy(uartBaudrate, params->data, bytesRead); |
robo8080 | 11:c25480277877 | 480 | } |
robo8080 | 9:a6ae6f8c95b7 | 481 | if(bytesRead>=2){ |
robo8080 | 9:a6ae6f8c95b7 | 482 | uart_baudrate=((uint16_t)(uartBaudrate[0]))<<8; |
robo8080 | 9:a6ae6f8c95b7 | 483 | uart_baudrate+=uartBaudrate[1]; |
robo8080 | 9:a6ae6f8c95b7 | 484 | } |
mbed_tw_hoehoe | 12:dd866bd5eaf9 | 485 | } else if (params->handle == uartTxCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 6:9c9630cb454b | 486 | DEBUG("uartTxCharacteristic!\n\r"); |
robo8080 | 11:c25480277877 | 487 | // uint16_t bytesRead; |
robo8080 | 11:c25480277877 | 488 | // ble.readCharacteristicValue(uartTxCharacteristic.getValueAttribute().getHandle(), uartTx, &bytesRead); |
robo8080 | 11:c25480277877 | 489 | if (bytesRead < sizeof(uartTx)) { |
robo8080 | 11:c25480277877 | 490 | memcpy(uartTx, params->data, bytesRead); |
robo8080 | 11:c25480277877 | 491 | } |
robo8080 | 6:9c9630cb454b | 492 | DEBUG("ECHO: %s\n\r", (char *)uartTx); |
robo8080 | 9:a6ae6f8c95b7 | 493 | #if NEED_CONSOLE_OUTPUT |
robo8080 | 11:c25480277877 | 494 | ble.updateCharacteristicValue(uartRxCharacteristic.getValueAttribute().getHandle(), uartTx, bytesRead); |
robo8080 | 9:a6ae6f8c95b7 | 495 | #else |
robo8080 | 11:c25480277877 | 496 | pc.putc(uartTx[0]); |
robo8080 | 11:c25480277877 | 497 | #endif //NEED_CONSOLE_OUTPUT |
robo8080 | 11:c25480277877 | 498 | #endif //USE_KONASHI_UART |
robo8080 | 11:c25480277877 | 499 | /* } else if (charHandle == hardwareRestCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 6:9c9630cb454b | 500 | DEBUG("hardwareRestCharacteristic!\n\r");*/ |
robo8080 | 5:61109bce11fe | 501 | } |
Rohit Grover |
2:e060367b9024 | 502 | } |
robo8080 | 10:1f4cfd4dd6c8 | 503 | static volatile bool pioInputUpdatesEnable = false; |
robo8080 | 10:1f4cfd4dd6c8 | 504 | static volatile bool uartRxUpdatesEnable = false; |
robo8080 | 10:1f4cfd4dd6c8 | 505 | void onUpdatesEnabled(uint16_t charHandle) |
robo8080 | 10:1f4cfd4dd6c8 | 506 | { |
robo8080 | 10:1f4cfd4dd6c8 | 507 | DEBUG("onUpdatesEnabled handle %u!\n\r", charHandle); |
robo8080 | 10:1f4cfd4dd6c8 | 508 | // led2 = 1; |
robo8080 | 11:c25480277877 | 509 | if (charHandle == pioInputCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 10:1f4cfd4dd6c8 | 510 | DEBUG("pioInputCharacteristic!\n\r"); |
robo8080 | 10:1f4cfd4dd6c8 | 511 | pioInputUpdatesEnable = true; |
robo8080 | 11:c25480277877 | 512 | #if USE_KONASHI_UART |
robo8080 | 11:c25480277877 | 513 | } else if (charHandle == uartRxCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 10:1f4cfd4dd6c8 | 514 | DEBUG("uartRxCharacteristic!\n\r"); |
robo8080 | 10:1f4cfd4dd6c8 | 515 | uartRxUpdatesEnable = true; |
robo8080 | 11:c25480277877 | 516 | #endif |
robo8080 | 10:1f4cfd4dd6c8 | 517 | } |
robo8080 | 10:1f4cfd4dd6c8 | 518 | } |
robo8080 | 10:1f4cfd4dd6c8 | 519 | void onUpdatesDisabled(uint16_t charHandle) |
robo8080 | 10:1f4cfd4dd6c8 | 520 | { |
robo8080 | 10:1f4cfd4dd6c8 | 521 | DEBUG("onUpdatesDisabled handle %u!\n\r", charHandle); |
robo8080 | 10:1f4cfd4dd6c8 | 522 | // led2 = 1; |
robo8080 | 11:c25480277877 | 523 | if (charHandle == pioInputCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 10:1f4cfd4dd6c8 | 524 | DEBUG("pioInputCharacteristic!\n\r"); |
robo8080 | 10:1f4cfd4dd6c8 | 525 | pioInputUpdatesEnable = false; |
robo8080 | 11:c25480277877 | 526 | #if USE_KONASHI_UART |
robo8080 | 11:c25480277877 | 527 | } else if (charHandle == uartRxCharacteristic.getValueAttribute().getHandle()) { |
robo8080 | 10:1f4cfd4dd6c8 | 528 | DEBUG("uartRxCharacteristic!\n\r"); |
robo8080 | 10:1f4cfd4dd6c8 | 529 | uartRxUpdatesEnable = false; |
robo8080 | 11:c25480277877 | 530 | #endif |
robo8080 | 10:1f4cfd4dd6c8 | 531 | } |
robo8080 | 10:1f4cfd4dd6c8 | 532 | } |
robo8080 | 10:1f4cfd4dd6c8 | 533 | static volatile bool triggerIoPolling = false; |
robo8080 | 10:1f4cfd4dd6c8 | 534 | uint8_t ledUpdateCounter = 0; |
robo8080 | 10:1f4cfd4dd6c8 | 535 | uint8_t batteryLevelUpdateCounter = 0; |
Rohit Grover |
2:e060367b9024 | 536 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 537 | { |
robo8080 | 10:1f4cfd4dd6c8 | 538 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
robo8080 | 10:1f4cfd4dd6c8 | 539 | * heavy-weight sensor polling from the main thread. */ |
robo8080 | 10:1f4cfd4dd6c8 | 540 | triggerIoPolling = true; |
Rohit Grover |
2:e060367b9024 | 541 | } |
yihui | 0:e910d9bb040f | 542 | |
yihui | 0:e910d9bb040f | 543 | int main(void) |
yihui | 0:e910d9bb040f | 544 | { |
Rohit Grover |
2:e060367b9024 | 545 | led1 = 1; |
robo8080 | 6:9c9630cb454b | 546 | led2 = 1; |
robo8080 | 10:1f4cfd4dd6c8 | 547 | set_pwm_config(0); |
robo8080 | 10:1f4cfd4dd6c8 | 548 | ioIn.mode(PullUp); |
robo8080 | 10:1f4cfd4dd6c8 | 549 | // ioIn.mode(PullNone); |
robo8080 | 10:1f4cfd4dd6c8 | 550 | // ioIn.mode(PullDown); |
Rohit Grover |
2:e060367b9024 | 551 | Ticker ticker; |
robo8080 | 10:1f4cfd4dd6c8 | 552 | ticker.attach(periodicCallback, 0.1); |
yihui | 0:e910d9bb040f | 553 | |
Rohit Grover |
2:e060367b9024 | 554 | DEBUG("Initialising the nRF51822\n\r"); |
Rohit Grover |
2:e060367b9024 | 555 | ble.init(); |
robo8080 | 11:c25480277877 | 556 | ble.onConnection(onConnectionCallback); |
Rohit Grover |
2:e060367b9024 | 557 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
2:e060367b9024 | 558 | ble.onDataWritten(onDataWritten); |
robo8080 | 10:1f4cfd4dd6c8 | 559 | ble.onUpdatesEnabled(onUpdatesEnabled); |
robo8080 | 10:1f4cfd4dd6c8 | 560 | ble.onUpdatesDisabled(onUpdatesDisabled); |
robo8080 | 10:1f4cfd4dd6c8 | 561 | |
robo8080 | 11:c25480277877 | 562 | // ble.getPreferredConnectionParams(&connectionParams); |
yihui | 0:e910d9bb040f | 563 | |
Rohit Grover |
2:e060367b9024 | 564 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 565 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
robo8080 | 10:1f4cfd4dd6c8 | 566 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); |
robo8080 | 8:a62b8f7d5dcf | 567 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Rohit Grover |
2:e060367b9024 | 568 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
yihui | 0:e910d9bb040f | 569 | |
Rohit Grover |
2:e060367b9024 | 570 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
Rohit Grover |
2:e060367b9024 | 571 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 572 | |
robo8080 | 5:61109bce11fe | 573 | // ble.addService(batteryService); |
robo8080 | 10:1f4cfd4dd6c8 | 574 | // ble.addService(informationService); |
robo8080 | 6:9c9630cb454b | 575 | ble.addService(konashiService); |
yihui | 0:e910d9bb040f | 576 | |
Rohit Grover |
2:e060367b9024 | 577 | while (true) { |
robo8080 | 10:1f4cfd4dd6c8 | 578 | if (triggerIoPolling) { |
robo8080 | 10:1f4cfd4dd6c8 | 579 | triggerIoPolling = false; |
robo8080 | 10:1f4cfd4dd6c8 | 580 | ledUpdateCounter++; |
robo8080 | 10:1f4cfd4dd6c8 | 581 | if(ledUpdateCounter >= 10) { //1sec |
robo8080 | 10:1f4cfd4dd6c8 | 582 | ledUpdateCounter = 0; |
robo8080 | 10:1f4cfd4dd6c8 | 583 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
robo8080 | 10:1f4cfd4dd6c8 | 584 | // /* Update the battery measurement */ |
robo8080 | 10:1f4cfd4dd6c8 | 585 | // batteryLevel--; |
robo8080 | 10:1f4cfd4dd6c8 | 586 | // if (batteryLevel == 1) { |
robo8080 | 10:1f4cfd4dd6c8 | 587 | // batteryLevel = 100; |
robo8080 | 10:1f4cfd4dd6c8 | 588 | // } |
robo8080 | 10:1f4cfd4dd6c8 | 589 | // ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel)); |
robo8080 | 10:1f4cfd4dd6c8 | 590 | // ble.updateCharacteristicValue(hardwareLowBatCharacteristic.getHandle(), &batteryLevel, sizeof(batteryLevel)); |
robo8080 | 10:1f4cfd4dd6c8 | 591 | } |
robo8080 | 10:1f4cfd4dd6c8 | 592 | if(ble.getGapState().connected) { |
robo8080 | 10:1f4cfd4dd6c8 | 593 | /* Do blocking calls or whatever is necessary for sensor polling. */ |
robo8080 | 10:1f4cfd4dd6c8 | 594 | if(pioInputUpdatesEnable) { |
robo8080 | 10:1f4cfd4dd6c8 | 595 | uint8_t io = ioIn; |
robo8080 | 10:1f4cfd4dd6c8 | 596 | if(pioInput[0] != io) { |
robo8080 | 10:1f4cfd4dd6c8 | 597 | pioInput[0] = io; |
robo8080 | 11:c25480277877 | 598 | ble.updateCharacteristicValue(pioInputCharacteristic.getValueAttribute().getHandle(), pioInput, 1); |
robo8080 | 10:1f4cfd4dd6c8 | 599 | } |
robo8080 | 10:1f4cfd4dd6c8 | 600 | } |
robo8080 | 11:c25480277877 | 601 | #if USE_KONASHI_UART |
robo8080 | 11:c25480277877 | 602 | #if !NEED_CONSOLE_OUTPUT |
robo8080 | 10:1f4cfd4dd6c8 | 603 | if(uartRxUpdatesEnable) { |
robo8080 | 10:1f4cfd4dd6c8 | 604 | if(pc.readable()) { |
robo8080 | 10:1f4cfd4dd6c8 | 605 | char data; |
robo8080 | 10:1f4cfd4dd6c8 | 606 | data = pc.getc(); |
robo8080 | 11:c25480277877 | 607 | ble.updateCharacteristicValue(uartRxCharacteristic.getValueAttribute().getHandle(), (uint8_t*)&data, 1); |
robo8080 | 10:1f4cfd4dd6c8 | 608 | } |
robo8080 | 10:1f4cfd4dd6c8 | 609 | } |
robo8080 | 11:c25480277877 | 610 | #endif |
robo8080 | 11:c25480277877 | 611 | #endif |
robo8080 | 10:1f4cfd4dd6c8 | 612 | } |
robo8080 | 11:c25480277877 | 613 | } |
robo8080 | 11:c25480277877 | 614 | // else { |
robo8080 | 10:1f4cfd4dd6c8 | 615 | ble.waitForEvent(); |
robo8080 | 11:c25480277877 | 616 | // } |
yihui | 0:e910d9bb040f | 617 | } |
yihui | 0:e910d9bb040f | 618 | } |