​ ​ ​ ​ Maths on #macro parameters
Results 1 to 8 of 8

Thread: Maths on #macro parameters

  1. #1
    Senior Member
    Join Date
    Nov 2009
    Location
    UK
    Posts
    1,151

    Default Maths on #macro parameters

    I am working with some APA102 LEDs. One of the variable functions is the brightness.
    To get the correct set of bit to send to the LEDs a number between 0 and 31 is or with %11100000

    Code:
    alter_bright = brightness | %11100000
    it did dawn on me that instead of having this before calling the send macro, I could add this to the macro itself
    is there any known reason why this wont work, it compiles but I can't test until a few days time and I am wondering if anyone can shed any light on it

    Code:
    #macro sendPacket( n1, n2, n3, n4 )
          sendbyte = n1 | %11100000 : gosub send_byte
          sendbyte = n2 : gosub send_byte
          sendbyte = n3 : gosub send_byte
          sendbyte = n4 : gosub send_byte
    #endmacro
    I did try:
    Code:
    #macro sendPacket( n1, n2, n3, n4 )
          n1 = n1 | %11100000
          sendbyte = n1 : gosub send_byte
          sendbyte = n2 : gosub send_byte
          sendbyte = n3 : gosub send_byte
          sendbyte = n4 : gosub send_byte
    #endmacro
    and get a syntax error despite being basically the same

    Also is it possible to pass number out as well? so if I pass BXX into n1, add or subtract one, then pass it back out to BXX. BXX is going to be at least 3 different variables, maybe 4

  2. #2
    Senior Member
    Join Date
    Nov 2009
    Location
    UK
    Posts
    1,151

    Default

    [code]
    n1 = n1 | %11100000
    [/code ]

    does not work what ever reason despite compiling just fine.

  3. #3
    Senior Member
    Join Date
    Jan 2010
    Location
    34 France
    Posts
    3,907

    Default

    Hi,
    n1 is not a variable!
    This code work :
    Code:
    symbol sendbyte = b10
    #macro sendPacket( n1,n2,n3,n4 )
    
         sendbyte = n1 | %11100000 : gosub send_byte
         sendbyte = n2 : gosub send_byte
         sendbyte = n3 : gosub send_byte
         sendbyte = n4 : gosub send_byte
    #endmacro
    
    main:
    sendPacket( 2,3,4,5)
    end
    
    send_byte:
    @bptrinc = sendbyte
    return
    You get
    b0 = 226 ( 2 | %11100000)
    b1 = 3
    b2 = 4
    b3 = 5
    S'il n'y a pas de solution, c'est qu'il n'y a pas de problème . (Les Shadoks)

  4. #4
    Senior Member
    Join Date
    Sep 2011
    Location
    Montpellier (FRANCE)
    Posts
    2,788

    Default

    Quote Originally Posted by PieM View Post
    Hi,
    n1 is not a variable!
    So, with PieM example, "n1" is replaced by the value "2" and you get :
    2= 2 | %11100000

    ==> Syntax error...
    There are 10 types of people in the world: those who understand binary, and those who don't.

  5. #5
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    24,304

    Default

    As PieM notes; "n1 is not a variable!". A simpler example is ...

    Code:
    #Macro Send(n1)
      sendByte = n1 : Gosub Do_Send
    #endMacro
    Send(b1)
    Send(1)
    Remembering that parameter names in the #MACRO are simply replaced by the parameter specified when the macro is invoked; that generates ...

    Code:
      sendByte = b1 : Gosub Do_Send
      sendByte = 1  : Gosub Do_Send
    All fine and dandy. Now we want to Or with %11100000 ...

    Code:
    #Macro Send(n1)
      sendByte = n1 | %11100000 : Gosub Do_Send
    #endMacro
    Send(b1)
    Send(1)
    That generates ...

    Code:
      sendByte = b1 | %11100000 : Gosub Do_Send
      sendByte = 1  | %11100000 : Gosub Do_Send
    Again, all fine and dandy. But if we try ...

    Code:
    #Macro Send(n1)
      n1 = n1 | %1110000 : sendByte = n1 : Gosub Do_Send
    #endMacro
    Send(b1)
    Send(1)
    That generates ...

    Code:
      b1 = b1 | %11100000 : sendByte = b1 : Gosub Do_Send
      1  = 1  | %11100000 : sendByte = 1  : Gosub Do_Send
    The first with a variable works but with a constant "1 = 1 | %11100000" is meaningless, hence a syntax error.

  6. #6
    Senior Member
    Join Date
    Nov 2009
    Location
    UK
    Posts
    1,151

    Default

    OK
    could instead use something like
    Code:
    temp_byte = n1                  ;load n1 in variable
    sendbyte = temp_byte | %11100000 : gosub do_send    ;now send the corrected information
    I suppose N1 could actually be ignored completely. That value isn't updated very often, the information is kept in a variable with the code itself any way meaning that the temp_byte = n1 could be dropped
    Code:
    #macro sendPacket( n1, n2, n3, n4 )
          sendbyte = BXX | %11100000 : gosub send_byte
          sendbyte = n2 : gosub send_byte
          sendbyte = n3 : gosub send_byte
          sendbyte = n4 : gosub send_byte
    #endmacro
    where BXX is the working variable

    I do find it a little surprising that that trying to is a parameter as a variable doesn't get flagged during a syntax check.

  7. #7
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    24,304

    Default

    Quote Originally Posted by oracacle View Post
    I do find it a little surprising that that trying to is a parameter as a variable doesn't get flagged during a syntax check.
    If the way you are doing things is entirely acceptable to the compiler there will be no syntax error.

    It is not clear exactly what you are trying to achieve overall. Yes, you can do as your second example ...

    Code:
    #macro sendPacket( n1, n2, n3, n4 )
          sendbyte = BXX | %11100000 : gosub send_byte
          sendbyte = n2 : gosub send_byte
          sendbyte = n3 : gosub send_byte
          sendbyte = n4 : gosub send_byte
    #endmacro
    Making BXX a global variable, but it would make sense to remove the 'n1' parameter and update the 'SendPacket' invocations ...

    Code:
    #macro sendPacket( n2, n3, n4 )
          sendbyte = BXX | %11100000 : gosub send_byte
          sendbyte = n2 : gosub send_byte
          sendbyte = n3 : gosub send_byte
          sendbyte = n4 : gosub send_byte
    #endmacro
    
    SendPacket( x, y, z )
    Or you could keep things as they are and simply update the 'SendPacket' invocations to have BXX as the first parameter ...

    Code:
    #macro sendPacket( n1, n2, n3, n4 )
          sendbyte = n1 | %11100000 : gosub send_byte
          sendbyte = n2 : gosub send_byte
          sendbyte = n3 : gosub send_byte
          sendbyte = n4 : gosub send_byte
    #endmacro
    
    SendPacket( BXX, x, y, z )
    Both are entirely valid and the choice is down to your own preference. Both will generate exactly the same code.

    You could even do this ...

    Code:
    #macro sendPacket( n1, n2, n3, n4 )
          sendbyte = n1 : gosub send_byte
          sendbyte = n2 : gosub send_byte
          sendbyte = n3 : gosub send_byte
          sendbyte = n4 : gosub send_byte
    #endmacro
    
    SendPacket( BXX | %11100000, x, y, z )
    I would favour keeping things as they originally were, putting BXX as the first parameter, because then BXX can always be kept with its top bits set and that only needs to be altered when it's changed. That will use less memory than having an Or operation in every macro expansion.

    The fundamental things to remember are that macros are not subroutines, are not a means of creating local or temporary variables, are simply a text substitution when the compiler encounters them.

    In PE6, under the File -> Options menu, on the Diagnostics page, there's a "Display pre-processor output" option which might be worth enabling temporarily so you can see what the results of macro expansion are. That may help make things clearer.

  8. #8
    Senior Member
    Join Date
    Nov 2009
    Location
    UK
    Posts
    1,151

    Default

    I think I will be keeping the original. As the code has developed it being altered more than I had imagined

    Thanks for info

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •