Created
August 14, 2016 17:08
-
-
Save JustinFincher/16b1f43ece08313237c0053b293d6531 to your computer and use it in GitHub Desktop.
Recreate METER using Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.net.wifi.*; | |
import android.content.*; | |
import android.app.*; | |
import processing.core.*; | |
import android.os.Bundle; | |
import android.content.Context; | |
import android.hardware.Sensor; | |
import android.hardware.SensorManager; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import java.util.*; | |
import android.view.*; | |
String wifiName; | |
int wifiLevel; // 0 - 10 | |
android.content.Context context; | |
boolean canRefreshWifi; | |
SensorManager manager; | |
SensorListener listener; | |
Sensor accelerometer,magnetometer; | |
float Point1X,Point2X,Point3X; | |
float Point1Y,Point2Y,Point3Y; | |
void setup() | |
{ | |
fullScreen(P3D); | |
Timer t = new Timer(); | |
t.scheduleAtFixedRate(new TimerTask() { | |
@Override | |
public void run() { | |
if (canRefreshWifi) | |
{ | |
context = (Context) surface.getComponent(); | |
WifiManager wifiManager = (WifiManager) context.getSystemService (Context.WIFI_SERVICE); | |
WifiInfo info = wifiManager.getConnectionInfo (); | |
wifiName = info.getSSID(); | |
wifiLevel = WifiManager.calculateSignalLevel(info.getRssi(), 11); | |
} | |
} | |
},0,10000); | |
} | |
void draw() | |
{ | |
background(12, 39, 43); | |
beginShape(TRIANGLES); | |
fill(0,94,83); | |
vertex(Point1X, Point1Y); | |
vertex(Point2X, Point2Y); | |
vertex(Point3X, Point3Y); | |
endShape(); | |
float rotateMultiplier = (float)listener.angle / 90 * 0.4 ; | |
float WaterPoint1X,WaterPoint2X,WaterPoint3X,WaterPoint4X; | |
float WaterPoint1Y,WaterPoint2Y,WaterPoint3Y,WaterPoint4Y; | |
WaterPoint1X = Point2X + (Point1X - Point2X) * wifiLevel / 10 * (1 - rotateMultiplier); | |
WaterPoint1Y = Point2Y + (Point1Y - Point2Y) * wifiLevel / 10 * (1 - rotateMultiplier); | |
WaterPoint2X = Point2X; | |
WaterPoint2Y = Point2Y; | |
WaterPoint3X = Point2X + (Point3X - Point2X) * wifiLevel / 10 * (1 + rotateMultiplier); | |
WaterPoint3Y = Point2Y + (Point3Y - Point2Y) * wifiLevel / 10 * (1 + rotateMultiplier); | |
if (WaterPoint1Y > Point1Y || WaterPoint3Y > Point3Y) | |
{ | |
WaterPoint4X = (Point1Y - WaterPoint1Y)*(WaterPoint1X - WaterPoint3X)/(WaterPoint1Y - WaterPoint3Y)+WaterPoint1X; | |
WaterPoint4Y = Point1Y; | |
if (WaterPoint1Y < Point1Y) | |
{ | |
beginShape(QUADS); | |
fill(37,206,182); | |
vertex(WaterPoint2X, WaterPoint2Y); | |
vertex(Point1X, Point1Y); | |
vertex(WaterPoint4X, WaterPoint4Y); | |
vertex(WaterPoint3X, WaterPoint3Y); | |
endShape(); | |
}else | |
{ | |
beginShape(QUADS); | |
fill(37,206,182); | |
vertex(WaterPoint2X, WaterPoint2Y); | |
vertex(Point3X, Point3Y); | |
vertex(WaterPoint4X, WaterPoint4Y); | |
vertex(WaterPoint1X, WaterPoint1Y); | |
endShape(); | |
} | |
}else | |
{ | |
beginShape(TRIANGLES); | |
fill(37,206,182); | |
vertex(WaterPoint1X,WaterPoint1Y); | |
vertex(WaterPoint2X, WaterPoint2Y); | |
vertex(WaterPoint3X, WaterPoint3Y); | |
endShape(); | |
} | |
textSize(width / 20); | |
textAlign(CENTER, CENTER); | |
text("WIFI : " +wifiName, width / 2, height / 5 * 1 + width / 3 * 2 + height / 20); | |
fill(0, 102, 153); | |
} | |
public void onResume() { | |
super.onResume(); | |
canRefreshWifi = true; | |
Point1X = width / 6; | |
Point2X = width / 2; | |
Point3X = width / 6 * 5; | |
Point1Y = height / 5; | |
Point2Y = height / 5 * 1 + width / 3 * 2; | |
Point3Y = height / 5; | |
context = (Context) surface.getComponent(); | |
listener = new SensorListener(); | |
listener.context = context; | |
manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); | |
accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
magnetometer = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); | |
manager.registerListener(listener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); | |
manager.registerListener(listener, magnetometer, SensorManager.SENSOR_DELAY_NORMAL); | |
} | |
public void onPause() { | |
super.onPause(); | |
canRefreshWifi = false; | |
//manager.unregisterListener(listener); | |
} | |
class SensorListener implements SensorEventListener | |
{ | |
float azimut,pitch,roll; | |
double angle; | |
float[] mGravity = new float[3]; | |
float[] mGeomagnetic = new float[3]; | |
android.content.Context context; | |
public void onSensorChanged(SensorEvent event) { | |
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) | |
mGravity = event.values; | |
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) | |
mGeomagnetic = event.values; | |
if (mGravity != null && mGeomagnetic != null) { | |
float R[] = new float[9]; | |
float I[] = new float[9]; | |
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); | |
if (success) { | |
float orientation[] = new float[3]; | |
SensorManager.getOrientation(R, orientation); | |
azimut = orientation[0]; // orientation contains: azimut, pitch and roll | |
pitch = orientation[1]; | |
roll = orientation[2]; | |
double g = Math.sqrt(azimut * azimut +pitch * pitch); | |
double sin = azimut / g; | |
angle = Math.asin(sin) / Math.PI * 180.0f; | |
} | |
} | |
} | |
public void onAccuracyChanged(Sensor sensor, int accuracy) { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment