def get_rcs(): """Connect to the RCS on a user selected port.""" while True: WConio.cputs('Available Ports\nSEL Comm Port\n--- ---------\n') ports = ['Quit'] + port_list() for i, v in enumerate(ports): WConio.cputs('%3d %s\n'%(i, v)) sel_str = raw_input('Select a comm port or 0 to Quit -->') try: sel = int(sel_str) ports[sel] except Exception: WConio.cputs('\nAcceptable values are 0 to %d.\n'%i) else: if sel == 0: exit() elif sel > 0: try: rcs = Robust_Feusb(ports[sel]) except OpenError, e: WConio.cputs(str(e)+'\n') else: return rcs else: WConio.cputs('\nAcceptable values are 0 to %d.\n'%i)
def robust_write(self, command = ''): """Write to the RCS with robust exception handling.""" try: self.write(command) except DisconnectError: global servo_mode servo_mode = IDLE # Blank screen due to device disconnect. WConio.textattr(0xb0) WConio.clrscr() WConio.gotoxy(10, 10) WConio.cputs('USB-RCS has been disconnected during write. ' 'Trying to reconnect. ') self.robust_reconnect() usb = rcs.robust_read('U') firmware_version = usb[2] display_once(firmware_version) except Exception, e: WConio.textattr(0xc0) WConio.clrscr() WConio.gotoxy(0, 10) print "Unexpected exception in robust_write: ", type(e) print print e print raw_input("Press enter to exit ->") exit()
def run ( self ): time.sleep(3) oldLen = 0 while 1: newLen = len(l) WConio.textcolor(WConio.LIGHTCYAN) posX = WConio.wherex() posY = WConio.wherey() WConio.gotoxy(0,4) if newLen < oldLen: for n in range(oldLen+1): WConio.cputs(" \n\r") else: for n in range(len(l)+1): WConio.cputs(" \n\r") WConio.gotoxy(0,4) for i in range(len(l)): try: print str(i) + ': ' + str(l[i][2]) except: print str(i) + ': ' + str(l[i][1][0]) if posX > 16: WConio.gotoxy(posX,22) else: WConio.gotoxy(17,22) oldLen = len(l) time.sleep(3)
def display_suggestion(msg): global old_matches matches = [] if not msg or not msg.strip(): if old_matches != matches: W.gotoxy(0, C.height - 2) W.clreol() old_matches = matches return msg = msg.split()[0] for command in commands: if command.command.startswith(msg) and not command.hide: matches.append(command) if matches: #limit our match count matches = matches[:5] if old_matches != matches: old_matches = matches W.gotoxy(0, C.height - 2) W.clreol() targetwidth = C.width / len(matches) W.textcolor(W.DARKGREY) for command in matches: W.cputs(('%s (%s)' % (command.command, format_time(command.time, command.time_variable))).center(targetwidth)) return matches
def put(x, y, s, fgc=None, bgc=None): #------------------------------------------------------------------------------- wc.gotoxy(x, y) if bgc: wc.textbackground(bgc) if fgc: wc.textcolor(fgc) wc.cputs(s)
def put(x, y, s, fgc=None, bgc=None): #------------------------------------------------------------------------------- wc.gotoxy(x, y) if bgc: wc.textbackground(bgc) if fgc: wc.textcolor(fgc) wc.cputs(s)
def status(self, y, s): #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - wc.gotoxy(0, y) wc.movetext(0, y, self.ex, self.ey -1, 0, y+1) wc.gotoxy(0, y) wc.textbackground(7) wc.textcolor(15) wc.clreol() wc.cputs(s)
def put_at(x, y, msg, color=None): if color: W.textcolor(color) if len(msg) + x > C.width: log.debug('put_at: truncating %s by %i', msg, len(msg) + x - C.width) msg = msg[:C.width - x - 1] W.gotoxy(x, y) W.cputs(msg)
def status(self, y, s): #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - wc.gotoxy(0, y) wc.movetext(0, y, self.ex, self.ey - 1, 0, y + 1) wc.gotoxy(0, y) wc.textbackground(7) wc.textcolor(15) wc.clreol() wc.cputs(s)
def draw_box_text(box, text=None): if text is None: text = box.text W.textbackground(box.interior_background_color) for line in text: W.textcolor(line.color) if line.right_justify: W.gotoxy(box.left + box.width - line.x - len(line.text), box.top + line.y) else: W.gotoxy(box.left + line.x, box.top + line.y) W.cputs(line.text)
def display_bar(xcol, yrow, color, division, width): """Display a bar of 'color' and 'width', with two sections at 'division'.""" full_cells, cell_frac = divmod(int(width * division * 3), 3) if division < 0: display_string = SPACE * width elif full_cells == width: display_string = FULL_BAR * width elif cell_frac == 0: display_string = FULL_BAR * full_cells + SPACE * (width - full_cells) elif cell_frac == 1: display_string = FULL_BAR * full_cells + HALF_BAR + SPACE * (width - full_cells - 1) else: display_string = FULL_BAR * (full_cells + 1) + SPACE * (width - full_cells - 1) WConio.gotoxy(xcol, yrow) WConio.textattr(color) WConio.cputs(display_string)
def run(self): WConio.textcolor(WConio.LIGHTRED) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = '' port = 8888 s.bind((host, port)) s.listen(1) IPs = [] WConio.gotoxy(0, 25) WConio.cputs("Awaiting Connections...") WConio.gotoxy(19, 23) while 1: conn, addr = s.accept() WConio.gotoxy(0, 25) WConio.cputs("Connection Recieved -- Adding to File") WConio.gotoxy(19, 23) f = open('C:\\PyBot\\connections.txt', 'a') f.write(addr[0] + '\n') f.close()
def run ( self ): WConio.textcolor(WConio.LIGHTRED) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = '' port = 8888 s.bind((host, port)) s.listen(1) IPs = [] WConio.gotoxy(0,25) WConio.cputs("Awaiting Connections...") WConio.gotoxy(19,23) while 1: conn, addr = s.accept() WConio.gotoxy(0,25) WConio.cputs("Connection Recieved -- Adding to File") WConio.gotoxy(19,23) f = open('C:\\PyBot\\connections.txt', 'a') f.write(addr[0] + '\n') f.close()
def robust_reconnect(self): """Reconnect with exception handling.""" recon_count = 0 while True: time.sleep(0.500) try: self.reconnect() except OpenError: recon_count += 1 WConio.gotoxy(20, 14) WConio.cputs(' Reconnect retry attempts: %5i '% recon_count) except Exception, e: WConio.gotoxy(5, 16) print "Unexpected exception during reconnect:", type(e) print e raw_input("Press enter to exit ->") exit() else: return
def emit(self, record): WConio = self.WConio try: msg = self.format(record) levelname = record.levelname out = sys.__stdout__ # in case redirected by filelogger, etc if levelname in msg: part1, part2 = msg.split(levelname, 1) out.write(part1) out.flush() saved_color = WConio.gettextinfo()[4] WConio.textattr(self.colormap.get(levelname, WConio.LIGHTGREY)) if levelname == 'CRITICAL': WConio.textbackground(WConio.RED) WConio.cputs(levelname) WConio.textattr(saved_color) # restore print >> out, part2 else: print >> out, msg out.flush() except: self.handleError(record)
def emit(self, record): WConio = self.WConio try: msg = self.format(record) levelname = record.levelname out = sys.__stdout__ # in case redirected by filelogger, etc if levelname in msg: part1, part2 = msg.split(levelname, 1) out.write(part1) out.flush() saved_color = WConio.gettextinfo()[4] WConio.textattr( self.colormap.get(levelname, WConio.LIGHTGREY) ) if levelname == 'CRITICAL': WConio.textbackground(WConio.RED) WConio.cputs(levelname) WConio.textattr(saved_color) # restore print >> out, part2 else: print >> out, msg out.flush() except: self.handleError(record)
def run ( self ): WConio.textcolor(WConio.LIGHTGREEN) IPs = [] time.sleep(1) while 1: r = open('C:\\PyBot\\connections.txt', 'r') str = r.read().split("\n") r.close() for i in str: if i != '': try: IPs.index(i) except: IPs.append(i) f = open('C:\\PyBot\\update.txt', 'w') x = WConio.wherex() y = WConio.wherey() for st in IPs: f.write(st + '\n') WConio.gotoxy(0,4) WConio.cputs(st + '\n\r') f.close() WConio.gotoxy(x, y) time.sleep(2)
def run(self): WConio.textcolor(WConio.LIGHTGREEN) IPs = [] time.sleep(1) while 1: r = open('C:\\PyBot\\connections.txt', 'r') str = r.read().split("\n") r.close() for i in str: if i != '': try: IPs.index(i) except: IPs.append(i) f = open('C:\\PyBot\\update.txt', 'w') x = WConio.wherex() y = WConio.wherey() for st in IPs: f.write(st + '\n') WConio.gotoxy(0, 4) WConio.cputs(st + '\n\r') f.close() WConio.gotoxy(x, y) time.sleep(2)
def robust_read(self, command = None, count = 1): """Read from the RCS with robust exception handling.""" global servo_mode while True: try: return self.read(command, count) except DisconnectError: servo_mode = IDLE # Blank screen due to device disconnect. WConio.textattr(0xb0) WConio.clrscr() WConio.gotoxy(10, 10) WConio.cputs('USB-RCS has been disconnected during read. ' 'Trying to reconnect. ') self.robust_reconnect() usb = rcs.robust_read('U') firmware_version = usb[2] display_once(firmware_version) except ReadTimeoutError, e: servo_mode = IDLE # Blank screen due to device disconnect. WConio.textattr(0xc0) WConio.clrscr() WConio.gotoxy(10, 10) WConio.cputs('USB-RCS read timeout: ' 'Communications is not responding as expected.') WConio.gotoxy(10, 12) raw_input("Unplug the USB-RCS.") while self.status() != DISCONNECTED: time.sleep(0.500) WConio.gotoxy(10, 12) WConio.cputs("Now, plug-in the USB-RCS.") self.robust_reconnect() usb = rcs.robust_read('U') firmware_version = usb[2] display_once(firmware_version) except Exception, e: WConio.textattr(0xc0) WConio.clrscr() WConio.gotoxy(0, 10) print "Unexpected exception in robust_read: ", type(e) print e raw_input("Press enter to exit ->") exit()
def prompt_hbar(): W.textcolor(W.WHITE) W.gotoxy(0, C.height-3) #print C.Characters.box_single.horiz * C.width W.cputs(C.Characters.box_single.horiz * C.width)
def run ( self ): WConio.textcolor(WConio.LIGHTCYAN) while 1: WConio.gotoxy(79, 1) WConio.cputs(" ") WConio.gotoxy(78, 2) WConio.cputs(" ") WConio.gotoxy(77, 3) WConio.cputs(" ") WConio.gotoxy(76, 4) WConio.cputs(" ") WConio.gotoxy(2,23) WConio.cputs(" ") WConio.gotoxy(2,23) WConio.cputs("Enter command: ") cmd = raw_input() if cmd == 'Test' or cmd == 'test' or cmd == 'TEST': f = open('C:\\PyBot\\cmd.txt', 'w') f.write('test') f.close() WConio.gotoxy(0,25) WConio.cputs(" ") WConio.gotoxy(0,25) WConio.cputs("'Test' command accepted.") elif cmd == 'exit' or cmd == 'Exit' or cmd == 'close' or cmd == 'Close' or cmd == 'Quit' or cmd == 'quit': f = open('C:\\PyBot\\cmd.txt', 'w') f.write('exit') f.close() WConio.gotoxy(0,25) WConio.cputs(" ") WConio.gotoxy(0,25) WConio.cputs("Click the X button, this function isn't working.") WConio.gotoxy(79, 1) WConio.cputs("/") WConio.gotoxy(78, 2) WConio.cputs("/") WConio.gotoxy(77, 3) WConio.cputs("/") WConio.gotoxy(76, 4) WConio.cputs("/") else: WConio.gotoxy(0,25) WConio.cputs(" ") WConio.gotoxy(0,25) WConio.cputs("Command not recognized.")
# Requires the package WConio by Chris Gonnerman # E-Mail : [email protected] # Web : http://newcenturycomputers.net/projects/wconio.html import WConio #Store current attribute settings old_setting = WConio.gettextinfo()[4] & 0x00FF #Clear the screen WConio.clrscr() #Display something in low video WConio.lowvideo() WConio.cputs("Low video\r\n") #Display something in high video WConio.highvideo() WConio.cputs("High video\r\n") #Display something in normal video WConio.normvideo() WConio.cputs("Normal video\r\n") #Display some text in color WConio.textattr(WConio.LIGHTRED) WConio.cputs("Light Red text\r\n") #Display some more text in color WConio.textattr(WConio.LIGHTBLUE)
import os import io import time import random import urllib2 import re import WConio import threading import sys #(2,24) old_setting = WConio.gettextinfo()[4] & 0x00FF WConio.clrscr() WConio.settitle("PyBot Control Center") WConio.gotoxy(2,2) WConio.cputs("Welcome to the PyBot Control Center\n\r") f = open('C:\\PyBot\\connections.txt', 'w') j = open('C:\\PyBot\\update.txt', 'w') p = open('C:\\PyBot\\cmd.txt', 'w') f.write('') j.write('') p.write('') f.close() j.close() class GetConnections ( threading.Thread ): def run ( self ): WConio.textcolor(WConio.LIGHTRED) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = '' port = 8888
#(2,24) old_setting = WConio.gettextinfo()[4] & 0x00FF WConio.clrscr() WConio.textcolor(WConio.LIGHTRED) WConio.settitle("PyBot Control Center") WConio.gotoxy(2,2) print "Welcome to the Pybot Control Center" global l global key key = "this is the best key in the world, you'll never guess it :D 4&#*%:J" l = [] WConio.cputs( "\n username: "******" password: "******"" try: conn = mdb.connect('184.168.194.11','goldenboy', 'P@ssw0rd','goldenboy') cursor = conn.cursor() cursor.execute("SELECT Password FROM Users WHERE Username = '******';") data = cursor.fetchone() if str(data) != "None": if str(data[0]) == gpw: print "Authorized - Welcome " + user time.sleep(2)
# Works with Microsoft Windows dos box # Shows some use of WConio written by Chris Gonnerman # Written by Priyend Somaroo # Copyright (c) 2008 Vardaan Enterprises, www.vardaan.com # Use and distribute freely. # No liability for any use of this code will be accepted. Use is # without any warranty whatsoever # Requires the package WConio by Chris Gonnerman # E-Mail : [email protected] # Web : http://newcenturycomputers.net/projects/wconio.html import WConio old_setting = WConio.gettextinfo()[4] & 0x00FF WConio.clrscr() WConio.textcolor(WConio.LIGHTCYAN) WConio.settitle("PyBot Control Center") WConio.gotoxy(2, 2) WConio.cputs("Welcome to the PyBot Control Center\n\r") WConio.gotoxy(0, 4) WConio.cputs("127.0.0.1\n\r") WConio.cputs("192.168.1.1\n\r") WConio.gotoxy(0, 25) WConio.cputs("Random Output...") WConio.gotoxy(2, 23) WConio.cputs("Enter command: ") WConio.getch() WConio.gotoxy(0, 25)
# Works with Microsoft Windows dos box # Shows some use of WConio written by Chris Gonnerman # Written by Priyend Somaroo # Copyright (c) 2008 Vardaan Enterprises, www.vardaan.com # Use and distribute freely. # No liability for any use of this code will be accepted. Use is # without any warranty whatsoever # Requires the package WConio by Chris Gonnerman # E-Mail : [email protected] # Web : http://newcenturycomputers.net/projects/wconio.html import WConio old_setting = WConio.gettextinfo()[4] & 0x00FF WConio.clrscr() WConio.textcolor(WConio.LIGHTCYAN) WConio.settitle("PyBot Control Center") WConio.gotoxy(2,2) WConio.cputs("Welcome to the PyBot Control Center\n\r") WConio.gotoxy(0,4) WConio.cputs("127.0.0.1\n\r") WConio.cputs("192.168.1.1\n\r") WConio.gotoxy(0,25) WConio.cputs("Random Output...") WConio.gotoxy(2,23) WConio.cputs("Enter command: ") WConio.getch() WConio.gotoxy(0,25)
def cputs( car ) : """Affiche une chaine de caractère""" if sys.flags.interactive == 1 : # en execfile print car else : # en WConio WConio.cputs( car )
def run(self): WConio.textcolor(WConio.LIGHTCYAN) while 1: WConio.gotoxy(79, 1) WConio.cputs(" ") WConio.gotoxy(78, 2) WConio.cputs(" ") WConio.gotoxy(77, 3) WConio.cputs(" ") WConio.gotoxy(76, 4) WConio.cputs(" ") WConio.gotoxy(2, 23) WConio.cputs(" ") WConio.gotoxy(2, 23) WConio.cputs("Enter command: ") cmd = raw_input() if cmd == 'Test' or cmd == 'test' or cmd == 'TEST': f = open('C:\\PyBot\\cmd.txt', 'w') f.write('test') f.close() WConio.gotoxy(0, 25) WConio.cputs( " ") WConio.gotoxy(0, 25) WConio.cputs("'Test' command accepted.") elif cmd == 'exit' or cmd == 'Exit' or cmd == 'close' or cmd == 'Close' or cmd == 'Quit' or cmd == 'quit': f = open('C:\\PyBot\\cmd.txt', 'w') f.write('exit') f.close() WConio.gotoxy(0, 25) WConio.cputs( " ") WConio.gotoxy(0, 25) WConio.cputs( "Click the X button, this function isn't working.") WConio.gotoxy(79, 1) WConio.cputs("/") WConio.gotoxy(78, 2) WConio.cputs("/") WConio.gotoxy(77, 3) WConio.cputs("/") WConio.gotoxy(76, 4) WConio.cputs("/") else: WConio.gotoxy(0, 25) WConio.cputs( " ") WConio.gotoxy(0, 25) WConio.cputs("Command not recognized.")
# Requires the package WConio by Chris Gonnerman # E-Mail : [email protected] # Web : http://newcenturycomputers.net/projects/wconio.html import WConio #Store current attribute settings old_setting = WConio.gettextinfo()[4] & 0x00FF #Clear the screen WConio.clrscr() #Display something in low video WConio.lowvideo() WConio.cputs("Low video\r\n") #Display something in high video WConio.highvideo() WConio.cputs("High video\r\n") #Display something in normal video WConio.normvideo() WConio.cputs("Normal video\r\n") #Display some text in color WConio.textattr(WConio.LIGHTRED) WConio.cputs("Light Red text\r\n") #Display some more text in color WConio.textattr(WConio.LIGHTBLUE)
import os import io import time import random import urllib2 import re import WConio import threading import sys #(2,24) old_setting = WConio.gettextinfo()[4] & 0x00FF WConio.clrscr() WConio.settitle("PyBot Control Center") WConio.gotoxy(2, 2) WConio.cputs("Welcome to the PyBot Control Center\n\r") f = open('C:\\PyBot\\connections.txt', 'w') j = open('C:\\PyBot\\update.txt', 'w') p = open('C:\\PyBot\\cmd.txt', 'w') f.write('') j.write('') p.write('') f.close() j.close() class GetConnections(threading.Thread): def run(self): WConio.textcolor(WConio.LIGHTRED) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = ''
def display_update(): """Display changing information, call repeatedly.""" # Display the Disable Input Status. WConio.gotoxy(28, 0) if disabled: WConio.textattr(0xc0) WConio.cputs(' Enable Input Open -- All Servos Are Held Idle ') else: WConio.textattr(0xa0) WConio.cputs('Enable Input Shorted -- Servo Positioning Permitted') # Display the servo command mode. WConio.gotoxy(28, 2) WConio.textattr(0x70) if command_mode is INIT: WConio.cputs('Servos held initialized: AnyKey to run, Q to quit.') elif command_mode is RUN: WConio.cputs('Servos allowed to run: AnyKey to brake, Q to quit.') elif command_mode is BRAKE: WConio.cputs('Servos braking: AnyKey to idle servos, Q to quit.') elif command_mode is IDLE: WConio.cputs('Servos held idle: AnyKey to initialize, Q to quit.') else: WConio.cputs('Error: Unrecognized command mode!!! Q to quit.') # Display servo cycle, speed, position in mS and position bar graph. for i, each_servo in enumerate(all_servos): WConio.gotoxy(4, 3 + i) WConio.textattr(0x70) # Cyc..Spd..Pos mS if accel_dif: accel = accel_list[i] else: accel = 1 WConio.cputs('%3i %2i %3i %5i %01.4f' %(accel, each_servo[3], each_servo[2], each_servo[1], each_servo[0]/12000.0)) color = 0x2b if i % 2 else 0x2a #2a is green, 9b is blue display_bar(28, 3 + i, color, float(each_servo[0] - SERVO_MIN) / SERVO_RANGE, 51) # Display analog measurement values. for i, each_channel in enumerate(analog_channels): color = 0x3b if i % 2 else 0x3a #a is green, b is blue volts = each_channel * 5.0 / ANALOG_MAX if i < 4: xoffset = 0 yoffset = i else: xoffset = 40 yoffset = i-4 WConio.gotoxy(5 + xoffset, 21 + yoffset) WConio.textattr(0x70) WConio.cputs('%01.4fv'%volts) display_bar(13 + xoffset, 21 + yoffset, color, volts/5.0, 26)
print e raw_input("Press enter to exit ->") exit() else: return ########## Main Routine ########## if __name__=='__main__': try: WConio.settitle("TestRCS -- Test the RC Servo Controller") # Initial Page, Select and Open the Serial Port WConio.textattr(0x07) WConio.setcursortype(2) rcs = get_rcs() # select a port and open the rcs WConio.setcursortype(0) WConio.cputs("\nCommunications port is open!\n") time.sleep(0.5) # disable servos, configure analog, read: USB report, servo status, analog usb, all_servos, analog_channels = rcs.robust_read('CA0USM', 3) firmware_version = usb[2] disabled = (all_servos[-1][-1] == -1) # Servo and Analog Display Page servo_mode = IDLE # Set up variables for running servos command_mode = INIT accel_dif = False accel_list = [1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 255] display_once(firmware_version) # Display static and initial items while True: if len(all_servos) == 16: #check for valid servo data
def get_input(): buf = [] while True: msg = ''.join(buf) matches = display_suggestion(msg) W.gotoxy(0, C.height - 1) W.clreol() W.textcolor(W.GREEN) W.cputs(game.state.current_node.command_prompt + '$ '), W.textcolor(W.LIGHTGREEN) W.cputs(msg) if matches and len(matches) == 1: #show the argument help text match = matches[0] x = W.wherex() W.textcolor(W.DARKGREY) splitted = match.arguments.split() splitted = splitted[max(0, len(msg.split())-2):] W.cputs(' ' + ' '.join(splitted)) W.textcolor(W.LIGHTGREEN) W.gotoxy(x, C.height - 1) else: match = None #Read input W.setcursortype(1) (chn, chs) = W.getch() W.setcursortype(0) #figure out if we're done if chs == '\r': #enter, exit break if chn == 8: #backspace if len(buf): buf.pop() else: MessageBeep() continue if chn == 3: log.debug('took a ctrl-c') game.fire('specialkey', 'ctrlc') break if chn == 0 or chn == 224: #special keys come in two parts (chn2, _) = W.getch() if chn2 in W.__keydict: game.fire('specialkey', W.__keydict[chn2]) continue if len(buf) >= C.width: #way too long now break if chs not in string.printable: #dont care continue buf.append(chs) return buf, match
def display_once(firmware_version): """Display static information, call once.""" WConio.textattr(0x70) WConio.clrscr() # Display Firmware Version or Firmware Status. WConio.gotoxy(2, 0) if firmware_version < 1.0: WConio.textattr(0xc0) WConio.cputs(' Firmware Unreliable! ') else: WConio.textattr(0xa0) WConio.cputs(' Firmware Version %2.2f '%firmware_version) # Display servo static labels. WConio.gotoxy(0, 2) WConio.textattr(0x70) WConio.cputs(' S# Acc #Q Cyc Speed Pos mS\n') for i in range(1, 17): WConio.cputs(' %2i\n'%i) # Display analog channel labels. WConio.gotoxy(0, 20) WConio.textattr(0x70) WConio.cputs (' C# Voltage C# Voltage') for i in range(1, 5): WConio.cputs('\n %i %i'% (i, i+4))