slviajero / tinybasic

A BASIC interpreter for Arduino, ESP, RP2040, STM32, Infineon XMC and POSIX with IoT and microcontroller features.
GNU General Public License v3.0
208 stars 32 forks source link

Warm Restart #27

Closed EncomLab closed 2 years ago

EncomLab commented 2 years ago

On the SBC I use I have a button to pin 10 and this in setup:

//interrupt pin
  pinMode(interruptPin, INPUT);
  enableInterrupt(interruptPin, interruptFunction, FALLING);

that calls this function:

void interruptFunction() {
  current_line = 0;
  sp = program + sizeof(program);
  printmsg(okmsg);
}

Obviously this is for the BASIC interpreter I've been using - is there a way to implement something like this in your interpreter? When called it functions like a "break" and leaves the resident program and variable assignments intact.

slviajero commented 2 years ago

Transparent interrupt handling from BASIC is on my list for version 1.4. I will complete 1.3. and release it in a while and then start to work on this. It is not really easy to implement this. I have a concept for it but I am unsure which language features to use.

The way it needs to be done is to build an interrupt handler just like the function you show.

Then one has to catch the interrupt condition in the statement() loop and branch there to a new line. This way an interrupt could trigger a GOTO or GOSUB. Problem is that one has to handle infinite loops. If the interrupt comes frequently, the interpreter doesn’t have to to process the code. One would need an interrupt mask function in addition to the mechanism above.

If you want something very specific / quick and dirty, you could just add an interrupt handler and then deposite a value to a variable. Ideally a static variable A-Z. This is done through setvar(). The BASIC code could then poll state changes of this variable. Cleaner would be a new C variable and a function to access it.

Am 18.07.2022 um 22:53 schrieb Encom Lab @.***>:

On the SBC I use I have a button to pin 10 and this in setup:

//interrupt pin pinMode(interruptPin, INPUT); enableInterrupt(interruptPin, interruptFunction, FALLING); that calls this function:

void interruptFunction() { current_line = 0; sp = program + sizeof(program); printmsg(okmsg); } Obviously this is for the BASIC interpreter I've been using - is there a way to implement something like this in your interpreter? When called it functions like a "break" and leaves the resident program and variable assignments intact.

— Reply to this email directly, view it on GitHub https://github.com/slviajero/tinybasic/issues/27, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSY56BNBSGI3P2M2DO22A3VUW725ANCNFSM535TSHIA. You are receiving this because you are subscribed to this thread.

EncomLab commented 2 years ago

I appreciate this detailed description and look forward to the continued development of the interpreter. One of the functions I'm looking to add is the ability to handle data from rotary encoders which will require the implementation of ISR's in some form.

slviajero commented 2 years ago

Can you describe what you need / want? If I understand the use case I would be able to do something right away.

Am 19.07.2022 um 14:43 schrieb Encom Lab @.***>:

I appreciate this detailed description and look forward to the continued development of the interpreter. One of the functions I'm looking to add is the ability to handle data from rotary encoders which will require the implementation of ISR's in some form.

— Reply to this email directly, view it on GitHub https://github.com/slviajero/tinybasic/issues/27#issuecomment-1189006332, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSY56EDXFLLDYHPYZ6FS73VU2PF3ANCNFSM535TSHIA. You are receiving this because you commented.

EncomLab commented 2 years ago

Hi Stefan! I created a solution for my use case by adding the Attach Interrupts library and a loop function which calls XBREAK() when a pin is brought LOW. I have a ATmega1284 board that I use for controlling small robot platforms and it has a RESET button which resets the chip and a INTERRUPT button tied to INT0 in MightyCore. So now when I hit the Interrupt button it generates a BREAK which keeps the current program in memory which is ultimately what I wanted as obviously RESET does not.

Thanks for the follow up - I use this nearly every day. The ability to add functions is great - I've added servo commands, motor commands, ultrasonic sensors and soon am adding gyro and compass commands.

slviajero commented 2 years ago

Super! Can you send me the code with the interrupt routine? I would like to take a look at it. Currently I mostly work on hardware but will return to software during the winter. Then it is time for interrupt handling in the code. Best, Stefan

Am 11.10.2022 um 13:31 schrieb Encom Lab @.***>:

Hi Stefan! I created a solution for my use case by adding the Attach Interrupts library and a loop function which calls XBREAK() when a pin is brought LOW. I have a ATmega1284 board that I use for controlling small robot platforms and it has a RESET button which resets the chip and a INTERRUPT button tied to INT0 in MightyCore. So now when I hit the Interrupt button it generates a BREAK which keeps the current program in memory which is ultimately what I wanted as obviously RESET does not.

Thanks for the follow up - I use this nearly every day. The ability to add functions is great - I've added servo commands, motor commands, ultrasonic sensors and soon am adding gyro and compass commands.

— Reply to this email directly, view it on GitHub https://github.com/slviajero/tinybasic/issues/27#issuecomment-1274538378, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSY56F2RJR52DXU67SQR4DWCVFZVANCNFSM535TSHIA. You are receiving this because you modified the open/close state.

EncomLab commented 2 years ago

Hi Stefan! This is a pretty hacky implementation on my part but here goes - First I'm using my own SBC with the ATmega 1284 running Mightycore. On the PCB I have a tactile button connected to D10 for use as a Warm Reset on the previous version of BASIC I was using which is where the code I posted above comes from.
For this case I added the Arduino Library "EnableInterrupt". In the void loop I added: /* soft interrupt */ pinMode(10, INPUT_PULLUP); enableInterrupt(10, interruptFunctionSR, FALLING);

and then just call xbreak(): //soft reset void interruptFunctionSR() { xbreak(); } The main purpose for me is being able to break a loop without losing the program as would happen if I used a regular reset.

slviajero commented 2 years ago

That is pretty brutal. It works because most of the interpreter logic is stateless and xbreak() only changes „here“ i.e. the program execution cursor. This works almost always.There may be (very rare) situation where this breaks the interpreter.

By the way, BASIC can always be interrupted by sending the BREAKCHAR through the serial line. By default, BREAKCHAR is #. This is a clean way coming to interactive mode.

I will think about a clean way to implement your idea.

Am 14.10.2022 um 23:17 schrieb Encom Lab @.***>:

Hi Stefan! This is a pretty hacky implementation on my part but here goes - First I'm using my own SBC with the ATmega 1284 running Mightycore. On the PCB I have a tactile button connected to D10 for use as a Warm Reset on the previous version of BASIC I was using which is where the code I posted above comes from. For this case I added the Arduino Library "EnableInterrupt". In the void loop I added: / soft interrupt / pinMode(10, INPUT_PULLUP); enableInterrupt(10, interruptFunctionSR, FALLING);

and then just call xbreak(): //soft reset void interruptFunctionSR() { xbreak(); } The main purpose for me is being able to break a loop without losing the program as would happen if I used a regular reset.

— Reply to this email directly, view it on GitHub https://github.com/slviajero/tinybasic/issues/27#issuecomment-1279476868, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACSY56HBREVDW433QIVBRQTWDHEVFANCNFSM535TSHIA. You are receiving this because you modified the open/close state.

EncomLab commented 2 years ago

20221015_171025 This is the platform I am using - no keyboard or serial while operating hence the crash to break;)

slviajero commented 2 years ago

The difference between the hardware guy and the software guy. Sent from my iPhone

On Oct 16, 2022, at 1:40 AM, Encom Lab @.***> wrote:



This is the platform I am using - no keyboard or serial while operating hence the crash to break;)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you modified the open/close state.