RS232 control for TVOne products

Dependents:   SPK-DVIMXR

Files at this revision

API Documentation at this revision

Comitter:
tobyspark
Date:
Mon Oct 08 13:44:17 2012 +0000
Parent:
9:42c83cac2f6d
Child:
11:90e5a72a0034
Commit message:
SPKTVOne.command can now read from processor as well as write to it.; Made ack code clearer, fixing buffer-overflow-mindrot clearTime snafu

Changed in this revision

spk_tvone.h Show annotated file Show diff for this revision Revisions of this file
spk_tvone_mbed.cpp Show annotated file Show diff for this revision Revisions of this file
spk_tvone_mbed.h Show annotated file Show diff for this revision Revisions of this file
--- a/spk_tvone.h	Mon Oct 08 03:06:00 2012 +0000
+++ b/spk_tvone.h	Mon Oct 08 13:44:17 2012 +0000
@@ -257,6 +257,9 @@
 // Adjust Misc
 //#pragma mark -
 #define kTV1FunctionAdjustFrontPanelLock            0xFC    // Values: 0 = Unlocked, 1 = Locked
+#define kTV1FunctionReadSoftwareVersion             0x0D2
+#define kTV1FunctionReadProductType                 0x0C4
+#define kTV1FunctionReadBoardType                   0x0C2
 
 // Resolutions
 //#pragma mark -
--- a/spk_tvone_mbed.cpp	Mon Oct 08 03:06:00 2012 +0000
+++ b/spk_tvone_mbed.cpp	Mon Oct 08 13:44:17 2012 +0000
@@ -41,7 +41,16 @@
     debug = debugSerial;
 }
 
-bool SPKTVOne::command(uint8_t channel, uint8_t window, int32_t func, int32_t payload) 
+bool SPKTVOne::command(uint8_t channel, uint8_t window, int32_t func, int32_t payload)
+{
+    int ackBuff[standardAckLength];
+    
+    bool success = command(writeCommand, ackBuff, standardAckLength, channel, window, func, payload);
+    
+    return success;
+}
+
+bool SPKTVOne::command(commandType readWrite, int* ackBuffer, int ackLength, uint8_t channel, uint8_t window, int32_t func, int32_t payload) 
 {
   char i;
   
@@ -59,7 +68,7 @@
   uint8_t checksum = 0;
 
   // CMD
-  cmd[0] = 1<<2; // write
+  cmd[0] = readWrite<<7 | 1<<2;
   // CHA
   cmd[1] = channel;
   // WINDOW
@@ -76,14 +85,18 @@
   cmd[7] = payload & 0xFF;
 
   // TASK: Write the bytes of command to RS232 as correctly packaged 20 characters of ASCII
-
-  for (i=0; i<8; i++) 
+  
+  if (readWrite == writeCommand)
   {
-    checksum += cmd[i];
+    for (i=0; i<8; i++) checksum += cmd[i];
+    serial->printf("F%02X%02X%02X%02X%02X%02X%02X%02X%02X\r", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6], cmd[7], checksum);
   }
-  
-  serial->printf("F%02X%02X%02X%02X%02X%02X%02X%02X%02X\r", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6], cmd[7], checksum);
-  
+  if (readWrite == readCommand)
+  {
+    for (i=0; i<5; i++) checksum += cmd[i];
+    serial->printf("F%02X%02X%02X%02X%02X%02X\r", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], checksum);
+  } 
+   
   // TASK: Check the unit's return string, to enable return to main program as soon as unit is ready
 
   // Handling the timing of this return is critical to effective control.
@@ -91,31 +104,35 @@
   // TVOne turn out to say that receipt of the ack doesn't guarantee the unit is ready for the next command. 
   // According to the manual, operations typically take 30ms, and to simplify programming you can throttle commands to every 100ms.
   // 100ms is too slow for us. Going with returning after 30ms if we've received an acknowledgement, returning after 100ms otherwise.
-
-  int ack[20];
-  int safePeriod = 100;
-  int clearPeriod = 30;
+  
+  const int safePeriod = 100;
+  const int clearPeriod = 30;
+  
   bool ackReceived = false;
   bool success = false;
   Timer timer;
 
+  i = 0;
   timer.start();
-  i = 0;
-  while (timer.read_ms() < safePeriod) {
+  while (timer.read_ms() < safePeriod) 
+  {
+    if (ackReceived && timer.read_ms() > clearPeriod) 
+    {
+        break;
+    }
     if (!ackReceived && serial->readable())
+    {
+        ackBuffer[i] = serial->getc();
+        i++;
+        if (i == ackLength) 
         {
-            ack[i] = serial->getc();
-            i++;
-            if (i >= 20) 
+            ackReceived = true;
+            if (ackBuffer[0] == 'F' && ackBuffer[1] == '4') // TVOne start of message, acknowledgement with no error, rest will be repeat of sent command
             {
-                ackReceived = true;
-                if (ack[0] == 'F' && ack[1] == '4') // TVOne start of message, acknowledgement with no error, rest will be repeat of sent command
-                {
-                    success = true;
-                }
+                success = true;
             }
         }
-    if (timer.read_ms() > clearPeriod) break;
+    }
   }
   timer.stop();
   
--- a/spk_tvone_mbed.h	Mon Oct 08 03:06:00 2012 +0000
+++ b/spk_tvone_mbed.h	Mon Oct 08 13:44:17 2012 +0000
@@ -31,7 +31,11 @@
   public:
     SPKTVOne(PinName txPin, PinName rxPin, PinName signWritePin = NC, PinName signErrorPin = NC, Serial *debugSerial = NULL);
     
+    enum commandType {writeCommand = 0, readCommand = 1};
+    static const int standardAckLength = 20;
+    
     bool command(uint8_t channel, uint8_t window, int32_t func, int32_t payload);
+    bool command(commandType readWrite, int* ackBuffer, int ackLength, uint8_t channel, uint8_t window, int32_t func, int32_t payload);
     
     void setCustomResolutions();
     bool setHDCPOn(bool state);