RS232 control for TVOne products

Dependents:   SPK-DVIMXR

Revision:
6:767acf32fed5
Parent:
5:4b0bf9a724a4
--- a/spk_tvone_mbed.cpp	Sun Oct 07 12:47:24 2012 +0000
+++ b/spk_tvone_mbed.cpp	Sun Oct 07 23:13:42 2012 +0000
@@ -46,16 +46,31 @@
   if (debug) debug->printf("TVOne command: IN. ");
   
   char i;
+  Timer timer;
   
   // TASK: Sign start of serial command write
   if (writeDO) *writeDO = 1;
 
-  // TASK: Clear buffer.
-  while (serial->readable())
+  // TASK: Clear read buffer, and make sure we're not still receiving.
+
+  if (serial->readable())
   {
-        serial->getc();
+    timer.start();
+    if (debug) debug->printf("Serial incoming: ");
+    while (timer.read_ms() < 30)
+    {
+        if (serial->readable())
+        {
+            if (debug) debug->printf("%c", (char)serial->getc());
+            else serial->getc();
+            timer.reset();
+        }
+    }
+    if (debug) debug->printf("\r\n");
+    timer.stop();
+    timer.reset();
   }
-  
+
   // TASK: Create the bytes of command
 
   uint8_t cmd[8];
@@ -84,9 +99,18 @@
   {
     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);
   
+  // MBED library sub-par: printf will hang on rx interrupt, have to sprintf and loop putc instead.
+  // FFS. Still not fixed. 
+  char buffer[21];
+  sprintf(buffer,"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);
+  for (i = 0; i < 20; i++)
+  {
+    __disable_irq();
+    serial->putc(buffer[i]);
+    __enable_irq();
+  }
+    
   // 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.
@@ -100,8 +124,8 @@
   int clearPeriod = 30;
   bool ackReceived = false;
   bool success = false;
-  Timer timer;
 
+  timer.reset();
   timer.start();
   i = 0;
   while (timer.read_ms() < safePeriod) {
@@ -139,14 +163,25 @@
   };
 
   if (debug) debug->printf("OUT. Success = %s \r\n", success ? "true" : "false"); 
+  
   return success;
 }
 
 void SPKTVOne::setCustomResolutions() 
 {
+  int32_t unlock = 0;
+  int32_t lock = 1;
+  
+  // Lock front panel
+  command(0, 0, kTV1FunctionAdjustFrontPanelLock, lock);
+  
+  // Set resolutions
   set1920x480(kTV1ResolutionTripleHeadVGAp60);
   set1600x600(kTV1ResolutionDualHeadSVGAp60);
   set2048x768(kTV1ResolutionDualHeadXGAp60);
+  
+  // Unlock front panel
+  command(0, 0, kTV1FunctionAdjustFrontPanelLock, unlock);
 }
 
 bool SPKTVOne::setHDCPOn(bool state)