Accelerometer (from lat. Accelero - accelerating and μετρέω - measuring) - a device that measures the projection of apparent acceleration. The apparent acceleration is the resultant force of a non-gravitational nature, acting on the mass and related to the magnitude of this mass. The accelerometer can be used both for measuring the projections of absolute linear acceleration, and for indirect measurements of the projection of gravitational acceleration. The last property is used to create inclinometers. Accelerometers are part of the inertial navigation systems, where the measurements obtained with their help integrate, obtaining the inertial speed and coordinates of the carrier. Electronic accelerometers are often embedded in mobile devices (in particular, in telephones) and are used as pedometers, sensors for determining position in space, automatic display rotation, and other purposes. In game consoles, accelerometers are used to control without using buttons — by turning in space, shaking, etc.


[[UIAccelerometer sharedAccelerometer] setUpdateInterval: 1.0 / kUpdateFrequency]; [[UIAccelerometer sharedAccelerometer] setDelegate: self];
@interface AppDelegate: NSObject <UIApplicationDelegate> @interface accelerometerAppDelegate: NSObject <UIApplicationDelegate, UIAccelerometerDelegate>
- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration
{
// Get the event data
UIAccelerometValue x, y, z;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
} - (void) disableAccelerometerEvents
{
UIAccelerometer * acc = [UIAccelerometer sharedAccelerometer];
acc.delegate = nil;
} 

- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration
{
// Get the current device angle
float xx = - [acceleration x];
float yy = [acceleration y];
float angle = atan2 (yy, xx);
// Add 1.5 to the viewer.
[interfaceOrientationLabel setTransform: CGAffineTransformMakeRotation (angle + 1.5)];
// Read my blog. It should be obvious that you
// could fire a custom shouldAutorotateToInterfaceOrientation-event here.
if (angle> = -2.25 && angle <= -0.75)
{
if (deviceOrientation! = UIInterfaceOrientationPortrait)
{
deviceOrientation = UIInterfaceOrientationPortrait;
[interfaceOrientationLabel setText: @ "UIInterfaceOrientationPortrait"];
}
}
else if (angle> = -0.75 && angle <= 0.75)
{
if (deviceOrientation! = UIInterfaceOrientationLandscapeRight)
{
deviceOrientation = UIInterfaceOrientationLandscapeRight;
[interfaceOrientationLabel setText: @ "UIInterfaceOrientationLandscapeRight"];
}
}
else if (angle> = 0.75 && angle <= 2.25)
{
if (deviceOrientation! = UIInterfaceOrientationPortraitUpsideDown)
{
deviceOrientation = UIInterfaceOrientationPortraitUpsideDown;
[interfaceOrientationLabel setText: @ "UIInterfaceOrientationPortraitUpsideDown"];
}
}
else if (angle <= -2.25 || angle> = 2.25)
{
if (deviceOrientation! = UIInterfaceOrientationLandscapeLeft)
{
deviceOrientation = UIInterfaceOrientationLandscapeLeft;
[interfaceOrientationLabel setText: @ "UIInterfaceOrientationLandscapeLeft"];
}
}
} - (void) accelerometer: (UIAccelerometer *) accelerometer
didAccelerate: (UIAcceleration *) acceleration
{
double absY = fabs (acceleration.y);
if (absY <= 0.5) {
// Scroll to the top of the list
[viewController.tableView
scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: 0]
atScrollPosition: UITableViewScrollPositionTop
animated: YES];
}
else if (absY> = 0.86)
{
// Scroll to end of list
[viewController.tableView
scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: ([list count] - 1) inSection: 0]
atScrollPosition: UITableViewScrollPositionBottom
animated: YES];
}
} #define FILTERFACTOR 0.1 value = (newAcceleration * FILTERFACTOR) + (previousValue * (1.0 - FILTERFACTOR)); previousValue = value;
#define FILTERFACTOR 0.1 value = newAcceleration - (newAcceleration * FILTERFACTOR) + (previousValue * (1.0 - FILTERFACTOR)); previousValue = value;
- (void) accelerateWithX: (float) x Y: (float) y Z: (float) z
{
// Use double filtering for passed coords
acceleration [0] = x * kFilteringFactor + acceleration [0] * (1.0 - kFilteringFactor);
x = x acceleration [0];
acceleration [1] = y * kFilteringFactor + acceleration [1] * (1.0 - kFilteringFactor);
y = y - acceleration [1];
acceleration [2] = z * kFilteringFactor + acceleration [2] * (1.0 - kFilteringFactor);
z = z - acceleration [2];
} NSDate * now = [NSDate date];
NSTimeInterval interval = [now timeIntervalSinceDate: self.lastPlayedTime];
// Check playback time condition
if (interval> minTimeDelta)
{
[self play];
} // it is a shake.
// "Strong enough" means "greater than a client-supplied threshold" in G's.
static BOOL L0AccelerationIsShaking (UIAcceleration * last, UIAcceleration * current, double threshold) {
double
deltaX = fabs (last.x - current.x),
deltaY = fabs (last.y - current.y),
deltaZ = fabs (last.z - current.z);
return
(deltaX> threshold && deltaY> threshold) ||
(deltaX> threshold && deltaZ> threshold) ||
(deltaY> threshold && deltaZ> threshold);
}
@interface L0AppDelegate: NSObject {
BOOL histeresisExcited;
UIAcceleration * lastAcceleration;
}
@property (retain) UIAcceleration * lastAcceleration;
@end
@implementation L0AppDelegate
- (void) applicationDidFinishLaunching: (UIApplication *) application {
[UIAccelerometer sharedAccelerometer] .delegate = self;
}
- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration {
if (self.lastAcceleration) {
if (! histeresisExcited && L0AccelerationIsShaking (self.lastAcceleration, acceleration, 0.7)) {
histeresisExcited = YES;
/ * SHAKE DETECTED. DO HERE WHAT YOU WANT. * /
} else if (histeresisExcited &&! L0AccelerationIsShaking (self.lastAcceleration, acceleration, 0.2)) {
histeresisExcited = NO;
}
}
self.lastAcceleration = acceleration;
}
// and proper @synthesize and -dealloc boilerplate code
@end function updateOrientation () {
/ *
iphone in portrait mode
landscape mode with the screen turned to the left
with the screen turned to the right.
* /
var orientation = window.orientation;
switch (orientation)
{
case 0:
/ *
If in portrait mode, sets the body's class attribute to portrait.
Consequently, all the style definitions of the body [class = "portrait"]
declaration in the iPhoneOrientation.css file will be selected and used
to style "Handling iPhone or iPod touch Orientation Events".
* /
document.body.setAttribute ("class", "portrait");
/ *
Add a descriptive message on "Handling iPhone or iPod touch Orientation Events"
* /
document.getElementById ("currentOrientation"). innerHTML
= "Now in portrait orientation (Home button on the bottom).";
break;
case 90:
/ *
If in landscape mode with the screen turned to the left,
sets the body's class attribute to landscapeLeft.
In this case, all style definitions matching the body [class = "landscapeLeft"]
declaration in the iPhoneOrientation.css file will be
selected and used to style "Handling iPhone or iPod touch Orientation Events".
* /
document.body.setAttribute ("class", "landscapeLeft");
document.getElementById ("currentOrientation"). innerHTML
= "Now in landscape orientation and turned to the left (Home button to the right).";
break;
case -90:
/ *
If in landscape mode with the screen turned to the right,
sets the body's class attribute to landscapeRight.
Here, all style definitions matching the body [class = "landscapeRight"]
declaration in the iPhoneOrientation.css file will be selected and used
to style "Handling iPhone or iPod touch Orientation Events".
* /
document.body.setAttribute ("class", "landscapeRight");
break;
}
window.onorientationchange = updateOrientation;
<div id = "currentOrientation" style = "font-size: 40px;">
Now in portrait orientation (Home button on the bottom). </ Div> Source: https://habr.com/ru/post/65148/
All Articles