From fb8f9410f2259660b8c2c64c2b8ca2cef392bb89 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 8 Dec 2018 19:15:24 -0500 Subject: [PATCH] #129 current state of PWM --- firmware/controllers/system/event_queue.cpp | 3 ++ unit_tests/test_pwm_generator.cpp | 53 +++++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/firmware/controllers/system/event_queue.cpp b/firmware/controllers/system/event_queue.cpp index adebb3c5b0..5fa4637007 100644 --- a/firmware/controllers/system/event_queue.cpp +++ b/firmware/controllers/system/event_queue.cpp @@ -219,6 +219,9 @@ scheduling_s *EventQueue::getForUnitText(int index) { return current; index--; } +#if EFI_UNIT_TEST + firmwareError(OBD_PCM_Processor_Fault, "getForUnitText: null"); +#endif /* EFI_UNIT_TEST */ return NULL; } diff --git a/unit_tests/test_pwm_generator.cpp b/unit_tests/test_pwm_generator.cpp index a93ee34a26..6047521e1f 100644 --- a/unit_tests/test_pwm_generator.cpp +++ b/unit_tests/test_pwm_generator.cpp @@ -13,8 +13,27 @@ extern EventQueue schedulingQueue; extern int timeNowUs; -void testApplyPinState(PwmConfig *state, int stateIndex) { +static int nextTime = 800; +static void testApplyPinState(PwmConfig *state, int stateIndex) { + int value = state->multiWave.waves[0].pinStates[stateIndex]; + + printf("Applying value %d @ timeNow=%d\r\n", value, timeNowUs); +} + +static void assertNextEvent(const char *msg) { + printf("Asserting event [%s]\r\n", msg); + // only one action expected in queue + assertEquals(1, schedulingQueue.size()); + + // move time to next event timestamp + timeNowUs += nextTime; + + // execute pending actions and assert that only one action was executed + assertEqualsM(msg, 1, schedulingQueue.executeAll(timeNowUs)); + + // assert that we have one new action in queue + assertEquals(1, schedulingQueue.size()); } void testPwmGenerator() { @@ -24,18 +43,42 @@ void testPwmGenerator() { OutputPin pin; - //pwm.setFrequency(600); - schedulingQueue.clear(); startSimplePwm(&pwm, "unit_test", &pin, - 600 /* frequency */, + 1000 /* frequency */, 0.80 /* duty cycle */, &testApplyPinState); - assertEquals(1, schedulingQueue.size()); + int start = timeNowUs; + assertEqualsM2("1@1000/80", start + nextTime, schedulingQueue.getForUnitText(0)->momentX, 0); + + assertNextEvent("exec@0"); + assertEqualsM("time1", start + 800, timeNowUs); + + nextTime += 200; + assertEqualsM2("2@1000/80", start + nextTime, schedulingQueue.getForUnitText(0)->momentX, 0); + + + pwm.setSimplePwmDutyCycle(0); + + assertNextEvent("exec@1"); + assertEqualsM("time2", start + 1800, timeNowUs); + + assertEqualsM2("2@1000/80", start + nextTime, schedulingQueue.getForUnitText(0)->momentX, 0); + + assertNextEvent("exec@2"); + assertEqualsM("time3", start + 2800, timeNowUs); + + assertNextEvent("exec@3"); + assertEqualsM("time4", start + 3800, timeNowUs); + + assertNextEvent("exec@4"); + assertNextEvent("exec@5"); } + +