Created a new repository to start development at https://developer.mbed.org/users/rjpope42/code/simpleWatch/
Dependencies: BLE_API mbed nRF51822
Fork of Tiny_BLE_Simple by
Created a new repository to start development at https://developer.mbed.org/users/rjpope42/code/simpleWatch/
main.cpp@5:f2e77e62e395, 2016-05-01 (annotated)
- Committer:
- rjpope42
- Date:
- Sun May 01 06:16:55 2016 +0000
- Revision:
- 5:f2e77e62e395
- Parent:
- 4:bb933be5a507
Working!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:26da608265f8 | 1 | |
yihui | 0:26da608265f8 | 2 | #include "mbed.h" |
yihui | 2:b61ddbb8528e | 3 | #include "nrf51.h" |
yihui | 2:b61ddbb8528e | 4 | #include "nrf51_bitfields.h" |
rjpope42 | 5:f2e77e62e395 | 5 | #include <string> |
yihui | 0:26da608265f8 | 6 | |
yihui | 3:24e365bd1b97 | 7 | #include "BLE.h" |
yihui | 0:26da608265f8 | 8 | #include "DFUService.h" |
yihui | 0:26da608265f8 | 9 | #include "UARTService.h" |
yihui | 0:26da608265f8 | 10 | |
yihui | 0:26da608265f8 | 11 | #define LED_GREEN p21 |
yihui | 0:26da608265f8 | 12 | #define LED_RED p22 |
yihui | 0:26da608265f8 | 13 | #define LED_BLUE p23 |
yihui | 0:26da608265f8 | 14 | #define BUTTON_PIN p17 |
yihui | 0:26da608265f8 | 15 | #define BATTERY_PIN p1 |
yihui | 0:26da608265f8 | 16 | |
yihui | 0:26da608265f8 | 17 | #define UART_TX p9 |
yihui | 0:26da608265f8 | 18 | #define UART_RX p11 |
yihui | 0:26da608265f8 | 19 | #define UART_CTS p8 |
yihui | 0:26da608265f8 | 20 | #define UART_RTS p10 |
yihui | 0:26da608265f8 | 21 | |
yihui | 0:26da608265f8 | 22 | InterruptIn button(BUTTON_PIN); |
yihui | 0:26da608265f8 | 23 | AnalogIn battery(BATTERY_PIN); |
rjpope42 | 5:f2e77e62e395 | 24 | |
yihui | 0:26da608265f8 | 25 | |
yihui | 0:26da608265f8 | 26 | |
yihui | 2:b61ddbb8528e | 27 | int read_none_count = 0; |
yihui | 2:b61ddbb8528e | 28 | |
yihui | 0:26da608265f8 | 29 | BLEDevice ble; |
yihui | 0:26da608265f8 | 30 | UARTService *uartServicePtr; |
yihui | 0:26da608265f8 | 31 | |
yihui | 0:26da608265f8 | 32 | volatile bool bleIsConnected = false; |
yihui | 0:26da608265f8 | 33 | volatile uint8_t tick_event = 0; |
yihui | 0:26da608265f8 | 34 | |
yihui | 0:26da608265f8 | 35 | |
yihui | 2:b61ddbb8528e | 36 | |
yihui | 3:24e365bd1b97 | 37 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
yihui | 0:26da608265f8 | 38 | { |
yihui | 0:26da608265f8 | 39 | bleIsConnected = true; |
yihui | 0:26da608265f8 | 40 | } |
yihui | 0:26da608265f8 | 41 | |
yihui | 3:24e365bd1b97 | 42 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams) |
yihui | 0:26da608265f8 | 43 | { |
yihui | 0:26da608265f8 | 44 | ble.startAdvertising(); |
yihui | 0:26da608265f8 | 45 | bleIsConnected = false; |
yihui | 0:26da608265f8 | 46 | } |
yihui | 0:26da608265f8 | 47 | |
yihui | 0:26da608265f8 | 48 | void detect(void) |
yihui | 0:26da608265f8 | 49 | { |
rjpope42 | 5:f2e77e62e395 | 50 | //LOG("Button pressed\n"); |
rjpope42 | 5:f2e77e62e395 | 51 | //blue = !blue; |
yihui | 0:26da608265f8 | 52 | } |
yihui | 0:26da608265f8 | 53 | |
rjpope42 | 5:f2e77e62e395 | 54 | void led_init(void) |
rjpope42 | 5:f2e77e62e395 | 55 | { |
rjpope42 | 5:f2e77e62e395 | 56 | |
rjpope42 | 5:f2e77e62e395 | 57 | //set LED pins to digital output |
rjpope42 | 5:f2e77e62e395 | 58 | NRF_GPIO->PIN_CNF[LED_BLUE] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
rjpope42 | 5:f2e77e62e395 | 59 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
rjpope42 | 5:f2e77e62e395 | 60 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
rjpope42 | 5:f2e77e62e395 | 61 | | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) |
rjpope42 | 5:f2e77e62e395 | 62 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
yihui | 0:26da608265f8 | 63 | |
rjpope42 | 5:f2e77e62e395 | 64 | NRF_GPIO->PIN_CNF[LED_GREEN] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
rjpope42 | 5:f2e77e62e395 | 65 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
rjpope42 | 5:f2e77e62e395 | 66 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
rjpope42 | 5:f2e77e62e395 | 67 | | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) |
rjpope42 | 5:f2e77e62e395 | 68 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
rjpope42 | 5:f2e77e62e395 | 69 | |
rjpope42 | 5:f2e77e62e395 | 70 | NRF_GPIO->PIN_CNF[LED_RED] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
rjpope42 | 5:f2e77e62e395 | 71 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
rjpope42 | 5:f2e77e62e395 | 72 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
rjpope42 | 5:f2e77e62e395 | 73 | | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) |
rjpope42 | 5:f2e77e62e395 | 74 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
rjpope42 | 5:f2e77e62e395 | 75 | // NRF_GPIO - #define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE) |
rjpope42 | 5:f2e77e62e395 | 76 | // NRF_GPIO_Type - struct containing possible settings (DIR, INPUT, PULL, DRIVE, SENSE, as well as PIN_CNF[] (configuration array for all 32 outputs) |
rjpope42 | 5:f2e77e62e395 | 77 | // NRF_GPIO_BASE - #define NRF_GPIO_BASE 0x50000000UL //base register address for GPIO settings |
rjpope42 | 5:f2e77e62e395 | 78 | // -> just a way of accessing PIN_CNF[pin_we_want_to_mess_with], object oriented programming stuff |
rjpope42 | 5:f2e77e62e395 | 79 | // GPIO_PIN_CNF_SENSE_Disabled //the appropriate binary value to set sense as disabled |
rjpope42 | 5:f2e77e62e395 | 80 | // << //bit shift |
rjpope42 | 5:f2e77e62e395 | 81 | // GPIO_PIN_CNF_SENSE_pos //position that the value of GPIO_PIN_CNF_SENSE_Disabled should be placed at |
rjpope42 | 5:f2e77e62e395 | 82 | // | //OR all of these values together to set the register |
rjpope42 | 5:f2e77e62e395 | 83 | } |
rjpope42 | 5:f2e77e62e395 | 84 | |
yihui | 0:26da608265f8 | 85 | |
yihui | 0:26da608265f8 | 86 | int main(void) |
yihui | 0:26da608265f8 | 87 | { |
rjpope42 | 5:f2e77e62e395 | 88 | |
rjpope42 | 5:f2e77e62e395 | 89 | led_init(); |
yihui | 2:b61ddbb8528e | 90 | |
rjpope42 | 5:f2e77e62e395 | 91 | //Turn all LEDs off |
rjpope42 | 5:f2e77e62e395 | 92 | NRF_GPIO->OUTSET = (1UL << LED_BLUE); //remember that writing zeros with outset and outclear has no effect |
rjpope42 | 5:f2e77e62e395 | 93 | NRF_GPIO->OUTSET = (1UL << LED_RED); |
rjpope42 | 5:f2e77e62e395 | 94 | NRF_GPIO->OUTSET = (1UL << LED_GREEN); |
rjpope42 | 5:f2e77e62e395 | 95 | |
yihui | 2:b61ddbb8528e | 96 | wait(1); |
yihui | 2:b61ddbb8528e | 97 | |
yihui | 2:b61ddbb8528e | 98 | button.fall(detect); |
yihui | 2:b61ddbb8528e | 99 | |
rjpope42 | 5:f2e77e62e395 | 100 | //Initialising the nRF51822 |
yihui | 2:b61ddbb8528e | 101 | ble.init(); |
yihui | 3:24e365bd1b97 | 102 | ble.gap().onDisconnection(disconnectionCallback); |
yihui | 3:24e365bd1b97 | 103 | ble.gap().onConnection(connectionCallback); |
yihui | 2:b61ddbb8528e | 104 | |
yihui | 2:b61ddbb8528e | 105 | |
yihui | 2:b61ddbb8528e | 106 | /* setup advertising */ |
yihui | 2:b61ddbb8528e | 107 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
yihui | 2:b61ddbb8528e | 108 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
yihui | 2:b61ddbb8528e | 109 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
rjpope42 | 5:f2e77e62e395 | 110 | (const uint8_t *)"PWMtest", sizeof("PWMtest")); |
yihui | 2:b61ddbb8528e | 111 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
yihui | 2:b61ddbb8528e | 112 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
yihui | 2:b61ddbb8528e | 113 | DFUService dfu(ble); |
yihui | 2:b61ddbb8528e | 114 | UARTService uartService(ble); |
yihui | 2:b61ddbb8528e | 115 | uartServicePtr = &uartService; |
yihui | 2:b61ddbb8528e | 116 | //uartService.retargetStdout(); |
yihui | 2:b61ddbb8528e | 117 | |
yihui | 2:b61ddbb8528e | 118 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
yihui | 3:24e365bd1b97 | 119 | ble.gap().startAdvertising(); |
rjpope42 | 5:f2e77e62e395 | 120 | char str[8]; |
rjpope42 | 5:f2e77e62e395 | 121 | str[5]='\r'; |
rjpope42 | 5:f2e77e62e395 | 122 | str[6]='\n'; |
rjpope42 | 5:f2e77e62e395 | 123 | str[7]='\0'; |
rjpope42 | 5:f2e77e62e395 | 124 | |
rjpope42 | 5:f2e77e62e395 | 125 | //int rgb_res = 8; //RGB resolution, TODO define this as a constant later |
rjpope42 | 5:f2e77e62e395 | 126 | int vals[3] = {0}; |
rjpope42 | 5:f2e77e62e395 | 127 | bool pwmvals[3] = {0}; |
rjpope42 | 5:f2e77e62e395 | 128 | |
yihui | 2:b61ddbb8528e | 129 | |
yihui | 0:26da608265f8 | 130 | while (true) { |
wd5gnr | 4:bb933be5a507 | 131 | ble.waitForEvent(); |
rjpope42 | 5:f2e77e62e395 | 132 | str[0]=uartService._getc();//set |
rjpope42 | 5:f2e77e62e395 | 133 | if (str[0]=='S') |
rjpope42 | 5:f2e77e62e395 | 134 | { |
rjpope42 | 5:f2e77e62e395 | 135 | |
rjpope42 | 5:f2e77e62e395 | 136 | str[1]=uartService._getc();//R |
rjpope42 | 5:f2e77e62e395 | 137 | str[2]=uartService._getc();//G |
rjpope42 | 5:f2e77e62e395 | 138 | str[3]=uartService._getc();//B |
rjpope42 | 5:f2e77e62e395 | 139 | str[4]=uartService._getc(); |
rjpope42 | 5:f2e77e62e395 | 140 | |
rjpope42 | 5:f2e77e62e395 | 141 | vals[0] = str[1]-0x30;//convert from char to int |
rjpope42 | 5:f2e77e62e395 | 142 | vals[1] = str[2]-0x30; |
rjpope42 | 5:f2e77e62e395 | 143 | vals[2] = str[3]-0x30; |
rjpope42 | 5:f2e77e62e395 | 144 | uartService.writeString(str); |
rjpope42 | 5:f2e77e62e395 | 145 | |
rjpope42 | 5:f2e77e62e395 | 146 | str[0] = 0; |
rjpope42 | 5:f2e77e62e395 | 147 | } |
wd5gnr | 4:bb933be5a507 | 148 | |
rjpope42 | 5:f2e77e62e395 | 149 | for(uint32_t j = 100000; j>0; j--)//display color for a couple seconds |
rjpope42 | 5:f2e77e62e395 | 150 | { |
rjpope42 | 5:f2e77e62e395 | 151 | for(int i = 8; i>0; i--)// |
rjpope42 | 5:f2e77e62e395 | 152 | { |
rjpope42 | 5:f2e77e62e395 | 153 | pwmvals[0] = (i - vals[0] == 0);//control brightness by turning LED on after x iterations |
rjpope42 | 5:f2e77e62e395 | 154 | pwmvals[1] = (i - vals[1] == 0);//could compare directly to str and save an array |
rjpope42 | 5:f2e77e62e395 | 155 | pwmvals[2] = (i - vals[2] == 0); |
rjpope42 | 5:f2e77e62e395 | 156 | NRF_GPIO->OUTCLR = (pwmvals[0] << LED_RED)|(pwmvals[1] << LED_GREEN)|(pwmvals[2] << LED_BLUE); |
rjpope42 | 5:f2e77e62e395 | 157 | |
rjpope42 | 5:f2e77e62e395 | 158 | } |
rjpope42 | 5:f2e77e62e395 | 159 | NRF_GPIO->OUTSET = (0x7UL << LED_GREEN);//turn all 3 off |
yihui | 0:26da608265f8 | 160 | } |
yihui | 0:26da608265f8 | 161 | } |
rjpope42 | 5:f2e77e62e395 | 162 | } |
yihui | 0:26da608265f8 | 163 | |
yihui | 0:26da608265f8 | 164 |