28 # error This is a userspace-only header, not allowed by the current build.
31 #include <sifteo/abi.h>
32 #include <sifteo/cube.h>
33 #include <sifteo/math.h>
79 template <
unsigned tSize = 32 >
82 _SYSMotionBufferHeader header;
83 _SYSByte4 samples[tSize];
87 static const unsigned TICK_NS = _SYS_MOTION_TIMESTAMP_NS;
90 static const unsigned TICK_US = _SYS_MOTION_TIMESTAMP_NS / 1000;
93 static const unsigned TICK_HZ = _SYS_MOTION_TIMESTAMP_HZ;
96 operator _SYSMotionBuffer* () {
return reinterpret_cast<_SYSMotionBuffer*
>(&sys); }
97 operator const _SYSMotionBuffer* ()
const {
return reinterpret_cast<const _SYSMotionBuffer*
>(&sys); }
119 void attach(_SYSCubeID
id,
unsigned hz=100)
124 sys.header.last = tSize - 1;
125 sys.header.rate = TICK_HZ / hz;
132 ASSERT(reinterpret_cast<uintptr_t>(
this) +
sizeof(_SYSMotionBuffer) <= 0x18000);
134 _SYS_setMotionBuffer(
id, *
this);
158 _SYS_motion_integrate(*
this, duration, &result);
159 return vec(result.x, result.y, result.z);
176 _SYSMotionBuffer *buffer;
177 uint32_t tickCounter;
200 : buffer(buffer), tickCounter(0), lastAccel(
vec(0,0,0)),
201 head(buffer->header.tail) {}
212 unsigned tail = buffer->header.tail;
213 unsigned last = buffer->header.last;
214 unsigned head = this->head;
222 _SYSByte4 sample = buffer->samples[head];
223 this->head = head + 1;
225 lastAccel =
vec(sample.x, sample.y, sample.z);
226 tickCounter += unsigned(uint8_t(sample.w)) + 1;
287 tickCounter += value;
301 _SYSMotionMedian sys;
304 operator _SYSMotionMedian* () {
return &sys; }
305 operator const _SYSMotionMedian* ()
const {
return &sys; }
326 void calculate(_SYSMotionBuffer *mbuf,
unsigned duration) {
327 _SYS_motion_median(mbuf, duration, *
this);
340 return vec(sys.axes[0].median, sys.axes[1].median, sys.axes[2].median);
345 return vec(sys.axes[0].minimum, sys.axes[1].minimum, sys.axes[2].minimum);
350 return vec(sys.axes[0].maximum, sys.axes[1].maximum, sys.axes[2].maximum);
373 static const int kTiltThresholdMin = 15;
374 static const int kTiltThresholdMax = 26;
375 static const int kShakeThresholdMin = 1000;
376 static const int kShakeThresholdMax = 50000;
488 unsigned update(
int latency = kFilterLatency)
490 unsigned changed = 0;
497 if (wobble >= kShakeThresholdMax) {
501 }
else if (wobble < kShakeThresholdMin) {
508 if (m.x <= -kTiltThresholdMax) {
511 }
else if (m.x >= kTiltThresholdMax) {
514 }
else if (
abs(m.x) < kTiltThresholdMin) {
519 if (m.y <= -kTiltThresholdMax) {
522 }
else if (m.y >= kTiltThresholdMax) {
525 }
else if (
abs(m.y) < kTiltThresholdMin) {
530 if (m.z <= -kTiltThresholdMax) {
533 }
else if (m.z >= kTiltThresholdMax) {
536 }
else if (
abs(m.z) < kTiltThresholdMin) {
T x
Vector component X.
Definition: math.h:724
'tilt.x' has changed to -1
Definition: motion.h:456
'tilt.z' has changed to 0
Definition: motion.h:467
'tilt.x' has changed to +1
Definition: motion.h:458
Byte3 virtualTilt(Side orientation) const
Return the virtual tilt reading for the attached cube.
Definition: motion.h:444
T z
Vector component Z.
Definition: math.h:726
'shake' has changed
Definition: motion.h:454
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
A standard recognizer for shake and tilt gestures.
Definition: motion.h:370
static const unsigned TICK_NS
Duration of the MotionBuffer's timestamp unit, in nanoseconds.
Definition: motion.h:87
Side
An enumeration which names the four sides of a Sifteo cube.
Definition: cube.h:54
static const unsigned TICK_NS
Duration of the MotionBuffer's timestamp unit, in nanoseconds.
Definition: motion.h:183
void bzero(void *s, unsigned count)
Write 'n' zero bytes to memory.
Definition: memory.h:114
ChangeFlags
Change flags, returned by update() to indicate what just changed.
Definition: motion.h:451
'tilt.y' has changed to -1
Definition: motion.h:461
T len2() const
Calculate the scalar length (magnitude) of this vector, squared.
Definition: math.h:747
static const unsigned TICK_US
Duration of the MotionBuffer's timestamp unit, in microseconds.
Definition: motion.h:186
static const unsigned TICK_HZ
Reciprocal of the MotionBuffer's timestamp unit, in Hertz.
Definition: motion.h:93
static const unsigned TICK_US
Duration of the MotionBuffer's timestamp unit, in microseconds.
Definition: motion.h:90
'tilt' has changed
Definition: motion.h:471
Byte3 physicalTilt() const
Return the physical tilt reading for the attached cube.
Definition: motion.h:430
T abs(const T &value)
For any type, return the absolute value.
Definition: math.h:90
Byte3 tilt
The most recent tilt value.
Definition: motion.h:404
static const unsigned TICK_HZ
Reciprocal of the MotionBuffer's timestamp unit, in Hertz.
Definition: motion.h:189
bool shake
The most recent binary shake state.
Definition: motion.h:407
T y
Vector component Y.
Definition: math.h:725
Int3 integrate(unsigned duration)
Calculate a numerical integral over recent motion data.
Definition: motion.h:155
float seconds() const
Return the timestamp at the iterator's current position, in seconds.
Definition: motion.h:264
'tilt.y' has changed to +1
Definition: motion.h:463
void attach(_SYSCubeID id)
Initialize this TiltShakeRecognizer and attach it to a cube.
Definition: motion.h:415
MotionIterator(_SYSMotionBuffer *buffer)
Construct a new MotionIterator, attached to the provided MotionBuffer.
Definition: motion.h:199
'tilt.z' has changed
Definition: motion.h:469
void attach(_SYSCubeID id, unsigned hz=100)
Initialize the MotionBuffer and attach it to a cube.
Definition: motion.h:119
'shake' has changed to 'true'
Definition: motion.h:452
void adjustTicks(int32_t value)
Add a value to the tick counter.
Definition: motion.h:286
void setTicks(uint32_t value)
Modify the tick counter.
Definition: motion.h:272
uint32_t ticks() const
Return the timestamp at the iterator's current position, in ticks.
Definition: motion.h:251
Vector3< int > Int3
Typedef for a 2-vector of ints.
Definition: math.h:840
Vector3< T > zRotateI(int angle) const
Rotate this vector about the Z axis counterclockwise by an integer multiple of 90 degrees...
Definition: math.h:800
Byte3 accel() const
Return the acceleration sample at the iterator's current position.
Definition: motion.h:237
Generalized three-element cartesian coordinate vector.
Definition: math.h:723
unsigned update(int latency=kFilterLatency)
Update the state of the TiltShakeRecognizer.
Definition: motion.h:488
'tilt.y' has changed to 0
Definition: motion.h:462
Utility for reading low-level motion events from a MotionBuffer.
Definition: motion.h:174
'shake' has changed to 'false'
Definition: motion.h:453
'tilt.z' has changed to -1
Definition: motion.h:466
'tilt.x' has changed
Definition: motion.h:459
'tilt.z' has changed to +1
Definition: motion.h:468
#define STATIC_ASSERT(_x)
Definition: macros.h:342
MotionMedian median
The most recent median data calculated by the TiltShakeRecognizer.
Definition: motion.h:396
bool next()
Advance to the next motion sample, if possible.
Definition: motion.h:210
'tilt.x' has changed to 0
Definition: motion.h:457
MotionBuffer buffer
The MotionBuffer used by this TiltShakeRecognizer.
Definition: motion.h:386
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658
'tilt.y' has changed
Definition: motion.h:464
A memory buffer which holds captured motion data.
Definition: motion.h:80