1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
10 #include "proxguiqt.h"
14 #include <QPainterPath>
18 #include <QCloseEvent>
19 #include <QMouseEvent>
25 #include <QHBoxLayout>
31 bool g_useOverlays
= false;
37 void ProxGuiQT::ShowGraphWindow(void)
39 emit
ShowGraphWindowSignal();
42 void ProxGuiQT::RepaintGraphWindow(void)
44 emit
RepaintGraphWindowSignal();
47 void ProxGuiQT::HideGraphWindow(void)
49 emit
HideGraphWindowSignal();
52 void ProxGuiQT::Exit(void)
57 void ProxGuiQT::_ShowGraphWindow(void)
63 plotwidget
= new ProxWidget();
68 void ProxGuiQT::_RepaintGraphWindow(void)
70 if (!plotapp
|| !plotwidget
)
76 void ProxGuiQT::_HideGraphWindow(void)
78 if (!plotapp
|| !plotwidget
)
84 void ProxGuiQT::_Exit(void) {
88 void ProxGuiQT::_StartProxmarkThread(void) {
92 // if thread finished delete self and delete application
93 QObject::connect(proxmarkThread
, SIGNAL(finished()), proxmarkThread
, SLOT(deleteLater()));
94 QObject::connect(proxmarkThread
, SIGNAL(finished()), this, SLOT(_Exit()));
95 // start proxmark thread
96 proxmarkThread
->start();
99 void ProxGuiQT::MainLoop()
101 plotapp
= new QApplication(argc
, argv
);
103 connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
104 connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
105 connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
106 connect(this, SIGNAL(ExitSignal()), this, SLOT(_Exit()));
108 //start proxmark thread after starting event loop
109 QTimer::singleShot(200, this, SLOT(_StartProxmarkThread()));
114 ProxGuiQT::ProxGuiQT(int argc
, char **argv
, WorkerThread
*wthread
) : plotapp(NULL
), plotwidget(NULL
),
115 argc(argc
), argv(argv
), proxmarkThread(wthread
)
119 ProxGuiQT::~ProxGuiQT(void)
122 //plotwidget->destroy(true,true);
123 // delete plotwidget;
124 // plotwidget = NULL;
133 //--------------------
134 void ProxWidget::applyOperation()
136 //printf("ApplyOperation()");
137 save_restoreGB(GRAPH_SAVE
);
138 memcpy(GraphBuffer
, s_Buff
, sizeof(int) * GraphTraceLen
);
139 RepaintGraphWindow();
141 void ProxWidget::stickOperation()
143 save_restoreGB(GRAPH_RESTORE
);
144 //printf("stickOperation()");
146 void ProxWidget::vchange_autocorr(int v
)
149 ans
= AutoCorrelate(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, true, false);
150 if (g_debugMode
) printf("vchange_autocorr(w:%d): %d\n", v
, ans
);
151 g_useOverlays
= true;
152 RepaintGraphWindow();
154 void ProxWidget::vchange_askedge(int v
)
157 //extern int AskEdgeDetect(const int *in, int *out, int len, int threshold);
158 ans
= AskEdgeDetect(GraphBuffer
, s_Buff
, GraphTraceLen
, v
);
159 if (g_debugMode
) printf("vchange_askedge(w:%d)%d\n", v
, ans
);
160 g_useOverlays
= true;
161 RepaintGraphWindow();
163 void ProxWidget::vchange_dthr_up(int v
)
165 int down
= opsController
->horizontalSlider_dirthr_down
->value();
166 directionalThreshold(GraphBuffer
, s_Buff
, GraphTraceLen
, v
, down
);
167 //printf("vchange_dthr_up(%d)", v);
168 g_useOverlays
= true;
169 RepaintGraphWindow();
171 void ProxWidget::vchange_dthr_down(int v
)
173 //printf("vchange_dthr_down(%d)", v);
174 int up
= opsController
->horizontalSlider_dirthr_up
->value();
175 directionalThreshold(GraphBuffer
,s_Buff
, GraphTraceLen
, v
, up
);
176 g_useOverlays
= true;
177 RepaintGraphWindow();
179 ProxWidget::ProxWidget(QWidget
*parent
, ProxGuiQT
*master
) : QWidget(parent
)
181 this->master
= master
;
184 /** Setup the controller widget **/
186 controlWidget
= new QWidget();
187 opsController
= new Ui::Form();
188 opsController
->setupUi(controlWidget
);
189 //Due to quirks in QT Designer, we need to fiddle a bit
190 opsController
->horizontalSlider_dirthr_down
->setMinimum(-128);
191 opsController
->horizontalSlider_dirthr_down
->setMaximum(0);
192 opsController
->horizontalSlider_dirthr_down
->setValue(-20);
193 opsController
->horizontalSlider_dirthr_up
->setMinimum(-40);
194 opsController
->horizontalSlider_dirthr_up
->setMaximum(128);
195 opsController
->horizontalSlider_dirthr_up
->setValue(20);
196 opsController
->horizontalSlider_askedge
->setValue(25);
197 opsController
->horizontalSlider_window
->setValue(4000);
200 QObject::connect(opsController
->pushButton_apply
, SIGNAL(clicked()), this, SLOT(applyOperation()));
201 QObject::connect(opsController
->pushButton_sticky
, SIGNAL(clicked()), this, SLOT(stickOperation()));
202 QObject::connect(opsController
->horizontalSlider_window
, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int)));
203 QObject::connect(opsController
->horizontalSlider_dirthr_up
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int)));
204 QObject::connect(opsController
->horizontalSlider_dirthr_down
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int)));
205 QObject::connect(opsController
->horizontalSlider_askedge
, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int)));
207 controlWidget
->show();
209 // Set up the plot widget, which does the actual plotting
211 plot
= new Plot(this);
213 QSlider* slider = new QSlider(Qt::Horizontal);
214 slider->setFocusPolicy(Qt::StrongFocus);
215 slider->setTickPosition(QSlider::TicksBothSides);
216 slider->setTickInterval(10);
217 slider->setSingleStep(1);
219 QVBoxLayout
*layout
= new QVBoxLayout
;
220 //layout->addWidget(slider);
221 layout
->addWidget(plot
);
223 //printf("Proxwidget Constructor just set layout\r\n");
226 // not 100% sure what i need in this block
227 // feel free to fix - marshmellow...
228 ProxWidget::~ProxWidget(void)
231 controlWidget
->close();
232 delete controlWidget
;
233 controlWidget
= NULL
;
237 delete opsController
;
238 opsController
= NULL
;
247 void ProxWidget::closeEvent(QCloseEvent
*event
)
251 g_useOverlays
= false;
253 void ProxWidget::hideEvent(QHideEvent
*event
) {
254 controlWidget
->hide();
257 void ProxWidget::showEvent(QShowEvent
*event
) {
258 controlWidget
->show();
262 //----------- Plotting
264 int Plot::xCoordOf(int i
, QRect r
)
266 return r
.left() + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
269 int Plot::yCoordOf(int v
, QRect r
, int maxVal
)
271 int z
= (r
.bottom() - r
.top())/2;
272 return -(z
* v
) / maxVal
+ z
;
275 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
277 int z
= (r
.bottom() - r
.top())/2;
278 return (y
-z
) * maxVal
/ z
;
280 static const QColor GREEN
= QColor(100,255,100);
281 static const QColor RED
= QColor(255,100,100);
282 static const QColor BLUE
= QColor(100,100,255);
283 static const QColor GRAY
= QColor(240,240,240);
285 QColor
Plot::getColor(int graphNum
)
288 case 0: return GREEN
; //Green
289 case 1: return RED
; //Red
290 case 2: return BLUE
; //Blue
291 default: return GRAY
; //Gray
295 void Plot::setMaxAndStart(int *buffer
, int len
, QRect plotRect
)
297 if (len
== 0) return;
298 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
302 if(GraphStart
> startMax
) {
303 GraphStart
= startMax
;
305 if (GraphStart
> len
) return;
306 int vMin
= INT_MAX
, vMax
= INT_MIN
, v
= 0;
307 int sample_index
= GraphStart
;
308 for( ; sample_index
< len
&& xCoordOf(sample_index
,plotRect
) < plotRect
.right() ; sample_index
++) {
310 v
= buffer
[sample_index
];
311 if(v
< vMin
) vMin
= v
;
312 if(v
> vMax
) vMax
= v
;
316 if(fabs( (double) vMin
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMin
);
317 if(fabs( (double) vMax
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMax
);
318 g_absVMax
= (int)(g_absVMax
*1.25 + 1);
321 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
323 if (len
== 0 || PlotGridX
<= 0) return;
324 //clock_t begin = clock();
325 QPainterPath penPath
;
327 int grid_delta_x
= PlotGridX
;
328 int first_delta_x
= grid_delta_x
; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
329 if (GraphStart
> plotOffset
) first_delta_x
-= (GraphStart
-plotOffset
);
330 int DemodStart
= GraphStart
;
331 if (plotOffset
> GraphStart
) DemodStart
= plotOffset
;
335 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
336 first_delta_x
+= BitStart
* PlotGridX
;
337 if (BitStart
> (int)len
) return;
340 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
342 painter
->setPen(getColor(graphNum
));
344 int absVMax
= (int)(100*1.05+1);
345 int x
= xCoordOf(DemodStart
, plotRect
);
346 int y
= yCoordOf((buffer
[BitStart
]*200-100)*-1,plotRect
,absVMax
);
347 penPath
.moveTo(x
, y
);
349 int clk
= first_delta_x
;
350 for(int i
= BitStart
; i
< (int)len
&& xCoordOf(delta_x
+DemodStart
, plotRect
) < plotRect
.right(); i
++) {
351 for (int ii
= 0; ii
< (clk
) && i
< (int)len
&& xCoordOf(DemodStart
+delta_x
+ii
, plotRect
) < plotRect
.right() ; ii
++ ) {
352 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
353 v
= buffer
[i
]*200-100;
355 y
= yCoordOf( v
, plotRect
, absVMax
);
357 penPath
.lineTo(x
, y
);
359 if(GraphPixelsPerPoint
> 10) {
360 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
361 painter
->fillRect(f
, QColor(100, 255, 100));
363 if (ii
== (int)clk
/2) {
365 sprintf(str
, "%u",buffer
[i
]);
366 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
374 painter
->drawPath(penPath
);
377 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
379 if (len
== 0) return;
380 //clock_t begin = clock();
381 QPainterPath penPath
;
382 int vMin
= INT_MAX
, vMax
= INT_MIN
, vMean
= 0, v
= 0, i
= 0;
383 int x
= xCoordOf(GraphStart
, plotRect
);
384 int y
= yCoordOf(buffer
[GraphStart
],plotRect
,g_absVMax
);
385 penPath
.moveTo(x
, y
);
386 for(i
= GraphStart
; i
< len
&& xCoordOf(i
, plotRect
) < plotRect
.right(); i
++) {
388 x
= xCoordOf(i
, plotRect
);
391 y
= yCoordOf( v
, plotRect
, g_absVMax
);
393 penPath
.lineTo(x
, y
);
395 if(GraphPixelsPerPoint
> 10) {
396 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
397 painter
->fillRect(f
, QColor(100, 255, 100));
400 if(v
< vMin
) vMin
= v
;
401 if(v
> vMax
) vMax
= v
;
404 vMean
/= (i
- GraphStart
);
406 painter
->setPen(getColor(graphNum
));
409 int xo
= 5+(graphNum
*40);
410 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
412 int vMarkers
= (g_absVMax
- (g_absVMax
% 10)) / 5;
413 int minYDist
= 40; //Minimum pixel-distance between markers
420 for(v
= vMarkers
; yCoordOf(v
,plotRect
,g_absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
422 int y0
= yCoordOf(v
,plotRect
,g_absVMax
);
423 int y1
= yCoordOf(-v
,plotRect
,g_absVMax
);
425 if(lasty0
- y0
< minYDist
) continue;
427 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
429 sprintf(yLbl
, "%d", v
);
430 painter
->drawText(xo
+8,y0
+7,yLbl
);
432 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
433 sprintf(yLbl
, "%d",-v
);
434 painter
->drawText(xo
+8, y1
+5 , yLbl
);
439 painter
->drawPath(penPath
);
441 sprintf(str
, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
442 vMax
, vMin
, vMean
, i
, len
, buffer
[CursorAPos
], buffer
[CursorBPos
]);
443 painter
->drawText(20, annotationRect
.bottom() - 23 - 20 * graphNum
, str
);
445 //clock_t end = clock();
446 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
447 //printf("Plot time %f\n", elapsed_secs);
450 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
453 if (PlotGridX
<= 0) return;
454 int offset
= GridOffset
;
455 if (GridLocked
&& PlotGridX
) {
456 offset
= GridOffset
+ PlotGridX
- (GraphStart
% PlotGridX
);
457 } else if (!GridLocked
&& GraphStart
> 0 && PlotGridX
) {
458 offset
= PlotGridX
-((GraphStart
- offset
) % PlotGridX
) + GraphStart
- unlockStart
;
461 if (offset
< 0) offset
+= PlotGridX
;
464 int grid_delta_x
= (int) (PlotGridX
* GraphPixelsPerPoint
);
465 int grid_delta_y
= PlotGridY
;
466 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
467 for(i
= (offset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= grid_delta_x
) {
468 painter
->drawLine(r
.left()+i
, r
.top(), r
.left()+i
, r
.bottom());
472 for(i
= 0; yCoordOf(i
,r
,g_absVMax
) > r
.top(); i
+= grid_delta_y
) {
473 painter
->drawLine( r
.left(), yCoordOf(i
,r
,g_absVMax
), r
.right(), yCoordOf(i
,r
,g_absVMax
) );
474 painter
->drawLine( r
.left(), yCoordOf(i
*-1,r
,g_absVMax
), r
.right(), yCoordOf(i
*-1,r
,g_absVMax
) );
479 #define HEIGHT_INFO 60
480 #define WIDTH_AXES 80
482 void Plot::paintEvent(QPaintEvent
*event
)
485 QPainter
painter(this);
486 QBrush
brush(QColor(100, 255, 100));
487 QPen
pen(QColor(100, 255, 100));
489 painter
.setFont(QFont("Courier New", 10));
495 if (CursorAPos
> GraphTraceLen
)
497 if(CursorBPos
> GraphTraceLen
)
499 if(CursorCPos
> GraphTraceLen
)
501 if(CursorDPos
> GraphTraceLen
)
504 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
505 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
508 painter
.fillRect(rect(), QColor(60, 60, 60));
510 painter
.fillRect(plotRect
, QColor(0, 0, 0));
512 //init graph variables
513 setMaxAndStart(GraphBuffer
,GraphTraceLen
,plotRect
);
516 int zeroHeight
= plotRect
.top() + (plotRect
.bottom() - plotRect
.top()) / 2;
517 painter
.setPen(QColor(100, 100, 100));
518 painter
.drawLine(plotRect
.left(), zeroHeight
, plotRect
.right(), zeroHeight
);
519 // plot X and Y grid lines
520 plotGridLines(&painter
, plotRect
);
522 //Start painting graph
523 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
524 if (showDemod
&& DemodBufferLen
> 8) {
525 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
528 //init graph variables
529 setMaxAndStart(s_Buff
,GraphTraceLen
,plotRect
);
530 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
535 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
537 painter
.setPen(QColor(255, 255, 0));
538 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
540 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
542 painter
.setPen(QColor(255, 0, 255));
543 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
545 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
547 painter
.setPen(QColor(255, 153, 0)); //orange
548 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
550 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
552 painter
.setPen(QColor(0, 0, 205)); //light blue
553 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
558 sprintf(str
, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
559 GraphStart
, CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
560 GraphPixelsPerPoint
,CursorAPos
,CursorBPos
,PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked",GridOffset
);
561 painter
.setPen(QColor(255, 255, 255));
562 painter
.drawText(20, infoRect
.bottom() - 3, str
);
566 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
568 //Need to set this, otherwise we don't receive keypress events
569 setFocusPolicy( Qt::StrongFocus
);
572 QPalette
palette(QColor(0,0,0,0));
573 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
574 palette
.setColor(QPalette::Text
, QColor(255,255,255));
575 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
577 setAutoFillBackground(true);
581 setWindowTitle(tr("Sliders"));
584 void Plot::closeEvent(QCloseEvent
*event
)
588 g_useOverlays
= false;
591 void Plot::mouseMoveEvent(QMouseEvent
*event
)
595 x
= (int)(x
/ GraphPixelsPerPoint
);
597 if((event
->buttons() & Qt::LeftButton
)) {
599 } else if (event
->buttons() & Qt::RightButton
) {
607 void Plot::keyPressEvent(QKeyEvent
*event
)
611 if(event
->modifiers() & Qt::ShiftModifier
) {
613 offset
= PageWidth
- (PageWidth
% PlotGridX
);
617 if(event
->modifiers() & Qt::ControlModifier
)
620 offset
= (int)(20 / GraphPixelsPerPoint
);
622 switch(event
->key()) {
624 if(GraphPixelsPerPoint
<= 50) {
625 GraphPixelsPerPoint
*= 2;
630 if(GraphPixelsPerPoint
>= 0.02) {
631 GraphPixelsPerPoint
/= 2;
636 if(GraphPixelsPerPoint
< 20) {
637 GraphStart
+= offset
;
644 if(GraphPixelsPerPoint
< 20) {
645 GraphStart
-= offset
;
652 if(PlotGridX
|| PlotGridY
) {
656 PlotGridX
= PlotGridXdefault
;
657 PlotGridY
= PlotGridYdefault
;
662 puts("Plot Window Keystrokes:\n");
663 puts(" Key Action\n");
664 puts(" DOWN Zoom in");
665 puts(" G Toggle grid display");
666 puts(" H Show help");
667 puts(" L Toggle lock grid relative to samples");
668 puts(" LEFT Move left");
669 puts(" <CTL>LEFT Move left 1 sample");
670 puts(" <SHIFT>LEFT Page left");
671 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
672 puts(" Q Hide window");
673 puts(" RIGHT Move right");
674 puts(" <CTL>RIGHT Move right 1 sample");
675 puts(" <SHIFT>RIGHT Page right");
676 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
677 puts(" UP Zoom out");
679 puts("Use client window 'data help' for more plot commands\n");
683 GridLocked
= !GridLocked
;
685 GridOffset
+= (GraphStart
- unlockStart
);
687 unlockStart
= GraphStart
;
695 QWidget::keyPressEvent(event
);