SMPTE timedode (LTC) decode library for mbed

Dependents:   LTC_SMPTE_sample

SMPTE timedode (LTC) decode library

SMPTEタイムコードをデコード(受信)・エンコード(送信)するライブラリです。

平衡/不平衡/サウンド等によるLTC信号は、適当な回路で整形して入力してください。
出力は適当なドライバ回路を設けてください。

簡易的なプログラムのため、細かいフラグなどは無視しています。

LPC1768 専用、Timer 2 を占有します。

Sample

Import programLTC_SMPTE_sample

SMPTE timedode (LTC) decode library for mbed https://developer.mbed.org/users/okini3939/code/LTC_SMPTE/

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Mon May 14 08:50:39 2018 +0000
Parent:
1:63ceee4bfd05
Commit message:
Supported 24, 25fps, Fix algorithm

Changed in this revision

LTC_SMPTE.cpp Show annotated file Show diff for this revision Revisions of this file
LTC_SMPTE.h Show annotated file Show diff for this revision Revisions of this file
diff -r 63ceee4bfd05 -r 13a89fffbb75 LTC_SMPTE.cpp
--- a/LTC_SMPTE.cpp	Fri Apr 13 04:05:13 2018 +0000
+++ b/LTC_SMPTE.cpp	Mon May 14 08:50:39 2018 +0000
@@ -15,10 +15,14 @@
 
 // pulse width: 416.7us(30fps)
 // 80bit x 30frame/s --> 416.7us/bit
-#define ONE_TIME_MIN  100
-#define ONE_TIME_MAX  250
-#define ZERO_TIME_MIN 300
-#define ZERO_TIME_MAX 500
+#define ONE_TIME_MIN  180
+#define ONE_TIME_MAX  280
+#define ZERO_TIME_MIN 390
+#define ZERO_TIME_MAX 540
+
+#define FPS24_REF 521
+#define FPS25_REF 500
+#define FPS30_REF 417
 
 #define US_TICKER_TIMER      ((LPC_TIM_TypeDef *)LPC_TIM2_BASE)
 #define US_TICKER_TIMER_IRQn TIMER2_IRQn
@@ -80,61 +84,31 @@
     if (t >= ZERO_TIME_MIN && t < ZERO_TIME_MAX) {
         oneflg = 0;
         b = 0;
+
+        if (t >= FPS24_REF - 10 && t <= FPS24_REF + 10) {
+            fps = 0;
+        } else
+        if (t >= FPS25_REF - 10 && t <= FPS25_REF + 10) {
+            fps = 1;
+        } else
+        if (t >= FPS30_REF - 10 && t <= FPS30_REF + 10) {
+            fps = drop ? 2 : 3; // 29.97 / 30
+        }
     } else {
+        oneflg = 0;
+        count = 0;
         return;
     }
 
-    if (mode == 0) {
-        // sync word
-        if (b) {
-            // 1
-            if (count == 12) {
-                // error
-                count = 0;
-            } else
-            if (count == 13) {
-                // fwd
-                direction = 1;
-                count = 0;
-                mode = 1;
-                bit = 1;
-            } else {
-                count ++;
-            }
-        } else {
-            // 0
-            if (count == 12) {
-                count ++;
-            } else
-            if (count == 13) {
-                // rev
-                direction = -1;
-                count = 0;
-                mode = 1;
-                bit = 1;
-            } else {
-                // error
-                count = 0;
-            }
-        }
+    for (int i = 0; i < 9; i ++) {
+        code[i] = (code[i] >> 1) | ((code[i + 1] & 1) ? 0x80 : 0);
+    }
+    code[9] = (code[9] >> 1) | (b ? 0x80 : 0);
+    count ++;
 
-    } else {
-        // time code
-        if (b) {
-            code[count] |= bit; // 1
-        } else {
-            code[count] &= ~bit; // 0
-        }
-        bit <<= 1;
-        if (bit >= 0x100) {
-            bit = 1;
-            count ++;
-            if (count >= 8) {
-                count = 0;
-                mode = 0;
-                parse_code();
-            }
-        }
+    if (code[8] == 0xfc && code[9] == 0xbf && count >= 80) {
+        parse_code();
+        count = 0;
     }
 }
 
@@ -148,7 +122,7 @@
 }
 
 void LTC_SMPTE::read (int *hour, int *min, int *sec, int *frame, int *dir) {
-    *hour = this->hour;
+    *hour = this->hour | (this->fps << 5);
     *min = this->min;
     *sec = this->sec;
     *frame = this->frame;
diff -r 63ceee4bfd05 -r 13a89fffbb75 LTC_SMPTE.h
--- a/LTC_SMPTE.h	Fri Apr 13 04:05:13 2018 +0000
+++ b/LTC_SMPTE.h	Mon May 14 08:50:39 2018 +0000
@@ -40,7 +40,7 @@
     int bit;
     char code[10];
     int hour, min, sec, frame;
-    int drop, direction;
+    int drop, direction, fps;
     int received;
 
     enum LTC_TYPE type;