Shop OBEX P1 Docs P2 Docs Learn Events
Press any key to return to main menu. — Parallax Forums

Press any key to return to main menu.

jerdeejerdee Posts: 5
edited 2012-02-25 18:38 in General Discussion
Hello...

I'm just digging into the spin code, so my question is very basic.

I am using Terminal to monitor voltages in a repeat loop. What I'd like to accomplish is the loop repeating string commands to terminal unill any any key is pressed on the keyboard. Basically I need CASE 3 to repeat until I press any button on a keyboard. As you can see I'm using terminal control in a separate COG. At the moment...the code waits for commands to send another set of string commands to monitor in Terminal...I want it to continuously monitor until I press a key. I have a strong feeling that the wait command is an issue.

I'm sure this is very easy to implement...but this a stumbling block for me at this time.

Thanks for your help in advance.

Jeremy
CON
  _xinfreq = 5_000_000
  _clkmode = xtal1 + pll16x


OBJ
   
  Debug         : "FullDuplexSerialPlus"
  ADC           : "MCP3208"
  fMath         : "FloatMath"
  fString       : "FloatString" 

VAR

  Long value, dump, stack[128]
  Byte pattern, pin1, pin2 
  
Pub Main

cognew(Terminal, @stack[0])
cognew(Pulsewidth, @stack[50])

Pub Terminal 
    
Debug.start(31, 30, 0, 115200)
waitcnt(clkfreq*3 + cnt)
Debug.tx(16)
   
  repeat
    Debug.Str(String(13,"______________________________________________________________"))  
    Debug.Str(String(13,"1 = Inductor Settings | 2 = Cap Settings | 3 = Batt Voltages |"))
         
    Case Debug.getDec 

      1:Debug.Str(String(13,"Microsecond / Pulse = "))
        value := Debug.getDec 
        Debug.tx(13)
      
      2:Debug.Str(String(13,"1 = Hold | 2 = Peak ADC |"))

        Case Debug.getDec
          1:Debug.Str(String(13,"Hold Value = "))
            dump := Debug.getDec
            Debug.tx(13)

          2:Debug.Str(String(13, "Peak ADC = "))
            Debug.tx(13)
        
      3:Debug.Str(String(13,"Press any key to return to Main Menu"))
          repeat until Debug.getdec
            Debug.Str(String(13,"Input = "))
            BattV((fmath.FFloat(adc.in(0))),0.03571428571428571428571428571429)
            Debug.Str(String("   Output = "))
            BattV((fmath.FFloat(adc.in(1))),0.03571428571428571428571428571429)
            Debug.Str(String("   Batt Avrg = "))
            AverageV
            waitcnt(clkfreq*2 + cnt) 

PUB Pulsewidth

  value := 100                                          'Define Starting clock division
  pin1 :=16                                             'Start pin for output pulsing
  pin2 :=17                                             'End pin for output pulsing

  dira[pin1..pin2] ~~                                   'declare what series of pins as outputs
  
    repeat
       
      if pattern == 0                                  'Resets pattern
          pattern :=                                 'Flips first pin on in binary    
      
      waitcnt(clkfreq/1000000*value + cnt)              
      outa[pin1..pin2] := pattern                       'Assign Pins to pattern
      pattern >>= 1                                     'Shifts Pin State to the right


PUB BattV(adc_in,multfactor)| c

  c := fmath.FMul(adc_in, multfactor)
  debug.str(fstring.FloatToString(c))

PUB AverageV | a, b, c, d 

  a := fmath.Fmul((fmath.FFloat(adc.in(0))),0.0357142857) 
  b := fmath.Fmul((fmath.FFloat(adc.in(1))),0.0357142857)
  c := fmath.Fadd(a,b)
  d := fmath.Fdiv(c,fmath.FFloat(2))
  
  debug.str(fstring.FloatToString(d))    

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-02-24 22:25
    Welcome to the forums,

    Have you incorporated some kind of keyboard object? I didn't see one listed in your code, so I wasn't sure you had used one.

    Check out, for example: http://obex.parallax.com/objects/654/

    Then remember that key strokes are ascii characters, see for example: http://www.asciitable.com/index/asciifull.gif


    so if ButtonPress is the value you get from the Keyboard object, you make a bit of code that says something like Repeat Until ButtonPress > 7 AND ButtonPress < 128

    If ButtonPress ends up with a value in between 7 and 128, then you know something on the keyboard has been pressed. I might be off by some numbers on the ascii code, so you should check that over to make sure the range is right for your keyboard, etc. But I think that's the general idea.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-02-24 22:39
    Oops,

    I corrected the line of code from OR to AND, so it's now Repeat Until (ButtonPress > 7 AND ButtonPress < 128)

    Also, I corrected my own mistake where I wrote Wait instead of Repeat.

    Sorry. It's getting late here.
  • jerdeejerdee Posts: 5
    edited 2012-02-24 23:24
    Oops,

    I corrected the line of code from OR to AND, so it's now Repeat Until (ButtonPress > 7 AND ButtonPress < 128)

    Also, I corrected my own mistake where I wrote Wait instead of Repeat.

    Sorry. It's getting late here.

    Thanks ElectricAye,

    I'd like to continue using my serial terminal on computer for realtime varialble control. I'm using this project to calibrate settings...once found...adjust code to have settings and leave alone. If that makes since.

    So getting the repeat loop to stop from a key command in terminal would be best from the computer in my case. I do appreciate your time helping. In the future I do plan on using the keyboard object.

    Thanks,
    Jeremy
  • jerdeejerdee Posts: 5
    edited 2012-02-25 14:06
    There is something about the "Get Serial" command from the terminal object that stops a loop. How can I allow the loop to continue unless I send any character.

    Thanks,
    Jeremy
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-25 14:18
    I haven't read your code very carefully but I bet you want to use the rxcheck method.
    repeat
        result := Debug.rxcheck
      while result == -1
    

    The above will loop (you can have other things in the loop) until a key is pressed.

    The rxcheck method doesn't stop the loop.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2012-02-25 17:02
    jerdee wrote: »
    Thanks ElectricAye,

    I'd like to continue using my serial terminal on computer for realtime varialble control. ....

    Jeremy,

    sorry about my confused advice last night. My brain has definitely been on the blink this weekend.
  • jerdeejerdee Posts: 5
    edited 2012-02-25 18:36
    Duane Degn wrote: »
    I haven't read your code very carefully but I bet you want to use the rxcheck method.
    repeat
        result := Debug.rxcheck
      while result == -1
    


    The above will loop (you can have other things in the loop) until a key is pressed.

    The rxcheck method doesn't stop the loop.


    Thank you Duane!

    This did it....woohoooo!! I appreciate the help!

    Jeremy
  • jerdeejerdee Posts: 5
    edited 2012-02-25 18:38
    No worries Electric Aye, appreciate the try and help anyways! I've only bee on the PROP chip for two weeks now...and absolutely love it! This is after doing years of basic on atmell and picaxe...this is such a smarter route to go. Can't believe I did not try this chip earlier.

    Jeremy
Sign In or Register to comment.