mbed demo
/
Blinky_Tests
Tests for simple blinky library.
Fork of Blinky_Tests by
TESTS/Blinky/5_Blinks/main.cpp@4:09af3b2d723e, 2016-11-08 (annotated)
- Committer:
- mbed_demo
- Date:
- Tue Nov 08 22:12:16 2016 +0000
- Revision:
- 4:09af3b2d723e
- Parent:
- 2:a6755ef75598
- Child:
- 5:4f1e6b065e6b
Tests use blinky method instead of InterruptIn
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 | //To be set by interrupt handler |
sarahmarshy | 0:0d5d376157a0 | 13 | |
sarahmarshy | 0:0d5d376157a0 | 14 | //Generic blink test |
sarahmarshy | 0:0d5d376157a0 | 15 | //interval => the the amount of time between blinks |
sarahmarshy | 0:0d5d376157a0 | 16 | //led => PinName of LED to blink |
mbed_demo | 2:a6755ef75598 | 17 | void test_blinks(PinName led, int interval){ |
sarahmarshy | 0:0d5d376157a0 | 18 | //Set an appropriate wait time to measure exactly expected_blinks |
sarahmarshy | 0:0d5d376157a0 | 19 | //with the given interval |
sarahmarshy | 0:0d5d376157a0 | 20 | float wait_time = 2*interval*expected_blinks; |
sarahmarshy | 0:0d5d376157a0 | 21 | //Create Blinky |
sarahmarshy | 0:0d5d376157a0 | 22 | Blinky blinker(led, interval); |
sarahmarshy | 0:0d5d376157a0 | 23 | //start blinky |
sarahmarshy | 0:0d5d376157a0 | 24 | blinker.start(); |
sarahmarshy | 0:0d5d376157a0 | 25 | //Wait the appropriate amount of time for expected_blinks |
sarahmarshy | 0:0d5d376157a0 | 26 | Thread::wait(wait_time); |
sarahmarshy | 0:0d5d376157a0 | 27 | blinker.stop(); |
mbed_demo | 4:09af3b2d723e | 28 | |
mbed_demo | 4:09af3b2d723e | 29 | float measured_blinks = blinker.times_blinked(); |
mbed_demo | 2:a6755ef75598 | 30 | //Only assert if failure |
mbed_demo | 4:09af3b2d723e | 31 | TEST_ASSERT(measured_blinks == (float)expected_blinks); |
sarahmarshy | 0:0d5d376157a0 | 32 | } |
sarahmarshy | 0:0d5d376157a0 | 33 | |
sarahmarshy | 0:0d5d376157a0 | 34 | void blink_500(){ |
sarahmarshy | 0:0d5d376157a0 | 35 | //Blinky with a 500 ms interval between blinks |
mbed_demo | 2:a6755ef75598 | 36 | test_blinks(LED1, 500); |
sarahmarshy | 0:0d5d376157a0 | 37 | } |
sarahmarshy | 0:0d5d376157a0 | 38 | void blink_1000(){ |
sarahmarshy | 0:0d5d376157a0 | 39 | //Blinky with a 1000 ms interval between blinks |
mbed_demo | 2:a6755ef75598 | 40 | test_blinks(LED1, 1000); |
sarahmarshy | 0:0d5d376157a0 | 41 | } |
sarahmarshy | 0:0d5d376157a0 | 42 | |
sarahmarshy | 0:0d5d376157a0 | 43 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
sarahmarshy | 0:0d5d376157a0 | 44 | // Setup Greentea using a reasonable timeout in seconds |
sarahmarshy | 0:0d5d376157a0 | 45 | GREENTEA_SETUP(40, "default_auto"); |
sarahmarshy | 0:0d5d376157a0 | 46 | return verbose_test_setup_handler(number_of_cases); |
sarahmarshy | 0:0d5d376157a0 | 47 | } |
sarahmarshy | 0:0d5d376157a0 | 48 | |
sarahmarshy | 0:0d5d376157a0 | 49 | // Test cases |
sarahmarshy | 0:0d5d376157a0 | 50 | Case cases[] = { |
sarahmarshy | 0:0d5d376157a0 | 51 | Case("Testing 500 ms interval", blink_500), |
sarahmarshy | 0:0d5d376157a0 | 52 | Case("Testing 1000 ms interval", blink_1000), |
sarahmarshy | 0:0d5d376157a0 | 53 | }; |
sarahmarshy | 0:0d5d376157a0 | 54 | |
sarahmarshy | 0:0d5d376157a0 | 55 | Specification specification(test_setup, cases); |
sarahmarshy | 0:0d5d376157a0 | 56 | |
sarahmarshy | 0:0d5d376157a0 | 57 | // Entry point into the tests |
sarahmarshy | 0:0d5d376157a0 | 58 | int main() { |
sarahmarshy | 0:0d5d376157a0 | 59 | return !Harness::run(specification); |
sarahmarshy | 0:0d5d376157a0 | 60 | } |