Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of Tiny_BLE_Simple by
main.cpp
00001 00002 #include "mbed.h" 00003 #include "nrf51.h" 00004 #include "nrf51_bitfields.h" 00005 #include <string> 00006 00007 #include "BLE.h" 00008 #include "DFUService.h" 00009 #include "UARTService.h" 00010 00011 #define LED_GREEN p21 00012 #define LED_RED p22 00013 #define LED_BLUE p23 00014 #define BUTTON_PIN p17 00015 #define BATTERY_PIN p1 00016 00017 #define UART_TX p9 00018 #define UART_RX p11 00019 #define UART_CTS p8 00020 #define UART_RTS p10 00021 00022 InterruptIn button(BUTTON_PIN); 00023 AnalogIn battery(BATTERY_PIN); 00024 00025 00026 00027 int read_none_count = 0; 00028 00029 BLEDevice ble; 00030 UARTService *uartServicePtr; 00031 00032 volatile bool bleIsConnected = false; 00033 volatile uint8_t tick_event = 0; 00034 00035 00036 00037 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00038 { 00039 bleIsConnected = true; 00040 } 00041 00042 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams) 00043 { 00044 ble.startAdvertising(); 00045 bleIsConnected = false; 00046 } 00047 00048 void detect(void) 00049 { 00050 //LOG("Button pressed\n"); 00051 //blue = !blue; 00052 } 00053 00054 void led_init(void) 00055 { 00056 00057 //set LED pins to digital output 00058 NRF_GPIO->PIN_CNF[LED_BLUE] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) 00059 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) 00060 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) 00061 | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) 00062 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); 00063 00064 NRF_GPIO->PIN_CNF[LED_GREEN] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) 00065 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) 00066 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) 00067 | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) 00068 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); 00069 00070 NRF_GPIO->PIN_CNF[LED_RED] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) 00071 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) 00072 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) 00073 | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) 00074 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); 00075 // NRF_GPIO - #define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE) 00076 // NRF_GPIO_Type - struct containing possible settings (DIR, INPUT, PULL, DRIVE, SENSE, as well as PIN_CNF[] (configuration array for all 32 outputs) 00077 // NRF_GPIO_BASE - #define NRF_GPIO_BASE 0x50000000UL //base register address for GPIO settings 00078 // -> just a way of accessing PIN_CNF[pin_we_want_to_mess_with], object oriented programming stuff 00079 // GPIO_PIN_CNF_SENSE_Disabled //the appropriate binary value to set sense as disabled 00080 // << //bit shift 00081 // GPIO_PIN_CNF_SENSE_pos //position that the value of GPIO_PIN_CNF_SENSE_Disabled should be placed at 00082 // | //OR all of these values together to set the register 00083 } 00084 00085 00086 int main(void) 00087 { 00088 00089 led_init(); 00090 00091 //Turn all LEDs off 00092 NRF_GPIO->OUTSET = (1UL << LED_BLUE); //remember that writing zeros with outset and outclear has no effect 00093 NRF_GPIO->OUTSET = (1UL << LED_RED); 00094 NRF_GPIO->OUTSET = (1UL << LED_GREEN); 00095 00096 wait(1); 00097 00098 button.fall(detect); 00099 00100 //Initialising the nRF51822 00101 ble.init(); 00102 ble.gap().onDisconnection(disconnectionCallback); 00103 ble.gap().onConnection(connectionCallback); 00104 00105 00106 /* setup advertising */ 00107 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00108 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00109 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00110 (const uint8_t *)"PWMtest", sizeof("PWMtest")); 00111 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00112 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00113 DFUService dfu(ble); 00114 UARTService uartService(ble); 00115 uartServicePtr = &uartService; 00116 //uartService.retargetStdout(); 00117 00118 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ 00119 ble.gap().startAdvertising(); 00120 char str[8]; 00121 str[5]='\r'; 00122 str[6]='\n'; 00123 str[7]='\0'; 00124 00125 //int rgb_res = 8; //RGB resolution, TODO define this as a constant later 00126 int vals[3] = {0}; 00127 bool pwmvals[3] = {0}; 00128 00129 00130 while (true) { 00131 ble.waitForEvent(); 00132 str[0]=uartService._getc();//set 00133 if (str[0]=='S') 00134 { 00135 00136 str[1]=uartService._getc();//R 00137 str[2]=uartService._getc();//G 00138 str[3]=uartService._getc();//B 00139 str[4]=uartService._getc(); 00140 00141 vals[0] = str[1]-0x30;//convert from char to int 00142 vals[1] = str[2]-0x30; 00143 vals[2] = str[3]-0x30; 00144 uartService.writeString(str); 00145 00146 str[0] = 0; 00147 } 00148 00149 for(uint32_t j = 100000; j>0; j--)//display color for a couple seconds 00150 { 00151 for(int i = 8; i>0; i--)// 00152 { 00153 pwmvals[0] = (i - vals[0] == 0);//control brightness by turning LED on after x iterations 00154 pwmvals[1] = (i - vals[1] == 0);//could compare directly to str and save an array 00155 pwmvals[2] = (i - vals[2] == 0); 00156 NRF_GPIO->OUTCLR = (pwmvals[0] << LED_RED)|(pwmvals[1] << LED_GREEN)|(pwmvals[2] << LED_BLUE); 00157 00158 } 00159 NRF_GPIO->OUTSET = (0x7UL << LED_GREEN);//turn all 3 off 00160 } 00161 } 00162 } 00163 00164
Generated on Sat Jul 16 2022 15:24:06 by
1.7.2
