This is a simplified version of RedBearLab's SimpleControls for a one input iOS app
Dependencies: BLE_API mbed nRF51822
Fork of BLENano_SimpleControls by
Diff: main.cpp
- Revision:
- 4:9500e2b7cc36
- Parent:
- 3:f530ca03e014
--- a/main.cpp Thu Jan 07 02:43:53 2016 +0000 +++ b/main.cpp Wed Feb 24 18:38:54 2016 +0000 @@ -1,5 +1,7 @@ /* +This code is taken from RedBearLab's SimpleControl example, and stripped down and modified slightly. + Copyright (c) 2012-2014 RedBearLab Permission is hereby granted, free of charge, to any person obtaining a copy of this software @@ -19,8 +21,6 @@ #include "mbed.h" #include "ble/BLE.h" -#include "Servo.h" - #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ @@ -28,23 +28,15 @@ #define TXRX_BUF_LEN 20 -#define DIGITAL_OUT_PIN P0_9 //TXD +#define DIGITAL_OUT_PIN p19 //TXD #define DIGITAL_IN_PIN P0_10 //CTS -#define PWM_PIN P0_11 //RXD -#define SERVO_PIN P0_8 //RTS -#define ANALOG_IN_PIN P0_4 //P04 BLE ble; DigitalOut LED_SET(DIGITAL_OUT_PIN); +DigitalOut LEDOut(P0_5); DigitalIn BUTTON(DIGITAL_IN_PIN); -PwmOut PWM(PWM_PIN); -AnalogIn ANALOG(ANALOG_IN_PIN); -Servo MYSERVO(SERVO_PIN); -//Serial pc(USBTX, USBRX); - -static uint8_t analog_enabled = 0; static uint8_t old_state = 0; // The Nordic UART Service @@ -57,24 +49,15 @@ uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; -//static uint8_t rx_buf[TXRX_BUF_LEN]; -//static uint8_t rx_len=0; - -GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); - -GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); - +GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); +GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; - GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); - void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { - //pc.printf("Disconnected \r\n"); - //pc.printf("Restart advertising \r\n"); ble.gap().startAdvertising(); } @@ -89,75 +72,25 @@ memset(txPayload, 0, TXRX_BUF_LEN); memcpy(txPayload, buf, TXRX_BUF_LEN); - //for(index=0; index<bytesRead; index++) - //pc.putc(buf[index]); + //executed when phone connected to Nano + if(buf[0] == 0x04){ + //have LED blink twice to indicate connection established + LEDOut = 1; + wait(0.1); + LEDOut = 0; + wait(0.1); + LEDOut = 1; + wait(0.1); + LEDOut = 0; - if(buf[0] == 0x01) - { - if(buf[1] == 0x01) - LED_SET = 1; - else - LED_SET = 0; - } - else if(buf[0] == 0xA0) - { - if(buf[1] == 0x01) - analog_enabled = 1; - else - analog_enabled = 0; - } - else if(buf[0] == 0x02) - { - float value = (float)buf[1]/255; - PWM = value; - } - else if(buf[0] == 0x03) - { - MYSERVO.write(buf[1]); - } - else if(buf[0] == 0x04) - { - analog_enabled = 0; - PWM = 0; - MYSERVO.write(0); - LED_SET = 0; - old_state = 0; - } - + old_state = 1; + } } } -/* -void uartCB(void) -{ - while(pc.readable()) - { - rx_buf[rx_len++] = pc.getc(); - if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') - { - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); - pc.printf("RecHandler \r\n"); - pc.printf("Length: "); - pc.putc(rx_len); - pc.printf("\r\n"); - rx_len = 0; - break; - } - } -} -*/ + void m_status_check_handle(void) { uint8_t buf[3]; - if (analog_enabled) // if analog reading enabled - { - // Read and send out - float s = ANALOG; - uint16_t value = s*1024; - buf[0] = (0x0B); - buf[1] = (value >> 8); - buf[2] = (value); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); - } // If digital in changes, report the state if (BUTTON != old_state) @@ -168,7 +101,9 @@ { buf[0] = (0x0A); buf[1] = (0x01); - buf[2] = (0x00); + buf[2] = (0x00); + LED_SET = 1; + LEDOut = 0; ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); } else @@ -176,6 +111,8 @@ buf[0] = (0x0A); buf[1] = (0x00); buf[2] = (0x00); + LED_SET = 0; + LEDOut = 1; ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); } } @@ -191,11 +128,6 @@ ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); - //pc.baud(9600); - //pc.printf("SimpleChat Init \r\n"); - - //pc.attach( uartCB , pc.RxIrq); - // setup advertising ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); @@ -210,41 +142,9 @@ ble.startAdvertising(); - //pc.printf("Advertising Start \r\n"); while(1) { ble.waitForEvent(); } -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +} \ No newline at end of file