1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
import fcntl
import IOCTL
from IOCTL import *
import sys
import struct
import select
import posix
import time
DEVICE='/dev/ttyd2'
class UnixFile:
def open(self, name, mode):
self.fd = posix.open(name, mode)
return self
def read(self, len):
return posix.read(self.fd, len)
def write(self, data):
dummy = posix.write(self.fd, data)
def flush(self):
pass
def fileno(self):
return self.fd
def close(self):
dummy = posix.close(self.fd)
def packttyargs(*args):
if type(args) <> type(()):
raise 'Incorrect argtype for packttyargs'
if type(args[0]) == type(1):
iflag, oflag, cflag, lflag, line, chars = args
elif type(args[0]) == type(()):
if len(args) <> 1:
raise 'Only 1 argument expected'
iflag, oflag, cflag, lflag, line, chars = args[0]
elif type(args[0]) == type([]):
if len(args) <> 1:
raise 'Only 1 argument expected'
[iflag, oflag, cflag, lflag, line, chars] = args[0]
str = struct.pack('hhhhb', iflag, oflag, cflag, lflag, line)
for c in chars:
str = str + c
return str
def nullttyargs():
chars = ['\0']*IOCTL.NCCS
return packttyargs(0, 0, 0, 0, 0, chars)
def unpackttyargs(str):
args = str[:-IOCTL.NCCS]
rawchars = str[-IOCTL.NCCS:]
chars = []
for c in rawchars:
chars.append(c)
iflag, oflag, cflag, lflag, line = struct.unpack('hhhhb', args)
return (iflag, oflag, cflag, lflag, line, chars)
def initline(name):
fp = UnixFile().open(name, 2)
ofp = fp
fd = fp.fileno()
rv = fcntl.ioctl(fd, IOCTL.TCGETA, nullttyargs())
iflag, oflag, cflag, lflag, line, chars = unpackttyargs(rv)
iflag = iflag & ~(INPCK|ISTRIP|INLCR|IUCLC|IXON|IXOFF)
oflag = oflag & ~OPOST
cflag = B9600|CS8|CREAD|CLOCAL
lflag = lflag & ~(ISIG|ICANON|ECHO|TOSTOP)
chars[VMIN] = chr(1)
chars[VTIME] = chr(0)
arg = packttyargs(iflag, oflag, cflag, lflag, line, chars)
dummy = fcntl.ioctl(fd, IOCTL.TCSETA, arg)
return fp, ofp
#
#
error = 'VCR.error'
# Commands/replies:
COMPLETION = '\x01'
ACK ='\x0a'
NAK ='\x0b'
NUMBER_N = 0x30
ENTER = '\x40'
EXP_7= '\xde'
EXP_8= '\xdf'
CL ='\x56'
CTRL_ENABLE = EXP_8 + '\xc6'
SEARCH_DATA = EXP_8 + '\x93'
ADDR_SENSE = '\x60'
PLAY ='\x3a'
STOP ='\x3f'
EJECT='\x2a'
FF ='\xab'
REW ='\xac'
STILL='\x4f'
STEP_FWD ='\x2b' # Was: '\xad'
FM_SELECT=EXP_8 + '\xc8'
FM_STILL=EXP_8 + '\xcd'
PREVIEW=EXP_7 + '\x9d'
REVIEW=EXP_7 + '\x9e'
DM_OFF=EXP_8 + '\xc9'
DM_SET=EXP_8 + '\xc4'
FWD_SHUTTLE='\xb5'
REV_SHUTTLE='\xb6'
EM_SELECT=EXP_8 + '\xc0'
N_FRAME_REC=EXP_8 + '\x92'
SEARCH_PREROLL=EXP_8 + '\x90'
EDIT_PB_STANDBY=EXP_8 + '\x96'
EDIT_PLAY=EXP_8 + '\x98'
AUTO_EDIT=EXP_7 + '\x9c'
IN_ENTRY=EXP_7 + '\x90'
IN_ENTRY_RESET=EXP_7 + '\x91'
IN_ENTRY_SET=EXP_7 + '\x98'
IN_ENTRY_INC=EXP_7 + '\x94'
IN_ENTRY_DEC=EXP_7 + '\x95'
IN_ENTRY_SENSE=EXP_7 + '\x9a'
OUT_ENTRY=EXP_7 + '\x92'
OUT_ENTRY_RESET=EXP_7 + '\x93'
OUT_ENTRY_SET=EXP_7 + '\x99'
OUT_ENTRY_INC=EXP_7 + '\x96'
OUT_ENTRY_DEC=EXP_7 + '\x98'
OUT_ENTRY_SENSE=EXP_7 + '\x9b'
MUTE_AUDIO = '\x24'
MUTE_AUDIO_OFF = '\x25'
MUTE_VIDEO = '\x26'
MUTE_VIDEO_OFF = '\x27'
MUTE_AV = EXP_7 + '\xc6'
MUTE_AV_OFF = EXP_7 + '\xc7'
DEBUG=0
class VCR:
def __init__(self):
self.ifp, self.ofp = initline(DEVICE)
self.busy_cmd = None
self.async = 0
self.cb = None
self.cb_arg = None
def _check(self):
if self.busy_cmd:
raise error, 'Another command active: '+self.busy_cmd
def _endlongcmd(self, name):
if not self.async:
self.waitready()
return 1
self.busy_cmd = name
return 'VCR BUSY'
def fileno(self):
return self.ifp.fileno()
def setasync(self, async):
self.async = async
def setcallback(self, cb, arg):
self.setasync(1)
self.cb = cb
self.cb_arg = arg
def poll(self):
if not self.async:
raise error, 'Can only call poll() in async mode'
if not self.busy_cmd:
return
if self.testready():
if self.cb:
apply(self.cb, (self.cb_arg,))
def _cmd(self, cmd):
if DEBUG:
print '>>>',`cmd`
self.ofp.write(cmd)
self.ofp.flush()
def _waitdata(self, len, tout):
rep = ''
while len > 0:
if tout == None:
ready, d1, d2 = select.select( \
[self.ifp], [], [])
else:
ready, d1, d2 = select.select( \
[self.ifp], [], [], tout)
if ready == []:
## if rep:
## print 'FLUSHED:', `rep`
return None
data = self.ifp.read(1)
if DEBUG:
print '<<<',`data`
if data == NAK:
return NAK
rep = rep + data
len = len - 1
return rep
def _reply(self, len):
data = self._waitdata(len, 10)
if data == None:
raise error, 'Lost contact with VCR'
return data
def _getnumber(self, len):
data = self._reply(len)
number = 0
for c in data:
digit = ord(c) - NUMBER_N
if digit < 0 or digit > 9:
raise error, 'Non-digit in number'+`c`
number = number*10 + digit
return number
def _iflush(self):
dummy = self._waitdata(10000, 0)
## if dummy:
## print 'IFLUSH:', dummy
def simplecmd(self,cmd):
self._iflush()
for ch in cmd:
self._cmd(ch)
rep = self._reply(1)
if rep == NAK:
return 0
elif rep <> ACK:
raise error, 'Unexpected reply:' + `rep`
return 1
def replycmd(self, cmd):
if not self.simplecmd(cmd[:-1]):
return 0
self._cmd(cmd[-1])
def _number(self, number, digits):
if number < 0:
raise error, 'Unexpected negative number:'+ `number`
maxnum = pow(10, digits)
if number > maxnum:
raise error, 'Number too big'
while maxnum > 1:
number = number % maxnum
maxnum = maxnum / 10
digit = number / maxnum
ok = self.simplecmd(chr(NUMBER_N + digit))
if not ok:
raise error, 'Error while transmitting number'
def initvcr(self, *optarg):
timeout = None
if optarg <> ():
timeout = optarg[0]
starttime = time.time()
self.busy_cmd = None
self._iflush()
while 1:
## print 'SENDCL'
self._cmd(CL)
rep = self._waitdata(1, 2)
## print `rep`
if rep in ( None, CL, NAK ):
if timeout:
if time.time() - starttime > timeout:
raise error, \
'No reply from VCR'
continue
break
if rep <> ACK:
raise error, 'Unexpected reply:' + `rep`
dummy = self.simplecmd(CTRL_ENABLE)
def waitready(self):
rep = self._waitdata(1, None)
if rep == None:
raise error, 'Unexpected None reply from waitdata'
if rep not in (COMPLETION, ACK):
self._iflush()
raise error, 'Unexpected waitready reply:' + `rep`
self.busy_cmd = None
def testready(self):
rep = self._waitdata(1, 0)
if rep == None:
return 0
if rep not in (COMPLETION, ACK):
self._iflush()
raise error, 'Unexpected waitready reply:' + `rep`
self.busy_cmd = None
return 1
def play(self): return self.simplecmd(PLAY)
def stop(self): return self.simplecmd(STOP)
def ff(self): return self.simplecmd(FF)
def rew(self): return self.simplecmd(REW)
def eject(self):return self.simplecmd(EJECT)
def still(self):return self.simplecmd(STILL)
def step(self): return self.simplecmd(STEP_FWD)
def goto(self, (h, m, s, f)):
if not self.simplecmd(SEARCH_DATA):
return 0
self._number(h, 2)
self._number(m, 2)
self._number(s, 2)
self._number(f, 2)
if not self.simplecmd(ENTER):
return 0
return self._endlongcmd('goto')
# XXXX TC_SENSE doesn't seem to work
def faulty_where(self):
self._check()
self._cmd(TC_SENSE)
h = self._getnumber(2)
m = self._getnumber(2)
s = self._getnumber(2)
f = self._getnumber(2)
return (h, m, s, f)
def where(self):
return self.addr2tc(self.sense())
def sense(self):
self._check()
self._cmd(ADDR_SENSE)
num = self._getnumber(5)
return num
def addr2tc(self, num):
f = num % 25
num = num / 25
s = num % 60
num = num / 60
m = num % 60
h = num / 60
return (h, m, s, f)
def tc2addr(self, (h, m, s, f)):
return ((h*60 + m)*60 + s)*25 + f
def fmmode(self, mode):
self._check()
if mode == 'off':
arg = 0
elif mode == 'buffer':
arg = 1
elif mode == 'dnr':
arg = 2
else:
raise error, 'fmmode arg should be off, buffer or dnr'
if not self.simplecmd(FM_SELECT):
return 0
self._number(arg, 1)
if not self.simplecmd(ENTER):
return 0
return 1
def mute(self, mode, value):
self._check()
if mode == 'audio':
cmds = (MUTE_AUDIO_OFF, MUTE_AUDIO)
elif mode == 'video':
cmds = (MUTE_VIDEO_OFF, MUTE_VIDEO)
elif mode == 'av':
cmds = (MUTE_AV_OFF, MUTE_AV)
else:
raise error, 'mute type should be audio, video or av'
cmd = cmds[value]
return self.simplecmd(cmd)
def editmode(self, mode):
self._check()
if mode == 'off':
a0 = a1 = a2 = 0
elif mode == 'format':
a0 = 4
a1 = 7
a2 = 4
elif mode == 'asmbl':
a0 = 1
a1 = 7
a2 = 4
elif mode == 'insert-video':
a0 = 2
a1 = 4
a2 = 0
else:
raise 'editmode should be off,format,asmbl or insert-video'
if not self.simplecmd(EM_SELECT):
return 0
self._number(a0, 1)
self._number(a1, 1)
self._number(a2, 1)
if not self.simplecmd(ENTER):
return 0
return 1
def autoedit(self):
self._check()
return self._endlongcmd(AUTO_EDIT)
def nframerec(self, num):
if not self.simplecmd(N_FRAME_REC):
return 0
self._number(num, 4)
if not self.simplecmd(ENTER):
return 0
return self._endlongcmd('nframerec')
def fmstill(self):
if not self.simplecmd(FM_STILL):
return 0
return self._endlongcmd('fmstill')
def preview(self):
if not self.simplecmd(PREVIEW):
return 0
return self._endlongcmd('preview')
def review(self):
if not self.simplecmd(REVIEW):
return 0
return self._endlongcmd('review')
def search_preroll(self):
if not self.simplecmd(SEARCH_PREROLL):
return 0
return self._endlongcmd('search_preroll')
def edit_pb_standby(self):
if not self.simplecmd(EDIT_PB_STANDBY):
return 0
return self._endlongcmd('edit_pb_standby')
def edit_play(self):
if not self.simplecmd(EDIT_PLAY):
return 0
return self._endlongcmd('edit_play')
def dmcontrol(self, mode):
self._check()
if mode == 'off':
return self.simplecmd(DM_OFF)
if mode == 'multi freeze':
num = 1000
elif mode == 'zoom freeze':
num = 2000
elif mode == 'digital slow':
num = 3000
elif mode == 'freeze':
num = 4011
else:
raise error, 'unknown dmcontrol argument: ' + `mode`
if not self.simplecmd(DM_SET):
return 0
self._number(num, 4)
if not self.simplecmd(ENTER):
return 0
return 1
def fwdshuttle(self, num):
if not self.simplecmd(FWD_SHUTTLE):
return 0
self._number(num, 1)
return 1
def revshuttle(self, num):
if not self.simplecmd(REV_SHUTTLE):
return 0
self._number(num, 1)
return 1
def getentry(self, which):
self._check()
if which == 'in':
cmd = IN_ENTRY_SENSE
elif which == 'out':
cmd = OUT_ENTRY_SENSE
self.replycmd(cmd)
h = self._getnumber(2)
m = self._getnumber(2)
s = self._getnumber(2)
f = self._getnumber(2)
return (h, m, s, f)
def inentry(self, arg):
return self.ioentry(arg, (IN_ENTRY, IN_ENTRY_RESET, \
IN_ENTRY_SET, IN_ENTRY_INC, IN_ENTRY_DEC))
def outentry(self, arg):
return self.ioentry(arg, (OUT_ENTRY, OUT_ENTRY_RESET, \
OUT_ENTRY_SET, OUT_ENTRY_INC, OUT_ENTRY_DEC))
def ioentry(self, arg, (Load, Clear, Set, Inc, Dec)):
self._check()
if type(arg) == type(()):
h, m, s, f = arg
if not self.simplecmd(Set):
return 0
self._number(h,2)
self._number(m,2)
self._number(s,2)
self._number(f,2)
if not self.simplecmd(ENTER):
return 0
return 1
elif arg == 'reset':
cmd = Clear
elif arg == 'load':
cmd = Load
elif arg == '+':
cmd = Inc
elif arg == '-':
cmd = Dec
else:
raise error, 'Arg should be +,-,reset,load or (h,m,s,f)'
return self.simplecmd(cmd)
def cancel(self):
d = self.simplecmd(CL)
self.busy_cmd = None
|