mbed demo
/
Blinky_Tests
Tests for simple blinky library.
Fork of Blinky_Tests by
TESTS/Blinky/5_Blinks/main.cpp@7:cb3da0486f3c, 2016-11-09 (annotated)
- Committer:
- mbed_demo
- Date:
- Wed Nov 09 17:49:45 2016 +0000
- Revision:
- 7:cb3da0486f3c
- Parent:
- 5:4f1e6b065e6b
Comment update;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sarahmarshy | 0:0d5d376157a0 | 1 | #include "Blinky.h" |
sarahmarshy | 0:0d5d376157a0 | 2 | #include "mbed.h" |
sarahmarshy | 0:0d5d376157a0 | 3 | #include "greentea-client/test_env.h" |
sarahmarshy | 0:0d5d376157a0 | 4 | #include "unity.h" |
sarahmarshy | 0:0d5d376157a0 | 5 | #include "utest.h" |
sarahmarshy | 0:0d5d376157a0 | 6 | #include "rtos.h" |
sarahmarshy | 0:0d5d376157a0 | 7 | |
sarahmarshy | 0:0d5d376157a0 | 8 | using namespace utest::v1; |
sarahmarshy | 0:0d5d376157a0 | 9 | |
sarahmarshy | 0:0d5d376157a0 | 10 | //All tests will blink the LED 5 times |
sarahmarshy | 0:0d5d376157a0 | 11 | const int expected_blinks = 5; |
sarahmarshy | 0:0d5d376157a0 | 12 | |
sarahmarshy | 0:0d5d376157a0 | 13 | //Generic blink test |
sarahmarshy | 0:0d5d376157a0 | 14 | //interval => the the amount of time between blinks |
sarahmarshy | 0:0d5d376157a0 | 15 | //led => PinName of LED to blink |
mbed_demo | 2:a6755ef75598 | 16 | void test_blinks(PinName led, int interval){ |
sarahmarshy | 0:0d5d376157a0 | 17 | //Set an appropriate wait time to measure exactly expected_blinks |
sarahmarshy | 0:0d5d376157a0 | 18 | //with the given interval |
mbed_demo | 5:4f1e6b065e6b | 19 | float wait_time = interval*(2*expected_blinks-1); |
sarahmarshy | 0:0d5d376157a0 | 20 | //Create Blinky |
sarahmarshy | 0:0d5d376157a0 | 21 | Blinky blinker(led, interval); |
sarahmarshy | 0:0d5d376157a0 | 22 | //start blinky |
sarahmarshy | 0:0d5d376157a0 | 23 | blinker.start(); |
sarahmarshy | 0:0d5d376157a0 | 24 | //Wait the appropriate amount of time for expected_blinks |
sarahmarshy | 0:0d5d376157a0 | 25 | Thread::wait(wait_time); |
sarahmarshy | 0:0d5d376157a0 | 26 | blinker.stop(); |
mbed_demo | 4:09af3b2d723e | 27 | |
mbed_demo | 4:09af3b2d723e | 28 | float measured_blinks = blinker.times_blinked(); |
mbed_demo | 2:a6755ef75598 | 29 | //Only assert if failure |
mbed_demo | 4:09af3b2d723e | 30 | TEST_ASSERT(measured_blinks == (float)expected_blinks); |
sarahmarshy | 0:0d5d376157a0 | 31 | } |
sarahmarshy | 0:0d5d376157a0 | 32 | |
sarahmarshy | 0:0d5d376157a0 | 33 | void blink_500(){ |
sarahmarshy | 0:0d5d376157a0 | 34 | //Blinky with a 500 ms interval between blinks |
mbed_demo | 2:a6755ef75598 | 35 | test_blinks(LED1, 500); |
sarahmarshy | 0:0d5d376157a0 | 36 | } |
sarahmarshy | 0:0d5d376157a0 | 37 | void blink_1000(){ |
sarahmarshy | 0:0d5d376157a0 | 38 | //Blinky with a 1000 ms interval between blinks |
mbed_demo | 2:a6755ef75598 | 39 | test_blinks(LED1, 1000); |
sarahmarshy | 0:0d5d376157a0 | 40 | } |
sarahmarshy | 0:0d5d376157a0 | 41 | |
sarahmarshy | 0:0d5d376157a0 | 42 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
sarahmarshy | 0:0d5d376157a0 | 43 | // Setup Greentea using a reasonable timeout in seconds |
sarahmarshy | 0:0d5d376157a0 | 44 | GREENTEA_SETUP(40, "default_auto"); |
sarahmarshy | 0:0d5d376157a0 | 45 | return verbose_test_setup_handler(number_of_cases); |
sarahmarshy | 0:0d5d376157a0 | 46 | } |
sarahmarshy | 0:0d5d376157a0 | 47 | |
sarahmarshy | 0:0d5d376157a0 | 48 | // Test cases |
sarahmarshy | 0:0d5d376157a0 | 49 | Case cases[] = { |
sarahmarshy | 0:0d5d376157a0 | 50 | Case("Testing 500 ms interval", blink_500), |
sarahmarshy | 0:0d5d376157a0 | 51 | Case("Testing 1000 ms interval", blink_1000), |
sarahmarshy | 0:0d5d376157a0 | 52 | }; |
sarahmarshy | 0:0d5d376157a0 | 53 | |
sarahmarshy | 0:0d5d376157a0 | 54 | Specification specification(test_setup, cases); |
sarahmarshy | 0:0d5d376157a0 | 55 | |
sarahmarshy | 0:0d5d376157a0 | 56 | // Entry point into the tests |
sarahmarshy | 0:0d5d376157a0 | 57 | int main() { |
sarahmarshy | 0:0d5d376157a0 | 58 | return !Harness::run(specification); |
sarahmarshy | 0:0d5d376157a0 | 59 | } |