Motion Sensing¶
After this page you can move a paddle and change a light by tilting the device.
The Cyber Fidget has an LIS2DH12 accelerometer that measures tilt and acceleration. Use it for tilt-controlled games, gesture detection, or any app that reacts to how the device is held.
What is this?¶
An accelerometer measures acceleration (including gravity) along three axes. When the device is still, it mostly senses which way is "down" -- so you can infer tilt.
Like the tilt sensor in your phone
When you rotate your phone and the screen flips, that's the accelerometer. The Cyber Fidget uses the same idea: tilt the device and read X, Y, Z values to control your app.
Three axes¶
| Axis | Direction | Typical use |
|---|---|---|
| X | Left-right tilt | Paddle movement, horizontal control |
| Y | Forward-backward tilt | Depth, forward/back |
| Z | Up-down | Vertical, "is it flat?" |
With the device still and flat, the Z reading has a magnitude near 1000 and X and Y are near zero. Values are in milli-g (mg): 1000 mg is one g, the acceleration due to gravity. The sign depends on orientation. A shake can produce larger readings.
Global variables¶
The HAL updates these every 50 ms:
| Variable | Type | Description |
|---|---|---|
accelX |
float | Left-right acceleration |
accelY |
float | Forward-backward acceleration |
accelZ |
float | Up-down acceleration |
The studio starter already includes HAL.h through its .cpp file. Add the sections below where their comments say. In the emulator, use its tilt controls; test the feel on a device too.
Code example: tilt-controlled paddle¶
// Insert at the top of the file, below the #include lines
static float paddleX = 64.0f;
// Insert inside begin(), below setColorsOff();
paddleX = 64.0f;
// Insert inside update(), just above display.display();
paddleX += accelX * -0.002f;
// Keep the whole 40-pixel paddle on the screen.
if (paddleX < 20) {
paddleX = 20;
}
if (paddleX > 108) {
paddleX = 108;
}
display.clear();
display.fillRect((int)paddleX - 20, 56, 40, 4);
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
In the starter, nullptr means a pointer has no object. * in a declaration marks a pointer (an address), and before a pointer reads its object. & in a type refers to an existing object; before a value it gives its address. const means the code cannot change the value through that declaration.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
static float paddleX = 64.0f;
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
paddleX = 64.0f;
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
paddleX += accelX * -0.002f;
// Keep the whole 40-pixel paddle on the screen.
if (paddleX < 20) {
paddleX = 20;
}
if (paddleX > 108) {
paddleX = 108;
}
display.clear();
display.fillRect((int)paddleX - 20, 56, 40, 4);
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
Sign and scale
The readings are in milli-g: 1000 mg is one g. The example multiplies accelX by -0.002, so a reading of 1000 moves the paddle by -2 pixels per update. Adjust that factor to change the speed, or flip its sign to reverse the direction.
Code example: tilt-to-color¶
// Insert inside update(), just above display.display();
// Clamp first: a shake can exceed the range seen when tilting.
long x = constrain((long)accelX, -1000L, 1000L);
long y = constrain((long)accelY, -1000L, 1000L);
long z = constrain((long)accelZ, -1000L, 1000L);
uint8_t r = (uint8_t)map(x, -1000, 1000, 0, 255);
uint8_t g = (uint8_t)map(y, -1000, 1000, 0, 255);
uint8_t b = (uint8_t)map(z, -1000, 1000, 0, 255);
HAL::setRgbLed(pixel_Front_Top, r, g, b, 0);
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
// Clamp first: a shake can exceed the range seen when tilting.
long x = constrain((long)accelX, -1000L, 1000L);
long y = constrain((long)accelY, -1000L, 1000L);
long z = constrain((long)accelZ, -1000L, 1000L);
uint8_t r = (uint8_t)map(x, -1000, 1000, 0, 255);
uint8_t g = (uint8_t)map(y, -1000, 1000, 0, 255);
uint8_t b = (uint8_t)map(z, -1000, 1000, 0, 255);
HAL::setRgbLed(pixel_Front_Top, r, g, b, 0);
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
Code example: read the motion values¶
Show the readings on the screen to see how they change as you turn the device.
// Insert at the top of the file, below the #include lines
#include <stdio.h>
// Insert inside update(), just above display.display();
display.clear();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
char text[32];
snprintf(text, sizeof(text), "X %d Y %d", (int)accelX, (int)accelY);
display.drawString(10, 16, text);
snprintf(text, sizeof(text), "Z %d mg", (int)accelZ);
display.drawString(10, 32, text);
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
#include <stdio.h>
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.clear();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
char text[32];
snprintf(text, sizeof(text), "X %d Y %d", (int)accelX, (int)accelY);
display.drawString(10, 16, text);
snprintf(text, sizeof(text), "Z %d mg", (int)accelZ);
display.drawString(10, 32, text);
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
Functions on this page¶
| Call | What it does |
|---|---|
float accelXExample: accelX |
X acceleration, roughly 1000 units per g (gravity); emulator uses supplied tilt. |
float accelYExample: accelY |
Y acceleration, roughly 1000 units per g (gravity); emulator uses supplied tilt. |
float accelZExample: accelZ |
Z acceleration, roughly 1000 units per g (gravity); emulator uses supplied tilt. |
float tempCExample: tempC |
Temperature placeholder in degrees Celsius; both app targets leave it at 0. |
Framework details¶
Advanced: how the firmware reads motion
You do not need these details to build apps in the studio. They describe the firmware internals for readers writing an app by hand and building it into the firmware.
LIS2DH12¶
The accelerometer is an ST LIS2DH12, a 3-axis digital accelerometer. The firmware uses the SparkFun LIS2DH12 Arduino library for I2C communication.
TASK_50MS update rate¶
The HAL reads the accelerometer every 50 ms (20 Hz) in its TASK_50MS block. accelX, accelY, and accelZ are updated from s_accel.getX(), getY(), and getZ(). For most tilt-based apps, 20 Hz is sufficient; studio apps read the supplied values rather than changing the sensor polling rate.
Accessing the sensor directly¶
Direct sensor access is part of firmware internals. Studio apps cannot call HAL::accelerometer() or configure sensor interrupts. Read accelX, accelY and accelZ instead.