mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
TESTS/mbed_drivers/ticker/main.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved |
elessair | 0:f269e3021894 | 3 | * SPDX-License-Identifier: Apache-2.0 |
elessair | 0:f269e3021894 | 4 | * |
elessair | 0:f269e3021894 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
elessair | 0:f269e3021894 | 6 | * not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 7 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 8 | * |
elessair | 0:f269e3021894 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 10 | * |
elessair | 0:f269e3021894 | 11 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
elessair | 0:f269e3021894 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 14 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 15 | * limitations under the License. |
elessair | 0:f269e3021894 | 16 | */ |
elessair | 0:f269e3021894 | 17 | #include "mbed.h" |
elessair | 0:f269e3021894 | 18 | #include "greentea-client/test_env.h" |
elessair | 0:f269e3021894 | 19 | #include "utest/utest.h" |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | using namespace utest::v1; |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | static const int ONE_SECOND_MS = 1000; |
elessair | 0:f269e3021894 | 24 | static const int total_ticks = 10; |
elessair | 0:f269e3021894 | 25 | |
elessair | 0:f269e3021894 | 26 | DigitalOut led1(LED1); |
elessair | 0:f269e3021894 | 27 | DigitalOut led2(LED2); |
elessair | 0:f269e3021894 | 28 | |
elessair | 0:f269e3021894 | 29 | Ticker *ticker1; |
elessair | 0:f269e3021894 | 30 | Ticker *ticker2; |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | volatile int ticker_count = 0; |
elessair | 0:f269e3021894 | 33 | volatile bool print_tick = false; |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | void send_kv_tick() { |
elessair | 0:f269e3021894 | 36 | if (ticker_count <= total_ticks) { |
elessair | 0:f269e3021894 | 37 | print_tick = true; |
elessair | 0:f269e3021894 | 38 | } |
elessair | 0:f269e3021894 | 39 | } |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | void ticker_callback_0(void) { |
elessair | 0:f269e3021894 | 42 | static int fast_ticker_count = 0; |
elessair | 0:f269e3021894 | 43 | if (fast_ticker_count >= ONE_SECOND_MS) { |
elessair | 0:f269e3021894 | 44 | send_kv_tick(); |
elessair | 0:f269e3021894 | 45 | fast_ticker_count = 0; |
elessair | 0:f269e3021894 | 46 | led1 = !led1; |
elessair | 0:f269e3021894 | 47 | } |
elessair | 0:f269e3021894 | 48 | fast_ticker_count++; |
elessair | 0:f269e3021894 | 49 | } |
elessair | 0:f269e3021894 | 50 | |
elessair | 0:f269e3021894 | 51 | void ticker_callback_1(void) { |
elessair | 0:f269e3021894 | 52 | led1 = !led1; |
elessair | 0:f269e3021894 | 53 | send_kv_tick(); |
elessair | 0:f269e3021894 | 54 | } |
elessair | 0:f269e3021894 | 55 | |
elessair | 0:f269e3021894 | 56 | void ticker_callback_2_led(void) { |
elessair | 0:f269e3021894 | 57 | led2 = !led2; |
elessair | 0:f269e3021894 | 58 | } |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | void ticker_callback_2(void) { |
elessair | 0:f269e3021894 | 61 | ticker_callback_2_led(); |
elessair | 0:f269e3021894 | 62 | send_kv_tick(); |
elessair | 0:f269e3021894 | 63 | } |
elessair | 0:f269e3021894 | 64 | |
elessair | 0:f269e3021894 | 65 | void ticker_callback_1_switch_to_2(void); |
elessair | 0:f269e3021894 | 66 | void ticker_callback_2_switch_to_1(void); |
elessair | 0:f269e3021894 | 67 | |
elessair | 0:f269e3021894 | 68 | void ticker_callback_1_switch_to_2(void) { |
elessair | 0:f269e3021894 | 69 | ticker1->detach(); |
elessair | 0:f269e3021894 | 70 | ticker1->attach(ticker_callback_2_switch_to_1, 1.0); |
elessair | 0:f269e3021894 | 71 | ticker_callback_1(); |
elessair | 0:f269e3021894 | 72 | } |
elessair | 0:f269e3021894 | 73 | |
elessair | 0:f269e3021894 | 74 | void ticker_callback_2_switch_to_1(void) { |
elessair | 0:f269e3021894 | 75 | ticker2->detach(); |
elessair | 0:f269e3021894 | 76 | ticker2->attach(ticker_callback_1_switch_to_2, 1.0); |
elessair | 0:f269e3021894 | 77 | ticker_callback_2(); |
elessair | 0:f269e3021894 | 78 | } |
elessair | 0:f269e3021894 | 79 | |
elessair | 0:f269e3021894 | 80 | void wait_and_print() { |
elessair | 0:f269e3021894 | 81 | while(ticker_count <= total_ticks) { |
elessair | 0:f269e3021894 | 82 | if (print_tick) { |
elessair | 0:f269e3021894 | 83 | print_tick = false; |
elessair | 0:f269e3021894 | 84 | greentea_send_kv("tick", ticker_count++); |
elessair | 0:f269e3021894 | 85 | } |
elessair | 0:f269e3021894 | 86 | } |
elessair | 0:f269e3021894 | 87 | } |
elessair | 0:f269e3021894 | 88 | |
elessair | 0:f269e3021894 | 89 | void test_case_1x_ticker() { |
elessair | 0:f269e3021894 | 90 | led1 = 0; |
elessair | 0:f269e3021894 | 91 | led2 = 0; |
elessair | 0:f269e3021894 | 92 | ticker_count = 0; |
elessair | 0:f269e3021894 | 93 | ticker1->attach_us(ticker_callback_0, ONE_SECOND_MS); |
elessair | 0:f269e3021894 | 94 | wait_and_print(); |
elessair | 0:f269e3021894 | 95 | } |
elessair | 0:f269e3021894 | 96 | |
elessair | 0:f269e3021894 | 97 | void test_case_2x_ticker() { |
elessair | 0:f269e3021894 | 98 | led1 = 0; |
elessair | 0:f269e3021894 | 99 | led2 = 0; |
elessair | 0:f269e3021894 | 100 | ticker_count = 0; |
elessair | 0:f269e3021894 | 101 | ticker1->attach(&ticker_callback_1, 1.0); |
elessair | 0:f269e3021894 | 102 | ticker2->attach(&ticker_callback_2_led, 2.0); |
elessair | 0:f269e3021894 | 103 | wait_and_print(); |
elessair | 0:f269e3021894 | 104 | } |
elessair | 0:f269e3021894 | 105 | |
elessair | 0:f269e3021894 | 106 | void test_case_2x_callbacks() { |
elessair | 0:f269e3021894 | 107 | led1 = 0; |
elessair | 0:f269e3021894 | 108 | led2 = 0; |
elessair | 0:f269e3021894 | 109 | ticker_count = 0; |
elessair | 0:f269e3021894 | 110 | ticker1->attach(ticker_callback_1_switch_to_2, 1.0); |
elessair | 0:f269e3021894 | 111 | wait_and_print(); |
elessair | 0:f269e3021894 | 112 | } |
elessair | 0:f269e3021894 | 113 | |
elessair | 0:f269e3021894 | 114 | utest::v1::status_t one_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) { |
elessair | 0:f269e3021894 | 115 | ticker1 = new Ticker(); |
elessair | 0:f269e3021894 | 116 | return greentea_case_setup_handler(source, index_of_case); |
elessair | 0:f269e3021894 | 117 | } |
elessair | 0:f269e3021894 | 118 | |
elessair | 0:f269e3021894 | 119 | utest::v1::status_t two_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) { |
elessair | 0:f269e3021894 | 120 | ticker1 = new Ticker(); |
elessair | 0:f269e3021894 | 121 | ticker2 = new Ticker(); |
elessair | 0:f269e3021894 | 122 | return greentea_case_setup_handler(source, index_of_case); |
elessair | 0:f269e3021894 | 123 | } |
elessair | 0:f269e3021894 | 124 | |
elessair | 0:f269e3021894 | 125 | utest::v1::status_t one_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed, const failure_t reason) { |
elessair | 0:f269e3021894 | 126 | delete ticker1; |
elessair | 0:f269e3021894 | 127 | return greentea_case_teardown_handler(source, passed, failed, reason); |
elessair | 0:f269e3021894 | 128 | } |
elessair | 0:f269e3021894 | 129 | |
elessair | 0:f269e3021894 | 130 | utest::v1::status_t two_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed, const failure_t reason) { |
elessair | 0:f269e3021894 | 131 | delete ticker1; |
elessair | 0:f269e3021894 | 132 | delete ticker2; |
elessair | 0:f269e3021894 | 133 | return greentea_case_teardown_handler(source, passed, failed, reason); |
elessair | 0:f269e3021894 | 134 | } |
elessair | 0:f269e3021894 | 135 | |
elessair | 0:f269e3021894 | 136 | // Test cases |
elessair | 0:f269e3021894 | 137 | Case cases[] = { |
elessair | 0:f269e3021894 | 138 | Case("Timers: 1x ticker", one_ticker_case_setup_handler_t, test_case_1x_ticker, one_ticker_case_teardown_handler_t), |
elessair | 0:f269e3021894 | 139 | Case("Timers: 2x tickers", two_ticker_case_setup_handler_t, test_case_2x_ticker, two_ticker_case_teardown_handler_t), |
elessair | 0:f269e3021894 | 140 | Case("Timers: 2x callbacks", two_ticker_case_setup_handler_t, test_case_2x_callbacks,two_ticker_case_teardown_handler_t), |
elessair | 0:f269e3021894 | 141 | }; |
elessair | 0:f269e3021894 | 142 | |
elessair | 0:f269e3021894 | 143 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { |
elessair | 0:f269e3021894 | 144 | GREENTEA_SETUP((total_ticks + 5) * 3, "timing_drift_auto"); |
elessair | 0:f269e3021894 | 145 | return greentea_test_setup_handler(number_of_cases); |
elessair | 0:f269e3021894 | 146 | } |
elessair | 0:f269e3021894 | 147 | |
elessair | 0:f269e3021894 | 148 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
elessair | 0:f269e3021894 | 149 | |
elessair | 0:f269e3021894 | 150 | int main() { |
elessair | 0:f269e3021894 | 151 | Harness::run(specification); |
elessair | 0:f269e3021894 | 152 | } |