Skip to content

Commit d537c4c

Browse files
committed
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.
1 parent f7afb32 commit d537c4c

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

test-microbit/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
# along with this program; see the file COPYING3. If not, see
1717
# <http://www.gnu.org/licenses/>.
1818

19-
all: circle.hex events.hex seconds.hex
19+
all: circle.hex events.hex seconds.hex accelerometer.hex
2020

21-
circle events seconds: rebuild.stamp
21+
circle events seconds accelerometer: rebuild.stamp
2222
rebuild.stamp: force
2323
gprbuild -p -P tests
2424
touch $@

test-microbit/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# micro:bit tests and demos #
22

3-
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):
3+
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):
44

55
* `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.
66

77
* `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_.
88

99
* `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.
1010

11+
* `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.
12+
1113
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`:
1214
```
1315
Welcome to the Ada Drivers Library (ADL) project wizard. This script will

test-microbit/accelerometer.adb

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
with MMA8653;
2+
with MicroBit.I2C;
3+
with LEDs;
4+
with Ada.Real_Time;
5+
6+
procedure Accelerometer is
7+
Acc : MMA8653.MMA8653_Accelerometer (Port => MicroBit.I2C.Controller);
8+
begin
9+
if not MicroBit.I2C.Initialized then
10+
MicroBit.I2C.Initialize (S => MicroBit.I2C.S400kbps);
11+
end if;
12+
13+
if not Acc.Check_Device_Id then
14+
for R in LEDs.Coord'Range loop
15+
for C in LEDs.Coord'Range loop
16+
LEDs.Set_One_LED (R, C);
17+
end loop;
18+
end loop;
19+
delay until Ada.Real_Time.Time_Last;
20+
end if;
21+
22+
Acc.Configure (MMA8653.Two_G,
23+
MMA8653.High_Resolution,
24+
MMA8653.High_Resolution);
25+
26+
loop
27+
declare
28+
Detection_Limit : constant := 75;
29+
Data : constant MMA8653.All_Axes_Data := Acc.Read_Data;
30+
use type MMA8653.Axis_Data;
31+
use type Ada.Real_Time.Time;
32+
begin
33+
LEDs.Clear_All_LEDs;
34+
if Data.X > Detection_Limit then
35+
LEDs.Set_One_LED (3, 1);
36+
elsif Data.X < -Detection_Limit then
37+
LEDs.Set_One_LED (3, 5);
38+
elsif Data.Y > Detection_Limit then
39+
LEDs.Set_One_LED (1, 3);
40+
elsif Data.Y < -Detection_Limit then
41+
LEDs.Set_One_LED (5, 3);
42+
else
43+
LEDs.Set_One_LED (3, 3);
44+
end if;
45+
delay until Ada.Real_Time.Clock + Ada.Real_Time.Milliseconds (100);
46+
end;
47+
end loop;
48+
end Accelerometer;

test-microbit/tests.gpr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Copyright (C) 2018, 2020 Free Software Foundation, Inc.
1+
-- Copyright (C) 2018-2023 Free Software Foundation, Inc.
22
--
33
-- This file is part of the Cortex GNAT RTS package.
44
--
@@ -20,7 +20,10 @@ with "Ada_Drivers_Library/ada_drivers_library";
2020

2121
project Tests is
2222

23-
for Main use ("circle.adb", "events.adb", "seconds.adb");
23+
for Main use ("circle.adb",
24+
"events.adb",
25+
"seconds.adb",
26+
"accelerometer.adb");
2427
for Languages use ("Ada");
2528
for Source_Dirs use (".", "../test-common");
2629
for Object_Dir use ".build";

0 commit comments

Comments
 (0)