Page 1 of 1
Returning Const Value via Procedural Type
Posted: Mon Jul 06, 2015 5:41 am
by captbill
I am trying to implement a procedure type which returns the value of a constant. I need to implement a basic enumerator structure that can be processed in order. Is there a better way than the procedural approach I am attempting here? Is my use of a procedural type well founded? Can you point to any examples?
Code: Select all
TYPE
chararray = ARRAY OF CHAR;
ConstValFunc = PROCEDURE (RegName: chararray):INTEGER;
InitSequence = ARRAY OF ConstValFunc;
VAR
constvalue : INTEGER;
PROCEDURE RegValue(String: ConstValFunc):INTEGER;
BEGIN
constvalue := (String);
RETURN constvalue
END RegValue;
Re: Returning Const Value via Procedural Type
Posted: Mon Jul 06, 2015 12:25 pm
by cfbsoftware
I need more details, preferably with a number of examples, of what you are trying to do, rather than an example of how you are trying to do it, to be able to tell if you are on the right track or if there is a better way.
e.g. do you have a number of strings that you want to associate numbers with? Something like:
0: "Red"
1: "Orange"
2: "Yellow"
Then given a string you want to return its number? Or given a number you want to return its string? Do the strings / numbers change in any circumstances?
Or are you thinking of something completely different?
Re: Returning Const Value via Procedural Type
Posted: Mon Jul 06, 2015 7:40 pm
by captbill
I have a large listing of registers that I need to order sequentially to make SPI init sequences.
Code: Select all
CONST
RA8875_PWRR = 010H; (*Power and Display Control Register*)
RA8875_PWRR_DISPON = 080H;
RA8875_PWRR_DISPOFF = 000H;
RA8875_PWRR_SLEEP = 002H;
RA8875_PWRR_NORMAL = 000H;
RA8875_PWRR_SOFTRESET = 001H;
(* REG[02h] Memory Read/Write Command (MRWC)
Data to write in memory corresponding to the setting of
MWCR1[3:2]. Continuous data write cycle can be accepted in
bulk data write case.
*)
RA8875_MRWC = 002H; (*Memory Read/Write Command*)
RA8875_CMDWRITE = 080H;
RA8875_CMDREAD = 0C0H; (* 0xC0 !!! ??? !!!*)
RA8875_DATAWRITE = 000H;
RA8875_DATAREAD = 040H;
RA8875_STATREG = 040H;
...
...
Code: Select all
TYPE
chararray = ARRAY OF CHAR;
ConstValFunc = PROCEDURE (RegName: chararray):INTEGER;
InitSequence = ARRAY OF ConstValFunc;
VAR
constvalue : INTEGER;
PROCEDURE RegValue(String: ConstValFunc):INTEGER;
BEGIN
constvalue := (String);
RETURN constvalue
END RegValue;
I want to pass the CONST name and get the INTEGER value as the RETURN.
Code: Select all
VAR register :INTEGER;
register := RegValue(RA8875_CMDREAD);
It is obviously a simple casting error I am making here.
Re: Returning Const Value via Procedural Type
Posted: Mon Jul 06, 2015 10:33 pm
by cfbsoftware
I don't see how you could use procedure types to do this. All you need to do is something like:
Code: Select all
CONST
InitSequenceSize = ???;
TYPE
ConstVal = INTEGER; (* You need v5.3 Astrobe to use type aliases otherwise just use INTEGER instead of ConstVal *)
InitSequence = ARRAY InitSequenceSize OF ConstVal;
VAR
constValue : ConstVal;
FOR i := 0 TO LEN(InitSequence) - 1 DO
constValue := InitSequence[i];
DoSomething(constValue);
...
...
However, from what you have said so far, I see no advantage in storing the register values in a array and then extracting them out of the array as opposed to just using them in the sequence they are needed.
It is obviously a simple casting error I am making here.
A syntactically correct call would be:
However, I believe you were trying to achieve something else here by using a procedure type but I do not understand what. Procedure types are useful if you want to substitute different procedures at runtime - look at the Extensions / Lists example supplied with Astrobe in the General folder to see how they can be used.
Re: Returning Const Value via Procedural Type
Posted: Tue Jul 07, 2015 12:23 am
by captbill
However, I believe you were trying to achieve something else here by using a procedure type but I do not understand what.
What I am venturing to do is to port a library for the RA8875 TFT driver chip. It has a massive listing of registers that you drive directly, thus the term "porting" is better described as "interfacing", as you would create a dll interface library, except instead of a dll interface you are interfacing to a set of registers of the RA8875 chip over SPI directly.
Also there are many different settings that must be initialized plus many different board varieties/options. My idea was to have a way to make init strings that will be convenient to tweak when testing output on a logic analyzer. I think just simply doing it from a custom init procedure is probably the best way and leave it at that.
I have the CONST block converted to Oberon so far. Although this is a quite massive undertaking, it looks like it will be quite simple as it's merely a bunch of simple interfacing code wrappers. No real 'coding' to do here. Just a LOT of functions to interface. Please have a look:
https://github.com/billbuzzell/RA8875_O ... 8875_b.mod
Here is the RA8875 being run on an STM32 via SPI:
https://youtu.be/cwCxkLZRuN0
Also it would be great if you could have a look at my SSD1306_OLED code:
https://github.com/billbuzzell/SSD1306_ ... 6_OLED.mod
Re: Returning Const Value via Procedural Type
Posted: Tue Jul 07, 2015 11:49 am
by cfbsoftware
captbill wrote:I think just simply doing it from a custom init procedure is probably the best way and leave it at that.
Yes - I agree.
I have the CONST block converted to Oberon so far. Although this is a quite massive undertaking, it looks like it will be quite simple as it's merely a bunch of simple interfacing code wrappers. No real 'coding' to do here. Just a LOT of functions to interface.
These video display drivers are tedious and all very similar but they are quite straightforward. Have a look at the LCDST756R and LCDEpson example modules included with Astrobe if you want some more ideas.
I had a quick look at your code and it seems OK - the devil is in the detail. I would recommend that you take out all the C stuff in comments that it is ported from as its gets in the way of readability. Just keep a separate copy of the original for reference. If you refer back to the datasheet you might be able to improve on the original anyway.
Good luck!
Chris