Shop OBEX P1 Docs P2 Docs Learn Events
Variable in another object — Parallax Forums

Variable in another object

PodionPodion Posts: 90
edited 2012-06-27 23:08 in Propeller 1
Hello

Can you tel me how to call a variable in another object?

I have declare a variable in VAR long keycode and I have to call this variable " keycode" in another object to print the value in a LCD screen and I don't make it.

Comments

  • Vega256Vega256 Posts: 197
    edited 2012-06-26 17:41
    Are you passing data in SPIN to a routine in PASM or from across SPIN methods? If you are passing data between SPIN methods, you do that via method parameters. If you are passing data from the HUB to cog ram, you do that with the PAR register.
  • AribaAriba Posts: 2,690
    edited 2012-06-26 18:03
    You need to add a public methode to the other object:
    PUB get_keycode
      return keycode
    
    Then you can call this methode from the object that prints to the LCD:
    lcd.char(otherobj.get_Keycode)
    
    for sure you need to set the right names for otherobj and your lcd print methode

    Andy
    :
  • JonnyMacJonnyMac Posts: 9,194
    edited 2012-06-26 18:06
    If you're trying to access a variable that is declared in another object's VAR list you must provide a method for it. I tend to provide a pointer (address of) so that I can write or write, as well as do things like xxxxmove() and xxxxfill() instructions; this is particularly true with arrays.
    VAR
    
      long  myObjVar
    
    PUB get_myObjVar
    
      return myObjVar
    
    PUB pntr_myObjVar
    
      return @myObjVar
    

    The first method is easy with single-element variables because you can substitute the object's method name where you want that variable. Again, I tend to use the second as this provides greater flexibility IMHO.
  • turbosupraturbosupra Posts: 1,088
    edited 2012-06-26 18:20
    Can you provide a code example, the way that arriba does it is the way that I do it as well



    Podion wrote: »
    Hello

    Can you tel me how to call a variable in another object?

    I have declare a variable in VAR long keycode and I have to call this variable " keycode" in another object to print the value in a LCD screen and I don't make it.
  • PodionPodion Posts: 90
    edited 2012-06-26 21:05
    Thank you all for you're reply!

    @Vega256
    Im not sure about that.. but maybe the following code is going to answer your question.

    I try the this code and I only get this symbol " ~ " on the LCD were it suppose to be the " keycode " any idea why ?

    PUB get_keycode
       return key code
    



    There is my main OBJ
    {{ DS1302_LCD.spin}}
    
    CON
      _clkmode = xtal1 + pll16x 
      _xinfreq   = 5_000_000
       TX_PIN  = 3
       BAUD     = 19_200
     
    OBJ
    
    
      SN    : "Simple_Numbers"
      rtc     : "DS1302_full"       
      LCD   : "FullDuplexSerial"
      IR     : "ir_reader_1"
      
    VAR
    
    
      byte hour, minute, second, day, month, year, dow
      byte cmd, out
      byte data[12]
      long Stack[50]
      long Stack1[50]
    
    
    PUB Main
      LCD.start(TX_PIN, TX_PIN, 00, 19_200)
      IR.Start_IR
      rtc.init( 0, 1, 2 )
      LCD.str(string(12))
      LCD.str(string(17))
      repeat
        LCD.str(string(22)) 
        rtc.readTime( @hour, @minute, @second )     'read time from DS1302 
        LCD.str( SN.decx(hour,2) )
        LCD.str( string(":"))
        LCD.str( SN.decx(minute,2))
        LCD.str( string(":") )
        LCD.str( SN.decx(second,2))  
        waitcnt( clkfreq + cnt )
        LCD.str(string(148))
    [COLOR=#b22222]    LCD.str(IR.get_Keycode)  [/COLOR]
        LCD.str(string(13))    
        ALARME
         
    PUB ALARME | M,S
    
    
      M := 05 
      S := 20
      if minute == M and second < S
         LCD.str(string(222))
      elseif LCD.str(string(232)) 
    
    
    

    And the LCD OBJ
     
    CON
                                  
    _clkmode        = xtal1 + pll16x
    _xinfreq          = 5_000_000
    
    
    _irrpin            = 4     'ir receiver module on this pin
    _device         = 0     'accept any device code
    _dlyrpt           = 6     'delay before acception code as repeat
              
            
    VAR
         
         word Direction
         word vi
         long keycode 
         long Stack2[100]
    
    
    OBJ
          
      rcvir : "ir_reader_sony"
      LCD   : "FullDuplexSerial.spin"
    
    
    PUB Start_IR
    
    
      cognew(Main, @Stack2) 
      
    PUB Main
      
      rcvir.init(_irrpin,_device,_dlyrpt,true)   'startup
      repeat
        keycode := rcvir.fifo_get                 'try a get from fifo
        if keycode == -1                          'empty try again
          next                     
        out
           
        
    PUB out
     
      dira [16..23]~~
      outa [16..23]~
      case keycode
        0:   !outa[23]  
        1:   !outa[22]
        2:   !outa[21]
        3:   !outa[20]
        4:   !outa[19]
        5:   !outa[18]
        6:   !outa[17]
        7:   !outa[16]
        14:  Speed_UP
        15:  Speed_DOWN
        16:  Flash_G&#8592;
        17:  Flash_D&#8594;
        other :Flash
          
    PUB Flash
    
    
      dira [16..23]~~
      outa [16..23]~
      repeat  5
        !outa [16..23]
        waitcnt ( _xinfreq + cnt )
        !outa [16..23]
        waitcnt ( _xinfreq + cnt )
    
    
    PUB Speed_UP
    
    
      vi <#= 9
      vi ++
    
    
    PUB Speed_DOWN
    
    
      vi #>= 2
      vi --
    
    
    PUB Flash_D&#8594;
    
    
      dira [16..20]~~
      outa [16..20]~
      repeat 2
        Direction := 24
      repeat until Direction == 15
        !outa [Direction]
        waitcnt ( _xinfreq/vi + cnt )
        !outa [Direction]
        Direction --
    
    
    PUB Flash_G&#8592;
       
      dira [16..20]~~
      outa [16..20]~  
      repeat 2
        Direction  := 15 
      repeat until Direction == 24
        !outa [Direction]
        waitcnt ( _xinfreq/vi + cnt )
        !outa [Direction]
        Direction ++
    
    
    
    
    [COLOR=#b22222]PUB get_keycode[/COLOR]
    [COLOR=#b22222]  return keycode[/COLOR]
    
    
    DAT
    title   byte    "Push ir button for codes",13,13,0
    
  • AribaAriba Posts: 2,690
    edited 2012-06-26 22:04
    Podion

    You can not print the keycode with the str methode, it's not a string. If you want the code as decimal number use:
    LCD.dec(IR.get_keycode)

    But the other problem is that your keycode variable is continuosly polled in the Main methode, so you will see mostly the value -1. You can poll into a temporary variable and update the keycode variable only when a new vaild code is received. So you see always the last vaild number:
    PUB Main : tmp
      
      rcvir.init(_irrpin,_device,_dlyrpt,true)   'startup
      repeat
        tmp := rcvir.fifo_get                    'try a get from fifo
        if tmp > -1                              'keycode available
          keycode := tmp  
          out
    

    Andy
  • PodionPodion Posts: 90
    edited 2012-06-26 23:46
    Thank you Ariba !!! the code just work fine ! ;-) And you reply to my next question ... why I allays have this dam "-1" on the screen ? :-) now I know why. But I don't understand why in the following code I had the good "keycode" on the screen and not the "-1" ? I was using this code alone just to display the "keycode".
     
    CON
    
    
                                    
            _clkmode        = xtal1 + pll16x
            _xinfreq        = 5_000_000
    
    
            _irrpin          = 4     'ir receiver module on this pin
            _device        = 0     'accept any device code
            _dlyrpt         = 6     'delay before acception code as repeat
             TX_PIN      = 3
             BAUD         = 19_200  
            
    VAR
         
         word Direction
         word vi
         long keycode 
         long Stack2[100]
    OBJ
          
      rcvir : "ir_reader_sony"
      LCD   : "FullDuplexSerial.spin"
    
    
    
    
    PUB Start_IR
      
      LCD.start(TX_PIN, TX_PIN, 00, 19_200)           'start the tv terminal
      
      
      rcvir.init(_irrpin,_device,_dlyrpt,true)   'startup
      
      repeat
        
    [COLOR=#a52a2a]    keycode := rcvir.fifo_get   'try a get from fifo[/COLOR]
    [COLOR=#a52a2a]    if keycode == -1            'empty try again[/COLOR]
    [COLOR=#a52a2a]      next[/COLOR]
    
        else
    [COLOR=#a52a2a]    LCD.str(string(17,22))[/COLOR]
    [COLOR=#a52a2a]      LCD.str(string(12))[/COLOR]
    [COLOR=#a52a2a]        LCD.dec(keycode)         'show code                          [/COLOR]
        out
        
     
    PUB out
     
      dira [16..23]~~
      outa [16..23]~
      case keycode
        0:   !outa[23]  
        1:   !outa[22]
        2:   !outa[21]
        3:   !outa[20]
        4:   !outa[19]
        5:   !outa[18]
        6:   !outa[17]
        7:   !outa[16]
        14:  Speed_UP
        15:  Speed_DOWN
        16:  Flash_G&#8592;
        17:  Flash_D&#8594;
        other :Flash
          
    PUB Flash
    
    
      dira [16..23]~~
      outa [16..23]~
      repeat  5
        !outa [16..23]
        waitcnt ( _xinfreq + cnt )
        !outa [16..23]
        waitcnt ( _xinfreq + cnt )
    
    
    PUB Speed_UP
    
    
      vi <#= 9
      vi ++
    
    
    PUB Speed_DOWN
    
    
      vi #>= 2
      vi --
    
    
    PUB Flash_D&#8594;
    
    
      dira [16..20]~~
      outa [16..20]~
      repeat 2
        Direction := 24
      repeat until Direction == 15
        !outa [Direction]
        waitcnt ( _xinfreq/vi + cnt )
        !outa [Direction]
        Direction --
    
    
    PUB Flash_G&#8592;
       
      dira [16..20]~~
      outa [16..20]~  
      repeat 2
        Direction  := 15 
      repeat until Direction == 24
        !outa [Direction]
        waitcnt ( _xinfreq/vi + cnt )
        !outa [Direction]
        Direction ++
    
    
    
    
    
    
    DAT
    
    
    title   byte    "Push ir button for codes",13,13,0
    
  • AribaAriba Posts: 2,690
    edited 2012-06-27 02:34
    Because here you print the keycode only if it is not -1.
    If it is -1 then the NEXT command is executed, and this restarts the repeat loop, so all the code after NEXT is not executed until keycode is no longer -1.
    The ELSE in your code has no effect if you have the following LCD.str() command at the same indention level.

    Remember also that here you execute the polling and the print in the same cog, so they are sequential, while in the previous code the Main methode is executed in another cog than the get_keycode and the print methodes. So writing to the keycode and reading the current value happens in two parallel cogs totally un-synchronized.

    Andy
  • PodionPodion Posts: 90
    edited 2012-06-27 23:08
    OK Thank you so much for this explanation I rely appreciate ! ;-)
Sign In or Register to comment.