forked from ZnakZorro/androtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelerometer.js
203 lines (144 loc) · 4.77 KB
/
accelerometer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var watchID = null;
var watchLastX=0;
var watchLastY=0;
var watchLastZ=0;
var LastX=Math.random();
var LastY=Math.random();
var LastZ=Math.random();
var LastT=0;
function startWatch() {
var options = { frequency: 50 };
try {watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);}
catch(er){
//symulacja()
}
}
// Stop watching the acceleration
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}
// onSuccess: Get a snapshot of the current acceleration
function onSuccess(acceleration) {
if (acceleration){
var deltaX=acceleration.x - watchLastX;
var deltaY=acceleration.y - watchLastY;
var deltaZ=acceleration.z - watchLastZ;
var suma=deltaY+deltaZ;
var sumaa=acceleration.y+acceleration.z;
watchLastX = acceleration.x;
watchLastY = acceleration.y;
watchLastZ = acceleration.z;
//$('#infotest').innerHTML='<b>A:s:'+sumaa+'</b> <br />:x:'+acceleration.x+' <br />:y:'+acceleration.y+' <br />:z:'+acceleration.z+'<br />';
//$('#infotest').innerHTML+=' <b>D:s:'+suma+'</b> <br />:x:'+deltaX+' <br />:y:'+deltaY+' <br />:z:'+deltaZ+'<br />';
//if (suma < -3) {goback(); return;}
//if (suma > 9) {gonext(); return;}
//if (acceleration.y < -3) {goback(); return;}
//if (acceleration.y > 9) {gonext(); return;}
//acceleration.timestamp
rysuj(acceleration);
}
}
function onError() {
//alert('onError!');
}
function symulacja(){
var it=window.setInterval(function(){
var acceleration={};
acceleration.x=LastX+(Math.random()); if (acceleration.x>9.81) acceleration.x=-9.81; LastX=acceleration.x;
acceleration.y=LastY+(Math.random()); if (acceleration.y>9.81) acceleration.y=-9.81; LastY=acceleration.y;
acceleration.z=LastZ+(Math.random()); if (acceleration.z>9.81) acceleration.z=-9.81; LastZ=acceleration.z;
//acceleration.x=(Math.random()*20)-10;
//acceleration.y=(Math.random()*20)-10;
//acceleration.z=(Math.random()*10);
acceleration.timestamp=111111;
//console.log(acceleration);
onSuccess(acceleration);
return acceleration;
},333);
}
function rysuj(acceleration){
//var c=document.getElementById("myCanvas");
//var ctx=c.getContext("2d");
var dmax=60;
var xmax=398;
var ymax=398;
var pol=200;
try {
var z=skala(acceleration.z,10);
var x=skala(acceleration.x,20);
x=(x-(z/2))+pol;
x=xmax-x-z;
var y=skala(acceleration.y,20);
y=(y-(z/2))+pol;
var t=acceleration.timestamp;
} catch(er)
{
var z=Math.round((Math.random()*(dmax*0.8))+(dmax*0.2));
var x=Math.round((Math.random()*(xmax-z)));
var y=Math.round((Math.random()*(ymax-z)));
var t=Math.random();
}
//console.log(d+ ' ' +xlos+ ' ' +ylos);
//ctx.clearRect(0, 0,xmax,ymax);
//ctx.fillStyle="#ffa000";
//ctx.fillRect(x,y,z,z);
ctx.fillStyle="rgba(255,255,255,0.1)";
ctx.fillRect(0,0,xmax,ymax);
ctx.fillStyle="rgba(0,0,255,0.3)";
ctx.fillRect(x,y,z,z);
//$('#infotest').innerHTML+='x='+x+'<br />'+'y='+y+'<br />'+'z='+z+'<br />'+'t='+t;
}
function skala(v,s){
var a=((v)*s);
return Math.round(a);
}
function goback(){alert('back')};
function gonext(){alert('next')};
//phonegap
function playBeep() {
navigator.notification.beep(1);
}
function vibrate() {
navigator.notification.vibrate(200);
}
// kapiszon
var $=function kapiszon(id) {
var prefix=id[0];
var iid=id.slice(1,id.length);
switch (prefix){
case '#': return document.getElementById(iid); break;
case '.': return document.getElementsByClassName(iid); break;
default: return document.getElementsByTagName(id);
}
}
function toDate(date){
if (!date) date=new Date();
var d = new Date(date);
var miesiac = d.getMonth()+1; if (miesiac<10) miesiac = '0'+miesiac;
var dzien = d.getDate(); if (dzien<10) dzien = '0'+dzien;
var godzina = d.getHours(); if (godzina<10) godzina = '0'+godzina;
var minuta = d.getMinutes(); if (minuta<10) minuta = '0'+minuta;
var rok = d.getFullYear();
var pldata=rok+'-'+dzien+'-'+miesiac+' <b>'+godzina+':'+minuta+'</b>';
return pldata;
}
function zapal(){$("#strona").style.opacity='1.0';}
function zgas() {$("#strona").style.opacity='0.7';}
function koniec(){
try {
stopWatch();
navigator.app.exitApp();
}
catch(err) {
//window.scrollTo(0,0);
}
}
function loadURL(url){
try { navigator.app.loadUrl(url, { openExternal:true } ); }
catch(err) {
window.location.href=url;
}
}