Shop OBEX P1 Docs P2 Docs Learn Events
how to make a out_time? — Parallax Forums

how to make a out_time?

daniel dingdaniel ding Posts: 52
edited 2011-12-05 18:13 in Propeller 1
hi everyone
in my program I want to make a out-time of operation.
For one if I press the TAB key, the operation will get a out_time and when I press it again it continues.
I tried (repeat wiatcnt(), pub abort and quit commands but useless)
can u give me some advises.
thanks
Daniel

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-21 06:54
    Usually programs have some kind of a main loop. You would simply introduce a global variable, for example called outTime.
    In the main loop you would simply check this variable. If it is FALSE you would do what the loop has to do, if it is TRUE you would wait in an empty
    repeat while outTime
    loop for the flag set to FALSE again.

    The keyboard part needs to run in a separate COG and simply toggles the variable outTime each time the TAB is pressed.

    If you have more than one COG running a program that should be paused, they all have to check this variable.

    Of course this will only pause the execution when you reach that part of the main loop. Depending on the program this might be a long time to go. In this case you can add several of these checks and wait-loops at strategic places.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-21 07:28
    Hi Daniel,

    I don't have a LCD connected to a Propeller right now so I used a terminal object.

    This program will start the method "DoStuff". "DoStuff" will stop when the tab key is pressed. A second tab press will restart "DoStuff".

    Good luck.
    con
      _clkmode=xtal1+pll16x
      _xinfreq=5_000_000
      _Tab = 9
    obj
      key:"keyboard"
      'lcd:"lcdroutines4"
      term: "Parallax Serial Terminal"
         
    pub main |temp,counter,str,a,b,c,x
      'lcd.initialize_lcd
      key.start(17,16)
      term.start(57600)     
      repeat
        DoStuff
        term.str(string(13, "Press tab to continue."))
        repeat while key.key <> _Tab
    PUB DoStuff | x
      x := 0
      repeat
        term.str(string(13, "x = "))
        term.dec(x++)
        waitcnt(clkfreq + cnt)
      while key.key <> _Tab 
    
          
    
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-22 02:38
    Duane
    Thanks for ur reply
    U are so nice guy!
    Are u sure ur program ok?

    everytime I press TAB it stop and it restart when I press it again。
    But I want it continues!
    for example when x=4 I press TAB it stops, when I press TAB again it will display x=5
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-22 03:46
    What do you mean with "It did not work in my propeller tool!" ?

    It does not compile?
    It does not react on keyboard-input?
    It does not show anything at all in the Propeller Serial Terminal?
    It is showing garbage?

    We are not clairvoyants that can see by magic what exactly your problem is. You should put a little bit more efford in your questions if you want to have good answers.
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-22 03:51
    I mean the codes didn't work as I want to.
    In a word it didn't stop when I press TAB and continue when I press tab again.
    Thanks Magl02
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-22 06:45
    Ok, I see ... it always starts from the beginning.

    For this demo code it would be easy ... simply move x into the VAR section and it can continue where it stopped.
      _clkmode=xtal1+pll16x
      _xinfreq=5_000_000
      _Tab = 9
    obj
      key:"keyboard"
      'lcd:"lcdroutines4"
      term: "Parallax Serial Terminal"
    VAR
      long x
         
    pub main |temp,counter,str,a,b,c,x
      'lcd.initialize_lcd
      key.start(17,16)
      term.start(57600)     
      x := 0
    
      repeat
        DoStuff
        term.str(string(13, "Press tab to continue."))
        repeat while key.key <> _Tab
    
    PUB DoStuff
      repeat
        term.str(string(13, "x = "))
        term.dec(x++)
        waitcnt(clkfreq + cnt)
      while key.key <> _Tab
    

    For a more complex program maybe with different COGs running, I'd like to refere to my first post ... set a global flag and check this where ever your program should be stopable. Translated to this demo:
      _clkmode=xtal1+pll16x
      _xinfreq=5_000_000
      _Tab = 9
    obj
      key:"keyboard"
      'lcd:"lcdroutines4"
      term: "Parallax Serial Terminal"
    VAR
      long stopFlag
      long stack[30]
         
    pub main |temp,counter,str,a,b,c,x
      'lcd.initialize_lcd
      key.start(17,16)
      term.start(57600)     
      stopFlag:= FALSE
      
      cognew( DoStuff, @stack)
    
      repeat
        if key.key == _Tab
           stopFlag:=!stopFlag
           if stopFlag
              term.str(string(13, "Press tab to continue."))
        ' .... some other key-handling
    
    PUB DoStuff
      repeat
        repeat while stopFlag
    
        term.str(string(13, "x = "))
        term.dec(x++)
        waitcnt(clkfreq + cnt)
    

    (I did not test this code as I am at work, but hopefully it compiles and at least gives you an idea)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-22 08:57
    Daniel,

    I think MagIO2 already answered your question.

    The "DoStuff" method I included sets x to zero when the method is called. Local variables need to be initialized. I was hoping the difference between a local and global variable was already known to you.

    By the way, MagIO2 is a much better programmer than I am. I also think he's a nice person. I plan to read his example to see what I can learn.

    I just looked at it. Yes, that is a good way to do it. It does require "DoStuff" to run in a seperate cog. If I have enough cogs, I do the same sort of thing in my projects.

    Having one cog check a flag set by another cog is an important part of learning to use the Propeller. I like MagIO2's example better than the example I wrote.
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-22 18:02
    thanks Magl02
    con
      _clkmode=xtal1+pll16x
      _xinfreq=5_000_000
    var
      byte key_input
      long stack[100]
      long stack2[30]
    var
     
       byte keyinput9
         byte keyinput0 
       byte keyinput10
      
       long x,y,f
    obj
      key:"keyboard"
      lcd:"lcdroutines4"
      lcd2:"chabu1122"
      fs:"floatstring"
      fm:"floatmath"
         
    pub main |temp,counter,a1,b1,c1,a2,b2,c2,a3,b3,c3,vx,vy,vf   
    lcd2.startdevices
    lcd2.chabu
    lcd.initialize_lcd
     key.start(17,16)
     keyinput10:=key.newkey
            
    lcd.out(keyinput10)
    a3:=fs.floattostring(fm.fmul(fm.fsub(keyinput10,48),0.1))
    key.clearkeys
            
                     
    keyinput9:=key.newkey
            if keyinput9==13
              
              vf:=a3
              lcd.clear
              waitcnt(clkfreq/5+cnt)
              
              lcd.print(string("F="))
               lcd.print(fs.floattostring(a3))
               lcd.position(2,1)
              
              lcd.print(string("Y/N?"))
            keyinput0:=key.newkey
             if keyinput0==121
                                   
                             lcd.send_char("Y")  
                                      x:=20
                                    y:=30
                                     f:=a3        
          
                                    lcd2.go(@x,@y,@f)       
    
    these are part of my program. Can u tell me where is wrong? I want to display a floating number.
    for example if I press the keyboad 2,a3 will =(50-48)*0.1 50:is the ascii of 2.
    and the LCD should display a 0.2, but it display a wrong number .
    BUT when I change the "lcd.print(fs.floattostring(a3))" to "lcd.print(floattostring(0.2))" the LCD displays 0.2.
    Can u tell me what's wrong with my codes?
    Thanks
    Daniel
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-22 22:50
    The function fsub expects float-encoded parameters, but you feed it with ASCII.

    I'm not familiar with the float object so far, so I can't tell which functions are available to convert from one representation to float.
    If there is a LONG to float conversion available, you can call that with
    keyinput10-"2"
    and then use the result in the fmul.

    If there is a STRING to float conversion available, you can put the "2" into a conversion string-buffer, call the STRING to float and use the result in fmul. Or even easier then, you prepare the string buffer in a special way and get the fmul for free.

    ... ah ... I see ... you use floatstring ... which makes me believe that you can convert from string to float. Here is the way you could go:
    DAT
      convBuffer  byte  "0.x",0
    
    .....
      convBuffer[2]:=keyinput10
      a3 := fs.stringtofloat( @convBuffer )
    
    This code replaces the x in the DAT-string with the number entered. In a good program you would also make sure that keyinput10 really contains a number.

    In your LCD.str you can directly use @convBuffer as well, which saves you the conversion back to string again. If you want to see that the conversion to float worked correctly you can keep that code as it is.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-22 22:57
    Ok .. I expected more from FloatString than it delivers ... there is no string to float conversion.

    Then the first approach is the way to go:
       a3:=FFloat( keyinput10-"2" )
       a3:=fm.fmul(a3,0.1))
       ...
       LCD.str( fs.floattostring( a3 ) )
       ...
    

    PS: Another bug you had is that you did a double conversion. First you did this:
    a3:=fs.floattostring(fm.fmul(fm.fsub(keyinput10,48),0.1))

    and then
    lcd.print(fs.floattostring(a3))

    But a3 does not contain a float here. In strictly typed programming languages the compiler would check these things. But in the Propeller Tool you have to take care of what you do with your variables by yourself. So, for debugging you always should have the following questions in mind:
    What type do the functions expect as parameters (long, string pointer, float ...)
    What do I actually pass (e.g. in fm.fsub(2.0, 0.1) )
    Or what do the variables contain that are used in function calls (e.g. fs.floattostring(a3) )

    Good idea is to use variable-names which remind you what type of data they contain.
  • daniel dingdaniel ding Posts: 52
    edited 2011-11-23 00:23
    Magl02
    Thanks
    I got it!
  • daniel dingdaniel ding Posts: 52
    edited 2011-12-05 18:13
    Magl02
    Thanks a lot to your reply.Daniel
Sign In or Register to comment.