How to designate pin by reference for a sub?

Discuss the Integrated Development Environment (IDE)

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
Senacharim
Posts: 139
Joined: Tue Aug 10, 2010 5:19 pm
Location: Ventura, CA

How to designate pin by reference for a sub?

Post by Senacharim » Fri Dec 03, 2010 7:56 pm

Hello! (Not sure if this should be in IDE or complier...)

Anyhow, Let's say I have two routines, like these:

Code: Select all

Dim Relay0_Pin As PORTC.Booleans(2)
Dim Relay1_Pin As PORTB.Booleans(1)

'----------------------------------=-=-----------------------------------------
Sub RelayTwoShow() '
'----------------------------------=-=-----------------------------------------
  Dim
    Pin_Proxy As Boolean
'---------------=-=---------------
    CFA632.Clr()
    CFA632.TZ_Loc()
    CFA632.Write(" . Relay Two .  ")
    NHH()
    Pin_Proxy = Not Relay1_Pin
    While BtnVar = BtnNone
        If Pin_Proxy <> Relay1_Pin Then
            Pin_Proxy = Relay1_Pin
            CFA632.BZ_Loc()
            Select Pin_Proxy
                Case true
                    CFA632.Write("   [OPEN]       ")
                Case false
                    CFA632.Write("   [CLOSED]     ")
            End Select
        EndIf
        ReadKeys()
    Wend
End Sub

'----------------------------------=-=-----------------------------------------
Sub RelayOneShow() '
'----------------------------------=-=-----------------------------------------
  Dim
    Pin_Proxy As Boolean
'---------------=-=---------------
    CFA632.Clr()
    CFA632.TZ_Loc()
    CFA632.Write(" . Relay One .  ")
    NHH()
    Pin_Proxy = Not Relay0_Pin
    While BtnVar = BtnNone
        If Pin_Proxy <> Relay0_Pin Then
            Pin_Proxy = Relay0_Pin
            CFA632.BZ_Loc()
            Select Pin_Proxy
                Case true
                    CFA632.Write("   [OPEN]       ")
                Case false
                    CFA632.Write("   [CLOSED]     ")
            End Select
        EndIf
        ReadKeys()
    Wend
End Sub
(this is an actual copy/paste from a project I've worked on...)

So, I've two routines doing exactly the same thing but for different pins.
How would I set it up to be one routine where I pass in the pin reference?

Like:
Sub RelayShow(Pin x) (or something like that)
(...code...)
end sub

Thanx in advance.
Surviving Member
Bermuda Triangle Battalion
from 2026 to 1992

Voted "Most likely to time travel"--Class of 2024.

AndyO
Registered User
Registered User
Posts: 48
Joined: Sat Oct 27, 2007 7:08 pm
Location: Beijing, China

Post by AndyO » Sat Dec 04, 2010 7:00 am

You can use ByRef to pass a pin to a sub:

Code: Select all

Dim TestPin0 As portb.0
Dim TestPin1 as portb.1

sub FlashPin(byref Pin as bit)

    high(Pin)
    delayms(1000)
    low(Pin)

end sub


FlashPin(testpin0)
FlashPin(testpin1)

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Sat Dec 04, 2010 10:47 am


AndyO
Registered User
Registered User
Posts: 48
Joined: Sat Oct 27, 2007 7:08 pm
Location: Beijing, China

Post by AndyO » Sat Dec 04, 2010 11:44 am

Is this the sort of situation where macros would help?

If you change my original code to this, does it produce more efficient code?

Code: Select all

dim TestPin0 as portb.0
dim TestPin1 as portb.1

macro FlashPin(pin)

    high(pin)
    delayms(1000)
    low(pin)

end macro


FlashPin(testpin0)
FlashPin(testpin1)

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Sat Dec 04, 2010 12:38 pm

If you change my original code to this, does it produce more efficient code?
Yes, a macro would produce a lot more efficient code than a sub with pass by ref, but it will do this at the expense of code size. A macro is basically a way to do text substitution and gets compiled into inline code every time it's used in the source file.

I don't think it would help with the original problem if you're trying to use a common sub to reduce the code size, but if you don't mind the extra code it would let you have a single macro, and it would be as fast as the original.

Post Reply