this is ble keypad for nrf52832,ble nano.with ble uart code

Dependencies:  

Fork of mbed-os-example-ble-BatteryLevel by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <events/mbed_events.h>
00002 #include <mbed.h>
00003 #include "ble/BLE.h"
00004 #include "keypad.h"
00005 #include "ble/Gap.h"
00006 #include "ble/services/UARTService.h"
00007 
00008 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
00009                                * it will have an impact on code-size and power consumption. */
00010 #if NEED_CONSOLE_OUTPUT
00011 #define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); }
00012 #else
00013 #define DEBUG(...) /* nothing */
00014 #endif /* #if NEED_CONSOLE_OUTPUT */
00015 
00016 Timer timerset;
00017 const static char     DEVICE_NAME[] = "KeyPad";
00018 static UARTService* uartServicePtr;
00019 Thread keyThread;
00020 int mainData=0;
00021 Serial pc(USBTX, USBRX); 
00022 char pre;
00023 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
00024 
00025 void keyThreadFun(){
00026     Keypad keypad(p19,p18,p17,p16,p25,p24,p23,p20);
00027     keypad.enablePullUp();
00028     char key;
00029     bool done=false;
00030     int data=0;
00031     pc.printf("Please touch a key on the keypad\n\r");
00032     while (1) 
00033     {
00034          key = keypad.getKey();
00035          if(key != KEY_RELEASED)
00036          {
00037              if(key!='*' && key!='#' && key!='(' && key!=')' && key!=']' && key!='@'){
00038                 if(!done){
00039                     timerset.start();
00040                     data=data*10 + (key-'0');
00041                 }
00042              }
00043              if(key==']'){
00044                 timerset.stop();
00045                 done=true;    
00046              }
00047              pc.printf("%c\r\n",key);
00048              wait(0.6);
00049          }
00050          if(timerset.read()>=60){
00051              mainData=data;
00052              data=0;    
00053          }
00054     }
00055     
00056 }
00057 
00058 void updateVal(char v){
00059     char data[4];
00060     sprintf(data, "%c",v);
00061     uartServicePtr->writeString(data);
00062     uartServicePtr->writeString("\n");     
00063 }
00064 
00065 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00066 {
00067     BLE::Instance().gap().startAdvertising();
00068 }
00069 
00070 
00071 /*void dataReadCallback( const GattReadCallbackParams *params )
00072 {
00073     DEBUG("Receive\r\n");
00074 }*/
00075 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
00076     if ((uartServicePtr != NULL)) {
00077         pre = *(params->data);
00078          
00079     }
00080 }
00081 
00082 void onBleInitError(BLE &ble, ble_error_t error)
00083 {
00084     (void)ble;
00085     (void)error;
00086 }
00087 
00088 void periodicCallback(void)
00089 {   
00090     updateVal('time');
00091     updateVal(pre);
00092     updateVal(mainData);
00093 }
00094 
00095 
00096 
00097 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00098 {
00099     BLE&        ble   = params->ble;
00100     ble_error_t error = params->error;
00101 
00102     if (error != BLE_ERROR_NONE) {
00103         onBleInitError(ble, error);
00104         return;
00105     }
00106 
00107     if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00108         return;
00109     }
00110 
00111     ble.gap().onDisconnection(disconnectionCallback);
00112     ble.gattServer().onDataWritten(onDataWrittenCallback);
00113 
00114   
00115     /* Setup primary service. */
00116     uartServicePtr = new UARTService(ble);
00117     
00118 
00119     /* Setup advertising. */
00120     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00121     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00122     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00123 
00124     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00125 
00126     ble.gap().setAdvertisingInterval(100); /* 100ms */
00127     ble.gap().startAdvertising();
00128 }
00129 
00130 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00131     BLE &ble = BLE::Instance();
00132     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00133 }
00134 
00135 int main()
00136 {
00137     Ticker ticker;
00138     ticker.attach(periodicCallback,900);
00139     BLE &ble = BLE::Instance();
00140     ble.onEventsToProcess(scheduleBleEventsProcessing);
00141     ble.init(bleInitComplete);
00142     keyThread.start(keyThreadFun);
00143     eventQueue.dispatch_forever();
00144     return 0;
00145 }
00146 
00147 
00148