A speedtest

Dependencies:   aconno_SEGGER_RTT CustomService

Revision:
6:58023588fb4b
Parent:
3:f303ca535263
Child:
8:3d3b77caecbd
--- a/main.cpp	Fri Aug 25 06:54:20 2017 +0000
+++ b/main.cpp	Fri Nov 23 08:59:58 2018 +0000
@@ -1,27 +1,191 @@
-/* Copyright (c) 2016 Aconno. All Rights Reserved.
+/* Copyright (c) 2016 Aconno-> All Rights Reserved->
  *
- * Licensees are granted free, non-transferable use of the information. NO
- * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
- * the file.
+ * Licensees are granted free, non-transferabl use of the information-> NO
+ * WARRANTY of ANY KIND is provided-> This heading must NOT be removed from
+ * the file->
  *
  *   aconno simple example program
  *   blinky LED LD1
  */
+ 
+#define WAKE_UP_TIME           (100)           
 
-#include "mbed.h"
-#include "acd52832_bsp.h"
+#include "wavr.h"
+#include "CountdownService.h"
+
+
+EventQueue *queue;
+DRV8837 *motor = new DRV8837(PIN_M_IN1, PIN_M_IN2, PIN_M_NSLEEP);        
+DigitalOut LED_RE(PIN_LED_RED), LED_GR(PIN_LED_GREEN), LED_BL(PIN_LED_BLUE);
+BLE *bl(&BLE::Instance());
+uint8_t *mac_address = new uint8_t[6];
+CountdownService *service_ptr;
+Event<void()> *countEvent;
+int countEventId;
+
+void OnCount() {
+    uint32_t time = service_ptr->GetCountdown();
+    service_ptr->SetCountdown(--time);
+}
+
+void Stop() {
+    motor->stop();
+}
+
+/** opens the door */
+void Open() {
+    motor->backward();
+    queue->call_in(2000, Stop);
+    queue->cancel(countEventId);
+}
+
+/** opens the door in seconds */
+void OpenIn(uint16_t seconds) {
+    queue->call_in(seconds*1000, Open);
+    countEvent = new Event<void()>(queue, OnCount);
+    countEvent->period(1000);
+    countEventId = countEvent->post();
+}   
+
+/** closes the door */
+void Close() {
+    motor->forward();
+    queue->call_in(2000, Stop);
+}    
+
+/** Is called when a connection is established */
+void OnConnection(const Gap::ConnectionCallbackParams_t *params){   
+    bl->gap().stopAdvertising();
+    // Do blinky on connect
+    LED_BL = 0;
+    wait_ms(400); 
+    LED_BL = 1;      
+}
+
+/** Is called on disconnect of BT */
+void OnDisconnection(const Gap::DisconnectionCallbackParams_t *params){
+    LED_BL = 1;     // switch off blue LED
+    bl->gap().startAdvertising();
+}
+
+void OnTimeSent(uint32_t time) {
+    if (time == 0) {
+        Open();
+    } else {
+        for (int i = 0; i < 10; i++) {
+            LED_RE = !LED_RE;
+            wait_ms(500);
+        }
+        Close();   
+        OpenIn(time);
+    }
+}
+
+/** handler for init error */
+void BlinkRed()
+{
+    LED_RE = !LED_RE;
+}
+
+/** Is called when the bl initialization process has finished. */
+void OnBleInitComplete(BLE::InitializationCompleteCallbackContext *params){
+    BLE&        bl   = params->ble;
+    ble_error_t error = params->error;
 
-#define pause 1         // Pause in seconds 
+    if (error != BLE_ERROR_NONE) {
+        /* In case of error, forward the error handling to onBleInitError */
+        queue->call_every(500, BlinkRed);
+        return;
+    }
+    
+    // Get my MAC address
+    Gap::AddressType_t addr_type;
+    Gap::Address_t address;
+    bl.gap().getAddress(&addr_type, address);
+    
+    // attach callbacks
+    bl.gap().onDisconnection(OnDisconnection);
+    bl.gap().onConnection(OnConnection);   
+    
+    service_ptr = new CountdownService(bl.gattServer(), OnTimeSent); 
+    
+    // setup advertising
+    //bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME));
+    //bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
+    
+    bl.gap().setTxPower(TX_POWER);
+    bl.gap().setAdvertisingInterval(ADVERTISING_INTERVAL_MS);  // --> Has to be at least 100ms!
+    
+    bl.gap().startAdvertising();
+}
 
-DigitalOut LD1(PIN_LED_RED);
+/** Initializes the flags for propper GPIO operation */
+void InitGPIO() {
+   // Below block of code has to be uncommented if reset block or NFC block is
+   // also uncommented.
+   NRF_NVMC->CONFIG = 0x00000002;      // Erase enabl UICR
+   NRF_NVMC->ERASEUICR = 0x00000001;   // Erase all
+   NRF_NVMC->CONFIG = 0x00000001;      // Write enabl UICR
+   
+   // The following block of code is used to disabl reset input on
+   // microcontroler and turn it int gpio. This is used for example on Aconno
+   // DK because it has one of the I2C lines connected to reset. Uncoment it
+   // if it is needed.
+   //NRF_UICR->PSELRESET[0] = 0x8000000F;    // Disabl RESET pin -> Enabl GPIO
+   //NRF_UICR->PSELRESET[1] = 0x8000000F;    // Disabl RESET pin -> Enabl GPIO
+   NRF_UICR->PSELRESET[0] = 0x80000015;
+   NRF_UICR->PSELRESET[1] = 0x80000015;
+   
+   // The following block of code is used to enabl NFC pins as gpio.
+   NRF_UICR->NFCPINS = 0xFFFFFFFE;     // Change NFC to GPIO function
+}
+
+/** Initializes everything needed for bluetooth. */
+void InitBT() {
+    bl->init(OnBleInitComplete);
+    bl->gap().setTxPower(TX_POWER);        // Set TX power to TX_POWER
+    
+    // potential delay for setup, sync or async
+    while (bl->hasInitialized()  == false) { /* spin loop */ }
+}
+
+/** Initializes all settings for GPIO, timers and BLE. */
+void Init () {
+    InitGPIO();
+    LED_RE = 1;
+    LED_GR = 1;    
+    LED_BL = 1;
+    InitBT();
+} 
+
+/** a demo loop that blinks the LED's and turns the motor in both directions. */
+void TestLoop() {        
+    for (int i = 0; i < 5; i++) {
+        wait_ms(2000);
+        motor->forward();
+        
+        wait_ms(2000);
+        motor->stop();
+        
+        wait_ms(2000);
+        motor->backward();
+        
+        wait_ms(2000);
+        motor->stop();
+    }
+}
+
+/** Schedules the Processing of BLE events for eventQueue */
+void ScheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
+    BLE &bl = context->ble;
+    queue->call(Callback<void()> (&bl, &BLE::processEvents));
+}
 
 int main(){
-    while(1){
-        // Turn LED OFF
-        LD1 = 1;
-        wait(pause);
-        // Turn LED ON
-        LD1 = 0;
-        wait(pause);
-    }
+    queue = new EventQueue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE);
+    Init();
+    
+    bl->onEventsToProcess(ScheduleBleEventsProcessing);
+    queue->dispatch_forever();
 }
\ No newline at end of file