xref: /OK3568_Linux_fs/app/forlinx/forlinx_up4_qt/spitest/mainwindow.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include <fcntl.h>
4 #include <sys/ioctl.h>
5 #include <linux/types.h>
6 #include <linux/spi/spidev.h>
7 #include <QMessageBox>
8 #include <unistd.h>
9 #include <QDir>
10 #include <QDebug>
11 #include <DWKeyboard/KeyboardGlobal.h>
12 
MainWindow(QWidget * parent)13 MainWindow::MainWindow(QWidget *parent) :
14     QMainWindow(parent),
15     ui(new Ui::MainWindow)
16 {
17 
18     ui->setupUi(this);
19 	GlobalInit();
20 	ui->speed->installEventFilter(this);
21 	ui->delay->installEventFilter(this);
22 	ui->bits->installEventFilter(this);
23 	ui->editsend->installEventFilter(this);
24 
25 	setWindowState(Qt::WindowMaximized);
26 	setWindowFlags(Qt::FramelessWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
27 
28 	ui->spidevlists->addItem(QWidget::tr("Please select SPI device"));
29 
30 	QDir dir("/dev/");
31 	dir.setFilter(QDir::System | QDir::NoSymLinks );
32 	QFileInfoList list = dir.entryInfoList();
33 	for (int i = 0; i < list.size(); ++i) {
34 		QFileInfo fileInfo = list.at(i);
35 		if (fileInfo.fileName().contains("spidev",Qt::CaseSensitive)) {
36 			char buff[100];
37 			sprintf(buff, "/dev/%s", qPrintable(QString("%1").arg(fileInfo.fileName())));
38 			qDebug() << qPrintable(QString("%1").arg(fileInfo.fileName()));
39 			ui->spidevlists->addItem(QWidget::tr(buff));
40 		}
41 	}
42 
43 	connect(ui->spidevlists,
44             SIGNAL(currentIndexChanged(const QString &)),
45             this, SLOT(GetSpiDevName(const QString &)));
46 	connect(ui->exitBtn, &QPushButton::clicked, this, [=](){
47 			close();
48 			});
49 
50 
51 }
52 
eventFilter(QObject * watched,QEvent * event)53 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
54 {
55 	static bool mFocusIn = false;
56 	if (event->type()==QEvent::FocusIn)
57 	{
58 		mFocusIn = true;
59 	}
60 	else if (event->type()==QEvent::FocusOut)
61 	{
62 		PlatformInputContextBase->FocusOut(watched);
63 		mFocusIn = false;
64 	}
65 
66 	if (mFocusIn && event->type() == QEvent::MouseButtonPress) {
67 		QMouseEvent *e = (QMouseEvent *)event;
68 		PlatformInputContextBase->FocusIn(watched, e->globalPos());
69 	}
70 
71 	return QMainWindow::eventFilter(watched,event);
72 }
73 
GetSpiDevName(const QString & name)74 void MainWindow::GetSpiDevName(const QString &name)
75 {
76 	int ret;
77     char buf[32];
78     uint8_t mode;
79     uint8_t bits = 8;
80     uint32_t speed = 500000;
81 	uint16_t delay;
82 
83     sprintf(DevName, "%s", name.toStdString().data());
84 	if (fd > 0)
85 		::close(fd);
86     fd = ::open(DevName, O_RDWR);
87     if(fd < 0)
88     {
89         QMessageBox::about(this,"error","open spidev failure");
90 		return ;
91     }
92 
93     /*
94      * spi mode
95      */
96     ret = ::ioctl(fd, SPI_IOC_WR_MODE, &mode);
97     if (ret == -1)
98 	    fprintf(stderr, "can't set spi mode");
99 
100     ret = ::ioctl(fd, SPI_IOC_RD_MODE, &mode);
101     if (ret == -1)
102 	    fprintf(stderr, "can't get spi mode");
103 
104 
105     /*
106      * bits per word
107      */
108     ret = ::ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
109     if (ret == -1)
110 	    fprintf(stderr, "can't set bits per word");
111 
112     ret = ::ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
113     if (ret == -1)
114 	    fprintf(stderr, "can't get bits per word");
115 
116     /*
117      * max speed hz
118      */
119     ret = ::ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
120     if (ret == -1)
121 	    fprintf(stderr, "can't set max speed hz");
122 
123     ret = ::ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
124     if (ret == -1)
125 	    fprintf(stderr, "can't get max speed hz");
126 
127     fprintf(stderr,"spi mode: %d\n", mode);
128     fprintf(stderr,"bits per word: %d\n", bits);
129     fprintf(stderr,"max speed: %d Hz (%d KHz)\n", speed, speed/1000);
130 
131     update_params();
132 }
133 
update_params()134 void MainWindow::update_params()
135 {
136     unsigned char mode = 0,bits;
137     unsigned int speed,ret;
138     unsigned short delay;
139 
140     speed = ui->speed->text().toInt();
141     delay = ui->delay->text().toInt();
142     bits  = ui->bits->text().toInt();
143 
144     ::ioctl(fd, SPI_IOC_RD_MODE, &mode);
145 
146     if(ui->loop->isChecked())
147         mode |= SPI_LOOP;
148 
149     if(ui->clockphase->isChecked())
150         mode |= SPI_CPHA;
151 
152     if(ui->clockpolarity->isChecked())
153         mode |= SPI_CPOL;
154 
155     if(ui->lsb->isChecked())
156         mode |= SPI_LSB_FIRST;
157 
158     if(ui->cshigh->isChecked())
159         mode |= SPI_CS_HIGH;
160 
161     if(ui->wire->isChecked())
162         mode |= SPI_3WIRE;
163 
164     if(ui->nocs->isChecked())
165         mode |= SPI_NO_CS;
166 
167     if(ui->ready->isChecked())
168         mode |= SPI_READY;
169 
170     ret = ::ioctl(fd,SPI_IOC_WR_MODE,&mode);
171     if(ret < 0)
172     {
173         QMessageBox::about(this,"error","SPI_IOC_WR_MODE failure");
174         exit(0);
175     }
176 
177     ret = ::ioctl(fd,SPI_IOC_WR_BITS_PER_WORD,&bits);
178     if(ret < 0)
179     {
180         QMessageBox::about(this,"error","SPI_IOC_WR_BITS_PER_WORD failure");
181         exit(0);
182     }
183 
184     ret = ::ioctl(fd,SPI_IOC_WR_MAX_SPEED_HZ,&speed);
185     if(ret < 0)
186     {
187         QMessageBox::about(this,"error","SPI_IOC_WR_MAX_SPEED_HZ failure");
188         exit(0);
189     }
190 
191 }
192 
~MainWindow()193 MainWindow::~MainWindow()
194 {
195     delete ui;
196     ::close(fd);
197 }
198 
on_send_clicked()199 void MainWindow::on_send_clicked()
200 {
201     std::string str = ui->editsend->text().toStdString();
202     struct spi_ioc_transfer tr;
203     char buf[str.length()+1] = {0};
204     memset(buf,0,str.length()+1);
205 
206     memset(&tr,0,sizeof(tr));
207     tr.tx_buf = (__u64)str.c_str();
208     tr.rx_buf = (__u64)buf;
209     tr.len = str.length();
210     tr.delay_usecs = ui->delay->text().toInt();
211     tr.speed_hz = ui->speed->text().toInt();
212     tr.bits_per_word = ui->bits->text().toInt();
213     ::ioctl(fd,SPI_IOC_MESSAGE(1),&tr);
214 
215     ui->editrecv->append(QString(buf));
216 
217 }
218 
on_loop_clicked(bool checked)219 void MainWindow::on_loop_clicked(bool checked)
220 {
221     update_params();
222 }
223 
on_clockphase_clicked(bool checked)224 void MainWindow::on_clockphase_clicked(bool checked)
225 {
226     update_params();
227 }
228 
on_clockpolarity_clicked(bool checked)229 void MainWindow::on_clockpolarity_clicked(bool checked)
230 {
231     update_params();
232 }
233 
on_lsb_clicked(bool checked)234 void MainWindow::on_lsb_clicked(bool checked)
235 {
236     update_params();
237 }
238 
on_cshigh_clicked(bool checked)239 void MainWindow::on_cshigh_clicked(bool checked)
240 {
241     update_params();
242 }
243 
on_wire_clicked(bool checked)244 void MainWindow::on_wire_clicked(bool checked)
245 {
246     update_params();
247 }
248 
on_nocs_clicked(bool checked)249 void MainWindow::on_nocs_clicked(bool checked)
250 {
251     update_params();
252 }
253 
on_ready_clicked(bool checked)254 void MainWindow::on_ready_clicked(bool checked)
255 {
256     update_params();
257 }
258 
on_speed_textChanged(const QString & arg1)259 void MainWindow::on_speed_textChanged(const QString &arg1)
260 {
261     update_params();
262 }
263 
on_delay_textChanged(const QString & arg1)264 void MainWindow::on_delay_textChanged(const QString &arg1)
265 {
266     update_params();
267 }
268 
on_bits_textChanged(const QString & arg1)269 void MainWindow::on_bits_textChanged(const QString &arg1)
270 {
271     update_params();
272 }
273 
moveEvent(QMoveEvent *)274 void MainWindow::moveEvent(QMoveEvent *)
275 {
276     this->move(QPoint(0,0));
277 }
278 
resizeEvent(QResizeEvent *)279 void MainWindow::resizeEvent(QResizeEvent *)
280 {
281     this->showMaximized();
282 }
283 
closeEvent(QCloseEvent *)284 void MainWindow::closeEvent(QCloseEvent *)
285 {
286     exit(0);
287 }
288 
289