A speedtest

Dependencies:   aconno_SEGGER_RTT CustomService

Committer:
gaggenwaschke
Date:
Fri Nov 23 08:59:58 2018 +0000
Revision:
6:58023588fb4b
Parent:
3:f303ca535263
Child:
8:3d3b77caecbd
Initial commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gaggenwaschke 6:58023588fb4b 1 /* Copyright (c) 2016 Aconno-> All Rights Reserved->
jurica238814 0:796985b808bc 2 *
gaggenwaschke 6:58023588fb4b 3 * Licensees are granted free, non-transferabl use of the information-> NO
gaggenwaschke 6:58023588fb4b 4 * WARRANTY of ANY KIND is provided-> This heading must NOT be removed from
gaggenwaschke 6:58023588fb4b 5 * the file->
jurica238814 0:796985b808bc 6 *
jurica238814 0:796985b808bc 7 * aconno simple example program
jurica238814 0:796985b808bc 8 * blinky LED LD1
jurica238814 0:796985b808bc 9 */
gaggenwaschke 6:58023588fb4b 10
gaggenwaschke 6:58023588fb4b 11 #define WAKE_UP_TIME (100)
jurica238814 0:796985b808bc 12
gaggenwaschke 6:58023588fb4b 13 #include "wavr.h"
gaggenwaschke 6:58023588fb4b 14 #include "CountdownService.h"
gaggenwaschke 6:58023588fb4b 15
gaggenwaschke 6:58023588fb4b 16
gaggenwaschke 6:58023588fb4b 17 EventQueue *queue;
gaggenwaschke 6:58023588fb4b 18 DRV8837 *motor = new DRV8837(PIN_M_IN1, PIN_M_IN2, PIN_M_NSLEEP);
gaggenwaschke 6:58023588fb4b 19 DigitalOut LED_RE(PIN_LED_RED), LED_GR(PIN_LED_GREEN), LED_BL(PIN_LED_BLUE);
gaggenwaschke 6:58023588fb4b 20 BLE *bl(&BLE::Instance());
gaggenwaschke 6:58023588fb4b 21 uint8_t *mac_address = new uint8_t[6];
gaggenwaschke 6:58023588fb4b 22 CountdownService *service_ptr;
gaggenwaschke 6:58023588fb4b 23 Event<void()> *countEvent;
gaggenwaschke 6:58023588fb4b 24 int countEventId;
gaggenwaschke 6:58023588fb4b 25
gaggenwaschke 6:58023588fb4b 26 void OnCount() {
gaggenwaschke 6:58023588fb4b 27 uint32_t time = service_ptr->GetCountdown();
gaggenwaschke 6:58023588fb4b 28 service_ptr->SetCountdown(--time);
gaggenwaschke 6:58023588fb4b 29 }
gaggenwaschke 6:58023588fb4b 30
gaggenwaschke 6:58023588fb4b 31 void Stop() {
gaggenwaschke 6:58023588fb4b 32 motor->stop();
gaggenwaschke 6:58023588fb4b 33 }
gaggenwaschke 6:58023588fb4b 34
gaggenwaschke 6:58023588fb4b 35 /** opens the door */
gaggenwaschke 6:58023588fb4b 36 void Open() {
gaggenwaschke 6:58023588fb4b 37 motor->backward();
gaggenwaschke 6:58023588fb4b 38 queue->call_in(2000, Stop);
gaggenwaschke 6:58023588fb4b 39 queue->cancel(countEventId);
gaggenwaschke 6:58023588fb4b 40 }
gaggenwaschke 6:58023588fb4b 41
gaggenwaschke 6:58023588fb4b 42 /** opens the door in seconds */
gaggenwaschke 6:58023588fb4b 43 void OpenIn(uint16_t seconds) {
gaggenwaschke 6:58023588fb4b 44 queue->call_in(seconds*1000, Open);
gaggenwaschke 6:58023588fb4b 45 countEvent = new Event<void()>(queue, OnCount);
gaggenwaschke 6:58023588fb4b 46 countEvent->period(1000);
gaggenwaschke 6:58023588fb4b 47 countEventId = countEvent->post();
gaggenwaschke 6:58023588fb4b 48 }
gaggenwaschke 6:58023588fb4b 49
gaggenwaschke 6:58023588fb4b 50 /** closes the door */
gaggenwaschke 6:58023588fb4b 51 void Close() {
gaggenwaschke 6:58023588fb4b 52 motor->forward();
gaggenwaschke 6:58023588fb4b 53 queue->call_in(2000, Stop);
gaggenwaschke 6:58023588fb4b 54 }
gaggenwaschke 6:58023588fb4b 55
gaggenwaschke 6:58023588fb4b 56 /** Is called when a connection is established */
gaggenwaschke 6:58023588fb4b 57 void OnConnection(const Gap::ConnectionCallbackParams_t *params){
gaggenwaschke 6:58023588fb4b 58 bl->gap().stopAdvertising();
gaggenwaschke 6:58023588fb4b 59 // Do blinky on connect
gaggenwaschke 6:58023588fb4b 60 LED_BL = 0;
gaggenwaschke 6:58023588fb4b 61 wait_ms(400);
gaggenwaschke 6:58023588fb4b 62 LED_BL = 1;
gaggenwaschke 6:58023588fb4b 63 }
gaggenwaschke 6:58023588fb4b 64
gaggenwaschke 6:58023588fb4b 65 /** Is called on disconnect of BT */
gaggenwaschke 6:58023588fb4b 66 void OnDisconnection(const Gap::DisconnectionCallbackParams_t *params){
gaggenwaschke 6:58023588fb4b 67 LED_BL = 1; // switch off blue LED
gaggenwaschke 6:58023588fb4b 68 bl->gap().startAdvertising();
gaggenwaschke 6:58023588fb4b 69 }
gaggenwaschke 6:58023588fb4b 70
gaggenwaschke 6:58023588fb4b 71 void OnTimeSent(uint32_t time) {
gaggenwaschke 6:58023588fb4b 72 if (time == 0) {
gaggenwaschke 6:58023588fb4b 73 Open();
gaggenwaschke 6:58023588fb4b 74 } else {
gaggenwaschke 6:58023588fb4b 75 for (int i = 0; i < 10; i++) {
gaggenwaschke 6:58023588fb4b 76 LED_RE = !LED_RE;
gaggenwaschke 6:58023588fb4b 77 wait_ms(500);
gaggenwaschke 6:58023588fb4b 78 }
gaggenwaschke 6:58023588fb4b 79 Close();
gaggenwaschke 6:58023588fb4b 80 OpenIn(time);
gaggenwaschke 6:58023588fb4b 81 }
gaggenwaschke 6:58023588fb4b 82 }
gaggenwaschke 6:58023588fb4b 83
gaggenwaschke 6:58023588fb4b 84 /** handler for init error */
gaggenwaschke 6:58023588fb4b 85 void BlinkRed()
gaggenwaschke 6:58023588fb4b 86 {
gaggenwaschke 6:58023588fb4b 87 LED_RE = !LED_RE;
gaggenwaschke 6:58023588fb4b 88 }
gaggenwaschke 6:58023588fb4b 89
gaggenwaschke 6:58023588fb4b 90 /** Is called when the bl initialization process has finished. */
gaggenwaschke 6:58023588fb4b 91 void OnBleInitComplete(BLE::InitializationCompleteCallbackContext *params){
gaggenwaschke 6:58023588fb4b 92 BLE& bl = params->ble;
gaggenwaschke 6:58023588fb4b 93 ble_error_t error = params->error;
jurica238814 0:796985b808bc 94
gaggenwaschke 6:58023588fb4b 95 if (error != BLE_ERROR_NONE) {
gaggenwaschke 6:58023588fb4b 96 /* In case of error, forward the error handling to onBleInitError */
gaggenwaschke 6:58023588fb4b 97 queue->call_every(500, BlinkRed);
gaggenwaschke 6:58023588fb4b 98 return;
gaggenwaschke 6:58023588fb4b 99 }
gaggenwaschke 6:58023588fb4b 100
gaggenwaschke 6:58023588fb4b 101 // Get my MAC address
gaggenwaschke 6:58023588fb4b 102 Gap::AddressType_t addr_type;
gaggenwaschke 6:58023588fb4b 103 Gap::Address_t address;
gaggenwaschke 6:58023588fb4b 104 bl.gap().getAddress(&addr_type, address);
gaggenwaschke 6:58023588fb4b 105
gaggenwaschke 6:58023588fb4b 106 // attach callbacks
gaggenwaschke 6:58023588fb4b 107 bl.gap().onDisconnection(OnDisconnection);
gaggenwaschke 6:58023588fb4b 108 bl.gap().onConnection(OnConnection);
gaggenwaschke 6:58023588fb4b 109
gaggenwaschke 6:58023588fb4b 110 service_ptr = new CountdownService(bl.gattServer(), OnTimeSent);
gaggenwaschke 6:58023588fb4b 111
gaggenwaschke 6:58023588fb4b 112 // setup advertising
gaggenwaschke 6:58023588fb4b 113 //bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
gaggenwaschke 6:58023588fb4b 114 bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME));
gaggenwaschke 6:58023588fb4b 115 //bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
gaggenwaschke 6:58023588fb4b 116
gaggenwaschke 6:58023588fb4b 117 bl.gap().setTxPower(TX_POWER);
gaggenwaschke 6:58023588fb4b 118 bl.gap().setAdvertisingInterval(ADVERTISING_INTERVAL_MS); // --> Has to be at least 100ms!
gaggenwaschke 6:58023588fb4b 119
gaggenwaschke 6:58023588fb4b 120 bl.gap().startAdvertising();
gaggenwaschke 6:58023588fb4b 121 }
jurica238814 0:796985b808bc 122
gaggenwaschke 6:58023588fb4b 123 /** Initializes the flags for propper GPIO operation */
gaggenwaschke 6:58023588fb4b 124 void InitGPIO() {
gaggenwaschke 6:58023588fb4b 125 // Below block of code has to be uncommented if reset block or NFC block is
gaggenwaschke 6:58023588fb4b 126 // also uncommented.
gaggenwaschke 6:58023588fb4b 127 NRF_NVMC->CONFIG = 0x00000002; // Erase enabl UICR
gaggenwaschke 6:58023588fb4b 128 NRF_NVMC->ERASEUICR = 0x00000001; // Erase all
gaggenwaschke 6:58023588fb4b 129 NRF_NVMC->CONFIG = 0x00000001; // Write enabl UICR
gaggenwaschke 6:58023588fb4b 130
gaggenwaschke 6:58023588fb4b 131 // The following block of code is used to disabl reset input on
gaggenwaschke 6:58023588fb4b 132 // microcontroler and turn it int gpio. This is used for example on Aconno
gaggenwaschke 6:58023588fb4b 133 // DK because it has one of the I2C lines connected to reset. Uncoment it
gaggenwaschke 6:58023588fb4b 134 // if it is needed.
gaggenwaschke 6:58023588fb4b 135 //NRF_UICR->PSELRESET[0] = 0x8000000F; // Disabl RESET pin -> Enabl GPIO
gaggenwaschke 6:58023588fb4b 136 //NRF_UICR->PSELRESET[1] = 0x8000000F; // Disabl RESET pin -> Enabl GPIO
gaggenwaschke 6:58023588fb4b 137 NRF_UICR->PSELRESET[0] = 0x80000015;
gaggenwaschke 6:58023588fb4b 138 NRF_UICR->PSELRESET[1] = 0x80000015;
gaggenwaschke 6:58023588fb4b 139
gaggenwaschke 6:58023588fb4b 140 // The following block of code is used to enabl NFC pins as gpio.
gaggenwaschke 6:58023588fb4b 141 NRF_UICR->NFCPINS = 0xFFFFFFFE; // Change NFC to GPIO function
gaggenwaschke 6:58023588fb4b 142 }
gaggenwaschke 6:58023588fb4b 143
gaggenwaschke 6:58023588fb4b 144 /** Initializes everything needed for bluetooth. */
gaggenwaschke 6:58023588fb4b 145 void InitBT() {
gaggenwaschke 6:58023588fb4b 146 bl->init(OnBleInitComplete);
gaggenwaschke 6:58023588fb4b 147 bl->gap().setTxPower(TX_POWER); // Set TX power to TX_POWER
gaggenwaschke 6:58023588fb4b 148
gaggenwaschke 6:58023588fb4b 149 // potential delay for setup, sync or async
gaggenwaschke 6:58023588fb4b 150 while (bl->hasInitialized() == false) { /* spin loop */ }
gaggenwaschke 6:58023588fb4b 151 }
gaggenwaschke 6:58023588fb4b 152
gaggenwaschke 6:58023588fb4b 153 /** Initializes all settings for GPIO, timers and BLE. */
gaggenwaschke 6:58023588fb4b 154 void Init () {
gaggenwaschke 6:58023588fb4b 155 InitGPIO();
gaggenwaschke 6:58023588fb4b 156 LED_RE = 1;
gaggenwaschke 6:58023588fb4b 157 LED_GR = 1;
gaggenwaschke 6:58023588fb4b 158 LED_BL = 1;
gaggenwaschke 6:58023588fb4b 159 InitBT();
gaggenwaschke 6:58023588fb4b 160 }
gaggenwaschke 6:58023588fb4b 161
gaggenwaschke 6:58023588fb4b 162 /** a demo loop that blinks the LED's and turns the motor in both directions. */
gaggenwaschke 6:58023588fb4b 163 void TestLoop() {
gaggenwaschke 6:58023588fb4b 164 for (int i = 0; i < 5; i++) {
gaggenwaschke 6:58023588fb4b 165 wait_ms(2000);
gaggenwaschke 6:58023588fb4b 166 motor->forward();
gaggenwaschke 6:58023588fb4b 167
gaggenwaschke 6:58023588fb4b 168 wait_ms(2000);
gaggenwaschke 6:58023588fb4b 169 motor->stop();
gaggenwaschke 6:58023588fb4b 170
gaggenwaschke 6:58023588fb4b 171 wait_ms(2000);
gaggenwaschke 6:58023588fb4b 172 motor->backward();
gaggenwaschke 6:58023588fb4b 173
gaggenwaschke 6:58023588fb4b 174 wait_ms(2000);
gaggenwaschke 6:58023588fb4b 175 motor->stop();
gaggenwaschke 6:58023588fb4b 176 }
gaggenwaschke 6:58023588fb4b 177 }
gaggenwaschke 6:58023588fb4b 178
gaggenwaschke 6:58023588fb4b 179 /** Schedules the Processing of BLE events for eventQueue */
gaggenwaschke 6:58023588fb4b 180 void ScheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
gaggenwaschke 6:58023588fb4b 181 BLE &bl = context->ble;
gaggenwaschke 6:58023588fb4b 182 queue->call(Callback<void()> (&bl, &BLE::processEvents));
gaggenwaschke 6:58023588fb4b 183 }
jurica238814 0:796985b808bc 184
jurica238814 0:796985b808bc 185 int main(){
gaggenwaschke 6:58023588fb4b 186 queue = new EventQueue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE);
gaggenwaschke 6:58023588fb4b 187 Init();
gaggenwaschke 6:58023588fb4b 188
gaggenwaschke 6:58023588fb4b 189 bl->onEventsToProcess(ScheduleBleEventsProcessing);
gaggenwaschke 6:58023588fb4b 190 queue->dispatch_forever();
jurica238814 0:796985b808bc 191 }