Post by Rod on Jan 12, 2022 5:08:35 GMT -5
This subject has been mentioned in other threads. So I thought we could have a thread that helps beginners understand better how to use Wait and Scan within Subs and Functions. I hope several of our knowledgeable members can contribute little snippets to help beginners get to grips with the issue.
A variety of folks come to Liberty BASIC, many have previous experience of coding but not necessarily BASIC. Some are returning to BASIC after years of absence. BASIC has moved on apace particularly on the interrupt side and no longer do we have dedicated inkey$ function or a screen update function. On old eight bit processors BASIC really had the processor to itself. Now all of our interrupts, mouse, keyboard and joystick are managed by Windows as and when it gets round to it.
The problem in a nutshell. Wait sits and waits for an interrupt to occur, scan checks if an interrupt has occurred. Both cause the program flow to jump to the handler set up for the event. That might be a Timer event, a Mouse event or a Keyboard event. Liberty allows several event handlers to be set up and wait concurrently for their particular event to fire. It is the programmers job to manage these events off and on. The problem Subs and Functions bring to the table is that handlers not in their scope can cause the program to error and crash. So [branch label] handlers cannot be seen within a Sub or Function if they are defined elsewhere. A handler that is a Sub or function itself alleviates the problem but there are many strategies to avoid the problem.
I hope we can get some nice SIMPLE examples of strategies to manage interrupts.
A variety of folks come to Liberty BASIC, many have previous experience of coding but not necessarily BASIC. Some are returning to BASIC after years of absence. BASIC has moved on apace particularly on the interrupt side and no longer do we have dedicated inkey$ function or a screen update function. On old eight bit processors BASIC really had the processor to itself. Now all of our interrupts, mouse, keyboard and joystick are managed by Windows as and when it gets round to it.
The problem in a nutshell. Wait sits and waits for an interrupt to occur, scan checks if an interrupt has occurred. Both cause the program flow to jump to the handler set up for the event. That might be a Timer event, a Mouse event or a Keyboard event. Liberty allows several event handlers to be set up and wait concurrently for their particular event to fire. It is the programmers job to manage these events off and on. The problem Subs and Functions bring to the table is that handlers not in their scope can cause the program to error and crash. So [branch label] handlers cannot be seen within a Sub or Function if they are defined elsewhere. A handler that is a Sub or function itself alleviates the problem but there are many strategies to avoid the problem.
I hope we can get some nice SIMPLE examples of strategies to manage interrupts.