From d537c4cfdb0fcfd6da17e88cee7fc819f1a61268 Mon Sep 17 00:00:00 2001 From: Simon Wright Date: Sat, 25 Feb 2023 12:56:13 +0000 Subject: [PATCH] Add Microbit accelerometer demo. This only works for the V1.3B Microbit (assumes accelerometer is MMA8653). * test-microbit/Makefile: add accelerometer. * test-microbit/README.md: likewise. * test-microbit/tests.gpr: likewise. * test-microbit/accelerometer.adb: new. --- test-microbit/Makefile | 4 +-- test-microbit/README.md | 4 ++- test-microbit/accelerometer.adb | 48 +++++++++++++++++++++++++++++++++ test-microbit/tests.gpr | 7 +++-- 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 test-microbit/accelerometer.adb diff --git a/test-microbit/Makefile b/test-microbit/Makefile index b8e4e32..f1a9ed4 100644 --- a/test-microbit/Makefile +++ b/test-microbit/Makefile @@ -16,9 +16,9 @@ # along with this program; see the file COPYING3. If not, see # . -all: circle.hex events.hex seconds.hex +all: circle.hex events.hex seconds.hex accelerometer.hex -circle events seconds: rebuild.stamp +circle events seconds accelerometer: rebuild.stamp rebuild.stamp: force gprbuild -p -P tests touch $@ diff --git a/test-microbit/README.md b/test-microbit/README.md index 710b18a..0948c13 100644 --- a/test-microbit/README.md +++ b/test-microbit/README.md @@ -1,6 +1,6 @@ # micro:bit tests and demos # -There are three programs built by `make` (or `gprbuild`, but `make` goes on to build hex files that can be dropped onto the micro:bit): +There are four programs built by `make` (or `gprbuild`, but `make` goes on to build hex files that can be dropped onto the micro:bit): * `circle` displays a circle running round the LEDs: pressing button A alters the speed, button B alters the direction (clockwise/anticlockwise). The buttons are interrupt-driven. @@ -8,6 +8,8 @@ There are three programs built by `make` (or `gprbuild`, but `make` goes on to b * `seconds` checks out `Clock` functionality by flashing the centre LED once a second. You get to time a number of flashes with a stopwatch to make sure it really is once a second. +* `accelerometer` displays a single LED: if the card is level, in the centre; as the card is tilted, the lit LED moves to the lowest edge. + The tests interact with the micro:bit hardware using the [Ada\_Drivers\_Library](https://github.com/AdaCore/Ada_Drivers_Library). Configure it using that library's `project_wizard.py`: ``` Welcome to the Ada Drivers Library (ADL) project wizard. This script will diff --git a/test-microbit/accelerometer.adb b/test-microbit/accelerometer.adb new file mode 100644 index 0000000..f8ec143 --- /dev/null +++ b/test-microbit/accelerometer.adb @@ -0,0 +1,48 @@ +with MMA8653; +with MicroBit.I2C; +with LEDs; +with Ada.Real_Time; + +procedure Accelerometer is + Acc : MMA8653.MMA8653_Accelerometer (Port => MicroBit.I2C.Controller); +begin + if not MicroBit.I2C.Initialized then + MicroBit.I2C.Initialize (S => MicroBit.I2C.S400kbps); + end if; + + if not Acc.Check_Device_Id then + for R in LEDs.Coord'Range loop + for C in LEDs.Coord'Range loop + LEDs.Set_One_LED (R, C); + end loop; + end loop; + delay until Ada.Real_Time.Time_Last; + end if; + + Acc.Configure (MMA8653.Two_G, + MMA8653.High_Resolution, + MMA8653.High_Resolution); + + loop + declare + Detection_Limit : constant := 75; + Data : constant MMA8653.All_Axes_Data := Acc.Read_Data; + use type MMA8653.Axis_Data; + use type Ada.Real_Time.Time; + begin + LEDs.Clear_All_LEDs; + if Data.X > Detection_Limit then + LEDs.Set_One_LED (3, 1); + elsif Data.X < -Detection_Limit then + LEDs.Set_One_LED (3, 5); + elsif Data.Y > Detection_Limit then + LEDs.Set_One_LED (1, 3); + elsif Data.Y < -Detection_Limit then + LEDs.Set_One_LED (5, 3); + else + LEDs.Set_One_LED (3, 3); + end if; + delay until Ada.Real_Time.Clock + Ada.Real_Time.Milliseconds (100); + end; + end loop; +end Accelerometer; diff --git a/test-microbit/tests.gpr b/test-microbit/tests.gpr index 1458c82..6f91264 100644 --- a/test-microbit/tests.gpr +++ b/test-microbit/tests.gpr @@ -1,4 +1,4 @@ --- Copyright (C) 2018, 2020 Free Software Foundation, Inc. +-- Copyright (C) 2018-2023 Free Software Foundation, Inc. -- -- This file is part of the Cortex GNAT RTS package. -- @@ -20,7 +20,10 @@ with "Ada_Drivers_Library/ada_drivers_library"; project Tests is - for Main use ("circle.adb", "events.adb", "seconds.adb"); + for Main use ("circle.adb", + "events.adb", + "seconds.adb", + "accelerometer.adb"); for Languages use ("Ada"); for Source_Dirs use (".", "../test-common"); for Object_Dir use ".build";