Example greentea tests with Blinky library

Dependencies:   Blinky

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "Blinky.h"
00002 #include "mbed.h"
00003 #include "greentea-client/test_env.h"
00004 #include "unity.h"
00005 #include "utest.h"
00006 #include "rtos.h"
00007  
00008 using namespace utest::v1;
00009  
00010 //All tests will blink the LED 5 times
00011 const int expected_blinks = 5;
00012 //To be set by interrupt handler
00013 int measured_blinks = 0;
00014  
00015 //To be called by interrupt, increases the number of measured rising edges 
00016 void count_blinks(){
00017         TEST_ASSERT_MESSAGE(++measured_blinks<=expected_blinks, "******Too many blinks!******");
00018 }
00019  
00020 //Generic blink test
00021 //interval => the the amount of time between blinks
00022 //led => PinName of LED to blink
00023 bool test_blinks(int interval, PinName led){
00024     //reset the measured blinks to 0 for each case
00025     measured_blinks = 0;
00026     //Set an appropriate wait time to measure exactly expected_blinks
00027     //with the given interval 
00028     float wait_time = 2*interval*expected_blinks;
00029     //Create an interrupt that calls count_blinks on rising edge of LED pin
00030     InterruptIn blink_counter(led);
00031     //Create Blinky
00032     Blinky blinker(led, interval);
00033     //Attach interrupt
00034     blink_counter.rise(&count_blinks);
00035     //start blinky
00036     blinker.start();
00037     //Wait the appropriate amount of time for expected_blinks 
00038     Thread::wait(wait_time);
00039     blinker.stop();
00040 }
00041  
00042 void blink_500(){
00043     //Blinky with a 500 ms interval between blinks
00044     test_blinks(500, LED1);
00045 }
00046 void blink_1000(){
00047     //Blinky with a 1000 ms interval between blinks
00048     test_blinks(1000, LED1);
00049 }
00050  
00051 utest::v1::status_t test_setup(const size_t number_of_cases) {
00052     // Setup Greentea using a reasonable timeout in seconds
00053     GREENTEA_SETUP(40, "default_auto");
00054     return verbose_test_setup_handler(number_of_cases);
00055 }
00056  
00057 // Test cases
00058 Case cases[] = {
00059     Case("Testing 500 ms interval", blink_500),
00060     Case("Testing 1000 ms interval", blink_1000),
00061 };
00062  
00063 Specification specification(test_setup, cases);
00064  
00065 // Entry point into the tests
00066 int main() {
00067     return !Harness::run(specification);
00068 }