Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:57 01 Aug 2025 Privacy Policy
Jump to

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 : CMM2 -  Noobie to c=CMM2 question on basic

Author Message
Goksteroo
Senior Member

Joined: 15/03/2021
Location: Australia
Posts: 114
Posted: 02:42pm 26 Mar 2021
Copy link to clipboard 
Print this post

I'm new to CMM2 and am digging my way into it's version of basic. I have done programming before on the Atari ST in STOS Basic and on the Amstrad CPC. My first project to learn the CMM2 is a maze generation program. All goes well until I want to control the movement of the player though a maze using the cursor keys. I'm using inkey$ to read for key presses but this is a real bottleneck and slows movement down quite a bit. Here is some test code I wrote after having issues with speed of this routine.

mode 1,8
gui cursor load "mazist.cur":' simple 4,4 cursor sprite
dim integer xx,yy,a,md
xx=400:yy=300
gui cursor on 2,xx,yy
'settick 500,flsh: ' un-rem this for a flashing cursor

do
a=asc(inkey$): ' rem this line for rnd numbers for keys

'a=rnd*3+128: ' un-rem this for keyboard input
'for cc=1 to 20: ' un-rem this for keyboard input

if a=130 and xx>100 then
 inc xx,-4
   else if a=131 and xx<700then
   inc xx,4
     else if a=128 and yy >100 then
     inc yy,-4
       else if a=129 and yy<500 then
       inc yy,4
endif
gui cursor xx,yy

'pause 5:next cc: ' un-rem this for keyboard input

loop until a=32
settick 0,flsh
end

sub flsh
 inc md
 if (md mod 2)=1 then
 gui cursor show
   else
   gui cursor hide
endif
end sub


The mazist.cur file is just a 4x4 sprite definition file.

4,4,0,0
4444
4444
4444
4444


Using this piece of code the cursor crawls around the screen. If I REM out the inkey$ routines and un-REM the random movement code, the cursor flies around the screen. In Amstrad basic I used an INKEY routine that interrogated a particular key and was very fast at doing so.

Have I missed a more appropriate keyboard command? or have I just written poor code? The Maximite is running the latest beta firmware.

Thanks,
Geoff
 
RetroJoe

Senior Member

Joined: 06/08/2020
Location: Canada
Posts: 290
Posted: 03:20pm 26 Mar 2021
Copy link to clipboard 
Print this post

Welcome, Geoff, to the wonderful hobby of "Maximiting"  

I believe KEYDOWN () will do the trick.
Enjoy Every Sandwich / Joe P.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 04:30pm 26 Mar 2021
Copy link to clipboard 
Print this post

KEYDOWN is definitely quicker.

Use KEYDOWN(1) because you are looking for one character
It returns a numeric value.
<- 130
-> 131
^  128
v  129
no key pressed = 0

Have fun. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1132
Posted: 05:06pm 26 Mar 2021
Copy link to clipboard 
Print this post

By using INKEY$, you are limited to the keyboard repeat rate.
(Use OPTION KEYBOARD REPEAT <delay>,<rate> to set a faster repeat rate. The lowest value for <rate> is 25, meaning 25 ms between keys.)

As the others have posted, KEYDOWN() would let your program check if a key is held down, without waiting for a repeat. Plus you can check for multiple (to some degree) presses, as well as modifiers.
Visit Vegipete's *Mite Library for cool programs.
 
RetroJoe

Senior Member

Joined: 06/08/2020
Location: Canada
Posts: 290
Posted: 07:06pm 26 Mar 2021
Copy link to clipboard 
Print this post

Geoff, definitely read up on KEYDOWN as it does a ton of stuff, arguably everything you would ever need to capture user input from the keyboard..

FYI, that CMM Fun site is a relatively recent gift to the Maximite community from Jiri, who has also been banging out some awesome developer utilities, and the online MMBasic reference is courtesy of "Tassy Jim" Hiley, the author of "MM Edit" that allows you to develop code on your PC or Mac and download it to your CMM2 (versus using its built-in keyboard and VGA).

If you go the "standalone" route, mouse support was recently added to the CMM2 firmware by Peter M., and makes using the built-in editor a whole lot more enjoyable. Depending on what "model" of mouse and  CMM2 you have, interfacing the mouse can take different paths. There is a forum thread  that explains it.

I guess it goes without saying that just about everything that goes on here is a labor of love :)

Happy Maximiting!
Edited 2021-03-27 05:24 by RetroJoe
Enjoy Every Sandwich / Joe P.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 07:54pm 26 Mar 2021
Copy link to clipboard 
Print this post

If anyone would like it, here's the first CMM2 utility that did. It shows you the ASC codes in decimal and hex for most of the sensible characters. :)
'just an ASCII test file

mode 1
cls
dim dx(120),dy(120)
x=20:y=20
for I=32 to 137
? @(x,y)i @(x+50)hex$(i,2) @(x+80)chr$(i)
dx(i-32)=x
dy(i-32)=y
x=x+120
if x>620 then x=20:y=y+30
next
?@(2,550)"Press key to show code"
do  
 a=keydown(1)
 if a>=32 and a<138 and f=0 then
   p=a-32
   b=a
   colour rgb(0,0,0),rgb(255,255,0)
   ?@(dx(p),dy(p)) a @(dx(p)+50)hex$(a,2) @(dx(p)+80)chr$(a)
   f=1
 end if
 if a=0 and f=1 then
   colour rgb(255,255,255),rgb(0,0,0)
   ?@(dx(p),dy(p)) b @(dx(p)+50)hex$(b,2) @(dx(p)+80)chr$(b)
   f=0
 end if
loop

Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Goksteroo
Senior Member

Joined: 15/03/2021
Location: Australia
Posts: 114
Posted: 11:59pm 26 Mar 2021
Copy link to clipboard 
Print this post

Thanks guys, don't know how I missed KEYDOWN!!

One question on it and the use of KEYDOWN(7) to test for the modifier keys. It returns values as expected for L and R shift but also changes the values (adds 32 like shift would with any other letter) returned with KEYDOWN(1) for the Down and Right arrow keys. The Left and Up values are not affected. Other modifier keys (Ctrl and Alt) do not change these values. I can get around this by changing to the Alt or Ctrl keys in my programme but am curious as to why this occurs. Oddly the Delete key also produces a Shifted character.

Geoff
Edited 2021-03-27 10:15 by Goksteroo
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025