Keypad
Dependencies: BLE_API mbed nRF51822
Fork of EmtpyProgram by
main.cpp@1:115afdfbaa84, 2016-07-12 (annotated)
- Committer:
- Cataract
- Date:
- Tue Jul 12 14:35:09 2016 +0000
- Revision:
- 1:115afdfbaa84
- Parent:
- 0:4817f7301801
- Child:
- 2:343f39defab2
sorry for no commits :(
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cataract | 0:4817f7301801 | 1 | #include "mbed.h" |
Cataract | 0:4817f7301801 | 2 | #include "ble/BLE.h" |
Cataract | 0:4817f7301801 | 3 | #include "ble/UUID.h" |
Cataract | 0:4817f7301801 | 4 | #include "ble/GattAttribute.h" |
Cataract | 0:4817f7301801 | 5 | #include "ble/GattService.h" |
Cataract | 0:4817f7301801 | 6 | |
Cataract | 0:4817f7301801 | 7 | DigitalOut alivenessLED(LED1, 0); |
Cataract | 0:4817f7301801 | 8 | DigitalOut alivenessLED2(LED2, 1); |
Cataract | 0:4817f7301801 | 9 | DigitalOut alivenessLED3(LED3, 1); |
Cataract | 1:115afdfbaa84 | 10 | DigitalOut alivenessLED4(LED4, 1); |
Cataract | 0:4817f7301801 | 11 | |
Cataract | 0:4817f7301801 | 12 | Ticker ticker; |
Cataract | 0:4817f7301801 | 13 | |
Cataract | 0:4817f7301801 | 14 | const static char DEVICE_NAME[] = "KeyOpener3k"; |
Cataract | 0:4817f7301801 | 15 | static const char * uuid16_list[] = {"9085495d-f273-443d-9e37-e27c9194f63b"}; |
Cataract | 0:4817f7301801 | 16 | |
Cataract | 0:4817f7301801 | 17 | Serial pc(USBTX, USBRX); |
Cataract | 0:4817f7301801 | 18 | |
Cataract | 1:115afdfbaa84 | 19 | SPI device(P0_14,P0_13,P0_15); |
Cataract | 1:115afdfbaa84 | 20 | DigitalOut cs(P0_12); |
Cataract | 1:115afdfbaa84 | 21 | |
Cataract | 1:115afdfbaa84 | 22 | char * message; |
Cataract | 1:115afdfbaa84 | 23 | char * cpy = (char*) malloc(21); |
Cataract | 1:115afdfbaa84 | 24 | |
Cataract | 1:115afdfbaa84 | 25 | void sendToken() |
Cataract | 1:115afdfbaa84 | 26 | { |
Cataract | 1:115afdfbaa84 | 27 | pc.printf("Token"); |
Cataract | 1:115afdfbaa84 | 28 | alivenessLED4 = !alivenessLED4; |
Cataract | 1:115afdfbaa84 | 29 | |
Cataract | 1:115afdfbaa84 | 30 | //init SPI |
Cataract | 1:115afdfbaa84 | 31 | cs = 1; |
Cataract | 1:115afdfbaa84 | 32 | device.format(8,3); |
Cataract | 1:115afdfbaa84 | 33 | device.frequency(2000000); |
Cataract | 1:115afdfbaa84 | 34 | |
Cataract | 1:115afdfbaa84 | 35 | int len = strlen(message); |
Cataract | 1:115afdfbaa84 | 36 | |
Cataract | 1:115afdfbaa84 | 37 | // Write the length of the message |
Cataract | 1:115afdfbaa84 | 38 | cs = 0; |
Cataract | 1:115afdfbaa84 | 39 | device.write( *((char*) (&len)) ); |
Cataract | 1:115afdfbaa84 | 40 | device.write( *((char*) (&len)+1) ); |
Cataract | 1:115afdfbaa84 | 41 | device.write( *((char*) (&len)+2) ); |
Cataract | 1:115afdfbaa84 | 42 | device.write( *((char*) (&len)+3) ); |
Cataract | 1:115afdfbaa84 | 43 | cs = 1; |
Cataract | 1:115afdfbaa84 | 44 | |
Cataract | 1:115afdfbaa84 | 45 | // Write the message |
Cataract | 1:115afdfbaa84 | 46 | for(int i=0;i<len;i++){ |
Cataract | 1:115afdfbaa84 | 47 | cs = 0; |
Cataract | 1:115afdfbaa84 | 48 | device.write(*(message+i)); |
Cataract | 1:115afdfbaa84 | 49 | cs = 1; |
Cataract | 1:115afdfbaa84 | 50 | } |
Cataract | 1:115afdfbaa84 | 51 | |
Cataract | 1:115afdfbaa84 | 52 | // Free Memory |
Cataract | 1:115afdfbaa84 | 53 | free(message); |
Cataract | 1:115afdfbaa84 | 54 | |
Cataract | 1:115afdfbaa84 | 55 | // Reset board to unblock the BLE-connection |
Cataract | 1:115afdfbaa84 | 56 | NVIC_SystemReset(); |
Cataract | 1:115afdfbaa84 | 57 | } |
Cataract | 1:115afdfbaa84 | 58 | |
Cataract | 1:115afdfbaa84 | 59 | uint32_t countdata = 0; // Number of 20-byte blocks to be written. |
Cataract | 1:115afdfbaa84 | 60 | |
Cataract | 1:115afdfbaa84 | 61 | |
Cataract | 1:115afdfbaa84 | 62 | // Callback for the BLE connection |
Cataract | 1:115afdfbaa84 | 63 | void onDataWrittenCallback(const GattWriteCallbackParams* params) |
Cataract | 1:115afdfbaa84 | 64 | { |
Cataract | 1:115afdfbaa84 | 65 | pc.printf("read"); |
Cataract | 1:115afdfbaa84 | 66 | alivenessLED3 = !alivenessLED3; |
Cataract | 1:115afdfbaa84 | 67 | // if countdata == 0 the data will be interpretated as the Number of 20-byte blocks to be written. |
Cataract | 1:115afdfbaa84 | 68 | if (countdata == 0){ |
Cataract | 1:115afdfbaa84 | 69 | |
Cataract | 1:115afdfbaa84 | 70 | pc.printf("%u\r\n",*(uint32_t *) params->data); |
Cataract | 1:115afdfbaa84 | 71 | |
Cataract | 1:115afdfbaa84 | 72 | countdata = *(uint32_t *) params->data; |
Cataract | 1:115afdfbaa84 | 73 | |
Cataract | 1:115afdfbaa84 | 74 | //Malloc the size of the data to be written. +1 for the 0x00 byte. |
Cataract | 1:115afdfbaa84 | 75 | message = (char*) malloc(sizeof(char)*20*countdata + 1); |
Cataract | 1:115afdfbaa84 | 76 | } else { |
Cataract | 1:115afdfbaa84 | 77 | |
Cataract | 1:115afdfbaa84 | 78 | //If countdata is set the data will be interpretated as a string and concatenated to the previously recieved message |
Cataract | 1:115afdfbaa84 | 79 | pc.printf("Data: %s\r\n %d",params->data,countdata); |
Cataract | 1:115afdfbaa84 | 80 | memcpy(cpy, params->data, 20); |
Cataract | 1:115afdfbaa84 | 81 | cpy[20] = '\0'; |
Cataract | 1:115afdfbaa84 | 82 | strcat(message,cpy); |
Cataract | 1:115afdfbaa84 | 83 | countdata--; |
Cataract | 1:115afdfbaa84 | 84 | |
Cataract | 1:115afdfbaa84 | 85 | // if every block was send over BLE send the token via SPI to the other board. |
Cataract | 1:115afdfbaa84 | 86 | if (countdata == 0){ |
Cataract | 1:115afdfbaa84 | 87 | sendToken(); |
Cataract | 1:115afdfbaa84 | 88 | } |
Cataract | 1:115afdfbaa84 | 89 | } |
Cataract | 1:115afdfbaa84 | 90 | } |
Cataract | 1:115afdfbaa84 | 91 | |
Cataract | 0:4817f7301801 | 92 | void periodicCallback(void) |
Cataract | 0:4817f7301801 | 93 | { |
Cataract | 0:4817f7301801 | 94 | alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ |
Cataract | 0:4817f7301801 | 95 | } |
Cataract | 0:4817f7301801 | 96 | |
Cataract | 1:115afdfbaa84 | 97 | //Write the "reset" byte to the other board and reset |
Cataract | 1:115afdfbaa84 | 98 | void resetCallback(void) |
Cataract | 1:115afdfbaa84 | 99 | { |
Cataract | 1:115afdfbaa84 | 100 | cs = 0; |
Cataract | 1:115afdfbaa84 | 101 | device.write( 0x11 ); |
Cataract | 1:115afdfbaa84 | 102 | cs = 1; |
Cataract | 1:115afdfbaa84 | 103 | NVIC_SystemReset(); |
Cataract | 0:4817f7301801 | 104 | } |
Cataract | 0:4817f7301801 | 105 | |
Cataract | 1:115afdfbaa84 | 106 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
Cataract | 1:115afdfbaa84 | 107 | { |
Cataract | 1:115afdfbaa84 | 108 | // Start a timer for autoreset to "unblock" the board automaticly |
Cataract | 1:115afdfbaa84 | 109 | // after a bad connection |
Cataract | 1:115afdfbaa84 | 110 | ticker.attach(resetCallback,30); |
Cataract | 0:4817f7301801 | 111 | alivenessLED2 = !alivenessLED2; |
Cataract | 0:4817f7301801 | 112 | pc.printf("Connected!\r\n"); |
Cataract | 0:4817f7301801 | 113 | } |
Cataract | 0:4817f7301801 | 114 | |
Cataract | 1:115afdfbaa84 | 115 | UUID uuid = UUID("9085495d-f273-443d-9e37-e27c9194f63b"); |
Cataract | 1:115afdfbaa84 | 116 | UUID uuid_message = UUID("8085495d-f273-443d-9e37-e27c9194f63b"); |
Cataract | 1:115afdfbaa84 | 117 | |
Cataract | 1:115afdfbaa84 | 118 | //Buffer for the service |
Cataract | 1:115afdfbaa84 | 119 | char data[21]; |
Cataract | 0:4817f7301801 | 120 | |
Cataract | 1:115afdfbaa84 | 121 | // Init a service with "uuid" and a "20 char" characteristic with "uuid_message" |
Cataract | 1:115afdfbaa84 | 122 | void initService(BLE &ble) |
Cataract | 1:115afdfbaa84 | 123 | { |
Cataract | 1:115afdfbaa84 | 124 | //Note: 20 bytes are the max. length for BLE |
Cataract | 1:115afdfbaa84 | 125 | WriteOnlyArrayGattCharacteristic<char, 20> messagecharacter = WriteOnlyArrayGattCharacteristic<char, 20>(uuid_message,data); |
Cataract | 0:4817f7301801 | 126 | |
Cataract | 1:115afdfbaa84 | 127 | GattCharacteristic * chars[] = {&messagecharacter}; |
Cataract | 1:115afdfbaa84 | 128 | GattService service = GattService(uuid,chars,1); |
Cataract | 0:4817f7301801 | 129 | ble.gattServer().addService(service); |
Cataract | 0:4817f7301801 | 130 | } |
Cataract | 0:4817f7301801 | 131 | |
Cataract | 0:4817f7301801 | 132 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
Cataract | 0:4817f7301801 | 133 | { |
Cataract | 0:4817f7301801 | 134 | BLE &ble = params->ble; |
Cataract | 0:4817f7301801 | 135 | ble_error_t error = params->error; |
Cataract | 0:4817f7301801 | 136 | |
Cataract | 0:4817f7301801 | 137 | if (error != BLE_ERROR_NONE) { |
Cataract | 0:4817f7301801 | 138 | return; |
Cataract | 0:4817f7301801 | 139 | } |
Cataract | 1:115afdfbaa84 | 140 | |
Cataract | 0:4817f7301801 | 141 | ble.gap().onConnection(connectionCallback); |
Cataract | 0:4817f7301801 | 142 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
Cataract | 0:4817f7301801 | 143 | |
Cataract | 0:4817f7301801 | 144 | initService(ble); |
Cataract | 0:4817f7301801 | 145 | |
Cataract | 0:4817f7301801 | 146 | /* Setup advertising. */ |
Cataract | 0:4817f7301801 | 147 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Cataract | 0:4817f7301801 | 148 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Cataract | 0:4817f7301801 | 149 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Cataract | 0:4817f7301801 | 150 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Cataract | 0:4817f7301801 | 151 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
Cataract | 0:4817f7301801 | 152 | ble.gap().startAdvertising(); |
Cataract | 0:4817f7301801 | 153 | } |
Cataract | 0:4817f7301801 | 154 | |
Cataract | 0:4817f7301801 | 155 | int main(void) |
Cataract | 0:4817f7301801 | 156 | { |
Cataract | 1:115afdfbaa84 | 157 | //Init BLE |
Cataract | 0:4817f7301801 | 158 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
Cataract | 0:4817f7301801 | 159 | ble.init(bleInitComplete); |
Cataract | 0:4817f7301801 | 160 | |
Cataract | 0:4817f7301801 | 161 | while (ble.hasInitialized() == false) { /* spin loop */ } |
Cataract | 1:115afdfbaa84 | 162 | |
Cataract | 1:115afdfbaa84 | 163 | ticker.attach(periodicCallback, 1); /* Blink LED every second */ |
Cataract | 0:4817f7301801 | 164 | |
Cataract | 1:115afdfbaa84 | 165 | pc.printf("init\r\n"); |
Cataract | 1:115afdfbaa84 | 166 | while (1) { |
Cataract | 0:4817f7301801 | 167 | ble.waitForEvent(); |
Cataract | 0:4817f7301801 | 168 | } |
Cataract | 0:4817f7301801 | 169 | } |