void test_attach_us_time(void) { LowPowerTicker ticker; ticker_callback_flag = 0; gtimer.reset(); gtimer.start(); ticker.attach_us(callback(stop_gtimer_set_flag), DELAY_US); while(!ticker_callback_flag); ticker.detach(); const int time_diff = gtimer.read_us(); TEST_ASSERT_UINT64_WITHIN(TOLERANCE_US, DELAY_US, time_diff); }
/** Test multi callback time Given a Ticker When the callback is attached multiple times Then ticker properly execute callback multiple times */ void test_multi_call_time(void) { LowPowerTicker ticker; int time_diff; const int attach_count = 10; for (int i = 0; i < attach_count; i++) { ticker_callback_flag = 0; gtimer.reset(); gtimer.start(); ticker.attach_us(callback(stop_gtimer_set_flag), MULTI_TICKER_TIME_MS * 1000); while(!ticker_callback_flag); time_diff = gtimer.read_us(); TEST_ASSERT_UINT32_WITHIN(TOLERANCE_US, MULTI_TICKER_TIME_MS * 1000, time_diff); } }
/** Test if detach cancel scheduled callback event Given a Ticker with callback attached When the callback is detached Then the callback is not being called */ void test_detach(void) { LowPowerTicker ticker; int32_t ret; const float ticker_time_s = 0.1f; const uint32_t wait_time_ms = 500; Semaphore sem(0, 1); ticker.attach(callback(sem_release, &sem), ticker_time_s); ret = sem.wait(); TEST_ASSERT_TRUE(ret > 0); ret = sem.wait(); ticker.detach(); /* cancel */ TEST_ASSERT_TRUE(ret > 0); ret = sem.wait(wait_time_ms); TEST_ASSERT_EQUAL(0, ret); }
/** * Callback serial **/ void serial(int events) { unsigned char i; if(events & SERIAL_EVENT_RX_CHARACTER_MATCH) { for(i = 0; i < BUFFER_LENGTH; i++) { if(rx_buffer[i] == 't') break; // 't' is received, check for buffer length } if(blinking) { // Toggle blink counter.detach(); blinking = false; } else { counter.attach(blink, TOGGLE_SPEED); blinking = true; } } else if (events & SERIAL_EVENT_RX_COMPLETE) { i = BUFFER_LENGTH - 1; } test_connection.write(rx_buffer, i+1, 0, 0); // Echo string without callback test_connection.read(rx_buffer, BUFFER_LENGTH, serial_event, SERIAL_EVENT_RX_ALL, 't'); // Reset serial reception }