Shop OBEX P1 Docs P2 Docs Learn Events
setting a range of variables — Parallax Forums

setting a range of variables

Will this set a range of bytes
VAR
  byte  flags[7]

PRI  SetFlags     'set flags according to key press
  
  IF din[0] == $01       
    flags[0] := 1       '1st byte = 1
    flags[7-1] := 0     'all other bytes = 0
CON
  'Will this set a range of bytes? Or is there a different way?  (  ..  ) didn't work

Thanks
Aaron

Comments

  • No, that'll just set flags[6]. I think what you want is
    BYTEFILL(@flags[1],0,6)
    
    Look up BYTEFILL in the Propeller manual for details
  • yisiguroyisiguro Posts: 52
    edited 2019-12-05 12:22
    According to Propeller Manual, It seems that "range-expression" is available for INA, INB, OUTA, OUTB, DIRA, DIRB, and match-expressions in CASE statement.
  • You could use just a single byte
      if (din[0] == 1)
        flags := |<0
    
    This will set bit0 of flags and clear the other bits.
  • Looks like BYTEFILL might be the way to set 1 or several bytes but not in one instruction. It may be that I want to set bytes 1 and 4 to one and clear the rest. Probably should clear all 1st then come back and set the one or more to one with however many instructions it takes using bytefill.

    I just thought there might be an easier way. I might have to read up on CASE also

    Aaron
  • JonnyMacJonnyMac Posts: 9,102
    edited 2019-12-05 19:26
    Do your flags have to be bytes, or are they always going to be 0 or 1? If they're just binary -- not other values -- then a single byte can hold eight flags and you can set them in one go. For example:
      flags := |<4 | |<1
    
    ...will set bits 4 and 1 while clearing the others. Two flags set; six flags cleared; one line of code.
    Looks like BYTEFILL might be the way to set 1 or several bytes but not in one instruction.
    The bytefill() instruction does set one or many bytes in a single instruction, but they must be the same value.
  • Jon
    They only need to be 1 or 0 so that will work fine. I'll experiment with it early tomorrow morning.
    Thanks
    Aaron
  • Now that I think about it, it's probably way easier than I was trying to do and there are probably other ways to do it too.
  • JonnyMacJonnyMac Posts: 9,102
    edited 2019-12-05 20:26
    If you're going to be using bit flags, these routine might be useful -- they're from one of my templates.
    pub get_bit(value, pos)                                           
                                                                
    '' Returns bit (0..1) from pos (0..31) in value                   
                                                                      
      if ((pos => 0) and (pos =< 31))                                 
        return (value >> pos) & 1                                     
      else                                                            
        return 0
    
    
    pub set_bit(value, pos, bit)
    
    '' Write bit (0..1) to position (0..31) in value
    
      if (bit)                                                      ' set
        return value | |<pos
      else
        return value & !(|<pos)
    

    Something that I suggest you do to make your code more readable is to create CON masks for your flags.
    con
    
      LIVING_RM = %00000001
      DINING_RM = %00000010
      KITCHEN   = %00000100
    

    This allows you code to make more sense -- when you do things like this
      flags |= LIVING_RM
    

    My point is to be careful using "magic numbers" like I did in my example above. You can still set/clear multiple flags with one line, and make it readable:
      flags := KITCHEN | DINING_RM
    

    You can test with a named flag easily, too:
      if (flags & KITCHEN)
        kitchen_lights(OFF)
    
Sign In or Register to comment.