changeset 12:faec23aecc74

* action for writing to serial port
author cannam
date Fri, 08 Dec 2006 18:18:28 +0000
parents e90e4261ab2d
children 47b0a143db39
files host/FDWriteAction.cpp host/FDWriteAction.h host/SimpleXMLRuleLoader.cpp vamp-live-host.pro
diffstat 4 files changed, 130 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/FDWriteAction.cpp	Fri Dec 08 18:18:28 2006 +0000
@@ -0,0 +1,85 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+#include "FDWriteAction.h"
+
+#include <iostream>
+
+#include <termios.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <QMutexLocker>
+
+std::map<QString, int>
+FDWriteAction::m_fdMap;
+
+QString
+FDWriteAction::m_previousData = "";
+
+FDWriteAction::FDWriteAction(QString file,
+                             QString data,
+                             bool togglePrevious) :
+    m_data(data),
+    m_togglePrevious(togglePrevious)
+{
+    QMutexLocker locker(&m_mutex);
+    if (m_fdMap.find(file) != m_fdMap.end()) {
+        m_fd = m_fdMap[file];
+    } else {
+        m_fd = open(file.toLocal8Bit().data(), O_WRONLY | O_NOCTTY);
+        if (m_fd < 0) {
+            perror("Failed to open output file");
+            std::cerr << "ERROR: FDWriteAction: Unable to open \""
+                      << file.toStdString() << "\" for writing" << std::endl;
+            return;
+        }
+        struct termios t;
+        tcgetattr(m_fd, &t);
+        t.c_cflag = B9600 | CS8 | CLOCAL;
+        t.c_iflag = 0;
+        t.c_oflag = 0;
+        t.c_lflag = ICANON;
+        tcflush(m_fd, TCOFLUSH);
+        tcsetattr(m_fd, TCSANOW, &t);
+        m_fdMap[file] = m_fd;
+    }
+}
+
+FDWriteAction::~FDWriteAction()
+{
+    // don't close fds -- lazily leave them for program exit rather
+    // than go to the bother of reference counting
+}
+
+QString
+FDWriteAction::getName() const
+{
+    return QString("fdwrite: \"%1\",%2")
+        .arg(m_data)
+        .arg(m_togglePrevious);
+}
+
+void
+FDWriteAction::fire()
+{
+    std::cerr << getName().toStdString() << ": fire" << std::endl;
+
+    if (m_fd < 0) return;
+    QMutexLocker locker(&m_mutex);
+    if (m_togglePrevious) {
+        if (m_previousData == m_data) return;
+        if (m_previousData != "") {
+            // bug here
+            write(m_fd, m_previousData.toLocal8Bit().data(), m_previousData.length());
+            usleep(100000);
+        }
+    }
+    // bug here
+    write(m_fd, m_data.toLocal8Bit().data(), m_data.length());
+    m_previousData = m_data;
+    usleep(100000);
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/FDWriteAction.h	Fri Dec 08 18:18:28 2006 +0000
@@ -0,0 +1,32 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+#ifndef _FD_WRITE_ACTION_H_
+#define _FD_WRITE_ACTION_H_
+
+#include "Action.h"
+
+#include <map>
+
+#include <QMutex>
+
+class FDWriteAction : public Action    
+{
+    Q_OBJECT
+
+public:
+    FDWriteAction(QString file, QString data, bool togglePrevious);
+    virtual ~FDWriteAction();
+
+    virtual QString getName() const;
+    virtual void fire();
+
+protected:
+    int m_fd;
+    QString m_data;
+    bool m_togglePrevious;
+    QMutex m_mutex;
+    static std::map<QString, int> m_fdMap;
+    static QString m_previousData;
+};
+
+#endif
--- a/host/SimpleXMLRuleLoader.cpp	Mon Nov 27 20:00:53 2006 +0000
+++ b/host/SimpleXMLRuleLoader.cpp	Fri Dec 08 18:18:28 2006 +0000
@@ -4,6 +4,7 @@
 #include "Rule.h"
 #include "ImageAction.h"
 #include "ExternalProcessAction.h"
+#include "FDWriteAction.h"
 
 #include <QFile>
 #include <QIODevice>
@@ -171,6 +172,16 @@
             Action *action = new ExternalProcessAction(process, args);
             m_rule->addAction(action);
             std::cerr << "INFO: SimpleXMLRuleLoader: Added external process action" << std::endl;
+        } else if (type == "fdwrite") {
+            QString file = atts.value("file");
+            QString data = atts.value("data");
+            bool togglePrevious = false;
+            if (atts.value("togglePrevious") == "true") {
+                togglePrevious = true;
+            }
+            Action *action = new FDWriteAction(file, data, togglePrevious);
+            m_rule->addAction(action);
+            std::cerr << "INFO: SimpleXMLRuleLoader: Added fd write action" << std::endl;
         } else {
             std::cerr << "WARNING: SimpleXMLRuleLoader: Unknown action type \""
                       << type.toStdString() << "\"" << std::endl;
--- a/vamp-live-host.pro	Mon Nov 27 20:00:53 2006 +0000
+++ b/vamp-live-host.pro	Fri Dec 08 18:18:28 2006 +0000
@@ -25,6 +25,7 @@
            audioio/BufferingAudioCallbackRecordTarget.h \
            host/Action.h \
            host/ExternalProcessAction.h \
+           host/FDWriteAction.h \
            host/ImageAction.h \
            host/ImageWindow.h \
            host/Processor.h \
@@ -37,6 +38,7 @@
            audioio/BufferingAudioCallbackRecordTarget.cpp \
            host/host.cpp \
            host/ExternalProcessAction.cpp \
+           host/FDWriteAction.cpp \
            host/ImageAction.cpp \
            host/ImageWindow.cpp \
            host/Processor.cpp \