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
185 controlWidget
= new QWidget();
186 opsController
= new Ui::Form();
187 opsController
->setupUi(controlWidget
);
188 //Due to quirks in QT Designer, we need to fiddle a bit
189 opsController
->horizontalSlider_dirthr_down
->setMinimum(-128);
190 opsController
->horizontalSlider_dirthr_down
->setMaximum(0);
191 opsController
->horizontalSlider_dirthr_down
->setValue(-20);
192 opsController
->horizontalSlider_dirthr_up
->setMinimum(-40);
193 opsController
->horizontalSlider_dirthr_up
->setMaximum(128);
194 opsController
->horizontalSlider_dirthr_up
->setValue(20);
195 opsController
->horizontalSlider_askedge
->setValue(25);
196 opsController
->horizontalSlider_window
->setValue(4000);
199 QObject::connect(opsController
->pushButton_apply
, SIGNAL(clicked()), this, SLOT(applyOperation()));
200 QObject::connect(opsController
->pushButton_sticky
, SIGNAL(clicked()), this, SLOT(stickOperation()));
201 QObject::connect(opsController
->horizontalSlider_window
, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int)));
202 QObject::connect(opsController
->horizontalSlider_dirthr_up
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int)));
203 QObject::connect(opsController
->horizontalSlider_dirthr_down
, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int)));
204 QObject::connect(opsController
->horizontalSlider_askedge
, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int)));
206 // Set up the plot widget, which does the actual plotting
207 plot
= new Plot(this);
208 QVBoxLayout
*layout
= new QVBoxLayout
;
209 layout
->addWidget(plot
);
211 show(); // places the window on the screen.
213 // Move controller widget below plot
214 controlWidget
->move(x(),y()+frameSize().height());
215 controlWidget
->resize(size().width(), controlWidget
->size().height());
216 controlWidget
->show();
219 // not 100% sure what i need in this block
220 // feel free to fix - marshmellow...
221 ProxWidget::~ProxWidget(void)
224 controlWidget
->close();
225 delete controlWidget
;
226 controlWidget
= NULL
;
230 delete opsController
;
231 opsController
= NULL
;
240 void ProxWidget::closeEvent(QCloseEvent
*event
)
244 g_useOverlays
= false;
246 void ProxWidget::hideEvent(QHideEvent
*event
) {
247 controlWidget
->hide();
250 void ProxWidget::showEvent(QShowEvent
*event
) {
251 controlWidget
->show();
255 //----------- Plotting
257 int Plot::xCoordOf(int i
, QRect r
)
259 return r
.left() + (int)((i
- GraphStart
)*GraphPixelsPerPoint
);
262 int Plot::yCoordOf(int v
, QRect r
, int maxVal
)
264 int z
= (r
.bottom() - r
.top())/2;
265 if ( maxVal
== 0 ) maxVal
++;
266 return -(z
* v
) / maxVal
+ z
;
269 int Plot::valueOf_yCoord(int y
, QRect r
, int maxVal
)
271 int z
= (r
.bottom() - r
.top())/2;
272 return (y
-z
) * maxVal
/ z
;
274 static const QColor GREEN
= QColor(100,255,100);
275 static const QColor RED
= QColor(255,100,100);
276 static const QColor BLUE
= QColor(100,100,255);
277 static const QColor GRAY
= QColor(240,240,240);
279 QColor
Plot::getColor(int graphNum
)
282 case 0: return GREEN
; //Green
283 case 1: return RED
; //Red
284 case 2: return BLUE
; //Blue
285 default: return GRAY
; //Gray
289 void Plot::setMaxAndStart(int *buffer
, int len
, QRect plotRect
)
291 if (len
== 0) return;
292 startMax
= (len
- (int)((plotRect
.right() - plotRect
.left() - 40) / GraphPixelsPerPoint
));
296 if(GraphStart
> startMax
) {
297 GraphStart
= startMax
;
299 if (GraphStart
> len
) return;
300 int vMin
= INT_MAX
, vMax
= INT_MIN
, v
= 0;
301 int sample_index
= GraphStart
;
302 for( ; sample_index
< len
&& xCoordOf(sample_index
,plotRect
) < plotRect
.right() ; sample_index
++) {
304 v
= buffer
[sample_index
];
305 if(v
< vMin
) vMin
= v
;
306 if(v
> vMax
) vMax
= v
;
310 if(fabs( (double) vMin
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMin
);
311 if(fabs( (double) vMax
) > g_absVMax
) g_absVMax
= (int)fabs( (double) vMax
);
312 g_absVMax
= (int)(g_absVMax
*1.25 + 1);
315 void Plot::PlotDemod(uint8_t *buffer
, size_t len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
, int plotOffset
)
317 if (len
== 0 || PlotGridX
<= 0) return;
318 //clock_t begin = clock();
319 QPainterPath penPath
;
321 int grid_delta_x
= PlotGridX
;
322 int first_delta_x
= grid_delta_x
; //(plotOffset > 0) ? PlotGridX : (PlotGridX +);
323 if (GraphStart
> plotOffset
) first_delta_x
-= (GraphStart
-plotOffset
);
324 int DemodStart
= GraphStart
;
325 if (plotOffset
> GraphStart
) DemodStart
= plotOffset
;
329 if (DemodStart
-plotOffset
> 0) BitStart
= (int)(((DemodStart
-plotOffset
)+(PlotGridX
-1))/PlotGridX
)-1;
330 first_delta_x
+= BitStart
* PlotGridX
;
331 if (BitStart
> (int)len
) return;
334 //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart);
336 painter
->setPen(getColor(graphNum
));
338 int absVMax
= (int)(100*1.05+1);
339 int x
= xCoordOf(DemodStart
, plotRect
);
340 int y
= yCoordOf((buffer
[BitStart
]*200-100)*-1,plotRect
,absVMax
);
341 penPath
.moveTo(x
, y
);
343 int clk
= first_delta_x
;
344 for(int i
= BitStart
; i
< (int)len
&& xCoordOf(delta_x
+DemodStart
, plotRect
) < plotRect
.right(); i
++) {
345 for (int ii
= 0; ii
< (clk
) && i
< (int)len
&& xCoordOf(DemodStart
+delta_x
+ii
, plotRect
) < plotRect
.right() ; ii
++ ) {
346 x
= xCoordOf(DemodStart
+delta_x
+ii
, plotRect
);
347 v
= buffer
[i
]*200-100;
349 y
= yCoordOf( v
, plotRect
, absVMax
);
351 penPath
.lineTo(x
, y
);
353 if(GraphPixelsPerPoint
> 10) {
354 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
355 painter
->fillRect(f
, QColor(100, 255, 100));
357 if (ii
== (int)clk
/2) {
359 sprintf(str
, "%u",buffer
[i
]);
360 painter
->drawText(x
-8, y
+ ((buffer
[i
] > 0) ? 18 : -6), str
);
368 painter
->drawPath(penPath
);
371 void Plot::PlotGraph(int *buffer
, int len
, QRect plotRect
, QRect annotationRect
, QPainter
*painter
, int graphNum
)
373 if (len
== 0) return;
374 //clock_t begin = clock();
375 QPainterPath penPath
;
376 int vMin
= INT_MAX
, vMax
= INT_MIN
, vMean
= 0, v
= 0, i
= 0;
377 int x
= xCoordOf(GraphStart
, plotRect
);
378 int y
= yCoordOf(buffer
[GraphStart
],plotRect
,g_absVMax
);
379 penPath
.moveTo(x
, y
);
380 for(i
= GraphStart
; i
< len
&& xCoordOf(i
, plotRect
) < plotRect
.right(); i
++) {
382 x
= xCoordOf(i
, plotRect
);
385 y
= yCoordOf( v
, plotRect
, g_absVMax
);
387 penPath
.lineTo(x
, y
);
389 if(GraphPixelsPerPoint
> 10) {
390 QRect
f(QPoint(x
- 3, y
- 3),QPoint(x
+ 3, y
+ 3));
391 painter
->fillRect(f
, QColor(100, 255, 100));
394 if(v
< vMin
) vMin
= v
;
395 if(v
> vMax
) vMax
= v
;
398 vMean
/= (i
- GraphStart
);
400 painter
->setPen(getColor(graphNum
));
403 int xo
= 5+(graphNum
*40);
404 painter
->drawLine(xo
, plotRect
.top(),xo
, plotRect
.bottom());
406 int vMarkers
= (g_absVMax
- (g_absVMax
% 10)) / 5;
407 int minYDist
= 40; //Minimum pixel-distance between markers
414 for(v
= vMarkers
; yCoordOf(v
,plotRect
,g_absVMax
) > plotRect
.top() && n
< 20; v
+= vMarkers
,n
++)
416 int y0
= yCoordOf(v
,plotRect
,g_absVMax
);
417 int y1
= yCoordOf(-v
,plotRect
,g_absVMax
);
419 if(lasty0
- y0
< minYDist
) continue;
421 painter
->drawLine(xo
-5,y0
, xo
+5, y0
);
423 sprintf(yLbl
, "%d", v
);
424 painter
->drawText(xo
+8,y0
+7,yLbl
);
426 painter
->drawLine(xo
-5, y1
, xo
+5, y1
);
427 sprintf(yLbl
, "%d",-v
);
428 painter
->drawText(xo
+8, y1
+5 , yLbl
);
433 painter
->drawPath(penPath
);
435 sprintf(str
, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]",
436 vMax
, vMin
, vMean
, i
, len
, buffer
[CursorAPos
], buffer
[CursorBPos
]);
437 painter
->drawText(20, annotationRect
.bottom() - 23 - 20 * graphNum
, str
);
439 //clock_t end = clock();
440 //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
441 //printf("Plot time %f\n", elapsed_secs);
444 void Plot::plotGridLines(QPainter
* painter
,QRect r
)
447 if (PlotGridX
<= 0) return;
448 int offset
= GridOffset
;
449 if (GridLocked
&& PlotGridX
) {
450 offset
= GridOffset
+ PlotGridX
- (GraphStart
% PlotGridX
);
451 } else if (!GridLocked
&& GraphStart
> 0 && PlotGridX
) {
452 offset
= PlotGridX
-((GraphStart
- offset
) % PlotGridX
) + GraphStart
- unlockStart
;
455 if (offset
< 0) offset
+= PlotGridX
;
458 int grid_delta_x
= (int) (PlotGridX
* GraphPixelsPerPoint
);
459 int grid_delta_y
= PlotGridY
;
460 if ((PlotGridX
> 0) && ((PlotGridX
* GraphPixelsPerPoint
) > 1)) {
461 for(i
= (offset
* GraphPixelsPerPoint
); i
< r
.right(); i
+= grid_delta_x
) {
462 painter
->drawLine(r
.left()+i
, r
.top(), r
.left()+i
, r
.bottom());
466 for(i
= 0; yCoordOf(i
,r
,g_absVMax
) > r
.top(); i
+= grid_delta_y
) {
467 painter
->drawLine( r
.left(), yCoordOf(i
,r
,g_absVMax
), r
.right(), yCoordOf(i
,r
,g_absVMax
) );
468 painter
->drawLine( r
.left(), yCoordOf(i
*-1,r
,g_absVMax
), r
.right(), yCoordOf(i
*-1,r
,g_absVMax
) );
473 #define HEIGHT_INFO 60
474 #define WIDTH_AXES 80
476 void Plot::paintEvent(QPaintEvent
*event
)
479 QPainter
painter(this);
480 QBrush
brush(QColor(100, 255, 100));
481 QPen
pen(QColor(100, 255, 100));
483 painter
.setFont(QFont("Courier New", 10));
489 if (CursorAPos
> GraphTraceLen
)
491 if(CursorBPos
> GraphTraceLen
)
493 if(CursorCPos
> GraphTraceLen
)
495 if(CursorDPos
> GraphTraceLen
)
498 QRect
plotRect(WIDTH_AXES
, 0, width()-WIDTH_AXES
, height()-HEIGHT_INFO
);
499 QRect
infoRect(0, height()-HEIGHT_INFO
, width(), HEIGHT_INFO
);
502 painter
.fillRect(rect(), QColor(60, 60, 60));
504 painter
.fillRect(plotRect
, QColor(0, 0, 0));
506 //init graph variables
507 setMaxAndStart(GraphBuffer
,GraphTraceLen
,plotRect
);
510 int zeroHeight
= plotRect
.top() + (plotRect
.bottom() - plotRect
.top()) / 2;
511 painter
.setPen(QColor(100, 100, 100));
512 painter
.drawLine(plotRect
.left(), zeroHeight
, plotRect
.right(), zeroHeight
);
513 // plot X and Y grid lines
514 plotGridLines(&painter
, plotRect
);
516 //Start painting graph
517 PlotGraph(GraphBuffer
, GraphTraceLen
,plotRect
,infoRect
,&painter
,0);
518 if (showDemod
&& DemodBufferLen
> 8) {
519 PlotDemod(DemodBuffer
, DemodBufferLen
,plotRect
,infoRect
,&painter
,2,g_DemodStartIdx
);
522 //init graph variables
523 setMaxAndStart(s_Buff
,GraphTraceLen
,plotRect
);
524 PlotGraph(s_Buff
, GraphTraceLen
,plotRect
,infoRect
,&painter
,1);
529 if(CursorAPos
> GraphStart
&& xCoordOf(CursorAPos
, plotRect
) < plotRect
.right())
531 painter
.setPen(QColor(255, 255, 0));
532 painter
.drawLine(xCoordOf(CursorAPos
, plotRect
),plotRect
.top(),xCoordOf(CursorAPos
, plotRect
),plotRect
.bottom());
534 if(CursorBPos
> GraphStart
&& xCoordOf(CursorBPos
, plotRect
) < plotRect
.right())
536 painter
.setPen(QColor(255, 0, 255));
537 painter
.drawLine(xCoordOf(CursorBPos
, plotRect
),plotRect
.top(),xCoordOf(CursorBPos
, plotRect
),plotRect
.bottom());
539 if(CursorCPos
> GraphStart
&& xCoordOf(CursorCPos
, plotRect
) < plotRect
.right())
541 painter
.setPen(QColor(255, 153, 0)); //orange
542 painter
.drawLine(xCoordOf(CursorCPos
, plotRect
),plotRect
.top(),xCoordOf(CursorCPos
, plotRect
),plotRect
.bottom());
544 if(CursorDPos
> GraphStart
&& xCoordOf(CursorDPos
, plotRect
) < plotRect
.right())
546 painter
.setPen(QColor(0, 0, 205)); //light blue
547 painter
.drawLine(xCoordOf(CursorDPos
, plotRect
),plotRect
.top(),xCoordOf(CursorDPos
, plotRect
),plotRect
.bottom());
552 sprintf(str
, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d",
553 GraphStart
, CursorBPos
- CursorAPos
, (CursorBPos
- CursorAPos
)/CursorScaleFactor
,
554 GraphPixelsPerPoint
,CursorAPos
,CursorBPos
,PlotGridXdefault
,PlotGridYdefault
,GridLocked
?"Locked":"Unlocked",GridOffset
);
555 painter
.setPen(QColor(255, 255, 255));
556 painter
.drawText(20, infoRect
.bottom() - 3, str
);
560 Plot::Plot(QWidget
*parent
) : QWidget(parent
), GraphStart(0), GraphPixelsPerPoint(1)
562 //Need to set this, otherwise we don't receive keypress events
563 setFocusPolicy( Qt::StrongFocus
);
566 QPalette
palette(QColor(0,0,0,0));
567 palette
.setColor(QPalette::WindowText
, QColor(255,255,255));
568 palette
.setColor(QPalette::Text
, QColor(255,255,255));
569 palette
.setColor(QPalette::Button
, QColor(100, 100, 100));
571 setAutoFillBackground(true);
575 setWindowTitle(tr("Sliders"));
580 void Plot::closeEvent(QCloseEvent
*event
)
584 g_useOverlays
= false;
587 void Plot::mouseMoveEvent(QMouseEvent
*event
)
591 x
= (int)(x
/ GraphPixelsPerPoint
);
593 if((event
->buttons() & Qt::LeftButton
)) {
595 } else if (event
->buttons() & Qt::RightButton
) {
603 void Plot::keyPressEvent(QKeyEvent
*event
)
607 if(event
->modifiers() & Qt::ShiftModifier
) {
609 offset
= PageWidth
- (PageWidth
% PlotGridX
);
613 if(event
->modifiers() & Qt::ControlModifier
)
616 offset
= (int)(20 / GraphPixelsPerPoint
);
618 switch(event
->key()) {
620 if(GraphPixelsPerPoint
<= 50) {
621 GraphPixelsPerPoint
*= 2;
626 if(GraphPixelsPerPoint
>= 0.02) {
627 GraphPixelsPerPoint
/= 2;
632 if(GraphPixelsPerPoint
< 20) {
633 GraphStart
+= offset
;
640 if(GraphPixelsPerPoint
< 20) {
641 GraphStart
-= offset
;
648 if(PlotGridX
|| PlotGridY
) {
652 PlotGridX
= PlotGridXdefault
;
653 PlotGridY
= PlotGridYdefault
;
658 puts("Plot Window Keystrokes:\n");
659 puts(" Key Action\n");
660 puts(" DOWN Zoom in");
661 puts(" G Toggle grid display");
662 puts(" H Show help");
663 puts(" L Toggle lock grid relative to samples");
664 puts(" LEFT Move left");
665 puts(" <CTL>LEFT Move left 1 sample");
666 puts(" <SHIFT>LEFT Page left");
667 puts(" LEFT-MOUSE-CLICK Set yellow cursor");
668 puts(" Q Hide window");
669 puts(" RIGHT Move right");
670 puts(" <CTL>RIGHT Move right 1 sample");
671 puts(" <SHIFT>RIGHT Page right");
672 puts(" RIGHT-MOUSE-CLICK Set purple cursor");
673 puts(" UP Zoom out");
675 puts("Use client window 'data help' for more plot commands\n");
679 GridLocked
= !GridLocked
;
681 GridOffset
+= (GraphStart
- unlockStart
);
683 unlockStart
= GraphStart
;
691 QWidget::keyPressEvent(event
);