OPL programming noob questions

An alternative to the 'Software' section for larger program based threads and those that the author put up for discussion or comments.
User avatar
Daren
Posts: 138
Joined: Fri Sep 23, 2022 5:17 pm

OPL programming noob questions

Post by Daren »

I am very much a beginner in programming, in general, I have over the years done a few very very simple things in BASIC and OPL normally no more than about 20 lines and a few variables :lol:

Anyway, I have decided to try and use OPL to make a few simple utilities to suit my own use, one of these is a kind of calculator for different music related calculations, I want the program to be a single OPL app with various sub routines, so launch app, a menu of the different functions appears, select one, do function, when done app asks “again or exit to menu” where another function can be chosen or to exit to main menu.

I am aware of Martin Reids great looking app launcher utility and I will certainly be making use of that for other duties, but in this case I want it all within one app (which in turn will end up in Martin’s app launcher :)) so that it is easily portable to other datapaks without needing to remember to copy a bunch of files, just a single file.

I think that it is doable but not sure where to start, I guess that ideally I’d create each function in their own OPL file, ensure that they work correctly and then combine them into one OPL file with the menu at the top level - I’m guessing that I can learn a bit by studying Martin’s app launcher, but then I’m not sure how to go about combing the whole lot into one OPL program.

I apologise in advance for the probably dumb assumptions, a lot of this stuff goes over my head, but I’m keen to learn so any pointers in simple terms would be appreciated, needless to say I will share the program with anyone that wants it when done, and any subsequent programs I make.

About the program - it will be calculations related to music tempo etc, for example song length in minutes/seconds when given tempo and number of measures (and the other way around), note duration in time given tempo, sample length in number of samples given sample rate, musical duration and tempo, note frequencies in hz, and so on, probably around 8-12 different functions.
User avatar
MartinReid
Posts: 218
Joined: Wed Jan 27, 2021 3:44 pm

OPL programming noob questions

Post by MartinReid »

If NickKostelidis reads this he will advocate for using ATRAN to translate OPL's into OB3's as this gives access to GOSUB/RETURN

I on the other hand, contrary to what we were taught when programming our ZX Spectrums and Sinclair QL's, advocate careful use of the GOTO statement.

The following is a possible template..

Code: Select all

ProgName:

REM Declare GLOBAL and LOCAL variables m% holds menu choice

TOP::	:REM everything returns to here
CLS

REM ---------------------------------------------------
REM Set up screen (this can be a stand alone program)
UDG 0,4,14,30,14,4,10,0,31
UDG 2,0,0,0,0,0,0,0,31
AT 1,1 :PRINT chr$(0);rept$(chr$(2),14) :CLOCK(1)
REM ----------------------------------------------------

DO
 m%=MENUN(1,"Frst,Scnd,Thrd,Frth,Quit") :REM Change these 'labels' as needed
     IF m%=0 :m%=5 :REM 0 if ON/CLEAR pressed
 ELSEIF m%=1 :GOTO Frst::
 ELSEIF m%=2 :GOTO Scnd::
 ELSEIF m%=3 :GOTO Thrd::
 ELSEIF m%=4 :GOTO Frth::
 ELSEIF m%=5 :GOTO Quit::
ENDIF
UNTIL m%>5 :REM be careful this is an 'endless loop'
STOP

Frst::
REM
REM Put your 1st routine here
REM
GOTO TOP::

Scnd::
REM
REM Put your 2nd routine here
REM
GOTO TOP::

Thrd::
REM
REM Put your 3rd routine here
REM
GOTO TOP::

Frth::
REM
REM Put your 4th routine here
REM
GOTO TOP::

Quit::
REM test for any unsaved data and ask are you sure
RETURN
STOP
User avatar
Daren
Posts: 138
Joined: Fri Sep 23, 2022 5:17 pm

Re: OPL programming noob questions

Post by Daren »

Thanks Martin, that template looks very handy, I will try some things out with it.
User avatar
Daren
Posts: 138
Joined: Fri Sep 23, 2022 5:17 pm

Re: OPL programming noob questions

Post by Daren »

I’m quite enjoying learning/bumbling my way through OPL, solving problems by trial and (many!) error :D

I am using a spare machine for my endeavours because I had a few infinite loops which could not be exited, seems to be the sensible approach.
User avatar
MartinReid
Posts: 218
Joined: Wed Jan 27, 2021 3:44 pm

OPL programming examples

Post by MartinReid »

Daren

Have a look and try the snippets... AP:(x%,y%,z$) - nameUDG: - nameTOP(x%)

Martin

PS... Can you write a routine to calculate the cost of petrol per year? To run on a CM/XP or LZ!

Ask for miles per year and average miles per gallon and a sample price for a litre.
Then convert cost per litres to cost per gallon (4.54609 litres per gallon)

Yearly cost of petrol = Miles per year ÷ MPG x 4.54609 x Cost per litre
.
MartinP
Posts: 48
Joined: Tue Oct 05, 2021 7:01 pm

Re: OPL programming noob questions

Post by MartinP »

Daren,
Have you considered the Formulator for this sort of thing?
https://www.jaapsch.net/psion/manform2.htm
It's not as flexible as an OPL program, but might do some of what you want?
Martin P.
toml12953
Posts: 31
Joined: Mon Dec 20, 2021 4:55 am

Re: OPL programming noob questions

Post by toml12953 »

MartinReid wrote: Fri Oct 21, 2022 7:52 pm If NickKostelidis reads this he will advocate for using ATRAN to translate OPL's into OB3's as this gives access to GOSUB/RETURN
You can use functions to provide just about the same functionality as GOSUB.
User avatar
Daren
Posts: 138
Joined: Fri Sep 23, 2022 5:17 pm

Re: OPL programming noob questions

Post by Daren »

Can you elaborate on this point @toml12953 ?

Also what advantages does gosub have over goto/label?
toml12953
Posts: 31
Joined: Mon Dec 20, 2021 4:55 am

Re: OPL programming noob questions

Post by toml12953 »

Daren wrote: Mon Nov 21, 2022 3:43 pm Can you elaborate on this point @toml12953 ?

Also what advantages does gosub have over goto/label?
GOSUB goes to an address then returns to the line after the gosub call. You can call it from anywhere in the program and it will work properly. With GOTO, it can only return to one place so you can't call the same routine from different places in the program. You would use GOSUB for a common routine that is executed more than once.
User avatar
Daren
Posts: 138
Joined: Fri Sep 23, 2022 5:17 pm

Re: OPL programming noob questions

Post by Daren »

Thanks for the answer @toml12953 It is structure and program flow aspects which I can’t seem to find a lot of info/examples to do a lot of what I want, without using GOTO, for example multiple choice GET/KEY/INPUT branches, which functions were you alluding to that can approximate GOSUB functionality?
Post Reply