You can control from iPhone and put some command. h=show commands help, v=show TY51822r3 VDD data, t=show chip temperature, q=you can make deep sleep condition.
Dependencies: BLE_API mbed nRF51822 nRF51_Vdd nRF51_WakeUp nRF51_LowPwr
Fork of BLE_LoopbackUART by
Please refer follows.
/users/kenjiArai/notebook/ty51822r3-current-consumption-using-nrf51_wakeup-l/#
main.cpp@16:c8f4e56fa239, 2016-06-11 (annotated)
- Committer:
- kenjiArai
- Date:
- Sat Jun 11 04:25:54 2016 +0000
- Revision:
- 16:c8f4e56fa239
- Parent:
- 15:d7764da3f37c
put function table
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 | |
kenjiArai | 14:f9b4d659d717 | 17 | /* |
kenjiArai | 14:f9b4d659d717 | 18 | * /////// Tested on Switch Science mbed TY51822r3 /////// |
kenjiArai | 14:f9b4d659d717 | 19 | * Modified by Kenji Arai |
kenjiArai | 14:f9b4d659d717 | 20 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 14:f9b4d659d717 | 21 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 14:f9b4d659d717 | 22 | * |
kenjiArai | 15:d7764da3f37c | 23 | * Started: March 7th, 2016 |
kenjiArai | 15:d7764da3f37c | 24 | * Revised: June 11th, 2016 |
kenjiArai | 14:f9b4d659d717 | 25 | * |
kenjiArai | 14:f9b4d659d717 | 26 | * Original program: |
kenjiArai | 14:f9b4d659d717 | 27 | * BLE_LoopbackUART |
kenjiArai | 14:f9b4d659d717 | 28 | * https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_LoopbackUART/ |
kenjiArai | 14:f9b4d659d717 | 29 | * Tested Controller Device: |
kenjiArai | 14:f9b4d659d717 | 30 | * iPhone6 nRF UART by Nordic |
kenjiArai | 14:f9b4d659d717 | 31 | * https://itunes.apple.com/us/app/nrf-uart/id614594903?mt=8 |
kenjiArai | 14:f9b4d659d717 | 32 | * |
kenjiArai | 14:f9b4d659d717 | 33 | */ |
yihui | 0:e910d9bb040f | 34 | |
kenjiArai | 14:f9b4d659d717 | 35 | // Include --------------------------------------------------------------------------------------- |
kenjiArai | 14:f9b4d659d717 | 36 | #include <string.h> |
kenjiArai | 14:f9b4d659d717 | 37 | #include "mbed.h" |
kenjiArai | 14:f9b4d659d717 | 38 | #include "BLE.h" |
kenjiArai | 14:f9b4d659d717 | 39 | #include "UARTService.h" |
kenjiArai | 14:f9b4d659d717 | 40 | #include "nRF51_Vdd.h" |
kenjiArai | 14:f9b4d659d717 | 41 | #include "nRF51_WakeUp.h" |
kenjiArai | 15:d7764da3f37c | 42 | #include "nRF51_lowpwr.h" |
yihui | 0:e910d9bb040f | 43 | |
kenjiArai | 14:f9b4d659d717 | 44 | // Definition ------------------------------------------------------------------------------------ |
kenjiArai | 16:c8f4e56fa239 | 45 | // Before using this function, please specify your program are used following functions or not. |
kenjiArai | 16:c8f4e56fa239 | 46 | #define USE_DEVICE_STDIO_MESSAGES 0 // printf |
kenjiArai | 16:c8f4e56fa239 | 47 | #define USE_DEVICE_SERIAL 0 // Serial or DEBUG & etc. |
kenjiArai | 16:c8f4e56fa239 | 48 | #define USE_DEVICE_I2C 0 // Sensors with I2C, LCD, EEPROM, Driver chips & etc. |
kenjiArai | 16:c8f4e56fa239 | 49 | #define USE_DEVICE_SPI 0 // Sensors with SOI, LCD, EEPROM, Driver chips & etc. |
kenjiArai | 16:c8f4e56fa239 | 50 | #define USE_DEVICE_SPISLAVE 0 // Communication with master vis SPI |
kenjiArai | 16:c8f4e56fa239 | 51 | #define USE_DEVICE_PWMOUT 0 // PWM duty output, Serve & etc. |
kenjiArai | 16:c8f4e56fa239 | 52 | #define USE_DEVICE_ANALOGIN 0 // Analog adc |
kenjiArai | 16:c8f4e56fa239 | 53 | |
kenjiArai | 15:d7764da3f37c | 54 | #if USE_DEVICE_STDIO_MESSAGES // See disable_cpu_functions.h |
rgrover1 | 6:e0fc9072e853 | 55 | #define DEBUG(...) { printf(__VA_ARGS__); } |
yihui | 0:e910d9bb040f | 56 | #else |
kenjiArai | 15:d7764da3f37c | 57 | #define DEBUG(...) |
kenjiArai | 15:d7764da3f37c | 58 | #endif |
yihui | 0:e910d9bb040f | 59 | |
kenjiArai | 14:f9b4d659d717 | 60 | #define HIGH_PWR 1 |
kenjiArai | 14:f9b4d659d717 | 61 | #define TXRX_BUF_LEN 22 // send & receive number = 20 |
kenjiArai | 14:f9b4d659d717 | 62 | #define PWR_RANG_NO 8 |
yihui | 0:e910d9bb040f | 63 | |
kenjiArai | 14:f9b4d659d717 | 64 | enum TX_Content { |
kenjiArai | 14:f9b4d659d717 | 65 | TX_VDD = 1, |
kenjiArai | 14:f9b4d659d717 | 66 | TX_TEMP, |
kenjiArai | 14:f9b4d659d717 | 67 | TX_PWR_UP, |
kenjiArai | 14:f9b4d659d717 | 68 | TX_PWR_DWN, |
kenjiArai | 14:f9b4d659d717 | 69 | TX_QUIT, |
kenjiArai | 14:f9b4d659d717 | 70 | TX_HELP |
kenjiArai | 14:f9b4d659d717 | 71 | }; |
yihui | 0:e910d9bb040f | 72 | |
kenjiArai | 14:f9b4d659d717 | 73 | // Object ---------------------------------------------------------------------------------------- |
kenjiArai | 14:f9b4d659d717 | 74 | BLEDevice ble; |
kenjiArai | 14:f9b4d659d717 | 75 | DigitalOut myled(LED2); |
kenjiArai | 14:f9b4d659d717 | 76 | InterruptIn wake_up_sw(P0_1); // If you change P0_1, you also need to change PORT_OUTPUT. |
kenjiArai | 14:f9b4d659d717 | 77 | nRF51_WakeUp wakeup(LED1, P0_0); |
kenjiArai | 15:d7764da3f37c | 78 | nRF51_Vdd vdd(3.0f, 2.2f, ONLY4VDD); |
kenjiArai | 14:f9b4d659d717 | 79 | UARTService *uartServicePtr; |
kenjiArai | 14:f9b4d659d717 | 80 | Ticker ticker; |
kenjiArai | 15:d7764da3f37c | 81 | #if USE_DEVICE_STDIO_MESSAGES |
kenjiArai | 14:f9b4d659d717 | 82 | Serial pc(P0_4,P0_0); |
kenjiArai | 14:f9b4d659d717 | 83 | #endif |
kenjiArai | 14:f9b4d659d717 | 84 | |
kenjiArai | 14:f9b4d659d717 | 85 | // ROM / Constant data --------------------------------------------------------------------------- |
kenjiArai | 14:f9b4d659d717 | 86 | const char *deviceName = "JH1PJL"; |
kenjiArai | 14:f9b4d659d717 | 87 | const uint8_t pwr_range[PWR_RANG_NO] = |
kenjiArai | 14:f9b4d659d717 | 88 | { |
kenjiArai | 14:f9b4d659d717 | 89 | RADIO_TXPOWER_TXPOWER_Neg30dBm,/*!< -30dBm. */ |
kenjiArai | 14:f9b4d659d717 | 90 | RADIO_TXPOWER_TXPOWER_Neg20dBm,/*!< -20dBm. */ |
kenjiArai | 14:f9b4d659d717 | 91 | RADIO_TXPOWER_TXPOWER_Neg16dBm,/*!< -16dBm. */ |
kenjiArai | 14:f9b4d659d717 | 92 | RADIO_TXPOWER_TXPOWER_Neg12dBm,/*!< -12dBm. */ |
kenjiArai | 14:f9b4d659d717 | 93 | RADIO_TXPOWER_TXPOWER_Neg8dBm, /*!< -8dBm. */ |
kenjiArai | 14:f9b4d659d717 | 94 | RADIO_TXPOWER_TXPOWER_Neg4dBm, /*!< -4dBm. */ |
kenjiArai | 14:f9b4d659d717 | 95 | RADIO_TXPOWER_TXPOWER_0dBm, /*!< 0dBm. */ |
kenjiArai | 14:f9b4d659d717 | 96 | RADIO_TXPOWER_TXPOWER_Pos4dBm, /*!< +4dBm. */ |
kenjiArai | 14:f9b4d659d717 | 97 | }; |
kenjiArai | 16:c8f4e56fa239 | 98 | const nRF51_LOWPWR_TypeDef lowpwr_table = |
kenjiArai | 16:c8f4e56fa239 | 99 | { |
kenjiArai | 16:c8f4e56fa239 | 100 | #if USE_DEVICE_STDIO_MESSAGES |
kenjiArai | 16:c8f4e56fa239 | 101 | true, |
kenjiArai | 16:c8f4e56fa239 | 102 | #else |
kenjiArai | 16:c8f4e56fa239 | 103 | false, |
kenjiArai | 16:c8f4e56fa239 | 104 | #endif |
kenjiArai | 16:c8f4e56fa239 | 105 | #if USE_DEVICE_SERIAL |
kenjiArai | 16:c8f4e56fa239 | 106 | true, |
kenjiArai | 16:c8f4e56fa239 | 107 | #else |
kenjiArai | 16:c8f4e56fa239 | 108 | false, |
kenjiArai | 16:c8f4e56fa239 | 109 | #endif |
kenjiArai | 16:c8f4e56fa239 | 110 | #if USE_DEVICE_I2C |
kenjiArai | 16:c8f4e56fa239 | 111 | true, |
kenjiArai | 16:c8f4e56fa239 | 112 | #else |
kenjiArai | 16:c8f4e56fa239 | 113 | false, |
kenjiArai | 16:c8f4e56fa239 | 114 | #endif |
kenjiArai | 16:c8f4e56fa239 | 115 | #if USE_DEVICE_SPI |
kenjiArai | 16:c8f4e56fa239 | 116 | true, |
kenjiArai | 16:c8f4e56fa239 | 117 | #else |
kenjiArai | 16:c8f4e56fa239 | 118 | false, |
kenjiArai | 16:c8f4e56fa239 | 119 | #endif |
kenjiArai | 16:c8f4e56fa239 | 120 | #if USE_DEVICE_SPISLAVE |
kenjiArai | 16:c8f4e56fa239 | 121 | true, |
kenjiArai | 16:c8f4e56fa239 | 122 | #else |
kenjiArai | 16:c8f4e56fa239 | 123 | false, |
kenjiArai | 16:c8f4e56fa239 | 124 | #endif |
kenjiArai | 16:c8f4e56fa239 | 125 | #if USE_DEVICE_PWMOUT |
kenjiArai | 16:c8f4e56fa239 | 126 | true, |
kenjiArai | 16:c8f4e56fa239 | 127 | #else |
kenjiArai | 16:c8f4e56fa239 | 128 | false, |
kenjiArai | 16:c8f4e56fa239 | 129 | #endif |
kenjiArai | 16:c8f4e56fa239 | 130 | #if USE_DEVICE_ANALOGIN |
kenjiArai | 16:c8f4e56fa239 | 131 | true |
kenjiArai | 16:c8f4e56fa239 | 132 | #else |
kenjiArai | 16:c8f4e56fa239 | 133 | false |
kenjiArai | 16:c8f4e56fa239 | 134 | #endif |
kenjiArai | 16:c8f4e56fa239 | 135 | }; |
yihui | 0:e910d9bb040f | 136 | |
kenjiArai | 14:f9b4d659d717 | 137 | // RAM ------------------------------------------------------------------------------------------- |
kenjiArai | 14:f9b4d659d717 | 138 | uint8_t tx_buf[TXRX_BUF_LEN]; |
kenjiArai | 14:f9b4d659d717 | 139 | uint8_t tx_len=0; |
kenjiArai | 14:f9b4d659d717 | 140 | uint8_t time_data[TXRX_BUF_LEN]; |
kenjiArai | 14:f9b4d659d717 | 141 | uint8_t pwr_selection; |
kenjiArai | 14:f9b4d659d717 | 142 | volatile bool trigger_transmit = false; |
kenjiArai | 14:f9b4d659d717 | 143 | volatile bool trigger_receive = false; |
kenjiArai | 14:f9b4d659d717 | 144 | volatile uint8_t command_continue = 0; |
kenjiArai | 14:f9b4d659d717 | 145 | TX_Content transmit_contents; |
kenjiArai | 15:d7764da3f37c | 146 | uint8_t time_out_cntr = 30; |
kenjiArai | 14:f9b4d659d717 | 147 | volatile bool time_out = false; |
yihui | 0:e910d9bb040f | 148 | |
kenjiArai | 14:f9b4d659d717 | 149 | // Function prototypes --------------------------------------------------------------------------- |
kenjiArai | 14:f9b4d659d717 | 150 | // BLE |
kenjiArai | 14:f9b4d659d717 | 151 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params); |
kenjiArai | 14:f9b4d659d717 | 152 | void onDataWritten(const GattWriteCallbackParams *params); |
kenjiArai | 14:f9b4d659d717 | 153 | void periodicCallback(void); |
kenjiArai | 14:f9b4d659d717 | 154 | // Application related |
kenjiArai | 14:f9b4d659d717 | 155 | void action_tx_help(void); |
kenjiArai | 14:f9b4d659d717 | 156 | void action_tx_vdd(void); |
kenjiArai | 14:f9b4d659d717 | 157 | void action_tx_temperature(void); |
kenjiArai | 14:f9b4d659d717 | 158 | void action_tx_pwrup(void); |
kenjiArai | 14:f9b4d659d717 | 159 | void action_tx_pwrdwn(void); |
kenjiArai | 14:f9b4d659d717 | 160 | void fill_space_n(uint8_t *bf, uint8_t n); |
kenjiArai | 14:f9b4d659d717 | 161 | void Update_Values(void); |
kenjiArai | 14:f9b4d659d717 | 162 | // Interrupt related |
kenjiArai | 14:f9b4d659d717 | 163 | void action_tx_quit(void); |
kenjiArai | 14:f9b4d659d717 | 164 | void interrupt_by_sw(void); |
yihui | 0:e910d9bb040f | 165 | |
kenjiArai | 14:f9b4d659d717 | 166 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 14:f9b4d659d717 | 167 | // Control Program |
kenjiArai | 14:f9b4d659d717 | 168 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 14:f9b4d659d717 | 169 | int main(void){ |
kenjiArai | 16:c8f4e56fa239 | 170 | LowPwr set_lowpwr(&lowpwr_table); |
kenjiArai | 14:f9b4d659d717 | 171 | myled = 1; |
Rohit Grover |
2:e060367b9024 | 172 | ticker.attach(periodicCallback, 1); |
kenjiArai | 14:f9b4d659d717 | 173 | // Interrupt by switch |
kenjiArai | 14:f9b4d659d717 | 174 | wake_up_sw.fall(&interrupt_by_sw); |
kenjiArai | 14:f9b4d659d717 | 175 | DEBUG("Initialising the nRF51822\r\n"); |
Rohit Grover |
2:e060367b9024 | 176 | ble.init(); |
kenjiArai | 14:f9b4d659d717 | 177 | //ble.setDeviceName((const uint8_t *)deviceName); |
Rohit Grover |
2:e060367b9024 | 178 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
2:e060367b9024 | 179 | ble.onDataWritten(onDataWritten); |
Rohit Grover |
2:e060367b9024 | 180 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 181 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 182 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 183 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
kenjiArai | 14:f9b4d659d717 | 184 | (const uint8_t *)deviceName, |
kenjiArai | 14:f9b4d659d717 | 185 | strlen(deviceName) |
kenjiArai | 14:f9b4d659d717 | 186 | ); |
Rohit Grover |
2:e060367b9024 | 187 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
kenjiArai | 14:f9b4d659d717 | 188 | (const uint8_t *)UARTServiceUUID_reversed, |
kenjiArai | 14:f9b4d659d717 | 189 | sizeof(UARTServiceUUID_reversed) |
kenjiArai | 14:f9b4d659d717 | 190 | ); |
kenjiArai | 14:f9b4d659d717 | 191 | // Power |
kenjiArai | 14:f9b4d659d717 | 192 | #if HIGH_PWR |
kenjiArai | 14:f9b4d659d717 | 193 | pwr_selection = 7; |
kenjiArai | 14:f9b4d659d717 | 194 | #else |
kenjiArai | 14:f9b4d659d717 | 195 | pwr_selection = 0; |
kenjiArai | 14:f9b4d659d717 | 196 | #endif |
kenjiArai | 14:f9b4d659d717 | 197 | ble.gap().setTxPower(pwr_range[pwr_selection]); |
kenjiArai | 14:f9b4d659d717 | 198 | // Advertize Interval |
rgrover1 | 12:33153cf38631 | 199 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
kenjiArai | 14:f9b4d659d717 | 200 | // Start |
Rohit Grover |
2:e060367b9024 | 201 | ble.startAdvertising(); |
rgrover1 | 6:e0fc9072e853 | 202 | UARTService uartService(ble); |
rgrover1 | 6:e0fc9072e853 | 203 | uartServicePtr = &uartService; |
kenjiArai | 14:f9b4d659d717 | 204 | while(true){ |
kenjiArai | 14:f9b4d659d717 | 205 | if (time_out){ |
kenjiArai | 14:f9b4d659d717 | 206 | myled = 0; |
kenjiArai | 15:d7764da3f37c | 207 | wakeup.set_and_wait(30); |
kenjiArai | 14:f9b4d659d717 | 208 | while(true){ // never come here but just in case |
kenjiArai | 14:f9b4d659d717 | 209 | deepsleep(); |
kenjiArai | 14:f9b4d659d717 | 210 | } |
kenjiArai | 14:f9b4d659d717 | 211 | } |
kenjiArai | 14:f9b4d659d717 | 212 | if (trigger_transmit){ |
kenjiArai | 14:f9b4d659d717 | 213 | trigger_transmit = false; |
kenjiArai | 14:f9b4d659d717 | 214 | switch(transmit_contents){ |
kenjiArai | 14:f9b4d659d717 | 215 | case TX_VDD: |
kenjiArai | 14:f9b4d659d717 | 216 | action_tx_vdd(); |
kenjiArai | 14:f9b4d659d717 | 217 | break; |
kenjiArai | 14:f9b4d659d717 | 218 | case TX_TEMP: |
kenjiArai | 14:f9b4d659d717 | 219 | action_tx_temperature(); |
kenjiArai | 14:f9b4d659d717 | 220 | break; |
kenjiArai | 14:f9b4d659d717 | 221 | case TX_QUIT: |
kenjiArai | 14:f9b4d659d717 | 222 | action_tx_quit(); |
kenjiArai | 14:f9b4d659d717 | 223 | break; |
kenjiArai | 14:f9b4d659d717 | 224 | case TX_PWR_UP: |
kenjiArai | 14:f9b4d659d717 | 225 | action_tx_pwrup(); |
kenjiArai | 14:f9b4d659d717 | 226 | break; |
kenjiArai | 14:f9b4d659d717 | 227 | case TX_PWR_DWN: |
kenjiArai | 14:f9b4d659d717 | 228 | action_tx_pwrdwn(); |
kenjiArai | 14:f9b4d659d717 | 229 | break; |
kenjiArai | 14:f9b4d659d717 | 230 | case TX_HELP: |
kenjiArai | 14:f9b4d659d717 | 231 | action_tx_help(); |
kenjiArai | 14:f9b4d659d717 | 232 | break; |
kenjiArai | 14:f9b4d659d717 | 233 | default: |
kenjiArai | 14:f9b4d659d717 | 234 | break; |
kenjiArai | 14:f9b4d659d717 | 235 | } |
kenjiArai | 14:f9b4d659d717 | 236 | } else { |
kenjiArai | 14:f9b4d659d717 | 237 | myled = !myled; |
kenjiArai | 14:f9b4d659d717 | 238 | ble.waitForEvent(); |
kenjiArai | 14:f9b4d659d717 | 239 | } |
kenjiArai | 14:f9b4d659d717 | 240 | } |
kenjiArai | 14:f9b4d659d717 | 241 | } |
yihui | 0:e910d9bb040f | 242 | |
kenjiArai | 15:d7764da3f37c | 243 | void led_control(float t){ |
kenjiArai | 15:d7764da3f37c | 244 | myled = !myled; |
kenjiArai | 15:d7764da3f37c | 245 | wait(t); |
kenjiArai | 15:d7764da3f37c | 246 | } |
kenjiArai | 15:d7764da3f37c | 247 | |
kenjiArai | 14:f9b4d659d717 | 248 | void onDataWritten(const GattWriteCallbackParams *params){ |
kenjiArai | 14:f9b4d659d717 | 249 | uint8_t buf[TXRX_BUF_LEN]; |
kenjiArai | 14:f9b4d659d717 | 250 | uint16_t bytesRead; |
kenjiArai | 14:f9b4d659d717 | 251 | |
kenjiArai | 14:f9b4d659d717 | 252 | if ((uartServicePtr != NULL) && |
kenjiArai | 14:f9b4d659d717 | 253 | (params->handle == uartServicePtr->getTXCharacteristicHandle())) |
kenjiArai | 14:f9b4d659d717 | 254 | { |
kenjiArai | 14:f9b4d659d717 | 255 | strcpy((char *)buf, (const char *)params->data); |
kenjiArai | 14:f9b4d659d717 | 256 | bytesRead = params->len; |
kenjiArai | 14:f9b4d659d717 | 257 | switch (buf[0]){ |
kenjiArai | 14:f9b4d659d717 | 258 | case 'v': |
kenjiArai | 14:f9b4d659d717 | 259 | trigger_transmit = true; |
kenjiArai | 14:f9b4d659d717 | 260 | transmit_contents = TX_VDD; |
kenjiArai | 14:f9b4d659d717 | 261 | break; |
kenjiArai | 14:f9b4d659d717 | 262 | case 't': |
kenjiArai | 14:f9b4d659d717 | 263 | trigger_transmit = true; |
kenjiArai | 14:f9b4d659d717 | 264 | transmit_contents = TX_TEMP; |
kenjiArai | 14:f9b4d659d717 | 265 | break; |
kenjiArai | 14:f9b4d659d717 | 266 | case 'u': |
kenjiArai | 14:f9b4d659d717 | 267 | trigger_transmit = true; |
kenjiArai | 14:f9b4d659d717 | 268 | transmit_contents = TX_PWR_UP; |
kenjiArai | 14:f9b4d659d717 | 269 | break; |
kenjiArai | 14:f9b4d659d717 | 270 | case 'd': |
kenjiArai | 14:f9b4d659d717 | 271 | trigger_transmit = true; |
kenjiArai | 14:f9b4d659d717 | 272 | transmit_contents = TX_PWR_DWN; |
kenjiArai | 14:f9b4d659d717 | 273 | break; |
kenjiArai | 14:f9b4d659d717 | 274 | case 'q': |
kenjiArai | 14:f9b4d659d717 | 275 | trigger_transmit = true; |
kenjiArai | 14:f9b4d659d717 | 276 | transmit_contents = TX_QUIT; |
kenjiArai | 14:f9b4d659d717 | 277 | break; |
kenjiArai | 14:f9b4d659d717 | 278 | case 'h': |
kenjiArai | 14:f9b4d659d717 | 279 | case '?': |
kenjiArai | 14:f9b4d659d717 | 280 | trigger_transmit = true; |
kenjiArai | 14:f9b4d659d717 | 281 | transmit_contents = TX_HELP; |
kenjiArai | 14:f9b4d659d717 | 282 | break; |
kenjiArai | 14:f9b4d659d717 | 283 | default: |
kenjiArai | 14:f9b4d659d717 | 284 | break; |
kenjiArai | 14:f9b4d659d717 | 285 | } |
kenjiArai | 14:f9b4d659d717 | 286 | fill_space_n(buf, bytesRead); |
kenjiArai | 14:f9b4d659d717 | 287 | DEBUG("RX_data\r\n"); |
kenjiArai | 14:f9b4d659d717 | 288 | DEBUG("Length: %d\r\n", bytesRead); |
kenjiArai | 14:f9b4d659d717 | 289 | DEBUG("Data: "); |
kenjiArai | 14:f9b4d659d717 | 290 | DEBUG("%s", buf); |
kenjiArai | 14:f9b4d659d717 | 291 | DEBUG("\r\n"); |
yihui | 0:e910d9bb040f | 292 | } |
yihui | 0:e910d9bb040f | 293 | } |
kenjiArai | 14:f9b4d659d717 | 294 | |
kenjiArai | 14:f9b4d659d717 | 295 | void action_tx_help(){ |
kenjiArai | 14:f9b4d659d717 | 296 | // 12345678901234567890 |
kenjiArai | 14:f9b4d659d717 | 297 | sprintf((char *)tx_buf,"?:help by JH1PJL"); |
kenjiArai | 14:f9b4d659d717 | 298 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 299 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 300 | wait(0.1); |
kenjiArai | 14:f9b4d659d717 | 301 | // 12345678901234567890 |
kenjiArai | 14:f9b4d659d717 | 302 | sprintf((char *)tx_buf,"v:vdd"); |
kenjiArai | 14:f9b4d659d717 | 303 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 304 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 305 | wait(0.1); |
kenjiArai | 14:f9b4d659d717 | 306 | // 12345678901234567890 |
kenjiArai | 14:f9b4d659d717 | 307 | sprintf((char *)tx_buf,"t:temperature"); |
kenjiArai | 14:f9b4d659d717 | 308 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 309 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 310 | wait(0.1); |
kenjiArai | 14:f9b4d659d717 | 311 | // 12345678901234567890 |
kenjiArai | 14:f9b4d659d717 | 312 | sprintf((char *)tx_buf,"u:power up"); |
kenjiArai | 14:f9b4d659d717 | 313 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 314 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 315 | wait(0.1); |
kenjiArai | 14:f9b4d659d717 | 316 | // 12345678901234567890 |
kenjiArai | 14:f9b4d659d717 | 317 | sprintf((char *)tx_buf,"d:power down"); |
kenjiArai | 14:f9b4d659d717 | 318 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 319 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 320 | wait(0.1); |
kenjiArai | 14:f9b4d659d717 | 321 | // 12345678901234567890 |
kenjiArai | 14:f9b4d659d717 | 322 | sprintf((char *)tx_buf,"q(0x71):quit/sleep"); |
kenjiArai | 14:f9b4d659d717 | 323 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 324 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 325 | } |
kenjiArai | 14:f9b4d659d717 | 326 | |
kenjiArai | 14:f9b4d659d717 | 327 | void action_tx_pwrup(){ |
kenjiArai | 14:f9b4d659d717 | 328 | if (pwr_selection != 0){ |
kenjiArai | 14:f9b4d659d717 | 329 | --pwr_selection; |
kenjiArai | 14:f9b4d659d717 | 330 | } |
kenjiArai | 14:f9b4d659d717 | 331 | ble.gap().setTxPower(pwr_range[pwr_selection]); |
kenjiArai | 14:f9b4d659d717 | 332 | sprintf((char *)tx_buf,"Pwr:%+ddBm", (4 - (pwr_selection * 4))); |
kenjiArai | 14:f9b4d659d717 | 333 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 334 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 335 | } |
kenjiArai | 14:f9b4d659d717 | 336 | |
kenjiArai | 14:f9b4d659d717 | 337 | void action_tx_pwrdwn(){ |
kenjiArai | 14:f9b4d659d717 | 338 | if (pwr_selection != (PWR_RANG_NO - 1)){ |
kenjiArai | 14:f9b4d659d717 | 339 | ++pwr_selection; |
kenjiArai | 14:f9b4d659d717 | 340 | } |
kenjiArai | 14:f9b4d659d717 | 341 | ble.gap().setTxPower(pwr_range[pwr_selection]); |
kenjiArai | 14:f9b4d659d717 | 342 | sprintf((char *)tx_buf,"Pwr:%+ddBm", (4 - (pwr_selection * 4))); |
kenjiArai | 14:f9b4d659d717 | 343 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 344 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 345 | } |
kenjiArai | 14:f9b4d659d717 | 346 | |
kenjiArai | 14:f9b4d659d717 | 347 | void action_tx_vdd(){ |
kenjiArai | 14:f9b4d659d717 | 348 | sprintf((char *)tx_buf,"Vdd:%3.2fV", vdd.read_real_value()); |
kenjiArai | 14:f9b4d659d717 | 349 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 350 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 351 | } |
kenjiArai | 14:f9b4d659d717 | 352 | |
kenjiArai | 14:f9b4d659d717 | 353 | void action_tx_temperature(){ |
kenjiArai | 14:f9b4d659d717 | 354 | int32_t p_temp; |
kenjiArai | 14:f9b4d659d717 | 355 | float temperature; |
kenjiArai | 14:f9b4d659d717 | 356 | |
kenjiArai | 14:f9b4d659d717 | 357 | // Update a temperature (inside nRF51822 chip) |
kenjiArai | 14:f9b4d659d717 | 358 | sd_temp_get(&p_temp); |
kenjiArai | 14:f9b4d659d717 | 359 | // -16.0f is offset vale for chip die temp to ambient temp (depend on your board) |
kenjiArai | 14:f9b4d659d717 | 360 | temperature = float(p_temp) / 4; // Original = float(p_temp)/4.0f - 16.0f; |
kenjiArai | 14:f9b4d659d717 | 361 | sprintf((char *)tx_buf,"T:%+4.1fdC", temperature); |
kenjiArai | 14:f9b4d659d717 | 362 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 363 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 364 | } |
kenjiArai | 14:f9b4d659d717 | 365 | |
kenjiArai | 14:f9b4d659d717 | 366 | void action_tx_quit(){ |
kenjiArai | 14:f9b4d659d717 | 367 | ticker.detach(); |
kenjiArai | 14:f9b4d659d717 | 368 | // 12345678901234567890 |
kenjiArai | 14:f9b4d659d717 | 369 | sprintf((char *)tx_buf,"Terminated the BLE"); |
kenjiArai | 14:f9b4d659d717 | 370 | tx_len = strlen((const char *)tx_buf); |
kenjiArai | 14:f9b4d659d717 | 371 | Update_Values(); |
kenjiArai | 14:f9b4d659d717 | 372 | myled = 0; |
kenjiArai | 14:f9b4d659d717 | 373 | wait(1.0); |
kenjiArai | 15:d7764da3f37c | 374 | wakeup.set_and_wait(30); |
kenjiArai | 14:f9b4d659d717 | 375 | while(true){ // never come here but just in case |
kenjiArai | 14:f9b4d659d717 | 376 | deepsleep(); |
kenjiArai | 14:f9b4d659d717 | 377 | } |
kenjiArai | 14:f9b4d659d717 | 378 | } |
kenjiArai | 14:f9b4d659d717 | 379 | |
kenjiArai | 14:f9b4d659d717 | 380 | void interrupt_by_sw(){ // Go to sleep |
kenjiArai | 14:f9b4d659d717 | 381 | NVIC_SystemReset(); |
kenjiArai | 14:f9b4d659d717 | 382 | // Not come here (Just in case) |
kenjiArai | 14:f9b4d659d717 | 383 | deepsleep(); |
kenjiArai | 14:f9b4d659d717 | 384 | } |
kenjiArai | 14:f9b4d659d717 | 385 | |
kenjiArai | 14:f9b4d659d717 | 386 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
kenjiArai | 14:f9b4d659d717 | 387 | DEBUG("Disconnected!\r\n"); |
kenjiArai | 14:f9b4d659d717 | 388 | DEBUG("Restarting the advertising process\r\n"); |
kenjiArai | 14:f9b4d659d717 | 389 | ble.startAdvertising(); |
kenjiArai | 14:f9b4d659d717 | 390 | } |
kenjiArai | 14:f9b4d659d717 | 391 | |
kenjiArai | 14:f9b4d659d717 | 392 | void periodicCallback(void){ |
kenjiArai | 14:f9b4d659d717 | 393 | if (--time_out_cntr == 0){ |
kenjiArai | 14:f9b4d659d717 | 394 | time_out = true; |
kenjiArai | 14:f9b4d659d717 | 395 | } |
kenjiArai | 14:f9b4d659d717 | 396 | } |
kenjiArai | 14:f9b4d659d717 | 397 | |
kenjiArai | 14:f9b4d659d717 | 398 | void fill_space_n(uint8_t *bf, uint8_t n){ |
kenjiArai | 14:f9b4d659d717 | 399 | bf += n; |
kenjiArai | 14:f9b4d659d717 | 400 | for (uint8_t i = n; i <= 20; bf++, i++){ |
kenjiArai | 14:f9b4d659d717 | 401 | *bf = ' '; |
kenjiArai | 14:f9b4d659d717 | 402 | } |
kenjiArai | 14:f9b4d659d717 | 403 | *bf = '.'; |
kenjiArai | 14:f9b4d659d717 | 404 | *(bf + 1) = 0; |
kenjiArai | 14:f9b4d659d717 | 405 | } |
kenjiArai | 14:f9b4d659d717 | 406 | |
kenjiArai | 14:f9b4d659d717 | 407 | void Update_Values(void){ |
kenjiArai | 15:d7764da3f37c | 408 | ble.updateCharacteristicValue( |
kenjiArai | 15:d7764da3f37c | 409 | uartServicePtr->getRXCharacteristicHandle(), |
kenjiArai | 15:d7764da3f37c | 410 | tx_buf, |
kenjiArai | 15:d7764da3f37c | 411 | tx_len |
kenjiArai | 15:d7764da3f37c | 412 | ); |
kenjiArai | 14:f9b4d659d717 | 413 | DEBUG("TX_data: %s\r\n", tx_buf); |
kenjiArai | 14:f9b4d659d717 | 414 | DEBUG("Length: %d\r\n", tx_len); |
kenjiArai | 14:f9b4d659d717 | 415 | tx_len = 0; |
kenjiArai | 14:f9b4d659d717 | 416 | } |