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 if ( maxVal
== 0 ) maxVal
++;
273 return -(z
* v
) / maxVal
+ z
;
276 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
278 int z
= (r
.bottom() - r
.top())/2;
279 return (y
-z
) * maxVal
/ z
;
281 static const QColor GREEN
= QColor(100,255,100);
282 static const QColor RED
= QColor(255,100,100);
283 static const QColor BLUE
= QColor(100,100,255);
284 static const QColor GRAY
= QColor(240,240,240);
286 QColor
Plot::getColor(int graphNum
)
289 case 0: return GREEN
; //Green
290 case 1: return RED
; //Red
291 case 2: return BLUE
; //Blue
292 default: return GRAY
; //Gray
296 void Plot::setMaxAndStart(int *buffer
, int len
, QRect plotRect
)
298 if (len
== 0) return;
299 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
303 if(GraphStart
> startMax
) {
304 GraphStart
= startMax
;
306 if (GraphStart
> len
) return;
307 int vMin
= INT_MAX
, vMax
= INT_MIN
, v
= 0;
308 int sample_index
= GraphStart
;
309 for( ; sample_index
< len
&& xCoordOf(sample_index
,plotRect
) < plotRect
.right() ; sample_index
++) {
311 v
= buffer
[sample_index
];
312 if(v
< vMin
) vMin
= v
;
313 if(v
> vMax
) vMax
= v
;
317 if(fabs( (double) vMin
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMin
);
318 if(fabs( (double) vMax
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMax
);
319 g_absVMax
= (int)(g_absVMax
*1.25 + 1);
322 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
324 if (len
== 0 || PlotGridX
<= 0) return;
325 //clock_t begin = clock();
326 QPainterPath penPath
;
328 int grid_delta_x
= PlotGridX
;
329 int first_delta_x
= grid_delta_x
; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
330 if (GraphStart
> plotOffset
) first_delta_x
-= (GraphStart
-plotOffset
);
331 int DemodStart
= GraphStart
;
332 if (plotOffset
> GraphStart
) DemodStart
= plotOffset
;
336 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
337 first_delta_x
+= BitStart
* PlotGridX
;
338 if (BitStart
> (int)len
) return;
341 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
343 painter
->setPen(getColor(graphNum
));
345 int absVMax
= (int)(100*1.05+1);
346 int x
= xCoordOf(DemodStart
, plotRect
);
347 int y
= yCoordOf((buffer
[BitStart
]*200-100)*-1,plotRect
,absVMax
);
348 penPath
.moveTo(x
, y
);
350 int clk
= first_delta_x
;
351 for(int i
= BitStart
; i
< (int)len
&& xCoordOf(delta_x
+DemodStart
, plotRect
) < plotRect
.right(); i
++) {
352 for (int ii
= 0; ii
< (clk
) && i
< (int)len
&& xCoordOf(DemodStart
+delta_x
+ii
, plotRect
) < plotRect
.right() ; ii
++ ) {
353 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
354 v
= buffer
[i
]*200-100;
356 y
= yCoordOf( v
, plotRect
, absVMax
);
358 penPath
.lineTo(x
, y
);
360 if(GraphPixelsPerPoint
> 10) {
361 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
362 painter
->fillRect(f
, QColor(100, 255, 100));
364 if (ii
== (int)clk
/2) {
366 sprintf(str
, "%u",buffer
[i
]);
367 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
375 painter
->drawPath(penPath
);
378 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
380 if (len
== 0) return;
381 //clock_t begin = clock();
382 QPainterPath penPath
;
383 int vMin
= INT_MAX
, vMax
= INT_MIN
, vMean
= 0, v
= 0, i
= 0;
384 int x
= xCoordOf(GraphStart
, plotRect
);
385 int y
= yCoordOf(buffer
[GraphStart
],plotRect
,g_absVMax
);
386 penPath
.moveTo(x
, y
);
387 for(i
= GraphStart
; i
< len
&& xCoordOf(i
, plotRect
) < plotRect
.right(); i
++) {
389 x
= xCoordOf(i
, plotRect
);
392 y
= yCoordOf( v
, plotRect
, g_absVMax
);
394 penPath
.lineTo(x
, y
);
396 if(GraphPixelsPerPoint
> 10) {
397 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
398 painter
->fillRect(f
, QColor(100, 255, 100));
401 if(v
< vMin
) vMin
= v
;
402 if(v
> vMax
) vMax
= v
;
405 vMean
/= (i
- GraphStart
);
407 painter
->setPen(getColor(graphNum
));
410 int xo
= 5+(graphNum
*40);
411 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
413 int vMarkers
= (g_absVMax
- (g_absVMax
% 10)) / 5;
414 int minYDist
= 40; //Minimum pixel-distance between markers
421 for(v
= vMarkers
; yCoordOf(v
,plotRect
,g_absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
423 int y0
= yCoordOf(v
,plotRect
,g_absVMax
);
424 int y1
= yCoordOf(-v
,plotRect
,g_absVMax
);
426 if(lasty0
- y0
< minYDist
) continue;
428 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
430 sprintf(yLbl
, "%d", v
);
431 painter
->drawText(xo
+8,y0
+7,yLbl
);
433 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
434 sprintf(yLbl
, "%d",-v
);
435 painter
->drawText(xo
+8, y1
+5 , yLbl
);
440 painter
->drawPath(penPath
);
442 sprintf(str
, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
443 vMax
, vMin
, vMean
, i
, len
, buffer
[CursorAPos
], buffer
[CursorBPos
]);
444 painter
->drawText(20, annotationRect
.bottom() - 23 - 20 * graphNum
, str
);
446 //clock_t end = clock();
447 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
448 //printf("Plot time %f\n", elapsed_secs);
451 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
454 if (PlotGridX
<= 0) return;
455 int offset
= GridOffset
;
456 if (GridLocked
&& PlotGridX
) {
457 offset
= GridOffset
+ PlotGridX
- (GraphStart
% PlotGridX
);
458 } else if (!GridLocked
&& GraphStart
> 0 && PlotGridX
) {
459 offset
= PlotGridX
-((GraphStart
- offset
) % PlotGridX
) + GraphStart
- unlockStart
;
462 if (offset
< 0) offset
+= PlotGridX
;
465 int grid_delta_x
= (int) (PlotGridX
* GraphPixelsPerPoint
);
466 int grid_delta_y
= PlotGridY
;
467 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
468 for(i
= (offset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= grid_delta_x
) {
469 painter
->drawLine(r
.left()+i
, r
.top(), r
.left()+i
, r
.bottom());
473 for(i
= 0; yCoordOf(i
,r
,g_absVMax
) > r
.top(); i
+= grid_delta_y
) {
474 painter
->drawLine( r
.left(), yCoordOf(i
,r
,g_absVMax
), r
.right(), yCoordOf(i
,r
,g_absVMax
) );
475 painter
->drawLine( r
.left(), yCoordOf(i
*-1,r
,g_absVMax
), r
.right(), yCoordOf(i
*-1,r
,g_absVMax
) );
480 #define HEIGHT_INFO 60
481 #define WIDTH_AXES 80
483 void Plot::paintEvent(QPaintEvent
*event
)
486 QPainter
painter(this);
487 QBrush
brush(QColor(100, 255, 100));
488 QPen
pen(QColor(100, 255, 100));
490 painter
.setFont(QFont("Courier New", 10));
496 if (CursorAPos
> GraphTraceLen
)
498 if(CursorBPos
> GraphTraceLen
)
500 if(CursorCPos
> GraphTraceLen
)
502 if(CursorDPos
> GraphTraceLen
)
505 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
506 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
509 painter
.fillRect(rect(), QColor(60, 60, 60));
511 painter
.fillRect(plotRect
, QColor(0, 0, 0));
513 //init graph variables
514 setMaxAndStart(GraphBuffer
,GraphTraceLen
,plotRect
);
517 int zeroHeight
= plotRect
.top() + (plotRect
.bottom() - plotRect
.top()) / 2;
518 painter
.setPen(QColor(100, 100, 100));
519 painter
.drawLine(plotRect
.left(), zeroHeight
, plotRect
.right(), zeroHeight
);
520 // plot X and Y grid lines
521 plotGridLines(&painter
, plotRect
);
523 //Start painting graph
524 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
525 if (showDemod
&& DemodBufferLen
> 8) {
526 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
529 //init graph variables
530 setMaxAndStart(s_Buff
,GraphTraceLen
,plotRect
);
531 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
536 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
538 painter
.setPen(QColor(255, 255, 0));
539 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
541 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
543 painter
.setPen(QColor(255, 0, 255));
544 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
546 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
548 painter
.setPen(QColor(255, 153, 0)); //orange
549 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
551 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
553 painter
.setPen(QColor(0, 0, 205)); //light blue
554 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
559 sprintf(str
, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
560 GraphStart
, CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
561 GraphPixelsPerPoint
,CursorAPos
,CursorBPos
,PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked",GridOffset
);
562 painter
.setPen(QColor(255, 255, 255));
563 painter
.drawText(20, infoRect
.bottom() - 3, str
);
567 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
569 //Need to set this, otherwise we don't receive keypress events
570 setFocusPolicy( Qt::StrongFocus
);
573 QPalette
palette(QColor(0,0,0,0));
574 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
575 palette
.setColor(QPalette::Text
, QColor(255,255,255));
576 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
578 setAutoFillBackground(true);
582 setWindowTitle(tr("Sliders"));
587 void Plot::closeEvent(QCloseEvent
*event
)
591 g_useOverlays
= false;
594 void Plot::mouseMoveEvent(QMouseEvent
*event
)
598 x
= (int)(x
/ GraphPixelsPerPoint
);
600 if((event
->buttons() & Qt::LeftButton
)) {
602 } else if (event
->buttons() & Qt::RightButton
) {
610 void Plot::keyPressEvent(QKeyEvent
*event
)
614 if(event
->modifiers() & Qt::ShiftModifier
) {
616 offset
= PageWidth
- (PageWidth
% PlotGridX
);
620 if(event
->modifiers() & Qt::ControlModifier
)
623 offset
= (int)(20 / GraphPixelsPerPoint
);
625 switch(event
->key()) {
627 if(GraphPixelsPerPoint
<= 50) {
628 GraphPixelsPerPoint
*= 2;
633 if(GraphPixelsPerPoint
>= 0.02) {
634 GraphPixelsPerPoint
/= 2;
639 if(GraphPixelsPerPoint
< 20) {
640 GraphStart
+= offset
;
647 if(GraphPixelsPerPoint
< 20) {
648 GraphStart
-= offset
;
655 if(PlotGridX
|| PlotGridY
) {
659 PlotGridX
= PlotGridXdefault
;
660 PlotGridY
= PlotGridYdefault
;
665 puts("Plot Window Keystrokes:\n");
666 puts(" Key Action\n");
667 puts(" DOWN Zoom in");
668 puts(" G Toggle grid display");
669 puts(" H Show help");
670 puts(" L Toggle lock grid relative to samples");
671 puts(" LEFT Move left");
672 puts(" <CTL>LEFT Move left 1 sample");
673 puts(" <SHIFT>LEFT Page left");
674 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
675 puts(" Q Hide window");
676 puts(" RIGHT Move right");
677 puts(" <CTL>RIGHT Move right 1 sample");
678 puts(" <SHIFT>RIGHT Page right");
679 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
680 puts(" UP Zoom out");
682 puts("Use client window 'data help' for more plot commands\n");
686 GridLocked
= !GridLocked
;
688 GridOffset
+= (GraphStart
- unlockStart
);
690 unlockStart
= GraphStart
;
698 QWidget::keyPressEvent(event
);