Menu

#IfDef to select Table

ToniG
2022-04-24
2022-04-25
  • ToniG

    ToniG - 2022-04-24

    I am trying to use #IfDef to select either of 2 tables to compile & use but GCB always compiles the first table only (see in .asm).
    I suspect the pre-processor is doing the tables first & as the 2 tables have the same name it ignores the second.
    [or out of scope for IfDef / I am not using correctly?]

    '              Testing #IfDef to choose Table
    #chip 12F683,8
    #config Osc = Int
    #Define KeyMap 2  ' < Select keycode table to compile
    
    Main:
       KeyVal = 233
       If KeyVal > 231 and KeyVal < 237 then
          Index1 = KeyVal - 231
          ReadTable ButnMap, Index1, ButnVal
       Else
          ButnVal = 0
       End If
    
    Goto Main
    End
    
    #ifdef KeyMap 1
     Table ButnMap ' Map Button number to Key Code
    'Butn, KeyVal
      1  ' 232
      2  ' 233
      3  ' 234
      4  ' 235
      5  ' 236
     End Table
    #Endif
    
    #ifdef KeyMap 2
     Table ButnMap ' Map Button number to Alt Key Code
    'Butn, KeyVal
      5 ' 232
      2 ' 233
      1 ' 234
      3 ' 235
      4 ' 236
     End Table
    #Endif
    
     
  • ToniG

    ToniG - 2022-04-25

    I edit the table names ButnMap1 & ButnMap2 (& ReadTable ButnMap2, ...) with result = both tables in asm file, this confirms the 2 table same name condition.

     
  • Anobium

    Anobium - 2022-04-25

    ToniG a great post. Nice and easy to understand.

    The code below shows the method to use - It is a small tweak on what you had.

    The compiler does not does what you typically expected with #IF. All tables are initially preprocessed and then when you 'use' that table the compiler the handles the actual table. The #IF have no impact on the tables - the compiler simply finds the first table (in your case as you have two of the same name) and then 'uses' it.

    So, the method to resolve. Use different table names and then access via a constant.

    '              Testing #IfDef to choose Table
    #chip 12F683,8
    #config Osc = Int
    #Define KeyMap  ButnMap2  ' < Select keycode table to compile
    
    Main:
       KeyVal = 233
       If KeyVal > 231 and KeyVal < 237 then
          Index1 = KeyVal - 231
          ReadTable KeyMap, Index1, ButnVal
       Else
          ButnVal = 0
       End If
    
    Goto Main
    End
    
    Table ButnMap1 ' Map Button number to Key Code
    'Butn, KeyVal
      1  ' 232
      2  ' 233
      3  ' 234
      4  ' 235
      5  ' 236
     End Table
    
    Table ButnMap2 ' Map Button number to Alt Key Code
    'Butn, KeyVal
      5 ' 232
      2 ' 233
      1 ' 234
      3 ' 235
      4 ' 236
     End Table
    
     
    • stan cartwright

      stan cartwright - 2022-04-25

      I used 2 tables same size for graphic shape data under the same name but used a pointer to the table name and added 257 for second table.

              if frame=0 then;which sprite to draw
                erase_sprite (oldspx(temp),oldspy(temp)) ;erase sprite at last position
                ' 1 ;pointer to sprite 1 in table
                sprite (spx(temp),spy(temp), 1 ) ;draw sprite1 at new position
              else
                erase_sprite (oldspx(temp),oldspy(temp)) ;erase sprite at last position
                ' 257 ;pointer to sprite 2 in table
                sprite (spx(temp),spy(temp), 257) ;draw sprite2 at new position
              end if
      
              sub sprite ( in sprite_x, in sprite_y, in spritedata_ptr ) ;fills box with sprite data
      
                table spritedata ;sprite 1
          wh,bl,bl,bl,bl,bg,bg,bg,bg,bg,bg,bl,bl,bl,bl,wh
          bg,bl,re,re,re,bl,bl,bg,bg,bl,bl,re,re,re,bl,bg
          bg,bg,bl,re,re,re,bl,bg,bg,bl,re,re,re,bl,bg,bg
          bg,bg,bg,bl,re,wh,bl,bg,bg,bl,wh,re,bl,bg,bg,bg
          bg,bg,bg,bg,bl,wh,bl,bg,bg,bl,wh,bl,bg,bg,bg,bg
          bg,bg,bg,bg,bg,bl,bl,bg,bg,bl,bl,bg,bg,bg,bg,bg
          bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg
          bg,bg,bg,bg,ye,ye,ye,bg,bg,ye,ye,ye,bg,bg,bg,bg
          bg,bg,bg,ye,bg,bg,bg,bg,bg,bg,bg,bg,ye,bg,bg,bg
          bg,ye,ye,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,ye,ye,bg
          bg,ye,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,ye,bg
          ye,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bl,ye
          ye,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,ye
          ye,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,ye
          bg,ye,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,ye,bg
          bg,bg,ye,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,ye,bg,bg
          ;
          ;sprite 2
          bl,bl,bl,bl,bl,bg,bg,bg,bg,bg,bg,bl,bl,bl,bl,bl
          bg,bl,re,re,re,bl,bl,bg,bg,bl,bl,re,re,re,bl,bg
          bg,bg,bl,wh,wh,re,bl,bg,bg,bl,re,wh,wh,bl,bg,bg
          bg,bg,bg,bl,re,wh,bl,bg,bg,bl,wh,re,bl,bg,bg,bg
          bg,bg,bg,bg,bl,wh,bl,bg,bg,bl,wh,bl,bg,bg,bg,bg
          bg,bg,bg,bg,bg,bl,bl,bg,bg,bl,bl,bg,bg,bg,bg,bg
          bg,bg,bg,bg,bg,bg,bg,ye,ye,bg,bg,bg,bg,bg,bg,bg
          bg,bg,bg,bg,bg,ye,ye,bg,bg,ye,ye,bg,bg,bg,bg,bg
          bg,bg,bg,bg,ye,ye,bg,bg,bg,bg,ye,ye,bg,bg,bg,bg
          bg,bg,bg,bg,ye,bg,bg,bg,bg,bg,bg,ye,bg,bg,bg,bg
          bg,bg,bg,ye,bg,bg,bg,bg,bg,bg,bg,bg,ye,bg,bg,bg
          bg,bg,ye,bg,bg,bg,bg,bg,bg,bg,bg,bg,bg,ye,bg,bg
          bg,bg,bg,ye,ye,bg,bg,bg,bg,bg,bg,ye,ye,bg,bg,bg
          bg,bg,bg,bg,ye,ye,bg,bg,bg,bg,ye,ye,bg,bg,bg,bg
          bg,bg,bg,bg,bg,ye,bg,bg,bg,bg,ye,bg,bg,bg,bg,bg
          bg,bg,bg,bg,bg,bg,ye,bg,bg,ye,bg,bg,bg,bg,bg,bg
          end table  
      
       
  • stan cartwright

    stan cartwright - 2022-04-25

    That was my method years ago. I sorted that method but wanted a read mydata where mydata could be table1, table2, table xxx., etc. So I'm interested in a different/better way.
    Can mydata be table1, table2 when in a running program?

     

    Last edit: stan cartwright 2022-04-25
    • Anobium

      Anobium - 2022-04-25

      @Stan - I remember this method. Very good.

       
      • stan cartwright

        stan cartwright - 2022-04-25

        Not really, just what worked at the time. Is there a way to read different tables other than if then or case?
        I'm doing a sub "in my head" to read any table from a number. It will need an array and some vars. Just thinking of an elegant method of making a var be the pointer to any table.
        I posted reading a table to an array and using the array vs just using reading the table.
        Visually on glcd no difference! No speed difference but I didn't time it. So reading tables, which are in program memory ... lots of usually, is worth a thought to make it "easier".
        See what I can do.

        assuming tables are constant when compiled so know size of. This could .. or not be interesting.

         

        Last edit: stan cartwright 2022-04-25

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.