spi
git-svn-id: svn://db.shs.com.ru/pip@566 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
33
main.cpp
33
main.cpp
@@ -1,26 +1,21 @@
|
||||
#include "pip.h"
|
||||
#include "picompress.h"
|
||||
#include "pispi.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
PIString s = PIString::fromUTF8("hello, привет привет привет привет привет привет");
|
||||
PIByteArray ba = s.toUTF8();
|
||||
piCout << "original " << ba.toHex();
|
||||
ba = piCompress(ba);
|
||||
piCout << "compress " << ba.toHex();
|
||||
ba = piDecompress(ba);
|
||||
piCout << "decompress" << ba.toHex();
|
||||
piCout << PIString::fromUTF8(ba);
|
||||
ba = PIByteArray(160);
|
||||
piCout << ba.toHex() << ba.size();
|
||||
piCout << piCompress(ba).toHex() << piCompress(ba).size();
|
||||
/*PIString s = PIString::fromUTF8("hello, привет");
|
||||
piCout << s;
|
||||
PIByteArray ba = s.toUTF8();
|
||||
piCout << ba.toHex();
|
||||
PIString s2;
|
||||
s2 = PIString::fromUTF8(ba);
|
||||
piCout << s2;*/
|
||||
PICLI cli(argc, argv);
|
||||
cli.setOptionalArgumentsCount(1);
|
||||
cli.addArgument("dev", true);
|
||||
cli.addArgument("speed", true);
|
||||
if (!cli.hasArgument("dev")) {
|
||||
return 0;
|
||||
}
|
||||
PIString path = cli.argumentValue("dev");
|
||||
PISPI spi(path);
|
||||
if (cli.hasArgument("speed")) spi.setSpeed(cli.argumentValue("speed").toInt());
|
||||
piCout << "open" << spi.open();
|
||||
piCout << "write" << spi.write(PIByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000"));
|
||||
piCout << "read" << spi.readForTime(10).toHex();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
94
src_main/io/pispi.cpp
Normal file
94
src_main/io/pispi.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "pispi.h"
|
||||
#ifndef WINDOWS
|
||||
# include <fcntl.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <linux/spi/spidev.h>
|
||||
#endif
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PISPI)
|
||||
#ifndef WINDOWS
|
||||
int fd;
|
||||
spi_ioc_transfer spi_ioc_tr;
|
||||
#endif
|
||||
PRIVATE_DEFINITION_END(PISPI)
|
||||
|
||||
|
||||
REGISTER_DEVICE(PISPI)
|
||||
|
||||
|
||||
PISPI::PISPI(const PIString & path, uint speed, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
|
||||
setPath(path);
|
||||
setSpeed(speed);
|
||||
spi_mode = 0;
|
||||
spi_bits = 8;
|
||||
#ifndef WINDOWS
|
||||
fd = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PISPI::setSpeed(uint speed_hz) {
|
||||
spi_speed = speed_hz;
|
||||
}
|
||||
|
||||
|
||||
bool PISPI::openDevice() {
|
||||
#ifndef WINDOWS
|
||||
int ret = 0;
|
||||
fd = open(path().dataAscii(), O_RDWR);
|
||||
if (fd < 0) return false;
|
||||
ret = ioctl(fd, SPI_IOC_WR_MODE, &spi_mode);
|
||||
if (ret == -1) return false;
|
||||
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits);
|
||||
if (ret == -1) return false;
|
||||
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed);
|
||||
if (ret == -1) return false;
|
||||
piCout << "SPI open" << device << "speed:" << spi_speed/1000 << "KHz" << "mode" << spi_mode << "bits" << spi_bits;
|
||||
spi_ioc_tr.delay_usecs = 0;
|
||||
spi_ioc_tr.speed_hz = spi_speed;
|
||||
spi_ioc_tr.bits_per_word = spi_bits;
|
||||
return true;
|
||||
#else
|
||||
piCoutObj << "PISPI not implemented on windows";
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PISPI::closeDevice() {
|
||||
#ifndef WINDOWS
|
||||
if (fd) close(fd);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int PISPI::readDevice(void * read_to, int max_size) {
|
||||
int sz = piMini(recv_buf.size_s(), max_size);
|
||||
memcpy(read_to, recv_buf.data(), sz);
|
||||
recv_buf.resize(recv_buf.size_s() - sz);
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
int PISPI::writeDevice(const void * data, int max_size) {
|
||||
#ifndef WINDOWS
|
||||
if (max_size > 0) {
|
||||
if (tx_buf.size_s() != max_size) {
|
||||
tx_buf.resize(max_size);
|
||||
rx_buf.resize(max_size);
|
||||
spi_ioc_tr.tx_buf = (ulong)(tx_buf.data());
|
||||
spi_ioc_tr.rx_buf = (ulong)(rx_buf.data());
|
||||
spi_ioc_tr.len = max_size;
|
||||
}
|
||||
memcpy(tx_buf.data(), data, max_size);
|
||||
int ret;
|
||||
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &spi_ioc_tr);
|
||||
if (ret < 1) {piCout << "init can't send spi message" << ret; return -1;}
|
||||
recv_buf.append(rx_buf);
|
||||
return max_size;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
31
src_main/io/pispi.h
Normal file
31
src_main/io/pispi.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef PISPI_H
|
||||
#define PISPI_H
|
||||
|
||||
#include "piiodevice.h"
|
||||
|
||||
|
||||
class PIP_EXPORT PISPI: public PIIODevice
|
||||
{
|
||||
PIIODEVICE(PISPI)
|
||||
public:
|
||||
explicit PISPI(const PIString & path = PIString(), uint speed_hz = 100000, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
|
||||
~PISPI() {}
|
||||
|
||||
void setSpeed(uint speed_hz);
|
||||
|
||||
protected:
|
||||
bool openDevice();
|
||||
bool closeDevice();
|
||||
int readDevice(void * read_to, int max_size);
|
||||
int writeDevice(const void * data, int max_size);
|
||||
|
||||
private:
|
||||
uint spi_speed;
|
||||
uchar spi_mode;
|
||||
uchar spi_bits;
|
||||
PIByteArray tx_buf, rx_buf;
|
||||
PIByteArray recv_buf;
|
||||
PRIVATE_DECLARATION
|
||||
};
|
||||
|
||||
#endif // PISPI_H
|
||||
@@ -3,7 +3,7 @@
|
||||
#define PIVERSION_H
|
||||
|
||||
#define PIP_VERSION_MAJOR 1
|
||||
#define PIP_VERSION_MINOR 4
|
||||
#define PIP_VERSION_MINOR 5
|
||||
#define PIP_VERSION_REVISION 0
|
||||
#define PIP_VERSION_SUFFIX ""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user