Receiver interface for the fischertechnik IR control set

Dependencies:   NeedfulThings

Receiver interface for the fischertechnik IR control set.

An mbed port of the code found on this ft community WIKI page The ft IR remote control uses some kind of RC-MM Protocol . When any of the controls is applied the remote control sends updates at a rate of at 1/120ms or 1/90ms depending on the senders frequency setting. Each message delivers the complete remote control status encoded into 30 bits. The structure of the message can be seen in FtControlSetMessage.h.

I assume that the ft control set works fine with standard 38kHz IR detectors. I have used a CHQ0038 from a broken DVD Player. It can be connected directly to the mbed: GND, VCC->3.3V and the signal line to any of the numbered mbed gpios.

The PDM timing of the ft IR control set isn't that exact. Thus receive errors occur quite frequently. I am observing an error rate somewhere between 3% and 5%. This driver "ignores" up to 3 consecutive receive errors, i.e. it just indicates an error but still provides the last correctly received message, before it resets the message to zero after the fourth error.

Revision:
4:c1517188f6ec
Parent:
3:038f86bac6cc
Child:
5:7ce37fae13ff
--- a/FtControlSetReceiver.cpp	Wed Mar 20 21:55:20 2013 +0000
+++ b/FtControlSetReceiver.cpp	Sat Mar 23 21:51:13 2013 +0000
@@ -2,8 +2,10 @@
 
 #include "us_ticker_api.h"
 
-const uint32_t FtControlSetReceiver::c_messageTimeout[2] = {89000+20*c_maxPulseTime,118000+20*c_maxPulseTime}; 
-//const uint32_t FtControlSetReceiver::c_pulseWidthLimits[5] = {740,860,940,1055,1150};
+/// message timeout depends on selected sender frequency
+const uint32_t FtControlSetReceiver::c_messageTimeout[2] = {89000+20*c_maxPulseTime,118000+20*c_maxPulseTime};
+/// di-bit discrimination times
+const uint32_t FtControlSetReceiver::c_pulseWidthLimits[5] = {740,860,940,1055,1150};
 
 FtControlSetReceiver::FtControlSetReceiver(PinName in):
     m_pulseIRQ(in),
@@ -17,108 +19,105 @@
 {
     m_pulseIRQ.mode(PullNone); // the CHQ0038 does't like the default pull down
     m_pulseIRQ.fall(this, &FtControlSetReceiver::pulseISR); // we are measuring the distances of falling edges
+
+    // just attch the callbacks here
+    m_messageTimeout.attach_us(this,&FtControlSetReceiver::messageTimeoutISR, 1e9);
+    m_messageTimeout.remove();
+    m_receiveTimeout.attach_us(this,&FtControlSetReceiver::handleReceiveError, 1e9);
+    m_receiveTimeout.remove();
+    m_recoverTimeout.attach_us(this,&FtControlSetReceiver::recoverISR, 1e9);
+    m_recoverTimeout.remove();
 }
 
