1.. zephyr:code-sample:: tflite-magicwand
2   :name: Magic Wand
3
4   Recognize gestures from an accelerometer using TensorFlow Lite for Microcontrollers and a 20KB
5   neural network.
6
7Overview
8********
9
10This sample application shows how to use TensorFlow Lite Micro
11to run a 20 kilobyte neural network model that recognizes gestures
12from an accelerometer.
13
14.. Note::
15    This README and sample have been modified from
16    `the TensorFlow Magic Wand sample for Zephyr`_ and
17    `the Antmicro tutorial on Renode emulation for TensorFlow`_.
18
19.. _the TensorFlow Magic Wand sample for Zephyr:
20    https://github.com/tensorflow/tflite-micro-arduino-examples/tree/main/examples/magic_wand
21
22.. _the Antmicro tutorial on Renode emulation for TensorFlow:
23    https://github.com/antmicro/litex-vexriscv-tensorflow-lite-demo
24
25Building and Running
26********************
27
28Add the tflite-micro module to your West manifest and pull it:
29
30.. code-block:: console
31
32    west config manifest.project-filter -- +tflite-micro
33    west update
34
35The application can be built for the :ref:`litex-vexriscv` for
36emulation in Renode as follows:
37
38.. zephyr-app-commands::
39   :zephyr-app: samples/modules/tflite-micro/magic_wand
40   :host-os: unix
41   :board: litex_vexriscv
42   :goals: build
43   :compact:
44
45Once the application is built, `download and install Renode 1.12 or higher as a package`_
46following the instructions in the `Renode GitHub README`_ and
47start the emulator:
48
49.. code-block:: console
50
51    renode -e "set zephyr_elf @./build/zephyr/zephyr.elf; s @./samples/modules/tflite-micro/magic_wand/renode/litex-vexriscv-tflite.resc"
52
53.. _download and install Renode 1.12 or higher as a package:
54    https://github.com/renode/renode/releases/
55
56.. _Renode GitHub README:
57    https://github.com/renode/renode/blob/master/README.rst
58
59Sample Output
60=============
61
62The Renode-emulated LiteX/VexRiscv board is fed data that the
63application recognizes as a series of alternating ring and slope
64gestures.
65
66.. code-block:: console
67
68    Got accelerometer, label: accel-0
69
70    RING:
71              *
72           *     *
73         *         *
74        *           *
75         *         *
76           *     *
77              *
78
79    SLOPE:
80            *
81           *
82          *
83         *
84        *
85       *
86      *
87     * * * * * * * *
88
89    RING:
90              *
91           *     *
92         *         *
93        *           *
94         *         *
95           *     *
96              *
97
98    SLOPE:
99            *
100           *
101          *
102         *
103        *
104       *
105      *
106     * * * * * * * *
107
108Modifying Sample for Your Own Project
109*************************************
110
111It is recommended that you copy and modify one of the two TensorFlow
112samples when creating your own TensorFlow project. To build with
113TensorFlow, you must enable the below Kconfig options in your :file:`prj.conf`:
114
115.. code-block:: cfg
116
117    CONFIG_CPP=y
118    CONFIG_REQUIRES_FULL_LIBC=y
119    CONFIG_TENSORFLOW_LITE_MICRO=y
120
121Training
122********
123Follow the instructions in the :file:`train/` directory to train your
124own model for use in the sample.
125