A speedtest
Dependencies: aconno_SEGGER_RTT CustomService
Diff: main.cpp
- Revision:
- 8:3d3b77caecbd
- Parent:
- 6:58023588fb4b
diff -r 5460394f0653 -r 3d3b77caecbd main.cpp --- a/main.cpp Fri Nov 23 09:04:50 2018 +0000 +++ b/main.cpp Fri Feb 15 11:27:55 2019 +0000 @@ -12,47 +12,37 @@ #include "wavr.h" #include "CountdownService.h" +#include "SEGGER_RTT.h" +#define printf(...) SEGGER_RTT_printf(0,__VA_ARGS__) -EventQueue *queue; -DRV8837 *motor = new DRV8837(PIN_M_IN1, PIN_M_IN2, PIN_M_NSLEEP); +EventQueue *queue; DigitalOut LED_RE(PIN_LED_RED), LED_GR(PIN_LED_GREEN), LED_BL(PIN_LED_BLUE); +AnalogIn MEAS_BAT(PIN_BAT_VOL); BLE *bl(&BLE::Instance()); uint8_t *mac_address = new uint8_t[6]; -CountdownService *service_ptr; -Event<void()> *countEvent; +CountdownService *ctd_service; +uint32_t received = 0; + int countEventId; -void OnCount() { - uint32_t time = service_ptr->GetCountdown(); - service_ptr->SetCountdown(--time); -} +struct __attribute__((packed, aligned(1))) bleHeader +{ + uint16_t nordicID; // BLE SIG is 0x0059 + uint8_t aconnoID; // aconno id is 69 + uint8_t productID; + uint8_t versionNumber; +}; -void Stop() { - motor->stop(); -} - -/** opens the door */ -void Open() { - motor->backward(); - queue->call_in(2000, Stop); - queue->cancel(countEventId); +static bleHeader GetHeader() { + bleHeader header; + header.nordicID = 0x0059; + header.aconnoID = 0x69; + header.productID = PRODUCT_ID; + header.versionNumber = VERSION_NUMBER; + return header; } -/** 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(); @@ -68,25 +58,20 @@ 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; } +void OnWSent(uint8_t *data, uint16_t length) { + // do some blinky + LED_BL != LED_BL; + received += length; + if ((int)(received - length) / 1024 < (uint32_t)(received / 1024)) + printf("received %u bytes\n", received); +} + /** Is called when the bl initialization process has finished. */ void OnBleInitComplete(BLE::InitializationCompleteCallbackContext *params){ BLE& bl = params->ble; @@ -107,12 +92,13 @@ bl.gap().onDisconnection(OnDisconnection); bl.gap().onConnection(OnConnection); - service_ptr = new CountdownService(bl.gattServer(), OnTimeSent); + ctd_service = new CountdownService(bl.gattServer(), OnWSent); + bleHeader header = GetHeader(); // 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().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&header, sizeof(header)); bl.gap().setTxPower(TX_POWER); bl.gap().setAdvertisingInterval(ADVERTISING_INTERVAL_MS); // --> Has to be at least 100ms! @@ -159,23 +145,6 @@ 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; @@ -183,9 +152,13 @@ } int main(){ + printf("\n\n\n\n--------------------------------\nController bootet\n\n"); + queue = new EventQueue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE); Init(); bl->onEventsToProcess(ScheduleBleEventsProcessing); + // from now on all execution is controlled by the eventQueue. + // enables the automatic use of low power and other features. queue->dispatch_forever(); } \ No newline at end of file