this is ble keypad for nrf52832,ble nano.with ble uart code
Fork of mbed-os-example-ble-BatteryLevel by
Revision 63:a767379af092, committed 2018-06-25
- Comitter:
- linuxsonu
- Date:
- Mon Jun 25 03:16:04 2018 +0000
- Parent:
- 62:42a62fe25296
- Commit message:
- keypad
Changed in this revision
| keypad.lib | Show annotated file Show diff for this revision Revisions of this file |
| source/main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keypad.lib Mon Jun 25 03:16:04 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/linuxsonu/code/keypad/#2fcef3feb747
--- a/source/main.cpp Mon Jun 11 18:00:08 2018 +0100
+++ b/source/main.cpp Mon Jun 25 03:16:04 2018 +0000
@@ -1,113 +1,130 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2014 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
#include <events/mbed_events.h>
#include <mbed.h>
#include "ble/BLE.h"
+#include "keypad.h"
#include "ble/Gap.h"
-#include "ble/services/BatteryService.h"
+#include "ble/services/UARTService.h"
-DigitalOut led1(LED1, 1);
+#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
+ * it will have an impact on code-size and power consumption. */
+#if NEED_CONSOLE_OUTPUT
+#define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); }
+#else
+#define DEBUG(...) /* nothing */
+#endif /* #if NEED_CONSOLE_OUTPUT */
+
+Timer timerset;
+const static char DEVICE_NAME[] = "KeyPad";
+static UARTService* uartServicePtr;
+Thread keyThread;
+int mainData=0;
+Serial pc(USBTX, USBRX);
+char pre;
+static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
-const static char DEVICE_NAME[] = "BATTERY";
-static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE};
+void keyThreadFun(){
+ Keypad keypad(p19,p18,p17,p16,p25,p24,p23,p20);
+ keypad.enablePullUp();
+ char key;
+ bool done=false;
+ int data=0;
+ pc.printf("Please touch a key on the keypad\n\r");
+ while (1)
+ {
+ key = keypad.getKey();
+ if(key != KEY_RELEASED)
+ {
+ if(key!='*' && key!='#' && key!='(' && key!=')' && key!=']' && key!='@'){
+ if(!done){
+ timerset.start();
+ data=data*10 + (key-'0');
+ }
+ }
+ if(key==']'){
+ timerset.stop();
+ done=true;
+ }
+ pc.printf("%c\r\n",key);
+ wait(0.6);
+ }
+ if(timerset.read()>=60){
+ mainData=data;
+ data=0;
+ }
+ }
+
+}
-static uint8_t batteryLevel = 50;
-static BatteryService* batteryServicePtr;
-
-static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
+void updateVal(char v){
+ char data[4];
+ sprintf(data, "%c",v);
+ uartServicePtr->writeString(data);
+ uartServicePtr->writeString("\n");
+}
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
BLE::Instance().gap().startAdvertising();
}
-void updateSensorValue() {
- batteryLevel++;
- if (batteryLevel > 100) {
- batteryLevel = 20;
- }
- batteryServicePtr->updateBatteryLevel(batteryLevel);
-}
-
-void blinkCallback(void)
+/*void dataReadCallback( const GattReadCallbackParams *params )
{
- led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
-
- BLE &ble = BLE::Instance();
- if (ble.gap().getState().connected) {
- eventQueue.call(updateSensorValue);
+ DEBUG("Receive\r\n");
+}*/
+void onDataWrittenCallback(const GattWriteCallbackParams *params) {
+ if ((uartServicePtr != NULL)) {
+ pre = *(params->data);
+
}
}
-/**
- * This function is called when the ble initialization process has failled
- */
void onBleInitError(BLE &ble, ble_error_t error)
{
- /* Initialization error handling should go here */
+ (void)ble;
+ (void)error;
}
-void printMacAddress()
-{
- /* Print out device MAC address to the console*/
- Gap::AddressType_t addr_type;
- Gap::Address_t address;
- BLE::Instance().gap().getAddress(&addr_type, address);
- printf("DEVICE MAC ADDRESS: ");
- for (int i = 5; i >= 1; i--){
- printf("%02x:", address[i]);
- }
- printf("%02x\r\n", address[0]);
+void periodicCallback(void)
+{
+ updateVal('time');
+ updateVal(pre);
+ updateVal(mainData);
}
-/**
- * Callback triggered when the ble initialization process has finished
- */
+
+
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
ble_error_t error = params->error;
if (error != BLE_ERROR_NONE) {
- /* In case of error, forward the error handling to onBleInitError */
onBleInitError(ble, error);
return;
}
- /* Ensure that it is the default instance of BLE */
- if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+ if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
return;
}
ble.gap().onDisconnection(disconnectionCallback);
+ ble.gattServer().onDataWritten(onDataWrittenCallback);
- /* Setup primary service */
- batteryServicePtr = new BatteryService(ble, batteryLevel);
+
+ /* Setup primary service. */
+ uartServicePtr = new UARTService(ble);
+
- /* Setup advertising */
+ /* Setup advertising. */
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
- ble.gap().setAdvertisingInterval(1000); /* 1000ms */
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+
+ ble.gap().setAdvertisingInterval(100); /* 100ms */
ble.gap().startAdvertising();
-
- printMacAddress();
}
void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
@@ -117,13 +134,15 @@
int main()
{
- eventQueue.call_every(500, blinkCallback);
-
+ Ticker ticker;
+ ticker.attach(periodicCallback,900);
BLE &ble = BLE::Instance();
ble.onEventsToProcess(scheduleBleEventsProcessing);
ble.init(bleInitComplete);
-
+ keyThread.start(keyThreadFun);
eventQueue.dispatch_forever();
-
return 0;
}
+
+
+
