Abe Maclean / Mbed 2 deprecated BLENanoMotor1

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 Copyright (c) 2012-2014 RedBearLab
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 
00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
00009 subject to the following conditions:
00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 
00018 */
00019 
00020 /*
00021  *    The application works with the BLEController iOS/Android App.
00022  *    Type something from the Terminal to send
00023  *    to the BLEController App or vice verse.
00024  *    Characteristics received from App will print on Terminal.
00025  */
00026  
00027 #include "mbed.h"
00028 #include "ble/BLE.h"
00029 
00030 
00031 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
00032 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
00033 #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
00034 
00035 #define TXRX_BUF_LEN                     20
00036 
00037 BLE  ble;
00038 
00039 Serial pc(USBTX, USBRX);
00040 
00041 
00042 // The Nordic UART Service
00043 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00044 static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00045 static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00046 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
00047 
00048 
00049 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00050 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00051 
00052 static uint8_t rx_buf[TXRX_BUF_LEN];
00053 static uint8_t rx_len=0;
00054 
00055 
00056 // Connor's Code start -------------------------------------------------
00057 DigitalOut myled(P0_19); //onboard LED
00058 
00059 PwmOut pin3(P0_10); // enable h bridge
00060 
00061 PwmOut pin1(P0_9); // h bridge 1 Switch these two to change direction
00062 PwmOut pin2(P0_11); // h bridge 2
00063 
00064 
00065 
00066 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00067                                       
00068 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00069                                       
00070 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
00071 
00072 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
00073 
00074 
00075 
00076 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00077 {
00078     pc.printf("Disconnected \r\n");
00079     pc.printf("Restart advertising \r\n");
00080     ble.startAdvertising();
00081 }
00082 //Connor's methods Start
00083 
00084 void motor_stop(){
00085 pin3 = 1;
00086 pin1 = 0;
00087 pin2 = 0;
00088 pin3 = 0;
00089 wait(.25);
00090 }
00091 
00092 void drive_backward(){
00093 pin3 = 255;
00094 pin1 = 0;
00095 pin2 = 255;
00096 }
00097 
00098 void drive_forward(){
00099 pin3 = 1;
00100 pin1 = 255;
00101 pin2 = 0;
00102 }
00103 
00104 void pulse_forward(){
00105 drive_forward();
00106 wait(.100);
00107 motor_stop();
00108 }
00109 
00110 void pulse_backward(){
00111 drive_backward();
00112 wait(.100);
00113 motor_stop();
00114 }
00115 
00116 void dispense()
00117 {
00118 drive_forward();
00119 wait(.850);
00120 motor_stop();
00121 wait(.250);
00122 drive_backward();
00123 wait(.900);
00124 motor_stop();
00125    
00126     if (myled == 1) //so you can see if dispense is being called, toggles led on and off
00127     {
00128         myled = 0;
00129     }
00130     else if (myled == 0)
00131     {
00132         myled = 1;
00133     }
00134 }
00135 
00136 
00137 void WrittenHandler(const GattWriteCallbackParams *Handler)
00138 {   
00139     uint8_t buf[TXRX_BUF_LEN];
00140     uint16_t bytesRead, index;
00141     
00142     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
00143     {
00144         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00145         memset(txPayload, 0, TXRX_BUF_LEN);
00146         memcpy(txPayload, buf, TXRX_BUF_LEN);       
00147         pc.printf("WriteHandler \r\n");
00148         pc.printf("Length: ");
00149         pc.putc(bytesRead);
00150         pc.printf("\r\n");
00151         pc.printf("Data: ");
00152         for(index=0; index<bytesRead; index++)
00153         {
00154             pc.putc(txPayload[index]); //Connor's Code Listens for A then dispenses
00155             if (txPayload[index] == 'A')
00156             {
00157                 dispense();
00158             }       
00159         }
00160         pc.printf("\r\n");
00161     }
00162 }
00163 
00164 
00165     
00166 void uartCB(void)
00167 {   
00168     while(pc.readable())    
00169     {
00170         rx_buf[rx_len++] = pc.getc();    
00171         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
00172         {
00173             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
00174             pc.printf("RecHandler \r\n");
00175             pc.printf("Length: ");
00176             pc.putc(rx_len);
00177             pc.printf("\r\n");
00178             rx_len = 0;
00179             break;
00180         }
00181     }
00182 }
00183 
00184 int main(void)
00185 {
00186 
00187     ble.init();
00188     ble.onDisconnection(disconnectionCallback);
00189     ble.onDataWritten(WrittenHandler);  
00190     
00191     pc.baud(9600);
00192     pc.printf("SimpleChat Init \r\n");
00193     
00194     pc.attach( uartCB , pc.RxIrq);
00195    // setup advertising 
00196     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00197     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00198     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00199                                     (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
00200     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00201                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
00202     // 100ms; in multiples of 0.625ms. 
00203     ble.setAdvertisingInterval(160);
00204 
00205     ble.addService(uartService);
00206     
00207     ble.startAdvertising(); 
00208     pc.printf("Advertising Start \r\n");
00209     
00210     //while(1)
00211     //{
00212         //dispense();
00213         //ble.waitForEvent(); 
00214     //}
00215     
00216 }
00217 
00218 
00219 
00220 
00221 
00222 
00223 
00224 
00225 
00226 
00227 
00228 
00229 
00230 
00231 
00232