This program demonstrate how to apply BLE_nRF8001 library on mbed platform working with RedBearLab BLE Shield v2.1 or above.

Dependencies:   BLE_nRF8001 mbed

SimpleControls works with the BLEController iOS/Android App. The mbed's pin can acts as DIGITAL_IN, DIGITAL_OUT, PWM, SERVO, ANALOG_IN. The sketch is to show you how to control the pin as one of the abilities. Note that not every pin can own all of the abilities. You can change the following macro to specify which pin to be controlled.

DigitalInOut DIGITAL_OUT_PIN( PTD4 );

DigitalInOut DIGITAL_IN_PIN( PTA12 );

PwmOut pwm( PTA4 );

Servo myservo( PTA13 );

AnalogIn analog( PTB0 );

The application will report the state of DIGITAL_IN_PIN and ANALOG_IN_PIN to App. The App can control the state of DIGITAL_OUT_PIN / PWM_PIN / SERVO_PIN.

You have to change the belowing constructs manually corresponding to the platform you are using.

SPI spi(PTD2, PTD3, PTD1);

DigitalInOut BLE_RDY(PTA12);

DigitalInOut BLE_REQ(PTA2);

Committer:
RedBearLab
Date:
Fri Oct 31 14:45:22 2014 +0000
Revision:
1:d743c358f812
Parent:
0:4c34e71b2f6a
Add licence in files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 0:4c34e71b2f6a 1 /*
RedBearLab 0:4c34e71b2f6a 2
RedBearLab 0:4c34e71b2f6a 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 0:4c34e71b2f6a 4
RedBearLab 0:4c34e71b2f6a 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 0:4c34e71b2f6a 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 0:4c34e71b2f6a 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 0:4c34e71b2f6a 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 0:4c34e71b2f6a 9 subject to the following conditions:
RedBearLab 0:4c34e71b2f6a 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 0:4c34e71b2f6a 11
RedBearLab 0:4c34e71b2f6a 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 0:4c34e71b2f6a 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 0:4c34e71b2f6a 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 0:4c34e71b2f6a 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 0:4c34e71b2f6a 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 0:4c34e71b2f6a 17
RedBearLab 0:4c34e71b2f6a 18 */
RedBearLab 0:4c34e71b2f6a 19
RedBearLab 0:4c34e71b2f6a 20 /*
RedBearLab 0:4c34e71b2f6a 21 * SimpleControls
RedBearLab 0:4c34e71b2f6a 22 *
RedBearLab 0:4c34e71b2f6a 23 * SimpleControls works with the BLEController iOS/Android App.
RedBearLab 0:4c34e71b2f6a 24 * The mbed's pin can acts as DIGITAL_IN, DIGITAL_OUT, PWM, SERVO, ANALOG_IN.
RedBearLab 0:4c34e71b2f6a 25 * The sketch is to show you how to control the pin as one of the abilities.
RedBearLab 0:4c34e71b2f6a 26 * Note that not every pin can own all of the abilities.
RedBearLab 0:4c34e71b2f6a 27 * You can change the following macro to specify which pin to be controlled.
RedBearLab 0:4c34e71b2f6a 28 *
RedBearLab 0:4c34e71b2f6a 29 * DigitalInOut DIGITAL_OUT_PIN( PTD4 );
RedBearLab 0:4c34e71b2f6a 30 * DigitalInOut DIGITAL_IN_PIN( PTA12 );
RedBearLab 0:4c34e71b2f6a 31 * PwmOut pwm( PTA4 );
RedBearLab 0:4c34e71b2f6a 32 * Servo myservo( PTA13 );
RedBearLab 0:4c34e71b2f6a 33 * AnalogIn analog( PTB0 );
RedBearLab 0:4c34e71b2f6a 34 *
RedBearLab 0:4c34e71b2f6a 35 * The application will report the state of DIGITAL_IN_PIN and ANALOG_IN_PIN to App.
RedBearLab 0:4c34e71b2f6a 36 * The App can control the state of DIGITAL_OUT_PIN / PWM_PIN / SERVO_PIN
RedBearLab 0:4c34e71b2f6a 37 */
RedBearLab 0:4c34e71b2f6a 38
RedBearLab 0:4c34e71b2f6a 39 // Import libraries
RedBearLab 0:4c34e71b2f6a 40 #include "Arduino.h"
RedBearLab 0:4c34e71b2f6a 41 #include "BLEPeripheral.h"
RedBearLab 0:4c34e71b2f6a 42 #include "Servo.h"
RedBearLab 0:4c34e71b2f6a 43
RedBearLab 0:4c34e71b2f6a 44 Serial serial(USBTX, USBRX);
RedBearLab 0:4c34e71b2f6a 45
RedBearLab 0:4c34e71b2f6a 46 DigitalInOut DIGITAL_OUT_PIN( PTD4 );
RedBearLab 0:4c34e71b2f6a 47 DigitalInOut DIGITAL_IN_PIN( PTA12 );
RedBearLab 0:4c34e71b2f6a 48 PwmOut pwm( PTA4 );
RedBearLab 0:4c34e71b2f6a 49 Servo myservo( PTC9 );
RedBearLab 0:4c34e71b2f6a 50 AnalogIn analog( PTB0 );
RedBearLab 0:4c34e71b2f6a 51
RedBearLab 0:4c34e71b2f6a 52 // The SPI construct, REQN and RDYN IO construct should be modified manually
RedBearLab 0:4c34e71b2f6a 53 // It depend on the board you are using and the REQN&RDYN configuration on BLE Shield
RedBearLab 0:4c34e71b2f6a 54 SPI spi(PTD2, PTD3, PTD1);
RedBearLab 0:4c34e71b2f6a 55 DigitalInOut BLE_RDY(PTA13);
RedBearLab 0:4c34e71b2f6a 56 DigitalInOut BLE_REQ(PTD5);
RedBearLab 0:4c34e71b2f6a 57
RedBearLab 0:4c34e71b2f6a 58 unsigned char buf[16] = {0};
RedBearLab 0:4c34e71b2f6a 59 unsigned char len = 0;
RedBearLab 0:4c34e71b2f6a 60
RedBearLab 0:4c34e71b2f6a 61 unsigned char analog_enabled = 0;
RedBearLab 0:4c34e71b2f6a 62 unsigned char old_state = LOW;
RedBearLab 0:4c34e71b2f6a 63
RedBearLab 0:4c34e71b2f6a 64 /*----- BLE Utility -------------------------------------------------------------------------*/
RedBearLab 0:4c34e71b2f6a 65 // create peripheral instance, see pinouts above
RedBearLab 0:4c34e71b2f6a 66 BLEPeripheral blePeripheral = BLEPeripheral(&BLE_REQ, &BLE_RDY, NULL);
RedBearLab 0:4c34e71b2f6a 67
RedBearLab 0:4c34e71b2f6a 68 // create service
RedBearLab 0:4c34e71b2f6a 69 BLEService uartService = BLEService("713d0000503e4c75ba943148f18d941e");
RedBearLab 0:4c34e71b2f6a 70
RedBearLab 0:4c34e71b2f6a 71 // create characteristic
RedBearLab 0:4c34e71b2f6a 72 BLECharacteristic txCharacteristic = BLECharacteristic("713d0002503e4c75ba943148f18d941e", BLENotify, 20);
RedBearLab 0:4c34e71b2f6a 73 BLECharacteristic rxCharacteristic = BLECharacteristic("713d0003503e4c75ba943148f18d941e", BLEWriteWithoutResponse, 20);
RedBearLab 0:4c34e71b2f6a 74 /*--------------------------------------------------------------------------------------------*/
RedBearLab 0:4c34e71b2f6a 75
RedBearLab 0:4c34e71b2f6a 76 int main()
RedBearLab 0:4c34e71b2f6a 77 {
RedBearLab 0:4c34e71b2f6a 78 serial.baud(115200);
RedBearLab 0:4c34e71b2f6a 79 serial.printf("Serial begin!\r\n");
RedBearLab 0:4c34e71b2f6a 80
RedBearLab 0:4c34e71b2f6a 81 pinMode(&DIGITAL_OUT_PIN, OUTPUT);
RedBearLab 0:4c34e71b2f6a 82 pinMode(&DIGITAL_IN_PIN, INPUT_PULLUP);
RedBearLab 0:4c34e71b2f6a 83
RedBearLab 0:4c34e71b2f6a 84 /*----- BLE Utility ---------------------------------------------*/
RedBearLab 0:4c34e71b2f6a 85 // set advertised local name and service UUID
RedBearLab 0:4c34e71b2f6a 86 blePeripheral.setLocalName("BLE Shield");
RedBearLab 0:4c34e71b2f6a 87
RedBearLab 0:4c34e71b2f6a 88 blePeripheral.setAdvertisedServiceUuid(uartService.uuid());
RedBearLab 0:4c34e71b2f6a 89
RedBearLab 0:4c34e71b2f6a 90 // add service and characteristic
RedBearLab 0:4c34e71b2f6a 91 blePeripheral.addAttribute(uartService);
RedBearLab 0:4c34e71b2f6a 92 blePeripheral.addAttribute(rxCharacteristic);
RedBearLab 0:4c34e71b2f6a 93 blePeripheral.addAttribute(txCharacteristic);
RedBearLab 0:4c34e71b2f6a 94
RedBearLab 0:4c34e71b2f6a 95 // begin initialization
RedBearLab 0:4c34e71b2f6a 96 blePeripheral.begin();
RedBearLab 0:4c34e71b2f6a 97 /*---------------------------------------------------------------*/
RedBearLab 0:4c34e71b2f6a 98
RedBearLab 0:4c34e71b2f6a 99 serial.printf("BLE UART Peripheral begin!\r\n");
RedBearLab 0:4c34e71b2f6a 100
RedBearLab 0:4c34e71b2f6a 101 while(1)
RedBearLab 0:4c34e71b2f6a 102 {
RedBearLab 0:4c34e71b2f6a 103 BLECentral central = blePeripheral.central();
RedBearLab 0:4c34e71b2f6a 104
RedBearLab 0:4c34e71b2f6a 105 if (central)
RedBearLab 0:4c34e71b2f6a 106 {
RedBearLab 0:4c34e71b2f6a 107 // central connected to peripheral
RedBearLab 0:4c34e71b2f6a 108 serial.printf("Connected to central\r\n");
RedBearLab 0:4c34e71b2f6a 109
RedBearLab 0:4c34e71b2f6a 110 while (central.connected())
RedBearLab 0:4c34e71b2f6a 111 {
RedBearLab 0:4c34e71b2f6a 112 // central still connected to peripheral
RedBearLab 0:4c34e71b2f6a 113 if (rxCharacteristic.written())
RedBearLab 0:4c34e71b2f6a 114 {
RedBearLab 0:4c34e71b2f6a 115 unsigned char len = rxCharacteristic.valueLength();
RedBearLab 0:4c34e71b2f6a 116 const unsigned char *val = rxCharacteristic.value();
RedBearLab 0:4c34e71b2f6a 117 serial.printf("didCharacteristicWritten, Length: %d\r\n", len);
RedBearLab 0:4c34e71b2f6a 118
RedBearLab 0:4c34e71b2f6a 119 unsigned char i = 0;
RedBearLab 0:4c34e71b2f6a 120 while(i<len)
RedBearLab 0:4c34e71b2f6a 121 {
RedBearLab 0:4c34e71b2f6a 122 unsigned char data0 = val[i++];
RedBearLab 0:4c34e71b2f6a 123 unsigned char data1 = val[i++];
RedBearLab 0:4c34e71b2f6a 124 unsigned char data2 = val[i++];
RedBearLab 0:4c34e71b2f6a 125
RedBearLab 0:4c34e71b2f6a 126 if (data0 == 0x01) // Command is to control digital out pin
RedBearLab 0:4c34e71b2f6a 127 {
RedBearLab 0:4c34e71b2f6a 128 if (data1 == 0x01)
RedBearLab 0:4c34e71b2f6a 129 digitalWrite(&DIGITAL_OUT_PIN, HIGH);
RedBearLab 0:4c34e71b2f6a 130 else
RedBearLab 0:4c34e71b2f6a 131 digitalWrite(&DIGITAL_OUT_PIN, LOW);
RedBearLab 0:4c34e71b2f6a 132 }
RedBearLab 0:4c34e71b2f6a 133 else if (data0 == 0xA0) // Command is to enable analog in reading
RedBearLab 0:4c34e71b2f6a 134 {
RedBearLab 0:4c34e71b2f6a 135 if (data1 == 0x01)
RedBearLab 0:4c34e71b2f6a 136 analog_enabled = 1;
RedBearLab 0:4c34e71b2f6a 137 else
RedBearLab 0:4c34e71b2f6a 138 analog_enabled = 0;
RedBearLab 0:4c34e71b2f6a 139 }
RedBearLab 0:4c34e71b2f6a 140 else if (data0 == 0x02) // Command is to control PWM pin
RedBearLab 0:4c34e71b2f6a 141 {
RedBearLab 0:4c34e71b2f6a 142 float percent = (1.0 / 255) * data1; // Convert 0~255 to 0%~100%
RedBearLab 0:4c34e71b2f6a 143 pwm.write(percent);
RedBearLab 0:4c34e71b2f6a 144 }
RedBearLab 0:4c34e71b2f6a 145 else if (data0 == 0x03) // Command is to control Servo pin
RedBearLab 0:4c34e71b2f6a 146 {
RedBearLab 0:4c34e71b2f6a 147 myservo.write(data1);
RedBearLab 0:4c34e71b2f6a 148 }
RedBearLab 0:4c34e71b2f6a 149 else if (data0 == 0x04)
RedBearLab 0:4c34e71b2f6a 150 {
RedBearLab 0:4c34e71b2f6a 151 analog_enabled = 0;
RedBearLab 0:4c34e71b2f6a 152 myservo.write(0);
RedBearLab 0:4c34e71b2f6a 153 pwm.write(0.0);
RedBearLab 0:4c34e71b2f6a 154 digitalWrite(&DIGITAL_OUT_PIN, LOW);
RedBearLab 0:4c34e71b2f6a 155 }
RedBearLab 0:4c34e71b2f6a 156 }
RedBearLab 0:4c34e71b2f6a 157 }
RedBearLab 0:4c34e71b2f6a 158
RedBearLab 0:4c34e71b2f6a 159 if (analog_enabled) // if analog reading enabled
RedBearLab 0:4c34e71b2f6a 160 {
RedBearLab 0:4c34e71b2f6a 161 // Read and send out
RedBearLab 0:4c34e71b2f6a 162 unsigned short value = analog.read_u16();
RedBearLab 0:4c34e71b2f6a 163
RedBearLab 0:4c34e71b2f6a 164 const unsigned char val[3] = {0x0B, value >> 8, value};
RedBearLab 0:4c34e71b2f6a 165 txCharacteristic.setValue(val, 3);
RedBearLab 0:4c34e71b2f6a 166 }
RedBearLab 0:4c34e71b2f6a 167
RedBearLab 0:4c34e71b2f6a 168 // If digital in changes, report the state
RedBearLab 0:4c34e71b2f6a 169 if (digitalRead(&DIGITAL_IN_PIN) != old_state)
RedBearLab 0:4c34e71b2f6a 170 {
RedBearLab 0:4c34e71b2f6a 171 old_state = digitalRead(&DIGITAL_IN_PIN);
RedBearLab 0:4c34e71b2f6a 172
RedBearLab 0:4c34e71b2f6a 173 if (digitalRead(&DIGITAL_IN_PIN) == HIGH)
RedBearLab 0:4c34e71b2f6a 174 {
RedBearLab 0:4c34e71b2f6a 175 const unsigned char val[3] = {0x0A, 0x01, 0x00};
RedBearLab 0:4c34e71b2f6a 176 txCharacteristic.setValue(val, 3);
RedBearLab 0:4c34e71b2f6a 177 }
RedBearLab 0:4c34e71b2f6a 178 else
RedBearLab 0:4c34e71b2f6a 179 {
RedBearLab 0:4c34e71b2f6a 180 const unsigned char val[3] = {0x0A, 0x00, 0x00};
RedBearLab 0:4c34e71b2f6a 181 txCharacteristic.setValue(val, 3);
RedBearLab 0:4c34e71b2f6a 182 }
RedBearLab 0:4c34e71b2f6a 183 }
RedBearLab 0:4c34e71b2f6a 184 }
RedBearLab 0:4c34e71b2f6a 185
RedBearLab 0:4c34e71b2f6a 186 analog_enabled = 0;
RedBearLab 0:4c34e71b2f6a 187 digitalWrite(&DIGITAL_OUT_PIN, LOW);
RedBearLab 0:4c34e71b2f6a 188
RedBearLab 0:4c34e71b2f6a 189 // central disconnected
RedBearLab 0:4c34e71b2f6a 190 serial.printf("Disconnected from central\r\n");
RedBearLab 0:4c34e71b2f6a 191 }
RedBearLab 0:4c34e71b2f6a 192 }
RedBearLab 0:4c34e71b2f6a 193 }