From: marshmellow42 Date: Thu, 13 Apr 2017 21:24:33 +0000 (-0400) Subject: Merge branch 'graphwork2' into graphwork X-Git-Tag: v3.0.0~12^2~11^2 X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/c4809fd411a00e9d0ce2b7ad62dc9a19a9c7a89d?hp=-c Merge branch 'graphwork2' into graphwork --- c4809fd411a00e9d0ce2b7ad62dc9a19a9c7a89d diff --combined client/cmddata.c index 8c2049ad,664ef850..e291c924 --- a/client/cmddata.c +++ b/client/cmddata.c @@@ -28,8 -28,6 +28,8 @@@ uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN]; uint8_t g_debugMode=0; size_t DemodBufferLen=0; +int g_DemodStartIdx=0; +int g_DemodClock=0; static int CmdHelp(const char *Cmd); @@@ -247,6 -245,7 +247,6 @@@ int ASKDemod_ext(const char *Cmd, bool return 0; } if (verbose || g_debugMode) PrintAndLog("\nUsing Clock:%d, Invert:%d, Bits Found:%d",clk,invert,BitLen); - //output setDemodBuf(BitStream,BitLen,0); setClockGrid(clk, startIdx); @@@ -296,11 -295,11 +296,11 @@@ int Cmdaskmandemod(const char *Cmd } bool st = true; if (Cmd[0]=='s') - return ASKDemod_ext(Cmd++, true, true, 1, &st); + return ASKDemod_ext(Cmd++, true, false, 1, &st); else if (Cmd[1] == 's') - return ASKDemod_ext(Cmd+=2, true, true, 1, &st); + return ASKDemod_ext(Cmd+=2, true, false, 1, &st); else - return ASKDemod(Cmd, true, true, 1); + return ASKDemod(Cmd, true, false, 1); } //by marshmellow @@@ -394,7 -393,7 +394,7 @@@ int CmdBiphaseDecodeRaw(const char *Cmd uint8_t BitStream[MAX_DEMOD_BUF_LEN]={0}; size = sizeof(BitStream); if ( !getDemodBuf(BitStream, &size) ) return 0; - errCnt=BiphaseRawDecode(BitStream, &size, offset, invert); + errCnt=BiphaseRawDecode(BitStream, &size, &offset, invert); if (errCnt<0){ PrintAndLog("Error during decode:%d", errCnt); return 0; @@@ -407,12 -406,10 +407,12 @@@ if (errCnt>0){ PrintAndLog("# Errors found during Demod (shown as 7 in bit stream): %d",errCnt); } + PrintAndLog("Biphase Decoded using offset: %d - # invert:%d - data:",offset,invert); PrintAndLog("%s", sprint_bin_break(BitStream, size, 16)); if (offset) setDemodBuf(DemodBuffer,DemodBufferLen-offset, offset); //remove first bit from raw demod + setClockGrid(g_DemodClock, g_DemodStartIdx + g_DemodClock*offset/2); return 1; } @@@ -425,28 -422,26 +425,28 @@@ int ASKbiphaseDemod(const char *Cmd, bo sscanf(Cmd, "%i %i %i %i", &offset, &clk, &invert, &maxErr); uint8_t BitStream[MAX_GRAPH_TRACE_LEN]; - size_t size = getFromGraphBuf(BitStream); + size_t size = getFromGraphBuf(BitStream); + int startIdx = 0; //invert here inverts the ask raw demoded bits which has no effect on the demod, but we need the pointer - int errCnt = askdemod(BitStream, &size, &clk, &invert, maxErr, 0, 0); + int errCnt = askdemod_ext(BitStream, &size, &clk, &invert, maxErr, 0, 0, &startIdx); if ( errCnt < 0 || errCnt > maxErr ) { if (g_debugMode) PrintAndLog("DEBUG: no data or error found %d, clock: %d", errCnt, clk); return 0; - } + } //attempt to Biphase decode BitStream - errCnt = BiphaseRawDecode(BitStream, &size, offset, invert); + errCnt = BiphaseRawDecode(BitStream, &size, &offset, invert); if (errCnt < 0){ if (g_debugMode || verbose) PrintAndLog("Error BiphaseRawDecode: %d", errCnt); return 0; - } + } if (errCnt > maxErr) { if (g_debugMode || verbose) PrintAndLog("Error BiphaseRawDecode too many errors: %d", errCnt); return 0; } //success set DemodBuffer and return setDemodBuf(BitStream, size, 0); + setClockGrid(clk, startIdx + clk*offset/2); if (g_debugMode || verbose){ PrintAndLog("Biphase Decoded using offset: %d - clock: %d - # errors:%d - data:",offset,clk,errCnt); printDemodBuff(); @@@ -507,44 -502,45 +507,44 @@@ int Cmdaskrawdemod(const char *Cmd return ASKDemod(Cmd, true, false, 0); } -int AutoCorrelate(int window, bool SaveGrph, bool verbose) +int AutoCorrelate(const int *in, int *out, size_t len, int window, bool SaveGrph, bool verbose) { static int CorrelBuffer[MAX_GRAPH_TRACE_LEN]; size_t Correlation = 0; int maxSum = 0; int lastMax = 0; if (verbose) PrintAndLog("performing %d correlations", GraphTraceLen - window); - for (int i = 0; i < GraphTraceLen - window; ++i) { + for (int i = 0; i < len - window; ++i) { int sum = 0; for (int j = 0; j < window; ++j) { - sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256; + sum += (in[j]*in[i + j]) / 256; } CorrelBuffer[i] = sum; - if (sum >= maxSum-100 && sum <= maxSum+100){ + if (sum >= maxSum-100 && sum <= maxSum+100) { //another max Correlation = i-lastMax; lastMax = i; if (sum > maxSum) maxSum = sum; - } else if (sum > maxSum){ + } else if (sum > maxSum) { maxSum=sum; lastMax = i; } } - if (Correlation==0){ + if (Correlation==0) { //try again with wider margin - for (int i = 0; i < GraphTraceLen - window; i++){ - if (CorrelBuffer[i] >= maxSum-(maxSum*0.05) && CorrelBuffer[i] <= maxSum+(maxSum*0.05)){ + for (int i = 0; i < len - window; i++) { + if (CorrelBuffer[i] >= maxSum-(maxSum*0.05) && CorrelBuffer[i] <= maxSum+(maxSum*0.05)) { //another max Correlation = i-lastMax; lastMax = i; - //if (CorrelBuffer[i] > maxSum) maxSum = sum; } } } if (verbose && Correlation > 0) PrintAndLog("Possible Correlation: %d samples",Correlation); - if (SaveGrph){ - GraphTraceLen = GraphTraceLen - window; - memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int)); + if (SaveGrph) { + //GraphTraceLen = GraphTraceLen - window; + memcpy(out, CorrelBuffer, len * sizeof(int)); RepaintGraphWindow(); } return Correlation; @@@ -577,7 -573,7 +577,7 @@@ int CmdAutoCorr(const char *Cmd return 0; } if (grph == 'g') updateGrph=true; - return AutoCorrelate(window, updateGrph, true); + return AutoCorrelate(GraphBuffer, GraphBuffer, GraphTraceLen, window, updateGrph, true); } int CmdBitsamples(const char *Cmd) @@@ -680,18 -676,6 +680,18 @@@ int CmdGraphShiftZero(const char *Cmd return 0; } +int AskEdgeDetect(const int *in, int *out, int len, int threshold) { + int Last = 0; + for(int i = 1; i= threshold) //large jump up + Last = 127; + else if(in[i]-in[i-1] <= -1 * threshold) //large jump down + Last = -127; + out[i-1] = Last; + } + return 0; +} + //by marshmellow //use large jumps in read samples to identify edges of waves and then amplify that wave to max //similar to dirtheshold, threshold commands @@@ -699,12 -683,18 +699,12 @@@ int CmdAskEdgeDetect(const char *Cmd) { int thresLen = 25; - int Last = 0; + int ans = 0; sscanf(Cmd, "%i", &thresLen); - for(int i = 1; i=thresLen) //large jump up - Last = 127; - else if(GraphBuffer[i]-GraphBuffer[i-1]<=-1*thresLen) //large jump down - Last = -127; - GraphBuffer[i-1] = Last; - } + ans = AskEdgeDetect(GraphBuffer, GraphBuffer, GraphTraceLen, thresLen); RepaintGraphWindow(); - return 0; + return ans; } /* Print our clock rate */ @@@ -807,8 -797,8 +807,8 @@@ int FSKrawDemod(const char *Cmd, bool v if (size > 0) { setDemodBuf(BitStream,size,0); setClockGrid(rfLen, startIdx); - - // Now output the bitstream to the scrollback by line of 16 bits + - // Now output the bitstream to the scrollback by line of 16 bits ++ // Now output the bitstream to the scrollback by line of 16 bits if (verbose || g_debugMode) { PrintAndLog("\nUsing Clock:%u, invert:%u, fchigh:%u, fclow:%u", (unsigned int)rfLen, (unsigned int)invert, (unsigned int)fchigh, (unsigned int)fclow); PrintAndLog("%s decoded bitstream:",GetFSKType(fchigh,fclow,invert)); @@@ -1067,10 -1057,7 +1067,11 @@@ int CmdRawDemod(const char *Cmd } void setClockGrid(int clk, int offset) { - if (offset > clk) offset %= clk; + g_DemodStartIdx = offset; + g_DemodClock = clk; + if (g_debugMode) PrintAndLog("demodoffset %d, clk %d",offset,clk); - if (offset > clk) offset %= clk; ++ ++ if (offset > clk) offset %= clk; if (offset < 0) offset += clk; if (offset > GraphTraceLen || offset < 0) return; @@@ -1226,7 -1213,6 +1227,7 @@@ int getSamples(int n, bool silent GraphTraceLen = n; } + setClockGrid(0,0); RepaintGraphWindow(); return 0; } @@@ -1330,7 -1316,6 +1331,7 @@@ int CmdLoad(const char *Cmd } fclose(f); PrintAndLog("loaded %d samples", GraphTraceLen); + setClockGrid(0,0); RepaintGraphWindow(); return 0; } @@@ -1438,42 -1423,37 +1439,42 @@@ int CmdScale(const char *Cmd return 0; } -int CmdDirectionalThreshold(const char *Cmd) +int directionalThreshold(const int* in, int *out, size_t len, int8_t up, int8_t down) { - int8_t upThres = param_get8(Cmd, 0); - int8_t downThres = param_get8(Cmd, 1); - - printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres); + int lastValue = in[0]; + out[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in. - int lastValue = GraphBuffer[0]; - GraphBuffer[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in. - - for (int i = 1; i < GraphTraceLen; ++i) { + for (int i = 1; i < len; ++i) { // Apply first threshold to samples heading up - if (GraphBuffer[i] >= upThres && GraphBuffer[i] > lastValue) + if (in[i] >= up && in[i] > lastValue) { - lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. - GraphBuffer[i] = 127; + lastValue = out[i]; // Buffer last value as we overwrite it. + out[i] = 1; } // Apply second threshold to samples heading down - else if (GraphBuffer[i] <= downThres && GraphBuffer[i] < lastValue) + else if (in[i] <= down && in[i] < lastValue) { - lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. - GraphBuffer[i] = -127; + lastValue = out[i]; // Buffer last value as we overwrite it. + out[i] = -1; } else { - lastValue = GraphBuffer[i]; // Buffer last value as we overwrite it. - GraphBuffer[i] = GraphBuffer[i-1]; - + lastValue = out[i]; // Buffer last value as we overwrite it. + out[i] = out[i-1]; } } - GraphBuffer[0] = GraphBuffer[1]; // Aline with first edited sample. + out[0] = out[1]; // Align with first edited sample. + return 0; +} + +int CmdDirectionalThreshold(const char *Cmd) +{ + int8_t upThres = param_get8(Cmd, 0); + int8_t downThres = param_get8(Cmd, 1); + + printf("Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres); + + directionalThreshold(GraphBuffer, GraphBuffer,GraphTraceLen, upThres, downThres); RepaintGraphWindow(); return 0; } diff --combined client/proxguiqt.cpp index c1fc7e12,85f408f7..c1541999 --- a/client/proxguiqt.cpp +++ b/client/proxguiqt.cpp @@@ -7,6 -7,7 +7,7 @@@ //----------------------------------------------------------------------------- // GUI (QT) //----------------------------------------------------------------------------- + #include "proxguiqt.h" #include #include @@@ -19,14 -20,7 +20,14 @@@ #include #include #include +#include +#include +#include +#include "proxguiqt.h" #include "proxgui.h" +#include +//#include + int startMax; int PageWidth; @@@ -92,7 -86,6 +93,7 @@@ ProxGuiQT::ProxGuiQT(int argc, char **a ProxGuiQT::~ProxGuiQT(void) { if (plotwidget) { + //plotwidget->close(); delete plotwidget; plotwidget = NULL; } @@@ -104,387 -97,199 +105,387 @@@ } } -void ProxWidget::paintEvent(QPaintEvent *event) +//-------------------- +void ProxWidget::applyOperation() { - QPainter painter(this); - QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath; - QRect r; - QBrush brush(QColor(100, 255, 100)); - QPen pen(QColor(100, 255, 100)); + printf("ApplyOperation()"); + save_restoreGB(1); + memcpy(GraphBuffer, s_Buff, sizeof(int) * GraphTraceLen); + RepaintGraphWindow(); - painter.setFont(QFont("Arial", 10)); +} +void ProxWidget::stickOperation() +{ + save_restoreGB(0); + printf("stickOperation()"); +} +void ProxWidget::vchange_autocorr(int v) +{ + int ans; + ans = AutoCorrelate(GraphBuffer, s_Buff, GraphTraceLen, v, true, false); + printf("vchange_autocorr(w:%d): %d\n", v, ans); + RepaintGraphWindow(); +} +void ProxWidget::vchange_askedge(int v) +{ + int ans; + //extern int AskEdgeDetect(const int *in, int *out, int len, int threshold); + ans = AskEdgeDetect(GraphBuffer, s_Buff, GraphTraceLen, v); + printf("vchange_askedge(w:%d)\n", v); + RepaintGraphWindow(); +} +void ProxWidget::vchange_dthr_up(int v) +{ + int down = opsController->horizontalSlider_dirthr_down->value(); + directionalThreshold(GraphBuffer, s_Buff, GraphTraceLen, v, down); + printf("vchange_dthr_up(%d)", v); + RepaintGraphWindow(); - if(GraphStart < 0) { - GraphStart = 0; +} +void ProxWidget::vchange_dthr_down(int v) +{ + printf("vchange_dthr_down(%d)", v); + int up = opsController->horizontalSlider_dirthr_up->value(); + directionalThreshold(GraphBuffer,s_Buff, GraphTraceLen, v, up); + RepaintGraphWindow(); + +} +ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) +{ + this->master = master; + resize(800,500); + + /** Setup the controller widget **/ + + QWidget* controlWidget = new QWidget(); + opsController = new Ui::Form(); + opsController->setupUi(controlWidget); + //Due to quirks in QT Designer, we need to fiddle a bit + opsController->horizontalSlider_dirthr_down->setMinimum(-128); + opsController->horizontalSlider_dirthr_down->setMaximum(0); + opsController->horizontalSlider_dirthr_down->setValue(-20); + + + QObject::connect(opsController->pushButton_apply, SIGNAL(clicked()), this, SLOT(applyOperation())); + QObject::connect(opsController->pushButton_sticky, SIGNAL(clicked()), this, SLOT(stickOperation())); + QObject::connect(opsController->horizontalSlider_window, SIGNAL(valueChanged(int)), this, SLOT(vchange_autocorr(int))); + QObject::connect(opsController->horizontalSlider_dirthr_up, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_up(int))); + QObject::connect(opsController->horizontalSlider_dirthr_down, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int))); + QObject::connect(opsController->horizontalSlider_askedge, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int))); + + controlWidget->show(); + + // Set up the plot widget, which does the actual plotting + + plot = new Plot(this); + /* + QSlider* slider = new QSlider(Qt::Horizontal); + slider->setFocusPolicy(Qt::StrongFocus); + slider->setTickPosition(QSlider::TicksBothSides); + slider->setTickInterval(10); + slider->setSingleStep(1); + */ + QVBoxLayout *layout = new QVBoxLayout; + //layout->addWidget(slider); + layout->addWidget(plot); + setLayout(layout); + printf("Proxwidget Constructor just set layout\r\n"); +} + + +//----------- Plotting + +int Plot::xCoordOf(int i, QRect r ) +{ + return r.left() + (int)((i - GraphStart)*GraphPixelsPerPoint); +} + +int Plot::yCoordOf(int v, QRect r, int maxVal) +{ + int z = (r.bottom() - r.top())/2; + return -(z * v) / maxVal + z; +} + +int Plot::valueOf_yCoord(int y, QRect r, int maxVal) +{ + int z = (r.bottom() - r.top())/2; + return (y-z) * maxVal / z; +} +static const QColor GREEN = QColor(100,255,100); +static const QColor RED = QColor(255,100,100); +static const QColor BLUE = QColor(100,100,255); +static const QColor GRAY = QColor(240,240,240); + +QColor Plot::getColor(int graphNum) +{ + switch (graphNum) { + case 0: return GREEN; //Green + case 1: return RED; //Red + case 2: return BLUE; //Blue + default: return GRAY; //Gray } +} - if (CursorAPos > GraphTraceLen) - CursorAPos= 0; - if(CursorBPos > GraphTraceLen) - CursorBPos= 0; - if(CursorCPos > GraphTraceLen) - CursorCPos= 0; - if(CursorDPos > GraphTraceLen) - CursorDPos= 0; +void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum, int plotOffset) +{ + if (len == 0 || PlotGridX <= 0) return; + //clock_t begin = clock(); + QPainterPath penPath; + + int grid_delta_x = PlotGridX; + int first_delta_x = grid_delta_x; //(plotOffset > 0) ? PlotGridX : (PlotGridX +); + if (GraphStart > plotOffset) first_delta_x -= (GraphStart-plotOffset); + int DemodStart = GraphStart; + if (plotOffset > GraphStart) DemodStart = plotOffset; + + int BitStart = 0; + // round down + if (DemodStart-plotOffset > 0) BitStart = (int)(((DemodStart-plotOffset)+(PlotGridX-1))/PlotGridX)-1; + first_delta_x += BitStart * PlotGridX; + if (BitStart > len) return; + int delta_x = 0; + int v = 0; + //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart); + + painter->setPen(getColor(graphNum)); + char str[5]; + int absVMax = (int)(100*1.05+1); + int x = xCoordOf(DemodStart, plotRect); + int y = yCoordOf((buffer[BitStart]*200-100)*-1,plotRect,absVMax); + penPath.moveTo(x, y); + delta_x = 0; + int clk = first_delta_x; + for(int i = BitStart; i < len && xCoordOf(delta_x+DemodStart, plotRect) < plotRect.right(); i++) { + for (int ii = 0; ii < (clk) && i < len && xCoordOf(DemodStart+delta_x+ii, plotRect) < plotRect.right() ; ii++ ) { + x = xCoordOf(DemodStart+delta_x+ii, plotRect); + v = buffer[i]*200-100; + + y = yCoordOf( v, plotRect, absVMax); - r = rect(); + penPath.lineTo(x, y); - painter.fillRect(r, QColor(0, 0, 0)); + if(GraphPixelsPerPoint > 10) { + QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3)); + painter->fillRect(f, QColor(100, 255, 100)); + } + if (ii == (int)clk/2) { + //print label + sprintf(str, "%u",buffer[i]); + painter->drawText(x-8, y + ((buffer[i] > 0) ? 18 : -6), str); + } + } + delta_x += clk; + clk = grid_delta_x; + } - whitePath.moveTo(r.left() + 40, r.top()); - whitePath.lineTo(r.left() + 40, r.bottom()); + //Graph annotations + painter->drawPath(penPath); +} - int zeroHeight = r.top() + (r.bottom() - r.top()) / 2; +void Plot::PlotGraph(int *buffer, int len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum) +{ + if (len == 0) return; + //clock_t begin = clock(); + QPainterPath penPath; - greyPath.moveTo(r.left(), zeroHeight); - greyPath.lineTo(r.right(), zeroHeight); - painter.setPen(QColor(100, 100, 100)); - painter.drawPath(greyPath); - - PageWidth= (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint); - - // plot X and Y grid lines - int i; - if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) { - for(i = 40 + (GridOffset * GraphPixelsPerPoint); i < r.right(); i += (int)(PlotGridX * GraphPixelsPerPoint)) { - //SelectObject(hdc, GreyPenLite); - //MoveToEx(hdc, r.left + i, r.top, NULL); - //LineTo(hdc, r.left + i, r.bottom); - lightgreyPath.moveTo(r.left()+i,r.top()); - lightgreyPath.lineTo(r.left()+i,r.bottom()); - painter.drawPath(lightgreyPath); - } - } - if ((PlotGridY > 0) && ((PlotGridY * GraphPixelsPerPoint) > 1)){ - for(i = 0; i < ((r.top() + r.bottom())>>1); i += (int)(PlotGridY * GraphPixelsPerPoint)) { - lightgreyPath.moveTo(r.left() + 40,zeroHeight + i); - lightgreyPath.lineTo(r.right(),zeroHeight + i); - painter.drawPath(lightgreyPath); - lightgreyPath.moveTo(r.left() + 40,zeroHeight - i); - lightgreyPath.lineTo(r.right(),zeroHeight - i); - painter.drawPath(lightgreyPath); - } - } - - startMax = (GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint)); + startMax = (len - (int)((plotRect.right() - plotRect.left() - 40) / GraphPixelsPerPoint)); if(startMax < 0) { startMax = 0; } if(GraphStart > startMax) { GraphStart = startMax; } - - int absYMax = 1; - - for(i = GraphStart; ; i++) { - if(i >= GraphTraceLen) { - break; - } - if(fabs((double)GraphBuffer[i]) > absYMax) { - absYMax = (int)fabs((double)GraphBuffer[i]); - } - int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); - if(x > r.right()) { - break; - } + if (GraphStart > len) return; + int vMin = INT_MAX, vMax = INT_MIN, vMean = 0, v = 0, absVMax = 0; + int sample_index = GraphStart ; + for( ; sample_index < len && xCoordOf(sample_index,plotRect) < plotRect.right() ; sample_index++) { + + v = buffer[sample_index]; + if(v < vMin) vMin = v; + if(v > vMax) vMax = v; + vMean += v; } - absYMax = (int)(absYMax*1.2 + 1); - + vMean /= (sample_index - GraphStart); + + if(fabs( (double) vMin) > absVMax) absVMax = (int)fabs( (double) vMin); + if(fabs( (double) vMax) > absVMax) absVMax = (int)fabs( (double) vMax); + absVMax = (int)(absVMax*1.25 + 1); // number of points that will be plotted - int span = (int)((r.right() - r.left()) / GraphPixelsPerPoint); + int span = (int)((plotRect.right() - plotRect.left()) / GraphPixelsPerPoint); // one label every 100 pixels, let us say - int labels = (r.right() - r.left() - 40) / 100; + int labels = (plotRect.right() - plotRect.left() - 40) / 100; if(labels <= 0) labels = 1; int pointsPerLabel = span / labels; if(pointsPerLabel <= 0) pointsPerLabel = 1; - int yMin = INT_MAX; - int yMax = INT_MIN; - int yMean = 0; - int n = 0; + int x = xCoordOf(GraphStart, plotRect); + int y = yCoordOf(buffer[GraphStart],plotRect,absVMax); + penPath.moveTo(x, y); + for(int i = GraphStart; i < len && xCoordOf(i, plotRect) < plotRect.right(); i++) { - for(i = GraphStart; ; i++) { - if(i >= GraphTraceLen) { - break; - } - int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint); - if(x > r.right() + GraphPixelsPerPoint) { - break; - } + x = xCoordOf(i, plotRect); + v = buffer[i]; - int y = GraphBuffer[i]; - if(y < yMin) { - yMin = y; - } - if(y > yMax) { - yMax = y; - } - yMean += y; - n++; + y = yCoordOf( v, plotRect, absVMax);//(y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight; - y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight; - if(i == GraphStart) { - penPath.moveTo(x, y); - } else { - penPath.lineTo(x, y); - } + penPath.lineTo(x, y); if(GraphPixelsPerPoint > 10) { QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3)); - painter.fillRect(f, brush); + painter->fillRect(f, QColor(100, 255, 100)); } + } - if(((i - GraphStart) % pointsPerLabel == 0) && i != GraphStart) { - whitePath.moveTo(x, zeroHeight - 3); - whitePath.lineTo(x, zeroHeight + 3); + painter->setPen(getColor(graphNum)); - char str[100]; - sprintf(str, "+%d", (i - GraphStart)); + //Draw y-axis + int xo = 5+(graphNum*40); + painter->drawLine(xo, plotRect.top(),xo, plotRect.bottom()); - painter.setPen(QColor(255, 255, 255)); - QRect size; - QFontMetrics metrics(painter.font()); - size = metrics.boundingRect(str); - painter.drawText(x - (size.right() - size.left()), zeroHeight + 9, str); + int vMarkers = (absVMax - (absVMax % 10)) / 5; + int minYDist = 40; //Minimum pixel-distance between markers - penPath.moveTo(x,y); - } + char yLbl[20]; - if(i == CursorAPos || i == CursorBPos || i == CursorCPos || i == CursorDPos) { - QPainterPath *cursorPath; + int n = 0; + int lasty0 = 65535; - if(i == CursorAPos) { - cursorPath = &cursorAPath; - } else if (i == CursorBPos) { - cursorPath = &cursorBPath; - } else if (i == CursorCPos) { - cursorPath = &cursorCPath; - } else { - cursorPath = &cursorDPath; - } - cursorPath->moveTo(x, r.top()); - cursorPath->lineTo(x, r.bottom()); - penPath.moveTo(x, y); + for(int v = vMarkers; yCoordOf(v,plotRect,absVMax) > plotRect.top() && n < 20; v+= vMarkers ,n++) + { + int y0 = yCoordOf(v,plotRect,absVMax); + int y1 = yCoordOf(-v,plotRect,absVMax); + + if(lasty0 - y0 < minYDist) continue; + + painter->drawLine(xo-5,y0, xo+5, y0); + + sprintf(yLbl, "%d", v); + painter->drawText(xo+8,y0+7,yLbl); + + painter->drawLine(xo-5, y1, xo+5, y1); + sprintf(yLbl, "%d",-v); + painter->drawText(xo+8, y1+5 , yLbl); + lasty0 = y0; + } + + //Graph annotations + painter->drawPath(penPath); + char str[200]; + sprintf(str, "max=%d min=%d mean=%d n=%d/%d CursorAVal=[%d] CursorBVal=[%d]", + vMax, vMin, vMean, sample_index, len, buffer[CursorAPos], buffer[CursorBPos]); + painter->drawText(20, annotationRect.bottom() - 23 - 20 * graphNum, str); + + //clock_t end = clock(); + //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; + //printf("Plot time %f\n", elapsed_secs); +} + +void Plot::plotGridLines(QPainter* painter,QRect r) +{ + int zeroHeight = r.top() + (r.bottom() - r.top()) / 2; + + int i; + int grid_delta_x = (int) (PlotGridX * GraphPixelsPerPoint); + int grid_delta_y = (int) (PlotGridY * GraphPixelsPerPoint); + if ((PlotGridX > 0) && ((PlotGridX * GraphPixelsPerPoint) > 1)) { + for(i = (GridOffset * GraphPixelsPerPoint); i < r.right(); i += grid_delta_x) { + painter->drawLine(r.left()+i, r.top(), r.left()+i, r.bottom()); + } + } + if ((PlotGridY > 0) && ((PlotGridY * GraphPixelsPerPoint) > 1)){ + for(i = 0; i < ((r.top() + r.bottom())>>1); i += grid_delta_y) { + painter->drawLine(r.left(),zeroHeight + i,r.right(),zeroHeight + i); + painter->drawLine(r.left(),zeroHeight - i,r.right(),zeroHeight - i); } } +} + +#define HEIGHT_INFO 60 +#define WIDTH_AXES 80 - if(n != 0) { - yMean /= n; +void Plot::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + //QPainterPath penPath, whitePath, greyPath, lightgreyPath, cursorAPath, cursorBPath, cursorCPath, cursorDPath; + //QRect r; + QBrush brush(QColor(100, 255, 100)); + QPen pen(QColor(100, 255, 100)); + + painter.setFont(QFont("Courier New", 10)); + + if(GraphStart < 0) { + GraphStart = 0; } - painter.setPen(QColor(255, 255, 255)); - painter.drawPath(whitePath); - painter.setPen(pen); - painter.drawPath(penPath); - painter.setPen(QColor(255, 255, 0)); - painter.drawPath(cursorAPath); - painter.setPen(QColor(255, 0, 255)); - painter.drawPath(cursorBPath); - painter.setPen(QColor(255, 153, 0)); //orange - painter.drawPath(cursorCPath); - painter.setPen(QColor(0, 0, 205)); //light blue - painter.drawPath(cursorDPath); + if (CursorAPos > GraphTraceLen) + CursorAPos= 0; + if(CursorBPos > GraphTraceLen) + CursorBPos= 0; + if(CursorCPos > GraphTraceLen) + CursorCPos= 0; + if(CursorDPos > GraphTraceLen) + CursorDPos= 0; - char str[200]; - sprintf(str, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f] zoom=%.3f CursorA=%d [%d] CursorB=%d [%d] GridX=%d GridY=%d (%s)", - GraphStart, yMax, yMin, yMean, n, GraphTraceLen, - CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor,GraphPixelsPerPoint,CursorAPos,GraphBuffer[CursorAPos],CursorBPos,GraphBuffer[CursorBPos],PlotGridXdefault,PlotGridYdefault,GridLocked?"Locked":"Unlocked"); + QRect plotRect(WIDTH_AXES, 0, width()-WIDTH_AXES, height()-HEIGHT_INFO); + QRect infoRect(0, height()-HEIGHT_INFO, width(), HEIGHT_INFO); + + //Grey background + painter.fillRect(rect(), QColor(60, 60, 60)); + //Black foreground + painter.fillRect(plotRect, QColor(0, 0, 0)); + // center line + int zeroHeight = plotRect.top() + (plotRect.bottom() - plotRect.top()) / 2; + painter.setPen(QColor(100, 100, 100)); + painter.drawLine(plotRect.left(), zeroHeight, plotRect.right(), zeroHeight); + // plot X and Y grid lines + plotGridLines(&painter, plotRect); + + //Start painting graph + if (showDemod && DemodBufferLen > 8) { + PlotDemod(DemodBuffer, DemodBufferLen,plotRect,infoRect,&painter,2,g_DemodStartIdx); + } + PlotGraph(s_Buff, GraphTraceLen,plotRect,infoRect,&painter,1); + PlotGraph(GraphBuffer, GraphTraceLen,plotRect,infoRect,&painter,0); + // End graph drawing + + //Draw the cursors + if(CursorAPos > GraphStart && xCoordOf(CursorAPos, plotRect) < plotRect.right()) + { + painter.setPen(QColor(255, 255, 0)); + painter.drawLine(xCoordOf(CursorAPos, plotRect),plotRect.top(),xCoordOf(CursorAPos, plotRect),plotRect.bottom()); + } + if(CursorBPos > GraphStart && xCoordOf(CursorBPos, plotRect) < plotRect.right()) + { + painter.setPen(QColor(255, 0, 255)); + painter.drawLine(xCoordOf(CursorBPos, plotRect),plotRect.top(),xCoordOf(CursorBPos, plotRect),plotRect.bottom()); + } + if(CursorCPos > GraphStart && xCoordOf(CursorCPos, plotRect) < plotRect.right()) + { + painter.setPen(QColor(255, 153, 0)); //orange + painter.drawLine(xCoordOf(CursorCPos, plotRect),plotRect.top(),xCoordOf(CursorCPos, plotRect),plotRect.bottom()); + } + if(CursorDPos > GraphStart && xCoordOf(CursorDPos, plotRect) < plotRect.right()) + { + painter.setPen(QColor(0, 0, 205)); //light blue + painter.drawLine(xCoordOf(CursorDPos, plotRect),plotRect.top(),xCoordOf(CursorDPos, plotRect),plotRect.bottom()); + } + + //Draw annotations + char str[200]; + sprintf(str, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d", + GraphStart, CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor, + GraphPixelsPerPoint,CursorAPos,CursorBPos,PlotGridXdefault,PlotGridYdefault,GridLocked?"Locked":"Unlocked",GridOffset); painter.setPen(QColor(255, 255, 255)); - painter.drawText(50, r.bottom() - 20, str); + painter.drawText(20, infoRect.bottom() - 3, str); + } -ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1) +Plot::Plot(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1) { + //Need to set this, otherwise we don't receive keypress events + setFocusPolicy( Qt::StrongFocus); resize(600, 300); QPalette palette(QColor(0,0,0,0)); @@@ -495,20 -300,18 +496,20 @@@ setAutoFillBackground(true); CursorAPos = 0; CursorBPos = 0; + + setWindowTitle(tr("Sliders")); } -void ProxWidget::closeEvent(QCloseEvent *event) +void Plot::closeEvent(QCloseEvent *event) { event->ignore(); this->hide(); } -void ProxWidget::mouseMoveEvent(QMouseEvent *event) +void Plot::mouseMoveEvent(QMouseEvent *event) { int x = event->x(); - x -= 40; + x -= WIDTH_AXES; x = (int)(x / GraphPixelsPerPoint); x += GraphStart; if((event->buttons() & Qt::LeftButton)) { @@@ -521,7 -324,7 +522,7 @@@ this->update(); } -void ProxWidget::keyPressEvent(QKeyEvent *event) +void Plot::keyPressEvent(QKeyEvent *event) { int offset; int gridchanged;