Best method to alias other Vars

Discuss the Firewing language

Best method to alias other Vars

Postby Timbo » Mon May 18, 2015 7:48 pm

Hi,

I have a load of variables used in my code. My code is really self sustaining requiring no input apart from the interrupt running to service it.

But there is a need to pass information out to the main program that will be doing the front end as well as it passing the odd command back to my code.

I'm wondering if there is a system in Firewing to make the aliasing neater something like Enum.
My variables are all over the place as I try to keep them grouped for clarity reasons and not all of them would need to be exposed.

I could just just present a list aliased but wanted to know if there is a better more elegant way.

Thanks

Tim
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Best method to alias other Vars

Postby Jerry Messina » Mon May 18, 2015 8:33 pm

Tim,

Not 100% sure what you're after but what you're talking about doesn't sound like aliasing. Really all aliasing does is just assign a new name to something that already exists... it's more a feature for you than anything else.
And, Enum is a way of assigning constant values to symbolic names, so that's not what you're after, either.

The way you make things visible outside of your module is just to declare them Public, and that way anybody that includes the module can see the thing.
A lot of people use the convention of declaring all of the module's variables at the top of the module. They don't all need to be declared together for this to work, but it can be handy if they're not scattered all around the place just from a housekeeping point of view.
If you do want them to be grouped together you can use a Structure to do that.

One thing I find that helps is to use the "modulename.variable" syntax if I have a number of different modules, since that helps me associate the variable with where it's defined.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Best method to alias other Vars

Postby Timbo » Mon May 18, 2015 10:27 pm

Hi Jerry

I had not thought about the visibility aspect so of course I would need to make them public.

Really it is just about aliasing, and making it look neat. The module.name is good and really what I was looking for. I kind of think of modules as reusable code and really mine is for one program only but for the purpose of slotting it in with the main program a module would be a good idea. So infact thats what I will do.

The reason for the indecision and hence the question is that when I wrote modbus slave code in Proton. I had to number all the registers as arrays (broken up a bit through) to cover the reg's range. eg 100 - 110, 210 to 215 etc this is because modbus can ask for x regs starting from y. Its not possible to scatter variables around in Proton and sequentially access them. I am not that up on Firewing to know all the working and was wondering if there was some way that to group a section of vars into a block using aliases. Not that I thought you would have been able to read them like an array.

So basically as is normal halfway through a post I changed my mind and the post comes out sounding odd.

So back to the point, module.var is exactly what I need. I can alias the names in the module to give them better names that the internal working ones.

CurrentRawFlowTimed is a not intuitive, better something like SysHandler.CurrentFlow

Thanks
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Best method to alias other Vars

Postby Timbo » Mon May 18, 2015 10:46 pm

Ok something I forgot. I remember now why I was so keen to do it as regs in series

My code runs all in an interrupt so will have to disable the interrupt to read / write variables. Thats not possible if I just use Module.name

I will have to have something like a function or sub
y = ReadVar(CurrentFlow)

By having one portal, interrupts can be disabled, then it can read the appropriate var using an array index system. The Value of the index number can be aliased using the Enum system. I think to keep my code neat I will make an array then alias sections to my variable names internally.

Unless you can think of a better way?

Thanks
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Best method to alias other Vars

Postby Coccoliso » Tue May 19, 2015 7:35 am

Hello Timbo,
Timbo wrote:..was wondering if there was some way that to group a section of vars into a block using aliases

.. I answer only in part regarding an elegant way of grouping by a look at Structures and Unions (http://www.firewing.info/pmwiki.php?n=Reference.Structures).

Code: Select all
Private Structure RIP_MSG
  XID        As UShort
  Secs       As UShort
  Flags      As UShort
End Structure

Private Dim
    pRIPMSG             As RIP_MSG
...
Private Sub SetValues(lMess as RIP_MSG)
    lMess .XID = &HFFFF
    lMess .Secs = 0
    lMess .Flags = 1
Snd Sub
...
Private Sub Main()
    Dim msg as RIP_MSG
    Erase(msg)
    SetValues(msg)
    ...
    Console.Write(Hex(msg.XID),13,10)
End Sub




User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am

Re: Best method to alias other Vars

Postby Timbo » Tue May 19, 2015 7:53 am

Hi Coccoliso

Yes I did think about structures but then they are really arrays at heart. And that made it an issue with structuring them in my code. However now I'm resigned to doing that structure are back on the cards
I wonder if I can alias to an element of a structure. The real goal here is to have just one portal to read/write variables that need the interrupt servicing them to be disabled. Some testing I think is in order.
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Best method to alias other Vars

Postby Jerry Messina » Tue May 19, 2015 10:22 am

By having one portal, interrupts can be disabled, then it can read the appropriate var using an array index system. The Value of the index number can be aliased using the Enum system. I think to keep my code neat I will make an array then alias sections to my variable names internally

Now I see what you're after.

Keep in mind that if you use an array then all of your variables have to be the same type... you can't mix them up like you would a structure where you can have a collection of different types. The same thing goes for having a single ReadVar() accessor function. The function would have to return the same type.

For your Modbus example, that's probably the case since everything's a 'Register' and the Modbus protocol uses the register number to access things. For most other applications that's probably not the case.

An alias name is just another way of referring to an object (any object) or another existing name, so yes, you can create an alias for a structure member or even a single array element.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Best method to alias other Vars

Postby Timbo » Tue May 19, 2015 10:47 am

Looks like it's a plan.

When I get back to doing some coding I will have to do some tests.
In Proton I could declare a large byte array then alias sections into what ever variable I wanted.

Dim MyArray[10] As Byte
Dim MyWord As Word At MyArray#0
Dim MyDword as Dword at MyArray#2
Dim MyByte as Byte at MyArray#6

I will see if FireWing has something like that.
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Best method to alias other Vars

Postby Jerry Messina » Tue May 19, 2015 11:39 am

The cleanest syntax for doing that might be to use a union and promotional modifiers

Code: Select all
/*
Dim MyArray[10] As Byte
Dim MyWord As Word At MyArray#0
Dim MyDword as Dword at MyArray#2
Dim MyByte as Byte at MyArray#6
*/

structure MyArray_t
   Array(10)   as byte
   MyWord      as Array(0).AsUshort    // 16-bit word
   MyDword     as Array(2).AsUinteger  // 32-bit dword
   MyByte      as Array(6)             // already a byte... no modifier needed
end structure

dim MyArray as MyArray_t

sub main()
    dim b as byte,
       w as ushort,
       dw as uinteger

    // clear the structure
    erase(MyArray)
    // another way
    erase(MyArray.Array)

    b = MyArray.MyByte
    w = MyArray.MyWord
    dw = MyArray.MyDword
end sub
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Best method to alias other Vars

Postby Timbo » Tue May 19, 2015 12:44 pm

Brilliant.....
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Next

Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x