Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : ON KEY Interrupt
Author | Message | ||||
panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1101 |
Hi Folks, This is my second "Am I doing something wrong?" in 24 hours so bear with me if this is just something dumb I am doing or not doing (or an advanced "senior's moment"). I keep getting a syntax error in the code below - early days in a line editing routine I am working on. Can anyone point out where I am going wrong with the ON KEY command? ' Line Editor Routine ' uses 3 global variables ' LineBuf$ - holds the current line and added to on every keypress ' CursPos - position in LineBuf$ where next character will be stored ' KeyChr - the last key pressed ' Uses the ON KEY interrupt to catch characters ' to use, preset LineBuf$ to empty, CursPos to 1, KeyChr to 0 then ' enter a loop waiting for either a KeyChr = 10 (CR) or optionally, ' a timeout to finnish. ' Sample useage Cls LineBuf$ = "" ' initialise variables CursPos = 1 KeyChr = 0 EdLinX = 1 ' top left of line being entered and/or edited EdLinY = 24 FlipInv = 2 Print "Testing ON KEY interrupt routine" On Key KeyPress ' set up keypress interrupt Timer ' optional timeout if no user input Do While KeyChr <> 10 Or Timer <> 20000 For LinePos = 1 To Len(LineBuf$) + 1 If LinePos = CursPos Then If Len(LineBuf$) = 0 Then Print @(EdLinX,EdLinY,FlipInv)" " 'cursor as inverse space at end of line EndIf Print @(EdLinX,EdLinY,FlipInv)Mid$(LineBuf$,LinePos + 1,1) 'character under cursor in inverse Else Print @(EdLinX,EdLinY,0)Mid$(LineBuf$,LinePos + 1,1) ' EndIf CursPos = CursPos + 1 Next Pause 500 Loop End 'ON KEY interrupt service routine KeyPress: KeyChr = Asc(Inkey$) If KeyChr = 10 Then ' CR ' dummy Else LineBuf$ = LineBuf$ + Chr$(KeyChr) EndIf IReturn Thanks, Doug. ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
Doug MMBasic usually provides a bit of help when you have a problem. If you go back into the editor it usually points to the line it thinks has the error. I think it mentions a line number also. That said, you have a problem with the following lines I assume you will fix this before you try out a line feed. This one may be a bit nit picking but when you test for something you should test for what you want. See the line You want the timer to be LESS THAN 20000 or else it times out. Then you should test for that. Testing for NOT EQUAL is not the same. I think it won't make any difference here because Timer is incremented (I think it is?) but in some other code situations if the value went above or below but never equalled the value then your test and your code would fail. How do you handle multiple keystrokes in the buffer? Allow to interrupt again immediately? Have you considered looping in the ISR until the key buffer is empty? Back to the syntax errors; is the ISR changing a variable value between two lines in the main loop and clobbering your code? The only one that looks a possibility is LineBuf$. That would then affect the Len test and the For loop. Would that matter? It's far too late for anything else tonight. Bob |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6100 |
Line 23 should be TIMER = 0 There are other problems but that is the show stopper for now. Jim VK7JH MMedit MMBasic Help |
||||
panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1101 |
Hi Jim, Thanks for the response - checked out your suggestions to no avail so I simplified the code to just test the ON KEY command (see below). The failing line according to MMB is the ON KEY command and I can't see what I am doing wrong! Cheers, Doug. LineBuf$ = "" ' initialise variables KeyChr = 0 Print "Testing ON KEY interrupt routine" On Key KeyPress ' set up keypress interrupt Print "Line after ON KEY comand" Do Pause 500 Loop Until KeyChr = 10 End 'ON KEY interrupt service routine KeyPress: KeyChr = Asc(Inkey$) LineBuf$ = LineBuf$ + Chr$(KeyChr) IReturn ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
palcal Guru Joined: 12/10/2011 Location: AustraliaPosts: 1873 |
Panky, I changed a couple of things and the code works fine. As well as what Jim said to change line 23 to Timer=0, I changed the the way we exit from the DO-LOOP to:- Do 'code Loop Until KeyChr=13 Or Timer>20000 Print LineBuf$ End It runs ok and prints out the line of characters that I entered after pressing 'ENTER' or it times out. I did not get any SYNTAX ERRORS. I don't think On Key has always been a command are you running the latest version of MM basic. Also the carriage return on my keyboard is ASCII 13 not 10. Palcal. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6100 |
A slight change to your last test code: LineBuf$ = "" ' initialise variables
KeyChr = 0 Print "Testing ON KEY interrupt routine" On Key KeyPress ' set up keypress interrupt Print "Line after ON KEY comand" Do Pause 500 Loop Until KeyChr = 13 print "Finished line:" print LineBuf$ End 'ON KEY interrupt service routine KeyPress: KeyChr = Asc(Inkey$) LineBuf$ = LineBuf$ + Chr$(KeyChr) print LineBuf$ IReturn I am using MMBasic 4.4 and a terminal program on the PC. I don't have a keyboard/screen handy. Interestingly, the PRINT LineBuf$ inside the KeyPress routine does NOT print for me. The LineBuf$ prints OK after Do LOOP ends. Jim VK7JH MMedit MMBasic Help |
||||
panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1101 |
Thanks to all for your responses, Much to my embarresment, I was trying to run on a Duinomite running 4.3 !!!! I'm at my brothers place on holidays for the moment who is also a MM nut - between us we have 2 x mono MM's, 3 x Duinomite Mega's, a CGCMM1 and a CGCMM2 and a mono mini MM. I updated them all to 4.4 (or so I thought) and , yep, you guessed it, the Duinomite I was using as a test bed was the only one that slipped through the cracks getting upgraded. So, end result, ON KEY works fine when you run it under 4.4 - very sorry to have wasted anyone's time. For Jim, I use MMEdit to edit and also communicate with the MM - when I put a print statement inside the ON KEY interrupt routine, it works fine. Regards, Doug. ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
Print this page |