this is ble keypad for nrf52832,ble nano.with ble uart code
Fork of mbed-os-example-ble-BatteryLevel by
source/main.cpp@63:a767379af092, 2018-06-25 (annotated)
- Committer:
- linuxsonu
- Date:
- Mon Jun 25 03:16:04 2018 +0000
- Revision:
- 63:a767379af092
- Parent:
- 46:6b66d08f304e
keypad
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 13:eaa1343657a8 | 1 | #include <events/mbed_events.h> |
| mbed_official | 4:52bffaff7f0b | 2 | #include <mbed.h> |
| mbed_official | 4:52bffaff7f0b | 3 | #include "ble/BLE.h" |
| linuxsonu | 63:a767379af092 | 4 | #include "keypad.h" |
| mbed_official | 4:52bffaff7f0b | 5 | #include "ble/Gap.h" |
| linuxsonu | 63:a767379af092 | 6 | #include "ble/services/UARTService.h" |
| mbed_official | 4:52bffaff7f0b | 7 | |
| linuxsonu | 63:a767379af092 | 8 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
| linuxsonu | 63:a767379af092 | 9 | * it will have an impact on code-size and power consumption. */ |
| linuxsonu | 63:a767379af092 | 10 | #if NEED_CONSOLE_OUTPUT |
| linuxsonu | 63:a767379af092 | 11 | #define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } |
| linuxsonu | 63:a767379af092 | 12 | #else |
| linuxsonu | 63:a767379af092 | 13 | #define DEBUG(...) /* nothing */ |
| linuxsonu | 63:a767379af092 | 14 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
| linuxsonu | 63:a767379af092 | 15 | |
| linuxsonu | 63:a767379af092 | 16 | Timer timerset; |
| linuxsonu | 63:a767379af092 | 17 | const static char DEVICE_NAME[] = "KeyPad"; |
| linuxsonu | 63:a767379af092 | 18 | static UARTService* uartServicePtr; |
| linuxsonu | 63:a767379af092 | 19 | Thread keyThread; |
| linuxsonu | 63:a767379af092 | 20 | int mainData=0; |
| linuxsonu | 63:a767379af092 | 21 | Serial pc(USBTX, USBRX); |
| linuxsonu | 63:a767379af092 | 22 | char pre; |
| linuxsonu | 63:a767379af092 | 23 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
| mbed_official | 4:52bffaff7f0b | 24 | |
| linuxsonu | 63:a767379af092 | 25 | void keyThreadFun(){ |
| linuxsonu | 63:a767379af092 | 26 | Keypad keypad(p19,p18,p17,p16,p25,p24,p23,p20); |
| linuxsonu | 63:a767379af092 | 27 | keypad.enablePullUp(); |
| linuxsonu | 63:a767379af092 | 28 | char key; |
| linuxsonu | 63:a767379af092 | 29 | bool done=false; |
| linuxsonu | 63:a767379af092 | 30 | int data=0; |
| linuxsonu | 63:a767379af092 | 31 | pc.printf("Please touch a key on the keypad\n\r"); |
| linuxsonu | 63:a767379af092 | 32 | while (1) |
| linuxsonu | 63:a767379af092 | 33 | { |
| linuxsonu | 63:a767379af092 | 34 | key = keypad.getKey(); |
| linuxsonu | 63:a767379af092 | 35 | if(key != KEY_RELEASED) |
| linuxsonu | 63:a767379af092 | 36 | { |
| linuxsonu | 63:a767379af092 | 37 | if(key!='*' && key!='#' && key!='(' && key!=')' && key!=']' && key!='@'){ |
| linuxsonu | 63:a767379af092 | 38 | if(!done){ |
| linuxsonu | 63:a767379af092 | 39 | timerset.start(); |
| linuxsonu | 63:a767379af092 | 40 | data=data*10 + (key-'0'); |
| linuxsonu | 63:a767379af092 | 41 | } |
| linuxsonu | 63:a767379af092 | 42 | } |
| linuxsonu | 63:a767379af092 | 43 | if(key==']'){ |
| linuxsonu | 63:a767379af092 | 44 | timerset.stop(); |
| linuxsonu | 63:a767379af092 | 45 | done=true; |
| linuxsonu | 63:a767379af092 | 46 | } |
| linuxsonu | 63:a767379af092 | 47 | pc.printf("%c\r\n",key); |
| linuxsonu | 63:a767379af092 | 48 | wait(0.6); |
| linuxsonu | 63:a767379af092 | 49 | } |
| linuxsonu | 63:a767379af092 | 50 | if(timerset.read()>=60){ |
| linuxsonu | 63:a767379af092 | 51 | mainData=data; |
| linuxsonu | 63:a767379af092 | 52 | data=0; |
| linuxsonu | 63:a767379af092 | 53 | } |
| linuxsonu | 63:a767379af092 | 54 | } |
| linuxsonu | 63:a767379af092 | 55 | |
| linuxsonu | 63:a767379af092 | 56 | } |
| mbed_official | 4:52bffaff7f0b | 57 | |
| linuxsonu | 63:a767379af092 | 58 | void updateVal(char v){ |
| linuxsonu | 63:a767379af092 | 59 | char data[4]; |
| linuxsonu | 63:a767379af092 | 60 | sprintf(data, "%c",v); |
| linuxsonu | 63:a767379af092 | 61 | uartServicePtr->writeString(data); |
| linuxsonu | 63:a767379af092 | 62 | uartServicePtr->writeString("\n"); |
| linuxsonu | 63:a767379af092 | 63 | } |
| mbed_official | 4:52bffaff7f0b | 64 | |
| mbed_official | 4:52bffaff7f0b | 65 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
| mbed_official | 4:52bffaff7f0b | 66 | { |
| mbed_official | 4:52bffaff7f0b | 67 | BLE::Instance().gap().startAdvertising(); |
| mbed_official | 4:52bffaff7f0b | 68 | } |
| mbed_official | 4:52bffaff7f0b | 69 | |
| mbed_official | 4:52bffaff7f0b | 70 | |
| linuxsonu | 63:a767379af092 | 71 | /*void dataReadCallback( const GattReadCallbackParams *params ) |
| mbed_official | 4:52bffaff7f0b | 72 | { |
| linuxsonu | 63:a767379af092 | 73 | DEBUG("Receive\r\n"); |
| linuxsonu | 63:a767379af092 | 74 | }*/ |
| linuxsonu | 63:a767379af092 | 75 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
| linuxsonu | 63:a767379af092 | 76 | if ((uartServicePtr != NULL)) { |
| linuxsonu | 63:a767379af092 | 77 | pre = *(params->data); |
| linuxsonu | 63:a767379af092 | 78 | |
| mbed_official | 4:52bffaff7f0b | 79 | } |
| mbed_official | 4:52bffaff7f0b | 80 | } |
| mbed_official | 4:52bffaff7f0b | 81 | |
| mbed_official | 4:52bffaff7f0b | 82 | void onBleInitError(BLE &ble, ble_error_t error) |
| mbed_official | 4:52bffaff7f0b | 83 | { |
| linuxsonu | 63:a767379af092 | 84 | (void)ble; |
| linuxsonu | 63:a767379af092 | 85 | (void)error; |
| mbed_official | 4:52bffaff7f0b | 86 | } |
| mbed_official | 4:52bffaff7f0b | 87 | |
| linuxsonu | 63:a767379af092 | 88 | void periodicCallback(void) |
| linuxsonu | 63:a767379af092 | 89 | { |
| linuxsonu | 63:a767379af092 | 90 | updateVal('time'); |
| linuxsonu | 63:a767379af092 | 91 | updateVal(pre); |
| linuxsonu | 63:a767379af092 | 92 | updateVal(mainData); |
| mbed_official | 46:6b66d08f304e | 93 | } |
| mbed_official | 46:6b66d08f304e | 94 | |
| linuxsonu | 63:a767379af092 | 95 | |
| linuxsonu | 63:a767379af092 | 96 | |
| mbed_official | 4:52bffaff7f0b | 97 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
| mbed_official | 4:52bffaff7f0b | 98 | { |
| mbed_official | 4:52bffaff7f0b | 99 | BLE& ble = params->ble; |
| mbed_official | 4:52bffaff7f0b | 100 | ble_error_t error = params->error; |
| mbed_official | 4:52bffaff7f0b | 101 | |
| mbed_official | 4:52bffaff7f0b | 102 | if (error != BLE_ERROR_NONE) { |
| mbed_official | 4:52bffaff7f0b | 103 | onBleInitError(ble, error); |
| mbed_official | 4:52bffaff7f0b | 104 | return; |
| mbed_official | 4:52bffaff7f0b | 105 | } |
| mbed_official | 4:52bffaff7f0b | 106 | |
| linuxsonu | 63:a767379af092 | 107 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
| mbed_official | 4:52bffaff7f0b | 108 | return; |
| mbed_official | 4:52bffaff7f0b | 109 | } |
| mbed_official | 4:52bffaff7f0b | 110 | |
| mbed_official | 4:52bffaff7f0b | 111 | ble.gap().onDisconnection(disconnectionCallback); |
| linuxsonu | 63:a767379af092 | 112 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
| mbed_official | 4:52bffaff7f0b | 113 | |
| linuxsonu | 63:a767379af092 | 114 | |
| linuxsonu | 63:a767379af092 | 115 | /* Setup primary service. */ |
| linuxsonu | 63:a767379af092 | 116 | uartServicePtr = new UARTService(ble); |
| linuxsonu | 63:a767379af092 | 117 | |
| mbed_official | 4:52bffaff7f0b | 118 | |
| linuxsonu | 63:a767379af092 | 119 | /* Setup advertising. */ |
| mbed_official | 4:52bffaff7f0b | 120 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
| mbed_official | 4:52bffaff7f0b | 121 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| linuxsonu | 63:a767379af092 | 122 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
| linuxsonu | 63:a767379af092 | 123 | |
| linuxsonu | 63:a767379af092 | 124 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
| linuxsonu | 63:a767379af092 | 125 | |
| linuxsonu | 63:a767379af092 | 126 | ble.gap().setAdvertisingInterval(100); /* 100ms */ |
| mbed_official | 4:52bffaff7f0b | 127 | ble.gap().startAdvertising(); |
| mbed_official | 4:52bffaff7f0b | 128 | } |
| mbed_official | 4:52bffaff7f0b | 129 | |
| mbed_official | 4:52bffaff7f0b | 130 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
| mbed_official | 4:52bffaff7f0b | 131 | BLE &ble = BLE::Instance(); |
| mbed_official | 13:eaa1343657a8 | 132 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
| mbed_official | 4:52bffaff7f0b | 133 | } |
| mbed_official | 4:52bffaff7f0b | 134 | |
| mbed_official | 4:52bffaff7f0b | 135 | int main() |
| mbed_official | 4:52bffaff7f0b | 136 | { |
| linuxsonu | 63:a767379af092 | 137 | Ticker ticker; |
| linuxsonu | 63:a767379af092 | 138 | ticker.attach(periodicCallback,900); |
| mbed_official | 4:52bffaff7f0b | 139 | BLE &ble = BLE::Instance(); |
| mbed_official | 4:52bffaff7f0b | 140 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
| mbed_official | 4:52bffaff7f0b | 141 | ble.init(bleInitComplete); |
| linuxsonu | 63:a767379af092 | 142 | keyThread.start(keyThreadFun); |
| mbed_official | 13:eaa1343657a8 | 143 | eventQueue.dispatch_forever(); |
| mbed_official | 4:52bffaff7f0b | 144 | return 0; |
| mbed_official | 4:52bffaff7f0b | 145 | } |
| linuxsonu | 63:a767379af092 | 146 | |
| linuxsonu | 63:a767379af092 | 147 | |
| linuxsonu | 63:a767379af092 | 148 |