+/// called on each falling edge on the IR receivers signal line
 void FtControlSetReceiver::pulseISR()
 {
-    // needs 4-8µs
-    uint32_t dt=us_ticker_read();
+    // takes 1µs-5µs, 4.1µs on average
     if(m_nPulses) {
+        // received a messge pulse
         uint32_t now = us_ticker_read();
-        m_receiveTimeout.detach();
-        uint32_t diBit = now - m_timeOfLastEdge;
+        m_receiveTimeout.remove();
+        uint32_t width = now - m_timeOfLastEdge;
         m_timeOfLastEdge = now;
-        diBit -= c_pulseWidthOffset-c_pulseWidthDelta/2;
-        diBit /= c_pulseWidthDelta;
-        if(diBit<4) {
-            /*
-            uint32_t w=diBit;
-            if(w>c_pulseWidthLimits[0] && w<c_pulseWidthLimits[4]){
-                if(w<c_pulseWidthLimits[1])
-                    diBit=0;
-                else if(w<c_pulseWidthLimits[2])
-                    diBit=1;
-                else if(w<c_pulseWidthLimits[3])
-                    diBit=2;
-                else
-                    diBit=3;
-            */
-            m_rxBuffer|=diBit;
+        if(width>=c_pulseWidthLimits[0] && width<c_pulseWidthLimits[4]) {
+            if(width<c_pulseWidthLimits[1]){
+                // m_rxBuffer|=0x0;
+            }else if(width<c_pulseWidthLimits[2]) {
+                m_rxBuffer|=0x1;
+                ++m_nOnes;
+            } else if(width<c_pulseWidthLimits[3]) {
+                m_rxBuffer|=0x2;
+                ++m_nOnes;
+            } else {
+                m_rxBuffer|=0x3;
+            }
             m_rxBuffer<<=2;
-            const uint8_t ones[4]= {0,1,1,2};
-            m_nOnes+=ones[diBit];
             if(++m_nPulses<=15) {
-                // still receiving re-attach the pulse timeout 
-                m_receiveTimeout.attach_us(this,&FtControlSetReceiver::handleReceiveError, c_maxPulseTime);
+                // still receiving re-attach the pulse timeout
+                m_receiveTimeout.insert(m_timeOfLastEdge+c_maxPulseTime);
             } else {
-                // we got a full message
+                // we have got a full message
+                FtControlSetMessage::Status status;
                 if(m_nOnes&1) { // message ones + parity one has to be even
-                    messageReceived(FtControlSetMessage::ParityKO);
+                    status = FtControlSetMessage::ParityKO;
                 } else if(m_rxBuffer>>28!=8 || m_rxBuffer&4) { // check header=8 and tail=0
-                    messageReceived(FtControlSetMessage::FrameKO);
+                    status = FtControlSetMessage::FrameKO;
                 } else { // happyhappyjoyjoy
-                    messageReceived(FtControlSetMessage::OK);
+                    status = FtControlSetMessage::OK;
                 }
+                // detach and reattach the message timeout
+                m_messageTimeout.remove();
+                m_messageTimeout.insert(m_timeOfLastEdge+c_messageTimeout[1]);//just take the longer timeout here, otherwise we run into problems
+                if(status == FtControlSetMessage::OK) {
+                    m_nGracefullyIgnoredErrors = 0;
+                    m_lastMessage = m_rxBuffer;
+                } else {
+                    if(++m_nGracefullyIgnoredErrors <= c_errorsToBeGracefullyIgnored) {
+                        m_lastMessage &= ~0x3; // just indicate the error but keep the old message
+                        m_lastMessage |= status;
+                    } else {
+                        m_lastMessage = status; // reset the message and indicate the error
+                    }
+                    ++m_errorCount;
+                }
+                m_callback.call();
+                m_rxBuffer=0;
+                m_nPulses=0;
+                m_nOnes=0;
             }
         } else {
-            // receive errors are not reported
-            // In case of repeated receive errors the message timeout will trigger and reset the data 
             handleReceiveError();
         }
     } else {
         // the very first edge of a potential message, just memorize time stamp
         m_timeOfLastEdge = us_ticker_read();
-        m_receiveTimeout.attach_us(this,&FtControlSetReceiver::handleReceiveError, c_maxPulseTime);
+        m_receiveTimeout.insert(m_timeOfLastEdge+c_maxPulseTime);
         ++m_nPulses;
     }
 }
 
-void FtControlSetReceiver::messageReceived(FtControlSetMessage::Status status)
-{
-    // detach and reattach the message timeout
-    m_messageTimeout.detach();
-    m_messageTimeout.attach_us(this,
-                               &FtControlSetReceiver::messageTimeoutISR,
-                               //c_messageTimeout[m_lastMessage.getSwitch1()]);
-                               c_messageTimeout[1]); //just take the longer timeout here, otherwise we run into problems
-    // check for errors and copy the result and the message data to m_lastMessage 
-    if(status == FtControlSetMessage::OK) {
-        m_nGracefullyIgnoredErrors = 0;
-        m_lastMessage = m_rxBuffer;
-    } else {
-        if(++m_nGracefullyIgnoredErrors <= c_errorsToBeGracefullyIgnored) {
-            m_lastMessage = m_lastMessage.getRawMessage() | status; // just indicate the error but keep the old message
-        } else {
-            m_lastMessage = status; // reset the message and indicate the error
-        }
-        ++m_errorCount;
-    }
-    m_callBack.call();
-    m_rxBuffer=0;
-    m_nPulses=0;
-    m_nOnes=0;
-}
-
 void FtControlSetReceiver::messageTimeoutISR()
 {
     m_lastMessage = 0;
-    m_callBack.call();
+    m_callback.call();
 }
 
+// called on errors detected in pulseISR or on pulse timeouts
 void FtControlSetReceiver::handleReceiveError()
 {
-    // don't reset the message here, it might have been just a disturbance between two messages
+    // receive errors are not reported via the status bits and the message is not reset here,
+    // because it might have been just a disturbance between two messages.
     // On severe transmission problems the message timeout will bail us out
     ++m_errorCount;
+    //m_errorCount+=10000;
     m_rxBuffer=0;
     m_nPulses=0;
     m_nOnes=0;
     // to be graceful in case of high frequency noise disconnect the ISR for some time
     m_pulseIRQ.fall(0);
-    m_receiveTimeout.attach_us(this,&FtControlSetReceiver::recoverISR, c_recoverTimeout);
+    m_recoverTimeout.insert(us_ticker_read()+c_recoverTimeout);
 }
 
-
 void FtControlSetReceiver::recoverISR()
 {
     // reattach the pulse handler