2015-06-07 14:31:52 (edited by Sukil 2015-06-07 18:18:42)

Hi,
I'm trying to improve Tyler Spivey's BNS emulator. Those who follow the Talking Dosbox mailing list will know that I'm trying to make it compatible with Habla, a Spanish screen reader. This screen reader, instead of following its messages with a carriage return, expect the bns to process them after some time (300 ms aprox.). So here's my problem:
I'm using argparse (from another module) to let the user, ammong other things, to tell the program whether it should be run in "habla" mode:
emu.py (main script):

if cmd.args.habla == True:
 habla()

cmd.py (the argument parser itself):

parser.add_argument("--habla", help = "If specified, makes the emulator compatible with Habla", action = "store_true")
args = parser.parse_args()

Then, of course, I define a function with no arguments called habla, which controls the timer, and calls other functions:

def habla():
 synth.speak("Modo habla activado")
 starttime = time.clock()
 while True:
  if time.clock()-starttime >= .300:
   process(lst)
   starttime = time.clock() 

Regardless of that, when I try to run in habla mode, I get this traceback:

Traceback (most recent call last):
  File "emu.py", line 9, in <module>
    habla()
NameError: name 'habla' is not defined

I also get an "exception in thread Thread-1", but none of the files there is directly modified by me.
Any help would be appreciated.

2015-06-07 14:53:09

Is habla() implemented in cmd.py or emu.py? because you're calling it from within emu.py, and it doesn't know abut cmd.py.

This is not a signature.

2015-06-07 14:58:56

Hi,
It's implemented in emu.py.

2015-06-07 17:10:30 (edited by dhruv 2015-06-07 17:12:41)

Hi,
Could you paste the complete emu.py file? and please do it in a [ code ] code here...[ /code ] tag. Here's why:

if spam == 'eggs':
    print 'hello world!'

that was with tags. Otherwsie:
if spam == 'eggs':
    print 'hello world!'
see the difference?

This is not a signature.

2015-06-07 18:18:05

OK, here it goes. Some things, though: my contribution to this file is just the habla function and its if statement, and the cmd.py file import and the file itself (previously it was using sys.argv instead of argparse to parse arguments). And as a side note, I wonder what purpose does that final unindented "(1)" have.


import serial, threading, cStringIO, cmd, time
import string
import espeak
rate_map = (80, 100, 120, 140, 160, 180, 200, 240, 260, 290, 320, 350, 370, 390, 400, 450)
rate, pitch = 5, 5
synth=espeak.Synth()
synth.speak("ready")
if cmd.args.habla == True:
 habla()
port = serial.serial_for_url(cmd.args.comport, 9600)
port.setDsrDtr(0)
cmdchar = '\x05'
buffer = cStringIO.StringIO()
in_command = False
num=""
lst=[]
def habla():
 synth.speak("Modo habla activado")
 starttime = time.clock()
 while True:
  if time.clock()-starttime >= .300:
   process(lst)
   starttime = time.clock()
def parse(ch):
 global in_command, num, lst
 if ch == '\x18':
  reset()
  synth.cancel()
 elif ch == cmdchar:
  in_command = True
 elif in_command and ch in string.digits:
  num += ch
 elif in_command and ch in string.letters:
  in_command = False
  if buffer.tell() > 0:
   lst.append(buffer.getvalue())
   buffer.reset()
   buffer.truncate()
  if ch in handlers:
   lst.append((handlers[ch], int(num)))
  num=""
 elif in_command: #fall through, unrecognized char
  in_command = False
  return
 elif not in_command and (ch == '\r' or ch == '\0'):
  if ch == '\0': port.write('\0')
  if buffer.tell() > 0: lst.append(buffer.getvalue())
  process(lst)
  reset()
 else:
  buffer.write(ch)
def process(lst):
 sb = cStringIO.StringIO()
 for item in lst:
  if isinstance(item, basestring):
   sb.write(item)
  elif isinstance(item, tuple):
   sb.write(item[0](item[1]))
 v = sb.getvalue()
 if v.strip() == '': return
 synth.speak(v)

def speed(x):
 return "\x01%dS " % rate_map[x]
def pitch(x):
 return ""
def reset():
 global buffer, lst, in_command, num
 buffer.reset()
 buffer.truncate()
 lst = []
 num=""
 in_command=False
handlers = {
'E': speed,
'P': pitch
}
while True:
 parse(port.read(1))
(1)

Another thing: if I try to time the 300 ms with threading.timer(), instead of time.time() like this:

t = Threading.Timer(.300, process, [lst])
t.start()

I get a "name 'process' is not defined'" error.

2015-06-07 18:49:26

The problem is that you defined the habla function after you call it. That function doesn't exist in a python script until you define it.

Deep in the human unconscious is a pervasive need for a logical universe that makes sense. But the real universe is always one step beyond logic.

2015-06-07 19:01:57

If that is the case, I don't understand why "process()" and "reset()" work.

2015-06-07 23:50:51

Hi,
Although I still don't understand the reason of the issue (see my previous post) I finally got this working. Thank you! It doesn't work as expected (I still don't know why, it should be  screen reader issue), but I've solved the problem of the timer.

2018-08-13 15:30:55

and, any news on this?

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station…

2018-08-13 21:44:40

No I doubt it. It's been so long, after so many years it's probably not going to happen.

Guitarman.
What has been created in the laws of nature holds true in the laws of magic as well. Where there is light, there is darkness,  and where there is life, there is also death.
Aerodyne: first of the wizard order