nn::hid::CTR::AccelerometerReader Classclass AccelerometerReader : private nn::util::NonCopyable< AccelerometerReader >
Class for loading sampling data from the accelerometer.
The accelerometer starts sampling when the first instance is generated, sampling data at intervals of roughly 10 milliseconds. Sampling stops when no instances exist.
If you want to monitor the exact timing of sampling, you can use the Accelerometer class to know precisely when sampling occurs.
No sampling is done right after generating a class instance or right after recovering from sleep, so there are times when the Read and ReadLatest functions do not get valid results. Waiting until results have been sampled at least once avoids this situation, so Nintendo recommends calling the nn::hid::CTR::HidBase::WaitSampling function of the nn::hid::CTR::Accelerometer class right after generating a class instance or when recovering from sleep. This wait time is the same as the sampling frequency, up to a maximum of approximately 10 milliseconds.
As mentioned, the generation/destruction of instances is linked to the switching of sampling processes. Because the switching of sampling processes puts a load on the system, you should try to use the same instance while using the accelerometer and otherwise refrain from frequent generation and destruction of instances.
Below is an example of what you should avoid.
void GetAccelerometerValue(f32* x,f32* y,f32* z)
{
AccelerometerReader reader; // Generate instance: Start of sampling
AccelerometerStatus status;
reader.ReadLatest( &status ); // Get value
*x = status.speed.x;
*y = status.speed.y;
*z = status.speed.z;
return; // Destroy reader: Sampling stops
}
void MainLoop() // Loop called once per frame
{
... some sort of processing .....
GetAccelerometerValue( &x, &y, &z ); // Value obtained once in every frame
... some sort of processing .....
}
In the example above, an instance of AccelerometerReader is created and destroyed on the stack in every frame. Because the process does not wait the minimal time for sampling by the accelerometer, in some cases appropriate values are not obtained. This is a bad design. The design should be revised, for example, by creating the instance in the heap, so that the instance continues to exist throughout the interval during which the application needs to get the acceleration.
This class includes the following features to process output values.
| Acceleration Variation Tolerance | You can apply a correction to control any major variation in acceleration during continuous sampling. |
|---|---|
| Offset Correction | Outputs after subtracting the specified value from sampling results. |
| Axial Rotation | Outputs sampling results multiplied by an optional rotation matrix. Enables the output of values as if the accelerometer is on an incline. |
The default accelerometer sensors are as follows.
| X+ | The direction indicated by Left on the +Control Pad |
|---|---|
| Y+ | The direction normal to and outward from the Touch Screen |
| Z+ | The direction indicated by Up on the +Control Pad |
Effective from the first network update, the HOME Menu has a feature enabling users to recalibrate the accelerometer whenever they like.
Thus, in some cases, correction of the accelerometer in accordance with the user's environment may take place while the system is transitioning from the application to the HOME Menu upon pressing the HOME Button.
After this operation, the values obtained by the Read and ReadLatest functions are the corrected values. In most cases, the corrected values accurately reflect the user's environment.
The applications, however, need not take any special measures to deal with this.
| S | MAX_READ_NUM | const s8 | The maximum number of data samples that can be read. |
|---|
| Constructors and Destructors | ||
|---|---|---|
AccelerometerReader
|
Constructor. | |
~AccelerometerReader
|
Destructor. | |
| Reading Sampling Data | ||
Read
|
Loads sampling data from the accelerometer starting with the newest samples. Does not load any data that has been previously loaded. | |
ReadLatest
|
Loads the newest sampling data from the accelerometer. Unlike the nn::hid::CTR::AccelerometerReader::Read function, this function can load the same sampling data more than once. |
|
| Control Functions for Acceleration Variation Tolerance | ||
GetSensitivity
|
Gets the settings for sensitivity and play tolerance for the change in acceleration between samples. | |
SetSensitivity
|
Sets the sensitivity and play tolerance for the change in acceleration between samples. | |
| Axial Rotation | ||
EnableAxisRotation
|
Enables axial rotation of the accelerometer. | |
DisableAxisRotation
|
Disables axial rotation of the accelerometer. | |
IsEnableAxisRotation
|
Checks whether axial rotation is enabled or disabled for the accelerometer. | |
SetAxisRotationMatrix
|
Specifies the rotation matrix used for axial rotation of the accelerometer's sampling data. | |
GetAxisRotationMatrix
|
Gets the rotation matrix currently specified to use for axial rotation of the accelerometer's sampling data. | |
ResetAxisRotationMatrix
|
Initializes the rotation matrix to use for transforming the accelerometer's sampling data. | |
| Offset | ||
EnableOffset
|
Enables the accelerometer offset. | |
DisableOffset
|
Disables the accelerometer offset. | |
IsEnableOffset
|
Checks whether the accelerometer offset is enabled or disabled. | |
SetOffset
|
Sets the accelerometer offset value. | |
SetOffsetFromBaseStatus
|
Sets the accelerometer offset value. | |
GetOffset
|
Gets the accelerometer offset value. | |
ResetOffset
|
Resets the accelerometer offset value to the default. | |
| Other | ||
ConvertToAcceleration
|
Converts the raw sampling data from the accelerometer into acceleration values (in g's). | |
nn::util::NonCopyable
nn::hid::CTR::AccelerometerReader
CONFIDENTIAL