This app has been written in C for Tizen 2.3.1, using the Samsung Gear S2 watch. The goal of the project was to enable one to collect accelerometer and gyroscope data from the watch that can later be used for other development purposes. These may include tracking user motions and advanced machine learning of the data.
A user starts the data collection by pressing the Start button. Data collection ends when the user presses Stop. A .txt file is then saved inside the app's media
folder with all the data.
Tizen OS allows developpers to write apps in either C or HTML/Javascript.
Writing in Javascript and HTML/CSS files is very easy and straightfoward. Tizen essentially abstracts virtually all functionality into an additional Web API that sits on top of the main API that is accessible in C.
The ultimate reason for why this app was developped in C is performance: the frequency at which the app is able to get the sensor data. Using Javascript, developers are restricted to an about 35 ms time interval between calls. In C, you can go as low as 10 ms.
/inc
- header files:
basicui.h
sensors.h
/src
- source files:
-
basicui.c
:write_data(...) // -> writes the collected data onto a .txt file save_data(...) // -> saves the .txt file in the `/media` folder _button_click_cb(...) // -> triggered on user press create_base_gui(...) // -> creates the app UI app_create(...) // -> initialises the app main(...)
-
sensors.c
:register_accelerometer_callback(...) accelerometer_cb(...) register_gyroscope_callback(...) gyroscope_cb(...)
- The app_data object, defined as a
struct
inbasicui.h
, holds all the important objects and data including sensor variables and objects and UI elements. /src/basicui.c
is the main file and includesint main()
the UI build and all data data collection functions.app_create()
is invoked and a UI is created withcreate_base_gui(ad)
wheread
is the pointer to the app_data object, defined as astruct
inbasicui.h
./src/sensors.c
contains all functions related to the sensors initialization and callback functions.- The app_data pointer is passed around multiple functions and ultimately is used to save the collected data in a file, after which the data array variable is reset.
tizen-manifest.xml
is to be used to get priveleges, e.g. for writing and storing files in the device.
In sensors.c
:
static int register_accelerometer_callback(...)
static int register_gyroscope_callback(...)
Tizen API functions that link to their respective callback and listening functions.
They are called in the app_create(...)
function of /src/basicui.c
The app_data ad
struct will point to the sensors objects:
// inside register_accelerometer_callback(...)
sensor_h *accelerometer = ad->accelerometer;
sensor_listener_h *accelerationListener = ad->accelerationListener;
In sensors.c
:
static void accelerometer_cb(...)
static void gyroscope_cb(...)
These functions are linked to the sensors in the sensor register functions. But they do not need to be called manually.
In order to start listening to sensors and get data, invoke API function sensor_listener_start(...)
:
// start accelerometer
int error = sensor_listener_start(ad->accelerationListener);
where ad
points to the app_data object and accelerationListener
has been defined in the register function.
The user starts data collection after pressing the Start button. The _button_click_cb(...)
function is then called. It first checks the current state of the button, i.e. its title and either starts collection or saves the collected data.
Data is oganised in an array and saved in the json syntax in the following order:
- timestamp
- accelerometer X
- accelerometer Y
- accelerometer Z
- gyroscope X
- gyroscope Y
- gyroscope Z
You might consider delaying the data collection by a few seconds while the user positions himself and starts doing the activity of interest.