Python ncurses

From Openmoko

(Difference between revisions)
Jump to: navigation, search
Line 8: Line 8:
 
Then I've put together a simple ncurses based app that'll sit in a loop waiting for input, and use ssh to execute xmms remote commands on the host computer.  The full source is below:
 
Then I've put together a simple ncurses based app that'll sit in a loop waiting for input, and use ssh to execute xmms remote commands on the host computer.  The full source is below:
  
<nowiki>
+
  # very helpful: http://www.amk.ca/python/howto/curses/curses.html#SECTION000300000000000000000
<pre><code>
+
# very helpful: http://www.amk.ca/python/howto/curses/curses.html#SECTION000300000000000000000
+
  
import os
+
  import os
import curses
+
  import curses
stdscr = curses.initscr()
+
  stdscr = curses.initscr()
  
curses.noecho()
+
  curses.noecho()
curses.cbreak()
+
  curses.cbreak()
stdscr.keypad(1)
+
  stdscr.keypad(1)
  
host = "192.168.1.200"
+
  host = "192.168.1.200"
user = "jadams"
+
  user = "jadams"
MESSAGE_X = 0
+
  MESSAGE_X = 0
MESSAGE_Y = 5
+
  MESSAGE_Y = 5
  
def send_xmms_arg(char):
+
  def send_xmms_arg(char):
    os.system('ssh ' + user + '@' + host + ' "xmms -' + char + '"')
+
      os.system('ssh ' + user + '@' + host + ' "xmms -' + char + '"')
  
def write_instructions():
+
  def write_instructions():
    stdscr.addstr(0, 0, "c: play/pause; b: next; z: prev")
+
      stdscr.addstr(0, 0, "c: play/pause; b: next; z: prev")
    stdscr.refresh()
+
      stdscr.refresh()
  
def write_message(message):
+
  def write_message(message):
    stdscr.addstr(MESSAGE_Y, MESSAGE_X, message) # clear out  
+
      stdscr.addstr(MESSAGE_Y, MESSAGE_X, message) # clear out  
    stdscr.refresh()
+
      stdscr.refresh()
  
def do_play_pause():
+
  def do_play_pause():
    send_xmms_arg('u')
+
      send_xmms_arg('u')
    write_message("XMMS was paused / unpaused")
+
      write_message("XMMS was paused / unpaused")
  
def do_next():
+
  def do_next():
    send_xmms_arg('f')
+
      send_xmms_arg('f')
    write_message("Skipped to the next song.")
+
      write_message("Skipped to the next song.")
  
def do_prev():
+
  def do_prev():
    send_xmms_arg('r')
+
      send_xmms_arg('r')
    write_message("Skipped to the previous song.")
+
      write_message("Skipped to the previous song.")
  
write_instructions()
+
  write_instructions()
  
while 1:
+
  while 1:
    c = stdscr.getch()
+
      c = stdscr.getch()
    if c == ord('q'): break
+
      if c == ord('q'): break
    elif c == ord('c'): do_play_pause()
+
      elif c == ord('c'): do_play_pause()
    elif c == ord('b'): do_next()
+
      elif c == ord('b'): do_next()
    elif c == ord('z'): do_prev()
+
      elif c == ord('z'): do_prev()
  
curses.nocbreak(); stdscr.keypad(0); curses.echo()
+
  curses.nocbreak(); stdscr.keypad(0); curses.echo()
curses.endwin()
+
  curses.endwin()
</code></pre>
+
</nowiki>
+

Revision as of 22:16, 26 August 2007

Writing python apps for the neo that use ncurses

To get started, visit Repositories and add ScaredyCat's repository to your list. Then:

 ipkg update
 ipkg install python-base python-curses

Then I've put together a simple ncurses based app that'll sit in a loop waiting for input, and use ssh to execute xmms remote commands on the host computer. The full source is below:

 # very helpful: http://www.amk.ca/python/howto/curses/curses.html#SECTION000300000000000000000
 import os
 import curses
 stdscr = curses.initscr()
 curses.noecho()
 curses.cbreak()
 stdscr.keypad(1)
 host = "192.168.1.200"
 user = "jadams"
 MESSAGE_X = 0
 MESSAGE_Y = 5
 def send_xmms_arg(char):
     os.system('ssh ' + user + '@' + host + ' "xmms -' + char + '"')
 def write_instructions():
     stdscr.addstr(0, 0, "c: play/pause; b: next; z: prev")
     stdscr.refresh()
 def write_message(message):
     stdscr.addstr(MESSAGE_Y, MESSAGE_X, message) # clear out 
     stdscr.refresh()
 def do_play_pause():
     send_xmms_arg('u')
     write_message("XMMS was paused / unpaused")
 def do_next():
     send_xmms_arg('f')
     write_message("Skipped to the next song.")
 def do_prev():
     send_xmms_arg('r')
     write_message("Skipped to the previous song.")
 write_instructions()
 while 1:
     c = stdscr.getch()
     if c == ord('q'): break
     elif c == ord('c'): do_play_pause()
     elif c == ord('b'): do_next()
     elif c == ord('z'): do_prev()
 curses.nocbreak(); stdscr.keypad(0); curses.echo()
 curses.endwin()
Personal tools

Writing python apps for the neo that use ncurses

To get started, visit Repositories and add ScaredyCat's repository to your list. Then:

 ipkg update
 ipkg install python-base python-curses

Then I've put together a simple ncurses based app that'll sit in a loop waiting for input, and use ssh to execute xmms remote commands on the host computer. The full source is below:

 # very helpful: http://www.amk.ca/python/howto/curses/curses.html#SECTION000300000000000000000
 import os
 import curses
 stdscr = curses.initscr()
 curses.noecho()
 curses.cbreak()
 stdscr.keypad(1)
 host = "192.168.1.200"
 user = "jadams"
 MESSAGE_X = 0
 MESSAGE_Y = 5
 def send_xmms_arg(char):
     os.system('ssh ' + user + '@' + host + ' "xmms -' + char + '"')
 def write_instructions():
     stdscr.addstr(0, 0, "c: play/pause; b: next; z: prev")
     stdscr.refresh()
 def write_message(message):
     stdscr.addstr(MESSAGE_Y, MESSAGE_X, message) # clear out 
     stdscr.refresh()
 def do_play_pause():
     send_xmms_arg('u')
     write_message("XMMS was paused / unpaused")
 def do_next():
     send_xmms_arg('f')
     write_message("Skipped to the next song.")
 def do_prev():
     send_xmms_arg('r')
     write_message("Skipped to the previous song.")
 write_instructions()
 while 1:
     c = stdscr.getch()
     if c == ord('q'): break
     elif c == ord('c'): do_play_pause()
     elif c == ord('b'): do_next()
     elif c == ord('z'): do_prev()
 curses.nocbreak(); stdscr.keypad(0); curses.echo()
 curses.endwin()