This video is the effect I want,I referenced that oscilloscope code,but I did not use that QwtPlotDirectPainter class,because I can't achieve the effect I want?there are 12 curves refreshed at the same time,but high CPU usage,average over 90% on my pi 3b.


2020-05-08_234635.jpg
2020-05-08_230040.jpg
2020-05-08_224200.jpg
This is my code compression package.myoscilloscope.zip

I use below method to update the graph in real time.
Qt Code:
  1. //class MPlotWaveCurve inherit from QwtPlotCurve
  2. void MPlotWaveCurve::drawCurve(QPainter * painter, int style,
  3. const QwtScaleMap & xMap, const QwtScaleMap & yMap,
  4. const QRectF & canvasRect, int from, int to) const
  5. {
  6. switch (style)
  7. {
  8. case Lines:
  9. if (testCurveAttribute(Fitted))
  10. {
  11. // we always need the complete
  12. // curve for fitting
  13. from = 0;
  14. to = dataSize() - 1;
  15. }
  16. /*
  17. below 10 lines code achieved my effect,
  18. variable m_iEaserCount represents the erased width,
  19. variable m_iCurrentPoint represents the latest data index
  20. */
  21. if ((from < m_iCurrentPoint) && (m_iCurrentPoint < to - m_iEaserCount))
  22. {
  23. drawLines(painter, xMap, yMap, canvasRect, from, m_iCurrentPoint);
  24. drawLines(painter, xMap, yMap, canvasRect, m_iCurrentPoint + m_iEaserCount, to);
  25. }
  26. else
  27. {
  28. drawLines(painter, xMap, yMap, canvasRect, from, m_iCurrentPoint);
  29. }
  30. break;
  31. case Sticks:
  32. drawSticks(painter, xMap, yMap, canvasRect, from, to);
  33. break;
  34. case Steps:
  35. drawSteps(painter, xMap, yMap, canvasRect, from, to);
  36. break;
  37. case Dots:
  38. drawDots(painter, xMap, yMap, canvasRect, from, to);
  39. break;
  40. case NoCurve:
  41. default:
  42. break;
  43. }
  44. }
To copy to clipboard, switch view to plain text mode 

Calculate the current refreshed area first, then draw the points that need to be drawn currently.The data container size is 1000.
Qt Code:
  1. void Plot::updateCurve(int index)
  2. {
  3. CurveData *curveData = static_cast<CurveData *>( d_curve[index]->data() );
  4. curveData->lock();
  5.  
  6. int iCurrentPoint = curveData->getCurrentPointIndex();//Get the index number of the latest data in the container, starting from 0
  7. int iMaxPointCount = curveData->getMaxPointCount();//Get the capacity of a container
  8. int size = curveData->size();//Get the current size of the container
  9. if(iCurrentPoint == m_iLastPaintedPoint)
  10. {
  11. curveData->unlock();
  12. return;
  13. }
  14. QRect canvasRect = canvas()->contentsRect();
  15.  
  16. QRegion region;//Save the area to be redrawn
  17. const QwtScaleMap xMap = canvasMap( d_curve[index]->xAxis() );
  18. const QwtScaleMap yMap = canvasMap( d_curve[index]->yAxis() );
  19.  
  20. //Get the last starting X coordinate
  21. int calc_start = (m_iLastPaintedPoint > 0)?m_iLastPaintedPoint -1:m_iLastPaintedPoint;
  22. double start_x = xMap.transform( d_curve[index]->sample(calc_start).x() );
  23.  
  24. if ( iCurrentPoint > m_iLastPaintedPoint )//The data does not appear to flip, new data is added from the back
  25. {
  26. /*
  27.   Depending on the platform setting a clip might be an important
  28.   performance issue. F.e. for Qt Embedded this reduces the
  29.   part of the backing store that has to be copied out - maybe
  30.   to an unaccelerated frame buffer device.
  31.   */
  32. QRect br;
  33. if(size < iMaxPointCount)//The initial container is not full
  34. {
  35. double stop_x = xMap.transform( d_curve[index]->sample(iCurrentPoint).x());
  36. br = QRect( start_x, canvasRect.top(), stop_x - start_x, canvasRect.height());
  37. }
  38. else//After the container is full
  39. {
  40. if(iCurrentPoint + d_iEaserCount <= iMaxPointCount - 1)//If the erased point does not exceed the boundary
  41. {
  42. double stop_x = xMap.transform( d_curve[index]->sample(iCurrentPoint + d_iEaserCount).x());
  43. br = QRect( start_x, canvasRect.top(), stop_x - start_x, canvasRect.height());
  44. }
  45. else//If the boundary is exceeded, only the remaining part is erased
  46. {
  47. br = QRect( start_x, canvasRect.top(), canvasRect.right() - start_x, canvasRect.height());
  48. }
  49. }
  50. region += br;
  51. }
  52. else //Data flipped
  53. {
  54. //Tail
  55. double stop_x = xMap.transform( d_curve[index]->sample(iCurrentPoint + d_iEaserCount).x());
  56. double stop2_x = xMap.transform( d_curve[index]->sample(iMaxPointCount - 1).x());
  57. QRect r1( start_x , canvasRect.top(), stop2_x - start_x , canvasRect.height());
  58. region += r1;
  59. //Head
  60. double start2_x = xMap.transform( d_curve[index]->sample(0).x());
  61. QRect r2( start2_x, canvasRect.top(), stop_x - start2_x, canvasRect.height());
  62. region += r2;
  63. }
  64. //Set the latest data index of 12 curves
  65. for(int i = 0;i < 12;i++)
  66. {
  67. d_curve[i]->SetCurrentPoint(iCurrentPoint);
  68. }
  69.  
  70. m_iLastPaintedPoint = iCurrentPoint;
  71. curveData->unlock();
  72. //Partial refresh,replot 12 curves
  73. canvas()->update(region);
  74. }
To copy to clipboard, switch view to plain text mode 

In order to achieve the effect I want, how to reduce CPU consumption ?