Skip to content

Instantly share code, notes, and snippets.

@VictorCoding
Created January 18, 2017 17:12
Show Gist options
  • Save VictorCoding/e33ca119246cf99e741a77654265a685 to your computer and use it in GitHub Desktop.
Save VictorCoding/e33ca119246cf99e741a77654265a685 to your computer and use it in GitHub Desktop.
import { Component } from '@angular/core';
import { DeviceMotion, AccelerationData } from 'ionic-native';
import { NavController, Platform } from 'ionic-angular';
Array['max'] = (array) => Math.max.apply(Math, array);
Array['min'] = (array) => Math.min.apply(Math, array);
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
acceleration;
x;
y;
z;
heights = [];
maxHeight;
minHeight;
subscription = null;
totalPushups = 0;
constructor(public navCtrl: NavController, private platform: Platform) {
}
ionViewDidLoad() {
}
calibrate() {
// Watch device acceleration
let initialHeight = 0;
this.subscription = DeviceMotion.watchAcceleration({
frequency: 1000
}).subscribe((acceleration: AccelerationData) => {
this.z = acceleration.z;
if (!initialHeight) initialHeight = acceleration.z;
if (Math.floor(initialHeight) !== Math.floor(acceleration.z))
this.heights.push(Math.floor(acceleration.z));
});
}
stop() {
this.maxHeight = Array['max'](this.heights);
this.minHeight = Array['min'](this.heights);
this.subscription.unsubscribe();
}
start() {
let up = 0;
let down = 0;
this.totalPushups = 0;
let heights = [Math.floor(this.minHeight)];
this.subscription = DeviceMotion.watchAcceleration({
frequency: 1000
}).subscribe((acceleration: AccelerationData) => {
this.z = acceleration.z;
console.log(Math.floor(heights[heights.length - 1]) - Math.floor(acceleration.z));
if ((Math.floor(heights[heights.length - 1]) - Math.floor(acceleration.z)) > 2) {
if (this.minHeight < Math.floor(acceleration.z) < this.maxHeight) {
const middleHeight = (this.maxHeight - this.minHeight) / 2;
if (Math.floor(acceleration.z) > (this.minHeight + middleHeight)) {
console.log('up')
up++;
} else {
console.log('down')
down++;
}
}
}
this.totalPushups = up + down;
heights.push(acceleration.z);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment