mbed demo / Mbed OS Blinky_Tests

Dependencies:   Blinky

Fork of Blinky_Tests by Sarah Marsh

Committer:
mbed_demo
Date:
Tue Nov 08 18:16:55 2016 +0000
Revision:
2:a6755ef75598
Parent:
1:b0034f8b2c42
Child:
4:09af3b2d723e
Edit test;

Who changed what in which revision?

UserRevisionLine numberNew 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 int measured_blinks = 0;
sarahmarshy 0:0d5d376157a0 14
sarahmarshy 0:0d5d376157a0 15 //To be called by interrupt, increases the number of measured rising edges
sarahmarshy 0:0d5d376157a0 16 void count_blinks(){
mbed_demo 2:a6755ef75598 17 measured_blinks++;
sarahmarshy 0:0d5d376157a0 18 }
sarahmarshy 0:0d5d376157a0 19
sarahmarshy 0:0d5d376157a0 20 //Generic blink test
sarahmarshy 0:0d5d376157a0 21 //interval => the the amount of time between blinks
sarahmarshy 0:0d5d376157a0 22 //led => PinName of LED to blink
mbed_demo 2:a6755ef75598 23 void test_blinks(PinName led, int interval){
sarahmarshy 0:0d5d376157a0 24 //reset the measured blinks to 0 for each case
sarahmarshy 0:0d5d376157a0 25 measured_blinks = 0;
sarahmarshy 0:0d5d376157a0 26 //Set an appropriate wait time to measure exactly expected_blinks
sarahmarshy 0:0d5d376157a0 27 //with the given interval
sarahmarshy 0:0d5d376157a0 28 float wait_time = 2*interval*expected_blinks;
sarahmarshy 0:0d5d376157a0 29 //Create an interrupt that calls count_blinks on rising edge of LED pin
sarahmarshy 0:0d5d376157a0 30 InterruptIn blink_counter(led);
sarahmarshy 0:0d5d376157a0 31 //Create Blinky
sarahmarshy 0:0d5d376157a0 32 Blinky blinker(led, interval);
sarahmarshy 0:0d5d376157a0 33 //Attach interrupt
sarahmarshy 0:0d5d376157a0 34 blink_counter.rise(&count_blinks);
sarahmarshy 0:0d5d376157a0 35 //start blinky
sarahmarshy 0:0d5d376157a0 36 blinker.start();
sarahmarshy 0:0d5d376157a0 37 //Wait the appropriate amount of time for expected_blinks
sarahmarshy 0:0d5d376157a0 38 Thread::wait(wait_time);
sarahmarshy 0:0d5d376157a0 39 blinker.stop();
mbed_demo 2:a6755ef75598 40 //Only assert if failure
mbed_demo 2:a6755ef75598 41 TEST_ASSERT(measured_blinks != expected_blinks);
sarahmarshy 0:0d5d376157a0 42 }
sarahmarshy 0:0d5d376157a0 43
sarahmarshy 0:0d5d376157a0 44 void blink_500(){
sarahmarshy 0:0d5d376157a0 45 //Blinky with a 500 ms interval between blinks
mbed_demo 2:a6755ef75598 46 test_blinks(LED1, 500);
sarahmarshy 0:0d5d376157a0 47 }
sarahmarshy 0:0d5d376157a0 48 void blink_1000(){
sarahmarshy 0:0d5d376157a0 49 //Blinky with a 1000 ms interval between blinks
mbed_demo 2:a6755ef75598 50 test_blinks(LED1, 1000);
sarahmarshy 0:0d5d376157a0 51 }
sarahmarshy 0:0d5d376157a0 52
sarahmarshy 0:0d5d376157a0 53 utest::v1::status_t test_setup(const size_t number_of_cases) {
sarahmarshy 0:0d5d376157a0 54 // Setup Greentea using a reasonable timeout in seconds
sarahmarshy 0:0d5d376157a0 55 GREENTEA_SETUP(40, "default_auto");
sarahmarshy 0:0d5d376157a0 56 return verbose_test_setup_handler(number_of_cases);
sarahmarshy 0:0d5d376157a0 57 }
sarahmarshy 0:0d5d376157a0 58
sarahmarshy 0:0d5d376157a0 59 // Test cases
sarahmarshy 0:0d5d376157a0 60 Case cases[] = {
sarahmarshy 0:0d5d376157a0 61 Case("Testing 500 ms interval", blink_500),
sarahmarshy 0:0d5d376157a0 62 Case("Testing 1000 ms interval", blink_1000),
sarahmarshy 0:0d5d376157a0 63 };
sarahmarshy 0:0d5d376157a0 64
sarahmarshy 0:0d5d376157a0 65 Specification specification(test_setup, cases);
sarahmarshy 0:0d5d376157a0 66
sarahmarshy 0:0d5d376157a0 67 // Entry point into the tests
sarahmarshy 0:0d5d376157a0 68 int main() {
sarahmarshy 0:0d5d376157a0 69 return !Harness::run(specification);
sarahmarshy 0:0d5d376157a0 70 }