📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 42行目: | 42行目: | ||
#include <QObject> | #include <QObject> | ||
#include < | #include <QFile> | ||
#include <QFuture> | #include <QFuture> | ||
#include <QtConcurrent> | #include <QtConcurrent> | ||
#include <QDebug> | #include <QDebug> | ||
#include <sys/ioctl.h> | |||
#include <linux/i2c-dev.h> | |||
#include <fcntl.h> | |||
#include <unistd.h> | |||
class I2CReader : public QObject | class I2CReader : public QObject | ||
| 52行目: | 56行目: | ||
private: | private: | ||
QFile *m_device; | |||
bool m_streamingActive = false; | |||
public: | public: | ||
explicit I2CReader(const QString &deviceName, QObject *parent = nullptr) : QObject(parent), m_device(new | explicit I2CReader(const QString &deviceName, QObject *parent = nullptr) : QObject(parent), m_device(new QFile(deviceName, this)) | ||
{ | { | ||
if (!m_device->open( | if (!m_device->open(QIODevice::ReadWrite)) { | ||
emit errorOccurred("デバイスのオープンに失敗: " + m_device->errorString()); | emit errorOccurred("デバイスのオープンに失敗: " + m_device->errorString()); | ||
} | } | ||
| 77行目: | 82行目: | ||
} | } | ||
m_device-> | // スレーブアドレスの設定 | ||
if (ioctl(m_device->handle(), I2C_SLAVE, address) < 0) { | |||
emit errorOccurred("スレーブアドレスの設定に失敗"); | |||
return QByteArray(); | |||
} | |||
QByteArray data(size, 0); | QByteArray data(size, 0); | ||
if (m_device->read(data.data(), size) != size) { | if (m_device->read(data.data(), size) != size) { | ||
emit errorOccurred("データの読み込みに失敗: " + m_device->errorString()); | emit errorOccurred("データの読み込みに失敗: " + m_device->errorString()); | ||
| 91行目: | 100行目: | ||
} | } | ||
void startStreaming(int address, int size, int interval) | |||
void | |||
{ | { | ||
m_streamingActive = true; | |||
QtConcurrent::run([this, address, size, interval]() { | |||
while (m_streamingActive) { | |||
QByteArray data = readDataAsync(address, size).result(); | |||
if (!data.isEmpty()) { | |||
emit dataStreamed(data); | |||
} | |||
QThread::msleep(interval); | |||
} | } | ||
}); | }); | ||
} | } | ||
void stopStreaming() | |||
void | |||
{ | { | ||
m_streamingActive = false; | |||
} | } | ||
void | signals: | ||
void dataReceived(const QByteArray &data); | |||
void dataStreamed(const QByteArray &data); | |||
void errorOccurred(const QString &error); | |||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| 145行目: | 129行目: | ||
<br> | <br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QCoreApplication> | |||
#include "I2CReader.h" | #include "I2CReader.h" | ||
| 151行目: | 136行目: | ||
QCoreApplication a(argc, argv); | QCoreApplication a(argc, argv); | ||
I2CReader reader("/dev/i2c-1"); | |||
// 非同期処理で受信する場合 | |||
QFuture<QByteArray> readFuture = reader.readDataAsync(0x50, 10); | |||
readFuture.then([](const QByteArray &data) { | |||
qDebug() << "読み込んだデータ:" << data.toHex(); | |||
}); | |||
// ストリーミング受信する場合 | |||
reader.startStreaming(0x50, 10, 1000); // 1秒間隔で10[byte]読み込み | |||
// エラーハンドリングの例 | |||
QObject::connect(&reader, &I2CReader::errorOccurred, [](const QString &error) { | |||
qDebug() << "読み込みエラー:" << error; | |||
}); | |||
// | // 10秒後にストリーミングを停止 | ||
QTimer::singleShot(10000, &reader, &I2CReader::stopStreaming); | |||
return a.exec(); | return a.exec(); | ||