repo time

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Committer:
darienf
Date:
Tue Apr 06 06:41:40 2021 +0000
Revision:
20:6d2af70c92ab
another repo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darienf 20:6d2af70c92ab 1 /*******************************************************************************
darienf 20:6d2af70c92ab 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
darienf 20:6d2af70c92ab 3 * <p>
darienf 20:6d2af70c92ab 4 * Permission is hereby granted, free of charge, to any person obtaining a
darienf 20:6d2af70c92ab 5 * copy of this software and associated documentation files (the "Software"),
darienf 20:6d2af70c92ab 6 * to deal in the Software without restriction, including without limitation
darienf 20:6d2af70c92ab 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
darienf 20:6d2af70c92ab 8 * and/or sell copies of the Software, and to permit persons to whom the
darienf 20:6d2af70c92ab 9 * Software is furnished to do so, subject to the following conditions:
darienf 20:6d2af70c92ab 10 * <p>
darienf 20:6d2af70c92ab 11 * The above copyright notice and this permission notice shall be included
darienf 20:6d2af70c92ab 12 * in all copies or substantial portions of the Software.
darienf 20:6d2af70c92ab 13 * <p>
darienf 20:6d2af70c92ab 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
darienf 20:6d2af70c92ab 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
darienf 20:6d2af70c92ab 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
darienf 20:6d2af70c92ab 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
darienf 20:6d2af70c92ab 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
darienf 20:6d2af70c92ab 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
darienf 20:6d2af70c92ab 20 * OTHER DEALINGS IN THE SOFTWARE.
darienf 20:6d2af70c92ab 21 * <p>
darienf 20:6d2af70c92ab 22 * Except as contained in this notice, the name of Maxim Integrated
darienf 20:6d2af70c92ab 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
darienf 20:6d2af70c92ab 24 * Products, Inc. Branding Policy.
darienf 20:6d2af70c92ab 25 * <p>
darienf 20:6d2af70c92ab 26 * The mere transfer of this software does not imply any licenses
darienf 20:6d2af70c92ab 27 * of trade secrets, proprietary technology, copyrights, patents,
darienf 20:6d2af70c92ab 28 * trademarks, maskwork rights, or any other form of intellectual
darienf 20:6d2af70c92ab 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
darienf 20:6d2af70c92ab 30 * ownership rights.
darienf 20:6d2af70c92ab 31 * ******************************************************************************
darienf 20:6d2af70c92ab 32 */
darienf 20:6d2af70c92ab 33 package graphing;
darienf 20:6d2af70c92ab 34
darienf 20:6d2af70c92ab 35 import android.content.Context;
darienf 20:6d2af70c92ab 36 import android.graphics.Bitmap;
darienf 20:6d2af70c92ab 37 import android.graphics.Canvas;
darienf 20:6d2af70c92ab 38 import android.graphics.Color;
darienf 20:6d2af70c92ab 39 import android.graphics.Paint;
darienf 20:6d2af70c92ab 40 import android.util.AttributeSet;
darienf 20:6d2af70c92ab 41 import android.view.View;
darienf 20:6d2af70c92ab 42
darienf 20:6d2af70c92ab 43 import java.util.ArrayList;
darienf 20:6d2af70c92ab 44
darienf 20:6d2af70c92ab 45 /**
darienf 20:6d2af70c92ab 46 * Provides simple line graphing for one or multiple lines on a single graph
darienf 20:6d2af70c92ab 47 */
darienf 20:6d2af70c92ab 48 public class LineGraphing extends View {
darienf 20:6d2af70c92ab 49
darienf 20:6d2af70c92ab 50 private Bitmap mBitmap;
darienf 20:6d2af70c92ab 51 private Paint mPaint = new Paint();
darienf 20:6d2af70c92ab 52 private Canvas mCanvas = new Canvas();
darienf 20:6d2af70c92ab 53
darienf 20:6d2af70c92ab 54 private float mLastX;
darienf 20:6d2af70c92ab 55 private float mScale;
darienf 20:6d2af70c92ab 56 private float mLastValue;
darienf 20:6d2af70c92ab 57 private float mYOffset;
darienf 20:6d2af70c92ab 58 /// the width of this view
darienf 20:6d2af70c92ab 59 private float mWidth;
darienf 20:6d2af70c92ab 60 /// the height of this view
darienf 20:6d2af70c92ab 61 private float mHeight;
darienf 20:6d2af70c92ab 62
darienf 20:6d2af70c92ab 63 private Paint linePaint = new Paint();
darienf 20:6d2af70c92ab 64 private Paint pointPaint = new Paint();
darienf 20:6d2af70c92ab 65 private static final int DEFAULT_LINE_STROKE_WIDTH_DP = 2;
darienf 20:6d2af70c92ab 66 protected float density;
darienf 20:6d2af70c92ab 67 ArrayList<LinePointCollection> pointCollections;
darienf 20:6d2af70c92ab 68 float runningMaxY;
darienf 20:6d2af70c92ab 69 float runningMinY;
darienf 20:6d2af70c92ab 70
darienf 20:6d2af70c92ab 71 /**
darienf 20:6d2af70c92ab 72 * Default constuctor for a given context
darienf 20:6d2af70c92ab 73 * @param context
darienf 20:6d2af70c92ab 74 */
darienf 20:6d2af70c92ab 75 public LineGraphing(Context context) {
darienf 20:6d2af70c92ab 76 super(context);
darienf 20:6d2af70c92ab 77 init();
darienf 20:6d2af70c92ab 78 }
darienf 20:6d2af70c92ab 79
darienf 20:6d2af70c92ab 80 /**
darienf 20:6d2af70c92ab 81 * Constructor that accomidated a contect and set of attributes
darienf 20:6d2af70c92ab 82 * @param context
darienf 20:6d2af70c92ab 83 * @param attrs
darienf 20:6d2af70c92ab 84 */
darienf 20:6d2af70c92ab 85 public LineGraphing(Context context, AttributeSet attrs) {
darienf 20:6d2af70c92ab 86 super(context, attrs);
darienf 20:6d2af70c92ab 87 this.density = context.getResources().getDisplayMetrics().density;
darienf 20:6d2af70c92ab 88 init();
darienf 20:6d2af70c92ab 89 }
darienf 20:6d2af70c92ab 90
darienf 20:6d2af70c92ab 91 /**
darienf 20:6d2af70c92ab 92 * Init this view with render paint defaults
darienf 20:6d2af70c92ab 93 */
darienf 20:6d2af70c92ab 94 private void init() {
darienf 20:6d2af70c92ab 95 mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
darienf 20:6d2af70c92ab 96 linePaint.setAntiAlias(true);
darienf 20:6d2af70c92ab 97 linePaint.setStyle(Paint.Style.STROKE);
darienf 20:6d2af70c92ab 98 linePaint.setStrokeCap(Paint.Cap.ROUND);
darienf 20:6d2af70c92ab 99 linePaint.setStrokeWidth(2); //dp2px(density, DEFAULT_LINE_STROKE_WIDTH_DP));
darienf 20:6d2af70c92ab 100 pointPaint.setAntiAlias(true);
darienf 20:6d2af70c92ab 101 pointPaint.setStyle(Paint.Style.FILL);
darienf 20:6d2af70c92ab 102 pointCollections = new ArrayList<>();
darienf 20:6d2af70c92ab 103 mLastX = 0;
darienf 20:6d2af70c92ab 104 }
darienf 20:6d2af70c92ab 105
darienf 20:6d2af70c92ab 106 /**
darienf 20:6d2af70c92ab 107 * Calculate the pixel width for a given density
darienf 20:6d2af70c92ab 108 * @param density
darienf 20:6d2af70c92ab 109 * @param dp
darienf 20:6d2af70c92ab 110 * @return
darienf 20:6d2af70c92ab 111 */
darienf 20:6d2af70c92ab 112 public static int dp2px(float density, int dp) {
darienf 20:6d2af70c92ab 113 if (dp == 0) {
darienf 20:6d2af70c92ab 114 return 0;
darienf 20:6d2af70c92ab 115 }
darienf 20:6d2af70c92ab 116 return (int) (dp * density + 0.5f);
darienf 20:6d2af70c92ab 117 }
darienf 20:6d2af70c92ab 118
darienf 20:6d2af70c92ab 119 /**
darienf 20:6d2af70c92ab 120 * Specify the point collection to use for this graph
darienf 20:6d2af70c92ab 121 * @param _pointCollection
darienf 20:6d2af70c92ab 122 */
darienf 20:6d2af70c92ab 123 public void addPointCollection(LinePointCollection _pointCollection) {
darienf 20:6d2af70c92ab 124 pointCollections.add(_pointCollection);
darienf 20:6d2af70c92ab 125 }
darienf 20:6d2af70c92ab 126
darienf 20:6d2af70c92ab 127 /**
darienf 20:6d2af70c92ab 128 * Calculate the extremes of all point collections
darienf 20:6d2af70c92ab 129 * Used to autoscale graph
darienf 20:6d2af70c92ab 130 */
darienf 20:6d2af70c92ab 131 public void calculateGraphExtremes() {
darienf 20:6d2af70c92ab 132 runningMaxY = Float.MIN_VALUE;
darienf 20:6d2af70c92ab 133 runningMinY = Float.MAX_VALUE;
darienf 20:6d2af70c92ab 134 for (LinePointCollection pointCollection : pointCollections) {
darienf 20:6d2af70c92ab 135 if (pointCollection.getRunningMaxY() > runningMaxY)
darienf 20:6d2af70c92ab 136 runningMaxY = pointCollection.getRunningMaxY();
darienf 20:6d2af70c92ab 137 if (pointCollection.getRunningMinY() < runningMinY)
darienf 20:6d2af70c92ab 138 runningMinY = pointCollection.getRunningMinY();
darienf 20:6d2af70c92ab 139 }
darienf 20:6d2af70c92ab 140 }
darienf 20:6d2af70c92ab 141
darienf 20:6d2af70c92ab 142 /**
darienf 20:6d2af70c92ab 143 * Plot all of the point collections and invalidate the view to force a view refresh
darienf 20:6d2af70c92ab 144 */
darienf 20:6d2af70c92ab 145 public void plotPointCollection() {
darienf 20:6d2af70c92ab 146 calculateGraphExtremes();
darienf 20:6d2af70c92ab 147 // clear the canvas
darienf 20:6d2af70c92ab 148 mCanvas.drawColor(Color.WHITE);
darienf 20:6d2af70c92ab 149 for (LinePointCollection pointCollection : pointCollections) {
darienf 20:6d2af70c92ab 150 // space out graph on x based on number of points to show and the width of the graph
darienf 20:6d2af70c92ab 151 linePaint.setColor(pointCollection.getColor());
darienf 20:6d2af70c92ab 152 mLastValue = pointCollection.getDroppedPointY();
darienf 20:6d2af70c92ab 153 mLastX = 0;
darienf 20:6d2af70c92ab 154 for (LinePoint point : pointCollection.getPoints()) {
darienf 20:6d2af70c92ab 155 plotDataPoint(mCanvas, point, runningMaxY, runningMinY, pointCollection.getMaxCount(), 10);
darienf 20:6d2af70c92ab 156 drawPoint(mCanvas, point, 3);
darienf 20:6d2af70c92ab 157 }
darienf 20:6d2af70c92ab 158 }
darienf 20:6d2af70c92ab 159 invalidate();
darienf 20:6d2af70c92ab 160 }
darienf 20:6d2af70c92ab 161
darienf 20:6d2af70c92ab 162 /**
darienf 20:6d2af70c92ab 163 * Plot a single point on the graph
darienf 20:6d2af70c92ab 164 * @param canvas Canvas to paint to
darienf 20:6d2af70c92ab 165 * @param point Point to plot
darienf 20:6d2af70c92ab 166 * @param max The calculated max for the set
darienf 20:6d2af70c92ab 167 * @param min The calculated min for the set
darienf 20:6d2af70c92ab 168 * @param numberPointsAcross Number of points to plot horizontally on the graph
darienf 20:6d2af70c92ab 169 * @param margin The top and bottom pixel margin to use
darienf 20:6d2af70c92ab 170 */
darienf 20:6d2af70c92ab 171 private void plotDataPoint(Canvas canvas, LinePoint point, float max, float min, float numberPointsAcross, float margin) {
darienf 20:6d2af70c92ab 172 final Paint paint = linePaint;
darienf 20:6d2af70c92ab 173 float newX;
darienf 20:6d2af70c92ab 174 float v;
darienf 20:6d2af70c92ab 175 float stepX;
darienf 20:6d2af70c92ab 176
darienf 20:6d2af70c92ab 177 // scale the graph horizontally
darienf 20:6d2af70c92ab 178 stepX = mWidth / numberPointsAcross;
darienf 20:6d2af70c92ab 179 newX = mLastX + stepX;
darienf 20:6d2af70c92ab 180 // scale the graph vertically
darienf 20:6d2af70c92ab 181 v = max - point.y;
darienf 20:6d2af70c92ab 182 v = (v / (max - min)) * (mHeight - margin) + margin / 2;
darienf 20:6d2af70c92ab 183
darienf 20:6d2af70c92ab 184 point.rawX = newX;
darienf 20:6d2af70c92ab 185 point.rawY = v;
darienf 20:6d2af70c92ab 186
darienf 20:6d2af70c92ab 187 canvas.drawLine(mLastX, mLastValue, newX, v, paint);
darienf 20:6d2af70c92ab 188 mLastValue = v;
darienf 20:6d2af70c92ab 189 mLastX += stepX;
darienf 20:6d2af70c92ab 190 }
darienf 20:6d2af70c92ab 191
darienf 20:6d2af70c92ab 192 /**
darienf 20:6d2af70c92ab 193 * Draw the X,Y point on the graph using a circle
darienf 20:6d2af70c92ab 194 * @param canvas Canvas to use to when drawing
darienf 20:6d2af70c92ab 195 * @param point Point that contains the raw pixel X,Y point
darienf 20:6d2af70c92ab 196 * @param pointRadius The radius of the circular dot to draw
darienf 20:6d2af70c92ab 197 */
darienf 20:6d2af70c92ab 198 private void drawPoint(Canvas canvas, LinePoint point,
darienf 20:6d2af70c92ab 199 float pointRadius) {
darienf 20:6d2af70c92ab 200 canvas.drawCircle(point.rawX, point.rawY, pointRadius, pointPaint);
darienf 20:6d2af70c92ab 201 }
darienf 20:6d2af70c92ab 202
darienf 20:6d2af70c92ab 203 /**
darienf 20:6d2af70c92ab 204 * Called by the view framework when the view is initially drawn or the size changes
darienf 20:6d2af70c92ab 205 * @param w New width of the view
darienf 20:6d2af70c92ab 206 * @param h New height of the view
darienf 20:6d2af70c92ab 207 * @param oldw Old width of the view
darienf 20:6d2af70c92ab 208 * @param oldh Old height of the view
darienf 20:6d2af70c92ab 209 */
darienf 20:6d2af70c92ab 210 @Override
darienf 20:6d2af70c92ab 211 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
darienf 20:6d2af70c92ab 212 mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
darienf 20:6d2af70c92ab 213 mCanvas.setBitmap(mBitmap);
darienf 20:6d2af70c92ab 214 mCanvas.drawColor(0xFFFFFFFF);
darienf 20:6d2af70c92ab 215 mYOffset = h;
darienf 20:6d2af70c92ab 216 mWidth = w;
darienf 20:6d2af70c92ab 217 mHeight = h;
darienf 20:6d2af70c92ab 218 super.onSizeChanged(w, h, oldw, oldh);
darienf 20:6d2af70c92ab 219 }
darienf 20:6d2af70c92ab 220
darienf 20:6d2af70c92ab 221 /**
darienf 20:6d2af70c92ab 222 * Called by the view when a draw is needed
darienf 20:6d2af70c92ab 223 * @param canvas Drawing canvas for this view
darienf 20:6d2af70c92ab 224 */
darienf 20:6d2af70c92ab 225 @Override
darienf 20:6d2af70c92ab 226 protected void onDraw(Canvas canvas) {
darienf 20:6d2af70c92ab 227 synchronized (this) {
darienf 20:6d2af70c92ab 228 if (mBitmap != null) {
darienf 20:6d2af70c92ab 229 canvas.drawBitmap(mBitmap, 0, 0, null);
darienf 20:6d2af70c92ab 230 }
darienf 20:6d2af70c92ab 231 }
darienf 20:6d2af70c92ab 232 }
darienf 20:6d2af70c92ab 233 }