Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os by
main.cpp
00001 /* 00002 * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include "mbed.h" 00018 #include "greentea-client/test_env.h" 00019 #include "utest/utest.h" 00020 00021 using namespace utest::v1; 00022 00023 static const int ONE_SECOND_MS = 1000; 00024 static const int total_ticks = 10; 00025 00026 DigitalOut led1(LED1); 00027 DigitalOut led2(LED2); 00028 00029 Ticker *ticker1; 00030 Ticker *ticker2; 00031 00032 volatile int ticker_count = 0; 00033 volatile bool print_tick = false; 00034 00035 void send_kv_tick() { 00036 if (ticker_count <= total_ticks) { 00037 print_tick = true; 00038 } 00039 } 00040 00041 void ticker_callback_0(void) { 00042 static int fast_ticker_count = 0; 00043 if (fast_ticker_count >= ONE_SECOND_MS) { 00044 send_kv_tick(); 00045 fast_ticker_count = 0; 00046 led1 = !led1; 00047 } 00048 fast_ticker_count++; 00049 } 00050 00051 void ticker_callback_1(void) { 00052 led1 = !led1; 00053 send_kv_tick(); 00054 } 00055 00056 void ticker_callback_2_led(void) { 00057 led2 = !led2; 00058 } 00059 00060 void ticker_callback_2(void) { 00061 ticker_callback_2_led(); 00062 send_kv_tick(); 00063 } 00064 00065 void ticker_callback_1_switch_to_2(void); 00066 void ticker_callback_2_switch_to_1(void); 00067 00068 void ticker_callback_1_switch_to_2(void) { 00069 ticker1->detach(); 00070 ticker1->attach(ticker_callback_2_switch_to_1, 1.0); 00071 ticker_callback_1(); 00072 } 00073 00074 void ticker_callback_2_switch_to_1(void) { 00075 ticker2->detach(); 00076 ticker2->attach(ticker_callback_1_switch_to_2, 1.0); 00077 ticker_callback_2(); 00078 } 00079 00080 void wait_and_print() { 00081 while(ticker_count <= total_ticks) { 00082 if (print_tick) { 00083 print_tick = false; 00084 greentea_send_kv("tick", ticker_count++); 00085 } 00086 } 00087 } 00088 00089 void test_case_1x_ticker() { 00090 led1 = 0; 00091 led2 = 0; 00092 ticker_count = 0; 00093 ticker1->attach_us(ticker_callback_0, ONE_SECOND_MS); 00094 wait_and_print(); 00095 } 00096 00097 void test_case_2x_ticker() { 00098 led1 = 0; 00099 led2 = 0; 00100 ticker_count = 0; 00101 ticker1->attach(&ticker_callback_1, 1.0); 00102 ticker2->attach(&ticker_callback_2_led, 2.0); 00103 wait_and_print(); 00104 } 00105 00106 void test_case_2x_callbacks() { 00107 led1 = 0; 00108 led2 = 0; 00109 ticker_count = 0; 00110 ticker1->attach(ticker_callback_1_switch_to_2, 1.0); 00111 wait_and_print(); 00112 } 00113 00114 utest::v1::status_t one_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) { 00115 ticker1 = new Ticker(); 00116 return greentea_case_setup_handler(source, index_of_case); 00117 } 00118 00119 utest::v1::status_t two_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) { 00120 ticker1 = new Ticker(); 00121 ticker2 = new Ticker(); 00122 return greentea_case_setup_handler(source, index_of_case); 00123 } 00124 00125 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) { 00126 delete ticker1; 00127 return greentea_case_teardown_handler(source, passed, failed, reason); 00128 } 00129 00130 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) { 00131 delete ticker1; 00132 delete ticker2; 00133 return greentea_case_teardown_handler(source, passed, failed, reason); 00134 } 00135 00136 // Test cases 00137 Case cases[] = { 00138 Case("Timers: 1x ticker", one_ticker_case_setup_handler_t, test_case_1x_ticker, one_ticker_case_teardown_handler_t), 00139 Case("Timers: 2x tickers", two_ticker_case_setup_handler_t, test_case_2x_ticker, two_ticker_case_teardown_handler_t), 00140 Case("Timers: 2x callbacks", two_ticker_case_setup_handler_t, test_case_2x_callbacks,two_ticker_case_teardown_handler_t), 00141 }; 00142 00143 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { 00144 GREENTEA_SETUP((total_ticks + 5) * 3, "timing_drift_auto"); 00145 return greentea_test_setup_handler(number_of_cases); 00146 } 00147 00148 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00149 00150 int main() { 00151 Harness::run(specification); 00152 }
Generated on Tue Jul 12 2022 13:15:58 by
