From 9fed0f10f9649b608d69bb0470483fd465f3e0dd Mon Sep 17 00:00:00 2001 From: IFX-Anusha Date: Mon, 8 Apr 2024 19:57:17 +0530 Subject: [PATCH] tests/psoc6: Timer Test Fix. Signed-off-by: IFX-Anusha --- tests/psoc6/timer.py | 79 ++++++++++++++++++++++++---------------- tests/psoc6/timer.py.exp | 25 +++++++++---- 2 files changed, 65 insertions(+), 39 deletions(-) diff --git a/tests/psoc6/timer.py b/tests/psoc6/timer.py index 3e3b31c725a0f..67eb0ab6fb7ee 100644 --- a/tests/psoc6/timer.py +++ b/tests/psoc6/timer.py @@ -1,37 +1,54 @@ from machine import Timer -import uasyncio as asyncio -interrupt_triggered = False # Flag to indicate interrupt triggered - -def call(timer): - global interrupt_triggered - interrupt_triggered = True - -async def print_message_thread(): - global interrupt_triggered - while True: - if interrupt_triggered: - print("Interrupt triggered") - interrupt_triggered = False # Reset the flag - await asyncio.sleep(0.1) # Use asyncio.sleep instead of time.sleep - -async def main(): - print("Oneshot Timer Execution") - tim = Timer(0, period=1000, mode=Timer.ONE_SHOT, callback=call) +import time + +oneshot_triggered = False +periodic_triggered = False + + +def call_oneshot(timer): + global oneshot_triggered + oneshot_triggered = True + + +def call_periodic(timer): + global periodic_triggered + periodic_triggered = True + + +def test_oneshot(): + # Periodic timer + global oneshot_triggered + tim_oneshot = Timer(0, period=1000, mode=Timer.ONE_SHOT, callback=call_oneshot) + try: - logger_task = asyncio.create_task(print_message_thread()) - await asyncio.sleep(5) # Wait for 5 seconds - logger_task.cancel() # Cancel the logger task + # Wait for 5 seconds + for i in range(5): + time.sleep(1) + if oneshot_triggered: + print("Oneshot timer triggered") + oneshot_triggered = False finally: - tim.deinit() # Deinitialize the timer - print("Periodic Timer Execution") - tim = Timer(0, period=1000, mode=Timer.PERIODIC, callback=call) + tim_oneshot.deinit() # Deinitialize the periodic timer + + +def test_periodic(): + # Periodic timer + global periodic_triggered + tim_periodic = Timer(0, period=1, mode=Timer.PERIODIC, callback=call_periodic) + try: - logger_task = asyncio.create_task(print_message_thread()) - await asyncio.sleep(5) # Wait for 5 seconds - logger_task.cancel() # Cancel the logger task + # Wait for 15 seconds + for i in range(15): + time.sleep(1) + if periodic_triggered: + print("Periodic timer triggered") + periodic_triggered = False finally: - tim.deinit() # Deinitialize the timer - + tim_periodic.deinit() # Deinitialize the periodic timer + + if __name__ == "__main__": - print("Timer Test") - asyncio.run(main()) \ No newline at end of file + print("*****Oneshot Timer Execution*****") + test_oneshot() + print("*****Periodic Timer Execution*****") + test_periodic() diff --git a/tests/psoc6/timer.py.exp b/tests/psoc6/timer.py.exp index e677f41f5238e..f35489a58832b 100644 --- a/tests/psoc6/timer.py.exp +++ b/tests/psoc6/timer.py.exp @@ -1,8 +1,17 @@ -Timer Test -Oneshot Timer Execution -Interrupt triggered -Periodic Timer Execution -Interrupt triggered -Interrupt triggered -Interrupt triggered -Interrupt triggered \ No newline at end of file +*****Oneshot Timer Execution***** +Oneshot timer triggered +*****Periodic Timer Execution***** +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered +Periodic timer triggered