Skip to content

Commit

Permalink
Add Microbit accelerometer demo.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simonjwright committed Feb 25, 2023
1 parent f7afb32 commit d537c4c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
4 changes: 2 additions & 2 deletions test-microbit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# along with this program; see the file COPYING3. If not, see
# <http://www.gnu.org/licenses/>.

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 $@
Expand Down
4 changes: 3 additions & 1 deletion test-microbit/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# 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.

* `events` demonstrates `Timing_Events`. The top-left LED (row 1, column 1) flashes every 2 seconds, the LED in row 4, column 5 flashes every 5 seconds. _Why not the bottom right LED? because of the complicated mapping of GPIOs to LEDS_.

* `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
Expand Down
48 changes: 48 additions & 0 deletions test-microbit/accelerometer.adb
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 5 additions & 2 deletions test-microbit/tests.gpr
Original file line number Diff line number Diff line change
@@ -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.
--
Expand All @@ -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";
Expand Down

0 comments on commit d537c4c

Please sign in to comment.