-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpico_timecode.py
More file actions
executable file
·1653 lines (1340 loc) · 51.5 KB
/
pico_timecode.py
File metadata and controls
executable file
·1653 lines (1340 loc) · 51.5 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Pico-Timcode for Raspberry-Pi Pico
# (c) 2023-05-05 Simon Wood <simon@mungewell.org>
#
# https://github.com/mungewell/pico-timecode
import _thread
import rp2
from machine import Timer, Pin, mem32, disable_irq, enable_irq, freq, lightsleep
from micropython import schedule, alloc_emergency_exception_buf, mem_info
from utime import sleep, ticks_us
from gc import collect, mem_free
from os import uname
# remember to do install lib to device
# 'mpremote mip install usb-device-midi'
try:
import usb.device
from usb.device.midi import MIDIInterface
_hasUsbDevice = True
except ImportError:
_hasUsbDevice = False
'''
_hasUsbDevice = False
'''
alloc_emergency_exception_buf(100)
VERSION="v3.1"
# set up Globals
eng = None
stop = False
tx_raw = 0
rx_ticks = 0
rx_ticks_us = 0
tx_ticks_us = 0
mtc = None
quarters = 0
core_dis = [0, 0]
# Constants for run mode
HALTED = -1
RUN = 0
MONITOR = 1
JAM = 64
# Constants for StateMachines
# PIO block 1
SM_START = 0
SM_BLINK = 1
SM_BUFFER = 2
SM_ENCODE = 3
# PIO block 2
SM_TX_RAW = 4
SM_SYNC = 5
SM_DECODE = 6
irq_callbacks = [None]*8
timer1 = Timer()
timer2 = Timer()
timer3 = Timer()
tlock = _thread.allocate_lock()
#---------------------------------------------
# 'SM_START' State Machine
@rp2.asm_pio(autopull=True, autopush=True)
def auto_start():
set(x, 0)
nop()
nop()
irq(clear, 4) # immediately trigger Sync
# --
label("wait_for_low") # loop length 4 clocks
jmp(x_dec, "null1")
label("null1")
jmp(pin, "wait_for_low") [2]
# --
label("triggered") # section length 4 clocks
# capture count when pin goes low
mov(isr, x) # mov X into ISR
push(noblock)
jmp(x_dec, "wait_for_high") [1]
# --
wrap_target()
label("wait_for_high") # loop length 4 clocks
jmp(x_dec, "null2")
label("null2")
jmp(pin, "wait_for_low") [2]
wrap()
# 'SM_START' State Machine
@rp2.asm_pio(autopull=True, autopush=True)
def start_from_sync():
set(x, 0)
wait(0, pin, 0) # Wait for pin to go low
wait(1, pin, 0) # Wait for pin to go high
irq(clear, 4) # Trigger Sync
# --
label("wait_for_low") # loop length 4 clocks
jmp(x_dec, "null1")
label("null1")
jmp(pin, "wait_for_low") [2]
# --
label("triggered") # section length 4 clocks
# capture count when pin goes low
mov(isr, x) # mov X into ISR
push(noblock)
jmp(x_dec, "wait_for_high") [1]
# --
wrap_target()
label("wait_for_high") # loop length 4 clocks
jmp(x_dec, "null2")
label("null2")
jmp(pin, "wait_for_low") [2]
wrap()
# 'SM_BLINK' State Machine
@rp2.asm_pio(out_init=(rp2.PIO.OUT_HIGH,)*2, autopull=True,
fifo_join=rp2.PIO.JOIN_TX, out_shiftdir=rp2.PIO.SHIFT_RIGHT)
def shift_led_irq_1x():
irq(block, 4) # Wait for sync'ed start
# ---
wrap_target() # nominally 20 divisions per frame
out(x, 6) # sub-total 1 cycle
# ---
label("next")
out(pins, 2) # LEDs are bit-shifted pattern
# each division should be ~128 cycles
# representing each of the bits in packet
jmp(pin, "skip_irq") # sub-total 2 cycles
# ---
irq(rel(0)) [1] # set IRQ clocking quarter frame MTC data
# _extra_ cycles 1x per frame = 2cycles
# ---
label("skip_irq")
set(y, 3) [3]
jmp(x_dec, "delay") # sub-total 5 cycles
# ---
pull() [25] # last division only, fall through
jmp(y_dec, "delay") # sub-total 26 cycles
# ---
label("delay")
jmp(y_dec, "delay") [29] # sub-total 30 cycles
# ---
jmp(x_not_y, "next") # sub-total 1 cycle
wrap() # total 1 + 19(2+5+(4*30)+1) + (2+6+26+(3*30)+1) + 2
# total 1 + (19 * 128) + 125 + 2
# 'SM_BLINK' State Machine
@rp2.asm_pio(out_init=(rp2.PIO.OUT_HIGH,)*2, autopull=True,
fifo_join=rp2.PIO.JOIN_TX, out_shiftdir=rp2.PIO.SHIFT_RIGHT)
def shift_led_irq_4x():
irq(block, 4) # Wait for sync'ed start
# ---
wrap_target() # nominally 20 divisions per frame
out(x, 6) # sub-total 1 cycle
# ---
label("next")
out(pins, 2) # LEDs are bit-shifted pattern
# each division should be ~128 cycles
# representing each of the bits in packet
jmp(pin, "skip_irq") # sub-total 2 cycles
# ---
irq(rel(0)) [4] # set IRQ clocking quarter frame MTC data
# _extra_ cycles 4x per frame = 20cycles
# ---
label("skip_irq")
set(y, 3) [2]
jmp(x_dec, "delay") # sub-total 4 cycles
# ---
pull() [27] # last division only, fall through
jmp(y_dec, "delay") # sub-total 29 cycles
# ---
label("delay")
jmp(y_dec, "delay") [29] # sub-total 30 cycles
# ---
jmp(x_not_y, "next") # sub-total 1 cycle
wrap() # total 1 + 19(2+4+(4*30)+1) + (2+4+29+(3*30)+1) + 20
# total 1 + (19 * 127) + 126 + 20 = 2560 cycles
# 'SM_ENCODE' State Machine
@rp2.asm_pio(out_init=rp2.PIO.OUT_LOW)
def encode_dmc():
irq(block, 4) # Wait for Sync'ed start
wrap_target()
label("toggle-0")
mov(pins, invert(pins)) [14] # Always toogle pin at start of cycle, "0" or "1"
jmp(pin, "toggle-1") # Check output of SM-1 buffer, jump if 1
jmp("toggle-0") [15]
label("toggle-1")
mov(pins, invert(pins)) [15] # Toggle pin to signal '1'
wrap()
# 'SM_ENCODE' State Machine for differential output
@rp2.asm_pio(out_init=(rp2.PIO.OUT_HIGH, rp2.PIO.OUT_LOW))
def encode_dmc2():
irq(block, 4) # Wait for Sync'ed start
wrap_target()
label("toggle-0")
mov(pins, invert(pins)) [14] # Always toogle pin at start of cycle, "0" or "1"
jmp(pin, "toggle-1") # Check output of SM-1 buffer, jump if 1
jmp("toggle-0") [15]
label("toggle-1")
mov(pins, invert(pins)) [15] # Toggle pin to signal '1'
wrap()
# 'SM_BUFFER' State Machine
@rp2.asm_pio(out_init=rp2.PIO.OUT_LOW, autopull=True,
fifo_join=rp2.PIO.JOIN_TX, out_shiftdir=rp2.PIO.SHIFT_RIGHT)
def buffer_out():
irq(block, 4) # Wait for Sync'ed start
label("start")
out(pins, 1) [30]
jmp(not_osre, "start")
# UNDERFLOW - when Python fails to fill FIFOs
irq(rel(0)) # set IRQ to warn other StateMachines
wrap_target()
set(pins, 0)
wrap()
# ---
# 'SM_DECODE' State Machine
@rp2.asm_pio(set_init=(rp2.PIO.OUT_LOW,)*2)
def decode_dmc():
label("previously_low")
wait(1, pin, 0) # Line going high
irq(clear, 5) [19] # trigger sync engine, and wait til 3/4s mark
jmp(pin, "staying_high")
set(pins, 3) # Second transition detected (data is `1`)
jmp("previously_low") # Wait for next bit...
label("staying_high")
set(pins, 0) # Line still high, no centre transition (data is `0`)
# |
# | fall through... a few cycles early
# V
wrap_target()
label("previously_high")
wait(0, pin, 0) # Line going Low
irq(clear, 5) [19] # trigger sync engine, and wait til 3/4s mark
jmp(pin, "going_high")
set(pins, 0) # Line still low, no centre transition (data is `0`)
jmp("previously_low") # Wait for next bit...
label("going_high")
set(pins, 3) # Second transition detected (therfore data is `1`)
wrap()
# 'SM_SYNC' State Machine
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW, out_init=rp2.PIO.OUT_LOW,
autopull=True, autopush=True, in_shiftdir=rp2.PIO.SHIFT_RIGHT)
def sync_and_read():
out(y, 32) # Read the expected sync word to Y, once only
wrap_target()
label("find_sync")
mov(isr, x) # force X value back into ISR, clears counter
irq(block, 5) # wait for input, databit ready
# Note: the following sample is from previous bit
in_(pins, 2) # Double clock input (ie duplicate bits)
mov(x, isr)[10]
jmp(x_not_y, "find_sync")
set(pins, 0)
set(x, 31)[8] # Read in the next 32 bits
mov(isr, null) # clear ISR
irq(rel(0)) # set IRQ for rx_ticks_us monitoring
set(pins, 0) # signal 'data section' start
label("next_bit")
in_(pins, 1) # 1st should be 32 cycles after last IRQ
jmp(x_dec, "next_bit")[30]
set(x, 30) # Read in the next 31 bits
label("next_bit2")
in_(pins, 1)
jmp(x_dec, "next_bit2")[30]
set(pins, 1)
in_(pins, 1) # Read last bit
wrap()
# 'SM_TX_RAW' State Machine
@rp2.asm_pio(autopull=True, autopush=True)
def tx_raw_value():
wrap_target()
out(x, 32) # will 'block' waiting for TX timecode
in_(x, 32) # and then write back into RX FIFO
wrap()
# Purge the TX-FIFOs
@rp2.asm_pio(autopull=True,
fifo_join=rp2.PIO.JOIN_TX, out_shiftdir=rp2.PIO.SHIFT_RIGHT)
def tx_fifo_purge():
wrap_target()
out(null, 1)
wrap()
#-------------------------------------------------------
# handler for IRQs
def irq_handler(m):
global eng, stop
global tx_raw, rx_ticks_us, tx_ticks_us
global core_dis
global quarters, _hasUsbDevice
core_dis[mem32[0xd0000000]] = disable_irq()
ticks = ticks_us()
if m==eng.sm[SM_BLINK]:
if quarters==0 and eng.sm[SM_TX_RAW].rx_fifo():
# only read RX FIFO every 4th interrupt
tx_raw = eng.sm[SM_TX_RAW].get()
if _hasUsbDevice:
quarters += 1
if quarters==4:
quarters = 0
tx_ticks_us = ticks
if m==eng.sm[SM_SYNC]:
rx_ticks_us = ticks
if m==eng.sm[SM_BUFFER]:
# Buffer Underflow
stop = 1
eng.mode = HALTED
# check/schedule any registered callbacks
for i in range(len(eng.sm)):
if irq_callbacks[i] and m==eng.sm[i]:
schedule(irq_callbacks[i], i)
'''
# prevent "Uncaught exception in IRQ callback handler"
# which I believe is due to code overloading the CPU,
# but this will invalidate the actual timecode...
try:
schedule(irq_callbacks[i], i)
except:
pass
'''
enable_irq(core_dis[mem32[0xd0000000]])
def timer_sched(timer):
schedule(timer_re_init, timer)
def timer_re_init(timer):
global timer1, timer2, timer3, tlock
global eng
# do not re-init if engine is stopped
if eng.is_stopped():
return
# timer1 means we are dithering between two values
if timer == timer1:
if eng.calval > 0:
eng.dec_divider()
else:
eng.inc_divider()
return
if timer == timer2:
eng.calval = eng.next_calval
eng.config_clocks(eng.tc.fps, eng.calval)
# are we dithering between two clock values?
part = int(eng.period * (abs(eng.calval) % 1))
tlock.acquire()
timer1.deinit()
timer2.deinit()
timer3.deinit()
if part == 0:
# timers are no longer needed
eng.timers = False
tlock.release()
return
if True: #try:
timer3.init(period=eng.period + 2000, mode=Timer.ONE_SHOT, callback=timer_sched)
timer2.init(period=eng.period, mode=Timer.ONE_SHOT, callback=timer_sched)
timer1.init(period=eng.period - part, mode=Timer.ONE_SHOT, callback=timer_sched)
'''
except:
# restart whole timer system
timer = timer3
'''
tlock.release()
if timer == timer3:
# This should NEVER occur, it means previous timers were missed.
print("!!!!")
# restart whole timer system
tlock.acquire()
timer1.deinit()
timer2.deinit()
timer3.deinit()
tlock.release()
eng.timers = False
schedule(eng.micro_adjust, eng.next_calval)
#---------------------------------------------
# https://web.archive.org/web/20240000000000*/http://www.barney-wol.net/time/timecode.html
# lookup this text in array, index is value used in TC
tzs = [ \
"+0000","-0100","-0200","-0300","-0400","-0500","-0600","-0700","-0800","-0900", \
"-0030","-0130","-0230","-0330","-0430","-0530", \
"-1000","-1100","-1200","+1300","+1200","+1100","+1000","+0900","+0800","+0700", \
"-0630","-0730","-0830","-0930","-1030","-1130", \
"+0600","+0500","+0400","+0300","+0200","+0100","Undef","Undef","TP-03","TP-02", \
"+1130","+1030","+0930","+0830","+0730","+0630", \
"TP-01","TP-00","+1245","Undef","Undef","Undef","Undef","Undef","+XXXX","Undef", \
"+0530","+0430","+0330","+0230","+0130","+0030"]
class timecode(object):
def __init__(self):
self.fps = 30.0
self.df = False # Drop-Frame
# Timecode - starting value
self.hh = 0
self.mm = 0
self.ss = 0
self.ff = 0
# Colour Frame flag
self.cf = False
# Clock flag
self.bgf1 = False
# User bits - format depends on BF2 and BF0
self.bgf0 = True # 4 ASCII characters
self.bgf2 = False
self.uf1 = 0x0 # 'PICO'
self.uf2 = 0x5
self.uf3 = 0x9
self.uf4 = 0x4
self.uf5 = 0x3
self.uf6 = 0x4
self.uf7 = 0xF
self.uf8 = 0x4
# Lock for multithreading
self.lock = _thread.allocate_lock()
def acquire(self):
self.lock.acquire()
def release(self):
self.lock.release()
def validate_for_drop_frame(self, reverse=False):
self.acquire()
if not reverse and self.df and self.ss == 0 and \
(self.ff == 0 or self.ff == 1):
if self.mm % 10 != 0:
self.ff += (2 - self.ff)
if reverse and self.df and self.ss == 0 and \
(self.ff == 0 or self.ff == 1):
if self.mm % 10 != 0:
if self.hh == 0:
self.hh = 23
else:
self.hh -= 1
self.mm = 59 # only happens on mm==0
self.ss = 59
self.ff = int(self.fps + 0.1) - 1
self.release()
def from_ascii(self, start="00:00:00:00", sep=True):
# Example "00:00:00:00"
# hh mm ss ff
# 01234567890
# convert ASCII to 'raw' BCD array
time = [x - 0x30 for x in bytes(start, "utf-8")]
self.acquire()
if sep == True:
# only change DF if separators are given
self.df = False
self.hh = (time[0]*10) + time[1]
self.mm = (time[3]*10) + time[4]
self.ss = (time[6]*10) + time[7]
self.ff = (time[9]*10) + time[10]
if time[8] != 10:
self.df = True
else:
self.hh = (time[0]*10) + time[1]
self.mm = (time[2]*10) + time[3]
self.ss = (time[4]*10) + time[5]
self.ff = (time[6]*10) + time[7]
self.release()
if self.df:
self.validate_for_drop_frame()
def to_ascii(self, sep=True):
self.acquire()
if sep == True:
time = [int(self.hh/10), (self.hh % 10), 10,
int(self.mm/10), (self.mm % 10), 10,
int(self.ss/10), (self.ss % 10),
(-2 if self.df == True else 10), # use '.' for DF
int(self.ff/10), (self.ff % 10)]
else:
time = [int(self.hh/10), (self.hh % 10),
int(self.mm/10), (self.mm % 10),
int(self.ss/10), (self.ss % 10),
int(self.ff/10), (self.ff % 10)]
self.release()
new = ""
for x in time:
new += chr(x + 0x30)
return(new)
def from_raw(self, raw=0):
self.acquire()
self.df = (raw & 0x00000080) >> 7
self.hh = (raw & 0x1F000000) >> 24
self.mm = (raw & 0x003F0000) >> 16
self.ss = (raw & 0x00003F00) >> 8
self.ff = (raw & 0x0000001F)
self.release()
def to_raw(self):
self.acquire()
raw = (self.df << 7) + (self.hh << 24) + (self.mm << 16) + (self.ss << 8) + self.ff
self.release()
return raw
def set_fps_df(self, fps=25.0, df=False):
# should probably validate FPS/DF combo
self.acquire()
self.fps = fps
self.df = df
self.release()
if self.df:
self.validate_for_drop_frame()
return True
def next_frame(self, repeats=1):
while repeats:
repeats -= 1
self.acquire()
self.ff += 1
if self.ff >= int(self.fps + 0.1):
self.ff = 0
self.ss += 1
if self.ss >= 60:
self.ss = 0
self.mm += 1
if self.mm >= 60:
self.mm = 0
self.hh += 1
if self.hh >= 24:
self.hh = 0
self.release()
if self.df:
self.validate_for_drop_frame()
def prev_frame(self, repeats=1):
while repeats:
repeats -= 1
self.acquire()
self.ff -= 1
if self.ff < 0:
self.ff = int(self.fps + 0.1) - 1
self.ss -= 1
if self.ss < 0:
self.ss = 59
self.mm -= 1
if self.mm < 0:
self.mm = 59
self.hh -= 1
if self.hh < 0:
self.hh = 23
self.release()
if self.df:
self.validate_for_drop_frame(True)
# parity check, count 1's in 32-bit word
def lp(self, b):
c = 0
for i in range(32):
c += (b >> i) & 1
return(c)
def to_ltc_packet(self, send_sync=False, release=True):
f27 = False
f43 = False
f59 = False
self.acquire()
if self.fps == 25.0:
f27 = self.bgf0
f43 = self.bgf2
else:
f43 = self.bgf0
f59 = self.bgf2
p = []
p.append((self.uf2 << 12) + (self.cf << 11) + (self.df << 10) +
((int(self.ff/10) & 0x3) << 8) +
(self.uf1 << 4) + (self.ff % 10) +
(self.uf4 << 28) + (f27 << 27) +
((int(self.ss/10) & 0x7) << 24) +
(self.uf3 << 20) + ((self.ss % 10) << 16))
p.append((self.uf6 << 12) + (f43 << 11) +
((int(self.mm/10) & 0x7) << 8) +
(self.uf5 << 4) + (self.mm % 10) +
(self.uf8 << 28) + (f59 << 27) + (self.bgf1 << 26) +
((int(self.hh/10) & 0x3) << 24) +
(self.uf7 << 20) + ((self.hh % 10) << 16))
# polarity correction
count = 13
for i in p:
count += self.lp(i)
if count & 1:
if self.fps == 25.0:
p[1] += (True << 27) # f59
else:
p[0] += (True << 27) # f27
if release:
self.release()
if send_sync:
# We want to send 'whole' 32bit words to FIFO, so add 2x Sync
s = []
s.append(((p[0] & 0x0000FFFF) << 16) + 0xBFFC)
s.append(((p[1] & 0x0000FFFF) << 16) + ((p[0] & 0xFFFF0000) >> 16))
s.append((0xBFFC << 16) + ((p[1] & 0xFFFF0000) >> 16))
return s
else:
return p
def from_ltc_packet(self, p, acquire=True):
if len(p) != 2:
if not acquire:
# assume previously aquired
self.release()
return False
# reject if parity is not 1, note we are not including Sync word
'''
c = self.lp(p[0])
c+= self.lp(p[1])
if not c & 1:
return False
'''
if acquire:
self.acquire()
self.df = ((p[0] >> 10) & 0x01)
self.ff = (((p[0] >> 8) & 0x3) * 10) + (p[0] & 0xF)
self.ss = (((p[0] >> 24) & 0x7) * 10) + ((p[0] >> 16) & 0xF)
self.mm = (((p[1] >> 8) & 0x7) * 10) + (p[1] & 0xF)
self.hh = (((p[1] >> 24) & 0x3) * 10) + ((p[1] >> 16) & 0xF)
if self.fps == 25.0:
self.bgf0 = (p[0] >> 27) & 0x01 # f27
self.bgf2 = (p[1] >> 11) & 0x01 # f43
else:
self.bgf0 = (p[1] >> 11) & 0x01 # f43
self.bgf2 = (p[1] >> 27) & 0x01 # f59
self.bgf1 = (p[1] >> 26) & 0x01
self.uf1 = ((p[0] >> 4) & 0x0F)
self.uf2 = ((p[0] >> 12) & 0x0F)
self.uf3 = ((p[0] >> 20) & 0x0F)
self.uf4 = ((p[0] >> 28) & 0x0F)
self.uf5 = ((p[1] >> 4) & 0x0F)
self.uf6 = ((p[1] >> 12) & 0x0F)
self.uf7 = ((p[1] >> 20) & 0x0F)
self.uf8 = ((p[1] >> 28) & 0x0F)
self.release()
if self.ff >= int(self.fps):
return False
return True
def user_to_ascii(self):
new = ""
if self.bgf1==True:
# TC is referenced to real time
new += "*"
if self.bgf0==True and self.bgf2==True:
return("Page/Line NA")
self.acquire()
if self.bgf0==False and self.bgf2==False:
# Userbits are BCD/Hex
dehex = [0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37, \
0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46]
user = [dehex[self.uf8], dehex[self.uf7], \
dehex[self.uf6], dehex[self.uf5], \
dehex[self.uf4], dehex[self.uf3], \
dehex[self.uf2], dehex[self.uf1]]
elif self.bgf0==False and self.bgf2==True:
# Userbits are Date/Timezone
user = [0x59, 0x30+self.uf6, 0x30+self.uf5, 0x2D, \
0x4D, 0x30+self.uf4, 0x30+self.uf3, 0x2D, \
0x44, 0x30+self.uf2, 0x30+self.uf1]
else:
# Userbits are ASCII
user = [(self.uf2 << 4) + self.uf1,
(self.uf4 << 4) + self.uf3,
(self.uf6 << 4) + self.uf5,
(self.uf8 << 4) + self.uf7]
for x in user:
new += chr(x)
if self.bgf0==False and self.bgf2==True:
i = (self.uf8 << 4) + self.uf7
if i < len(tzs):
new += tzs[i]
else:
new += tzs[0]
self.release()
return(new)
def user_from_ascii(self, asc="PICO"):
user = [x for x in bytes(asc+" ", "utf-8")]
self.acquire()
self.bgf0 = True
self.bgf2 = False
self.uf1 = (user[0] >> 0) & 0x0F
self.uf2 = (user[0] >> 4) & 0x0F
self.uf3 = (user[1] >> 0) & 0x0F
self.uf4 = (user[1] >> 4) & 0x0F
self.uf5 = (user[2] >> 0) & 0x0F
self.uf6 = (user[2] >> 4) & 0x0F
self.uf7 = (user[3] >> 0) & 0x0F
self.uf8 = (user[3] >> 4) & 0x0F
self.release()
return True
def user_from_bcd_hex(self, bcd="00000000"):
user = []
for x in bytes(bcd + "00000000", "utf-8"):
if (x >= 0x30) and (x < 0x3A):
user.append(x - 0x30)
if (x >= 0x41) and (x < 0x47):
user.append(x - 0x37)
if (x >= 0x61) and (x < 0x67):
user.append(x - 0x57)
self.acquire()
self.bgf0 = False
self.bgf2 = False
self.uf1 = (user[7] & 0x0F)
self.uf2 = (user[6] & 0x0F)
self.uf3 = (user[5] & 0x0F)
self.uf4 = (user[4] & 0x0F)
self.uf5 = (user[3] & 0x0F)
self.uf6 = (user[2] & 0x0F)
self.uf7 = (user[1] & 0x0F)
self.uf8 = (user[0] & 0x0F)
self.release()
return True
def user_from_date(self, date="Y74-M01-D01+0000"):
# Example "Y00-M00-D00+0000"
# Yyy Mmm Dddzzzzz
# 0123456789012345
user = [x-0x30 for x in bytes(date, "utf-8")]
self.acquire()
self.bgf0 = False
self.bgf2 = True
self.uf1 = user[10] # DD
self.uf2 = user[9]
self.uf3 = user[6] # MM
self.uf4 = user[5]
self.uf5 = user[2] # YY
self.uf6 = user[1]
self.uf7 = 0
self.uf8 = 0
for i in range(len(tzs)):
if date[11:] == tzs[i]:
self.uf7 = i & 0x0F
self.uf8 = (i>>4) & 0xFF
break
self.release()
return True
#---------------------------------------------
class engine(object):
def __init__(self):
self.mode = RUN
self.flashframe = 0
self.flashtime = 0 # 'raw' TC
self.dlock = _thread.allocate_lock()
self.tc = timecode()
self.rc = timecode()
self.sm = None
self.calval = 0
self.next_calval = 0
self.period = 10000 # 10s, can be update by client
self.timers = False
# state of running (ie whether being used for output)
self.stopped = True
self.powersave = False
self.ps_en0 = 0
self.ps_en1 = 0
def is_stopped(self):
return self.stopped
def is_running(self):
return not self.stopped
def set_stopped(self, s=True):
global stop
self.stopped = s
if s:
self.powersave = False
self.tc.bgf1 = False
stop = False
self.asserted = False
self.calval = 0
self.next_calval = 0
def set_powersave(self, p=True, ps_en0=0, ps_en1=0):
self.ps_en0 = ps_en0
self.ps_en1 = ps_en1
self.powersave = p
def get_powersave(self):
return self.powersave
def config_clocks(self, fps, calval=0):
if calval == 0:
calval = self.calval
# optimal divider computed for CPU clock at 180MHz
if fps == 30.00:
new_div = 0x0927c000
elif fps == 29.97:
new_div = 0x092a1800
elif fps == 25.00:
new_div = 0x0afc8000
elif fps == 24.98:
new_div = 0x0aff5000
elif fps == 24.00:
new_div = 0x0b71b000
elif fps == 23.98:
new_div = 0x0b749e00
else:
return
# apply divider offset, from calibration value
new_div -= int(calval) << 8
self.write_divider(new_div)
def write_divider(self, new_div):
# Set dividers for all PIO machines
self.dlock.acquire()
'''
for base in [0x50200000, 0x50300000]:
for offset in [0x0c8, 0x0e0, 0x0f8, 0x110]:
mem32[base + offset] = new_div
'''
mem32[0x502000c8] = new_div
mem32[0x502000e0] = new_div
mem32[0x502000f8] = new_div
mem32[0x50200110] = new_div
mem32[0x503000c8] = new_div
mem32[0x503000e0] = new_div
mem32[0x503000f8] = new_div
mem32[0x50300110] = new_div
self.dlock.release()
def inc_divider(self):
# increasing divider -> slower clock
self.write_divider(mem32[0x502000c8] + 0x0100)
def dec_divider(self):
# decreasing divider -> faster clock
self.write_divider(mem32[0x502000c8] - 0x0100)
def micro_adjust(self, calval, period=0):
global timer1, timer2, timer3, tlock
if self.stopped:
tlock.acquire()
timer1.deinit()
timer2.deinit()
timer3.deinit()
tlock.release()
self.timers = False
return
self.next_calval = calval
if period > 0:
# in ms, only change if specified
self.period = period
if not self.timers:
self.calval = calval
self.timers = True
# Force Garbage collection
#print("Available memory (bytes):", mem_free())
collect()
#print(mem_info())
# trigger re-init, as if timer2 had completed
timer_re_init(timer2)
def set_flashtime(self, ft):