📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
細 文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag… |
|||
| (同じ利用者による、間の2版が非表示) | |||
| 167行目: | 167行目: | ||
<br> | <br> | ||
適切なI2Cデバイス名 (例: /dev/i2c-1) とスレーブアドレスを指定する必要があることに注意する。<br> | 適切なI2Cデバイス名 (例: /dev/i2c-1) とスレーブアドレスを指定する必要があることに注意する。<br> | ||
<br> | |||
<u>※注意</u><br> | |||
<u>以下の例で使用しているlinux/i2c-dev.hファイルのライセンスは、GPL 2.0以降に準拠していることに注意する。</u><br> | |||
<br> | <br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
| 172行目: | 175行目: | ||
#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 I2CWriter : public QObject | class I2CWriter : public QObject | ||
| 182行目: | 189行目: | ||
private: | private: | ||
QFile *m_device; | |||
bool m_streamingActive = false; | |||
public: | public: | ||
explicit I2CWriter(const QString &deviceName, QObject *parent = nullptr) : QObject(parent), m_device(new | explicit I2CWriter(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()); | ||
} | } | ||
| 207行目: | 215行目: | ||
} | } | ||
m_device-> | // スレーブアドレスの設定 | ||
if (ioctl(m_device->handle(), I2C_SLAVE, address) < 0) { | |||
emit errorOccurred("スレーブアドレスの設定に失敗"); | |||
return false; | |||
} | |||
if (m_device->write(data | if (m_device->write(data) != data.size()) { | ||
emit errorOccurred("データの書き込みに失敗: " + m_device->errorString()); | emit errorOccurred("データの書き込みに失敗: " + m_device->errorString()); | ||
return false; | return false; | ||
} | } | ||
emit | emit dataWritten(data); | ||
return true; | return true; | ||
| 220行目: | 232行目: | ||
} | } | ||
void startStreamingWrite(int address, const QByteArray &data, int interval) | |||
void | |||
{ | { | ||
m_streamingActive = true; | |||
QtConcurrent::run([this, address, data, interval]() { | |||
while (m_streamingActive) { | |||
bool success = writeDataAsync(address, data).result(); | |||
if (success) { | |||
emit dataStreamWritten(data); | |||
} | |||
QThread::msleep(interval); | |||
} | } | ||
}); | }); | ||
} | } | ||
void stopStreamingWrite() | |||
void | |||
{ | { | ||
m_streamingActive = false; | |||
} | } | ||
void | signals: | ||
void dataWritten(const QByteArray &data); | |||
void dataStreamWritten(const QByteArray &data); | |||
void errorOccurred(const QString &error); | |||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| 274行目: | 261行目: | ||
<br> | <br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QCoreApplication> | |||
#include "I2CWriter.h" | #include "I2CWriter.h" | ||
| 280行目: | 268行目: | ||
QCoreApplication a(argc, argv); | QCoreApplication a(argc, argv); | ||
I2CWriter writer("/dev/i2c-1"); | |||
// | // 非同期処理で書き込みする場合 | ||
QByteArray dataToWrite = QByteArray::fromHex("0102030405"); | QByteArray dataToWrite = QByteArray::fromHex("0102030405"); | ||
QFuture<bool> writeFuture = writer.writeDataAsync(0x50, dataToWrite); | |||
writeFuture.then([](bool success) { | |||
qDebug() << "書き込み成功:" << success; | |||
}); | |||
// ストリーミング書き込みする場合 | |||
writer.startStreamingWrite(0x50, dataToWrite, 2000); // 2秒間隔で書き込み | |||
// エラーハンドリング | |||
QObject::connect(&writer, &I2CWriter::errorOccurred, [](const QString &error) { | |||
qDebug() << "書き込みエラー: " << error; | |||
}); | |||
// 10秒後にストリーミングを停止 | |||
QTimer::singleShot(10000, &writer, &I2CWriter::stopStreamingWrite); | |||
return a.exec(); | return a.exec(); | ||
| 291行目: | 293行目: | ||
<br><br> | <br><br> | ||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] | ||