Files
pip/libs/main/io_devices/pispi.h

111 lines
4.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*! \file pispi.h
* \ingroup IO
* \~\brief
* \~english SPI device
* \~russian Устройство SPI
*/
/*
PIP - Platform Independent Primitives
SPI
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PISPI_H
#define PISPI_H
#include "piiodevice.h"
//! \ingroup IO
//! \~\brief
//! \~english SPI device.
//! \~russian Устройство SPI.
class PIP_EXPORT PISPI: public PIIODevice {
PIIODEVICE(PISPI, "spi");
public:
//! \~english Constructs %PISPI with path "path", speed "speed_hz" and mode "mode"
//! \~russian Создаёт %PISPI с путём "path", скоростью "speed_hz" и режимом "mode"
explicit PISPI(const PIString & path = PIString(), uint speed_hz = 1000000, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! \~english Destructor
//! \~russian Деструктор
virtual ~PISPI();
//! \~english Parameters of PISPI
//! \~russian Параметры PISPI
enum Parameters {
ClockInverse /*! \~english SPI clk polarity control \~russian Управление полярностью SPI clk */ = 0x1,
ClockPhaseShift /*! \~english SPI clk phase control \~russian Управление фазой SPI clk */ = 0x2,
};
//! \~english Set SPI speed in Hz
//! \~russian Устанавливает скорость SPI в Гц
void setSpeed(uint speed_hz);
//! \~english Returns current SPI speed in Hz
//! \~russian Возвращает текущую скорость SPI в Гц
uint speed() const { return spi_speed; }
//! \~english Set number of bits per word, default is 8
//! \~russian Устанавливает количество бит на слово, по умолчанию 8
void setBits(uchar bits = 8);
//! \~english Returns number of bits per word
//! \~russian Возвращает количество бит на слово
uchar bits() const { return spi_bits; }
//! \~english Set parameters to "parameters_"
//! \~russian Устанавливает параметры в "parameters_"
void setParameters(PIFlags<PISPI::Parameters> parameters_) { spi_mode = (int)parameters_; }
//! \~english Set parameter "parameter" to "on" state
//! \~russian Устанавливает параметр "parameter" в состояние "on"
void setParameter(PISPI::Parameters parameter, bool on = true);
//! \~english Returns if parameter "parameter" is set
//! \~russian Возвращает установлен ли параметр "parameter"
bool isParameterSet(PISPI::Parameters parameter) const;
//! \~english Returns parameters
//! \~russian Возвращает параметры
PIFlags<PISPI::Parameters> parameters() const { return spi_mode; }
ssize_t bytesAvailable() const override;
protected:
bool openDevice() override;
bool closeDevice() override;
ssize_t readDevice(void * read_to, ssize_t max_size) override;
ssize_t writeDevice(const void * data, ssize_t max_size) override;
PIString constructFullPathDevice() const override;
void configureFromFullPathDevice(const PIString & full_path) override;
PIPropertyStorage constructVariantDevice() const override;
void configureFromVariantDevice(const PIPropertyStorage & d) override;
DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Sequential; }
private:
uint spi_speed;
uchar spi_mode;
uchar spi_bits;
PIByteArray tx_buf, rx_buf;
PIByteArray recv_buf;
PRIVATE_DECLARATION(PIP_EXPORT)
};
#endif // PISPI_H