Shop OBEX P1 Docs P2 Docs Learn Events
Command Line Interface — Parallax Forums

Command Line Interface

Matthias1992Matthias1992 Posts: 7
edited 2011-02-20 12:01 in Propeller 1
Hey there!

First of all, I am new to the forums and new to the propeller. I did research if any topic/thread existed on what I am about to ask but I could not find any. Please bare with me, I might ask some stupid questions...

I have the Propellor USB proto board + VGA and 2 PS2 connectors. They all work fine. I want to make a command line interface with the propellor. I did manage to get text on the VGA monitor and I could type stuff and all that but now I want to program the chip to recognize commands. Preferably I would type the commands without prefix so that I could type

1+1

But if easier I could also resort to this:

?1+1

in which the "?" tells the chip that a command is coming. My point is this, since I am using the included libraries (keyboard, vga, vga_text_demo(in which vga_text_demo is the top module)) how can I recognize commands?

So far I have tried to get the par_key buffer from the keyboard.spin file but I can't acces it from the top module (i 've tried # and @). Once again, I am a complete newbie (altough i did read trough the entire propellor spin programming manual/tutorial) so bare with me.

I know this is the most hated question in forums but, could anybody give me a small code example on how to achieve such a feat? So far example code that would output "You typed the add command" when I type ADD (or ?ADD).

Thanks!!

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-02-18 05:16
    You could use the ConfigReader from ObjectExchange for a command line-tool. The basic setup of the config reader is for reading files from an SD card, but you can change it to read keyboard input instead.

    You can also have a look into PropBasic or FemtoBasic or PropCMD in the ObjectExchange.

    The way I go with ConfigReader is to read input until the buffer limit has been reached or the return key has been pressed. The command-line is simply a command followed by up to 9 parameters. The space separates command/parameters. The command is converted to a hash-value. This way you can use the SPIN instruction "switch" for checking the command and calling the appropriate function. The parameters are already parsed, which means that numeric strings you pass in hex, binary or decimal format are converted to a long. All other input is recognized as string and the start address of the string is stored in a parameter array.
  • Mike GMike G Posts: 2,702
    edited 2011-02-18 05:16
    I'm not sure I fully understand what you're trying to do. Use the getkey method of the keyboard object => kb.getkey. Getkey returns a byte. You're looking for 4 bytes (1+1 <cr>) so parse the first byte and make sure it's what you expect then drop the "1" in HUB memory.


    Allocate some HUB memory
    DAT
     expression   byte    $0[256]
    

    Stick the getkey method's return value in memory.
    i := 0
    keyPressValue := kb.getkey
    byte[@expression][i++] := keyPressValue
    

    Well, you just go along sticking item in memory until <enter> is pressed (13). Then parse the expression and execute some logic. There's probably some encoding and decoding involved and validation but that's the basic idea. I'm sure you can come up with something a little more robust.
  • AribaAriba Posts: 2,690
    edited 2011-02-18 07:25
    I know this is the most hated question in forums but, could anybody give me a small code example on how to achieve such a feat? So far example code that would output "You typed the add command" when I type ADD (or ?ADD).
    Here we go:
    {{ Command Demo }}
    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
    OBJ
      kb :  "Keyboard"                         
      di :  "VGA_Text"
      
    VAR
      byte  cmdStr[32]
    
    PUB Main | k,i
    
      'start the drivers
      di.start(16)
    
      'start the keyboard
      kb.start(26, 27)
    
      repeat
        'get commands
        di.str(string("Type a command and <Return>:",13))
        repeat i from 0 to 30
          k := kb.getkey
          if k == 13        'return = cmd end
            quit
          cmdStr[i] := k    'else fill string
          di.out(k)         'and echo key
         
        'type command
        di.str(string(13,"you typed: "))
        di.str(@cmdStr)
        di.out(13)
         
        'recognize commands
        if strcomp(@cmdStr, string("help"))
          di.str(string("possible commands:",13))
          di.str(string("help",13))
          di.str(string("exit",13))
    
        elseif strcomp(@cmdStr, string("exit")) 
          di.str(string("by!",13))
          quit
    
        else
          di.str(string("no valid command!",13))
    

    Andy
  • jazzedjazzed Posts: 11,803
    edited 2011-02-18 08:48
    I know this is the most hated question in forums ...
    There are certainly more hated questions (interrupts anyone?). People actually enjoy sharing code here.
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-02-18 12:20
    matthias1992: Welcome to this fantastic forum.

    There have been a number of command line programs written. PropCmd is one such. It has SD card but you could strip it out. MagIo has suggested a few.

    Once you add an SD card, a whole new world opens up. This is easy to add - buy a SD to microSD converter card and solder wires to it. There are instructions in one of the threads. Otherwise buy an SD card expansion and wire it. Sphinx is an OS of sorts and it will compile on the prop itself, and edit. All still in development, but any help is always welcome.
  • Matthias1992Matthias1992 Posts: 7
    edited 2011-02-20 08:55
    Hmm that code works, but not completely. If I type something like help, then asd and then help again it displays the error message. IN other words, it seems cmdstring is never cleared...

    How would I clear cmdstring? Also would it be possible to make some sort of langauge to develop further on, on the propeller itself? So I would make a core application and then I can make new programs on-chip? Has this already been done?

    Thanks!

    P.S. As far as SphinxOs goes, thanks for the tip but for now I will have to rely on a non-SD system, or make one myself...
  • Mike GMike G Posts: 2,702
    edited 2011-02-20 09:04
    How would I clear cmdstring?
    bytefill(@cmdStr, 0, 32) page 57 in the propeller manual.
    So I would make a core application and then I can make new programs on-chip?

    Yes, you can find several in the OBEX.
  • Matthias1992Matthias1992 Posts: 7
    edited 2011-02-20 09:14
    Yes, you can find several in the OBEX.
    Any recommandations?
  • Matthias1992Matthias1992 Posts: 7
    edited 2011-02-20 09:28
    Never mind. I installed femtobasic it seems to be what I was looking for. If somebody has some better documentation then in the femto manual, feel free to provide...

    Thanks!
  • AribaAriba Posts: 2,690
    edited 2011-02-20 09:51
    Yes, there is FemtoBasic and Bean's EmbeddedBasic, which is similar, but faster.
    And Sphinx is a real Spin compiler but you need an SD card.

    Regarding the String clear, that is not necessary, I just have forgotten to write the ending zero, which terminatres the String:
    if k == 13        'return = cmd end
            quit
    
    shoud be:
    if k == 13        'return = cmd end
            cmdStr[i] := 0
            quit
    

    Andy
  • Matthias1992Matthias1992 Posts: 7
    edited 2011-02-20 11:34
    Once again thanks for the help. I do have some questions about Femtobasic though, is there something like a clearscreen command? It does not seems to be in the manual...Or i am blind...

    Second question is that since I have no SD card how can I now save a program in femto? Prefferably I would like it to run at startup but that is not neccessary.
  • Mike GMike G Posts: 2,702
    edited 2011-02-20 12:01
    You might want to ask the FemtoBasic question in a new thread.
Sign In or Register to comment.