only moves 1 direction

Dependencies:   BLE_API mbed nRF51822

Committer:
amackp
Date:
Wed Sep 02 19:47:45 2015 +0000
Revision:
4:3e3b4ce8905f
Parent:
3:cf8bb9a7756d
Child:
5:d4f2c480044c
still only goes 1 direction, but works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:1c058e553423 1 /*
RedBearLab 0:cffe8ac1bdf0 2
RedBearLab 1:1c058e553423 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:1c058e553423 4
RedBearLab 1:1c058e553423 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:1c058e553423 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:1c058e553423 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:1c058e553423 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:1c058e553423 9 subject to the following conditions:
RedBearLab 1:1c058e553423 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:1c058e553423 11
RedBearLab 1:1c058e553423 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:1c058e553423 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:1c058e553423 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:1c058e553423 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:1c058e553423 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:1c058e553423 17
RedBearLab 1:1c058e553423 18 */
RedBearLab 1:1c058e553423 19
RedBearLab 1:1c058e553423 20 /*
RedBearLab 1:1c058e553423 21 * The application works with the BLEController iOS/Android App.
RedBearLab 1:1c058e553423 22 * Type something from the Terminal to send
RedBearLab 1:1c058e553423 23 * to the BLEController App or vice verse.
RedBearLab 1:1c058e553423 24 * Characteristics received from App will print on Terminal.
RedBearLab 1:1c058e553423 25 */
RedBearLab 1:1c058e553423 26
RedBearLab 0:cffe8ac1bdf0 27 #include "mbed.h"
RedBearLab 2:4b66b69c7ecb 28 #include "ble/BLE.h"
RedBearLab 0:cffe8ac1bdf0 29
RedBearLab 0:cffe8ac1bdf0 30
RedBearLab 0:cffe8ac1bdf0 31 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 32 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 33 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 34
RedBearLab 0:cffe8ac1bdf0 35 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 36
RedBearLab 2:4b66b69c7ecb 37 BLE ble;
RedBearLab 0:cffe8ac1bdf0 38
RedBearLab 0:cffe8ac1bdf0 39 Serial pc(USBTX, USBRX);
RedBearLab 0:cffe8ac1bdf0 40
RedBearLab 0:cffe8ac1bdf0 41
RedBearLab 0:cffe8ac1bdf0 42 // The Nordic UART Service
RedBearLab 0:cffe8ac1bdf0 43 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 44 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 45 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 46 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
RedBearLab 0:cffe8ac1bdf0 47
RedBearLab 0:cffe8ac1bdf0 48
RedBearLab 0:cffe8ac1bdf0 49 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 50 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 51
RedBearLab 0:cffe8ac1bdf0 52 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 53 static uint8_t rx_len=0;
RedBearLab 0:cffe8ac1bdf0 54
RedBearLab 0:cffe8ac1bdf0 55
connorb 3:cf8bb9a7756d 56 // Connor's Code start -------------------------------------------------
connorb 3:cf8bb9a7756d 57 DigitalOut myled(P0_19); //onboard LED
connorb 3:cf8bb9a7756d 58
amackp 4:3e3b4ce8905f 59 PwmOut pin3(P0_10); // enable h bridge
connorb 3:cf8bb9a7756d 60
amackp 4:3e3b4ce8905f 61 PwmOut pin1(P0_9); // h bridge 1 Switch these two to change direction
connorb 3:cf8bb9a7756d 62 DigitalOut pin2(P0_11); // h bridge 2
connorb 3:cf8bb9a7756d 63
connorb 3:cf8bb9a7756d 64
connorb 3:cf8bb9a7756d 65
RedBearLab 0:cffe8ac1bdf0 66 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
RedBearLab 0:cffe8ac1bdf0 67
RedBearLab 0:cffe8ac1bdf0 68 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 69
RedBearLab 0:cffe8ac1bdf0 70 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 71
RedBearLab 0:cffe8ac1bdf0 72 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 73
RedBearLab 0:cffe8ac1bdf0 74
RedBearLab 0:cffe8ac1bdf0 75
RedBearLab 0:cffe8ac1bdf0 76 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 77 {
RedBearLab 0:cffe8ac1bdf0 78 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 79 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 80 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 81 }
connorb 3:cf8bb9a7756d 82 //Connor's methods Start
connorb 3:cf8bb9a7756d 83
connorb 3:cf8bb9a7756d 84
connorb 3:cf8bb9a7756d 85
connorb 3:cf8bb9a7756d 86 void motor_stop(){
connorb 3:cf8bb9a7756d 87 pin3 = 1;
connorb 3:cf8bb9a7756d 88 pin1 = 0;
connorb 3:cf8bb9a7756d 89 pin2 = 0;
connorb 3:cf8bb9a7756d 90 pin3 = 0;
connorb 3:cf8bb9a7756d 91 wait(.25);
connorb 3:cf8bb9a7756d 92 }
connorb 3:cf8bb9a7756d 93
connorb 3:cf8bb9a7756d 94 void drive_backward(){
connorb 3:cf8bb9a7756d 95 pin3 = 1;
amackp 4:3e3b4ce8905f 96 pin1 = 0;
amackp 4:3e3b4ce8905f 97 pin2 = 1;
connorb 3:cf8bb9a7756d 98 }
connorb 3:cf8bb9a7756d 99
connorb 3:cf8bb9a7756d 100 void drive_forward(){
connorb 3:cf8bb9a7756d 101 pin3 = 1;
connorb 3:cf8bb9a7756d 102 pin1 = 0;
connorb 3:cf8bb9a7756d 103 pin2 = 1;
connorb 3:cf8bb9a7756d 104 }
connorb 3:cf8bb9a7756d 105
connorb 3:cf8bb9a7756d 106 void pulse_forward(){
connorb 3:cf8bb9a7756d 107 drive_forward();
connorb 3:cf8bb9a7756d 108 wait(.100);
connorb 3:cf8bb9a7756d 109 motor_stop();
connorb 3:cf8bb9a7756d 110 }
connorb 3:cf8bb9a7756d 111
connorb 3:cf8bb9a7756d 112 void pulse_backward(){
connorb 3:cf8bb9a7756d 113 drive_backward();
connorb 3:cf8bb9a7756d 114 wait(.100);
connorb 3:cf8bb9a7756d 115 motor_stop();
connorb 3:cf8bb9a7756d 116 }
connorb 3:cf8bb9a7756d 117
connorb 3:cf8bb9a7756d 118 void dispense()
connorb 3:cf8bb9a7756d 119 {
connorb 3:cf8bb9a7756d 120 drive_forward();
connorb 3:cf8bb9a7756d 121 wait(.850);
connorb 3:cf8bb9a7756d 122 motor_stop();
connorb 3:cf8bb9a7756d 123 wait(.250);
connorb 3:cf8bb9a7756d 124 drive_backward();
connorb 3:cf8bb9a7756d 125 wait(.900);
connorb 3:cf8bb9a7756d 126 motor_stop();
connorb 3:cf8bb9a7756d 127
connorb 3:cf8bb9a7756d 128 if (myled == 1) //so you can see if dispense is being called, toggles led on and off
connorb 3:cf8bb9a7756d 129 {
connorb 3:cf8bb9a7756d 130 myled = 0;
connorb 3:cf8bb9a7756d 131 }
connorb 3:cf8bb9a7756d 132 else if (myled == 0)
connorb 3:cf8bb9a7756d 133 {
connorb 3:cf8bb9a7756d 134 myled = 1;
connorb 3:cf8bb9a7756d 135 }
connorb 3:cf8bb9a7756d 136 }
connorb 3:cf8bb9a7756d 137
RedBearLab 0:cffe8ac1bdf0 138
RedBearLab 2:4b66b69c7ecb 139 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 140 {
RedBearLab 0:cffe8ac1bdf0 141 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 142 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 143
RedBearLab 2:4b66b69c7ecb 144 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 145 {
RedBearLab 0:cffe8ac1bdf0 146 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 147 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 148 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 149 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 150 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 151 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 152 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 153 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 154 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 155 {
connorb 3:cf8bb9a7756d 156 pc.putc(txPayload[index]); //Connor's Code Listens for A then dispenses
connorb 3:cf8bb9a7756d 157 if (txPayload[index] == 'A')
connorb 3:cf8bb9a7756d 158 {
connorb 3:cf8bb9a7756d 159 dispense();
connorb 3:cf8bb9a7756d 160 }
RedBearLab 0:cffe8ac1bdf0 161 }
RedBearLab 0:cffe8ac1bdf0 162 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 163 }
RedBearLab 0:cffe8ac1bdf0 164 }
RedBearLab 0:cffe8ac1bdf0 165
connorb 3:cf8bb9a7756d 166
connorb 3:cf8bb9a7756d 167
RedBearLab 0:cffe8ac1bdf0 168 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 169 {
RedBearLab 0:cffe8ac1bdf0 170 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 171 {
RedBearLab 0:cffe8ac1bdf0 172 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 173 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 174 {
RedBearLab 0:cffe8ac1bdf0 175 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:cffe8ac1bdf0 176 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 177 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 178 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 179 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 180 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 181 break;
RedBearLab 0:cffe8ac1bdf0 182 }
RedBearLab 0:cffe8ac1bdf0 183 }
RedBearLab 0:cffe8ac1bdf0 184 }
RedBearLab 0:cffe8ac1bdf0 185
RedBearLab 0:cffe8ac1bdf0 186 int main(void)
RedBearLab 0:cffe8ac1bdf0 187 {
connorb 3:cf8bb9a7756d 188
RedBearLab 0:cffe8ac1bdf0 189 ble.init();
RedBearLab 0:cffe8ac1bdf0 190 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 191 ble.onDataWritten(WrittenHandler);
RedBearLab 0:cffe8ac1bdf0 192
RedBearLab 0:cffe8ac1bdf0 193 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 194 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 195
RedBearLab 0:cffe8ac1bdf0 196 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 197 // setup advertising
RedBearLab 0:cffe8ac1bdf0 198 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 199 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 200 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
RedBearLab 0:cffe8ac1bdf0 201 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
RedBearLab 0:cffe8ac1bdf0 202 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 203 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 204 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 205 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 206
RedBearLab 0:cffe8ac1bdf0 207 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 208
RedBearLab 0:cffe8ac1bdf0 209 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 210 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 211
amackp 4:3e3b4ce8905f 212 //while(1)
amackp 4:3e3b4ce8905f 213 //{
amackp 4:3e3b4ce8905f 214 //dispense();
connorb 3:cf8bb9a7756d 215 //ble.waitForEvent();
amackp 4:3e3b4ce8905f 216 //}
connorb 3:cf8bb9a7756d 217
RedBearLab 0:cffe8ac1bdf0 218 }
RedBearLab 0:cffe8ac1bdf0 219
RedBearLab 0:cffe8ac1bdf0 220
RedBearLab 0:cffe8ac1bdf0 221
RedBearLab 0:cffe8ac1bdf0 222
RedBearLab 0:cffe8ac1bdf0 223
RedBearLab 0:cffe8ac1bdf0 224
RedBearLab 0:cffe8ac1bdf0 225
RedBearLab 0:cffe8ac1bdf0 226
RedBearLab 0:cffe8ac1bdf0 227
RedBearLab 0:cffe8ac1bdf0 228
RedBearLab 0:cffe8ac1bdf0 229
RedBearLab 0:cffe8ac1bdf0 230
RedBearLab 0:cffe8ac1bdf0 231
RedBearLab 0:cffe8ac1bdf0 232
RedBearLab 0:cffe8ac1bdf0 233
RedBearLab 0:cffe8ac1bdf0 234