------------------------------------------------------------------------------
-- Template                                                                         --
------------------------------------------------------------------------------

with LSM303; use LSM303;

with MicroBit.Display;
with MicroBit.Accelerometer;
with MicroBit.Console;
-- with MicroBit.IOs;
with MicroBit.Time;
with nRF.GPIO; use nRF.GPIO;

use MicroBit;
-- use MicroBit.IOs;


procedure Main is

   -- PROGRAM VARIABLES
   Data : LSM303.All_Axes_Data;
   -- Value : MicroBit.IOs.Analog_Value;
   FreeFallCounter : Integer;
   FreeFallDetectionCycles : Integer;
   FreeFallCondition : Boolean;
   Threshold : Axis_Data;

   -- IO VARIABLES
   DO_1_Pt : GPIO_Point;
   DO_2_Pt : GPIO_Point;
   DO_3_Pt : GPIO_Point;
   DO_4_Pt : GPIO_Point;
   DO_Conf : GPIO_Configuration;


begin

   -- PROGRAM STARTUP
   Console.Put_Line ("Jumper - starting up ..");
   FreeFallCondition := False;
   FreeFallCounter := 0;
   FreeFallDetectionCycles := 70;  -- 70 * 5ms = 350ms
   Threshold := 150;  -- acceleration sensor threshold

   -- IO CONFIGURATION
   DO_1_Pt := MB_P2;
   DO_2_Pt := MB_P8;
   DO_3_Pt := MB_P9;
   DO_4_Pt := MB_P16;
   DO_Conf.Mode := Mode_Out;
   DO_Conf.Resistors := No_Pull;
   DO_Conf.Input_Buffer := Input_Buffer_Connect;
   DO_Conf.Sense := Sense_Disabled;

   -- APPLY IO CONFIGURATION
   DO_1_Pt.Configure_IO(DO_Conf);
   DO_1_Pt.Clear;
   DO_2_Pt.Configure_IO(DO_Conf);
   DO_2_Pt.Clear;
   DO_3_Pt.Configure_IO(DO_Conf);
   DO_3_Pt.Clear;
   DO_4_Pt.Configure_IO(DO_Conf);
   DO_4_Pt.Clear;

   loop

      --  Read the accelerometer data
      Data := Accelerometer.Data;

      --  Print the data on the serial port
      Console.Put_Line ("X:" & Data.X'Img & ASCII.HT &
                        "Y:" & Data.Y'Img & ASCII.HT &
                        "Z:" & Data.Z'Img);

      -- FREEFALL DETECTION
      -- check wether all axis are sub-threshold => increment counter
      if abs(Data.X) < Threshold and
         abs(Data.Y) < Threshold and
         abs(Data.Z) < Threshold then
         FreeFallCounter := FreeFallCounter + 1;
      -- otherwise reset counter
      else
         FreeFallCounter := 0;
      end if;



      -- FREEFALL DETECTION
      if FreeFallCounter >= FreeFallDetectionCycles then
         -- set free fall condition
         FreeFallCondition := True;
      end if;

      --  Clear the LED matrix
      Display.Clear;

      -- Check, wether FreeFallCondition was triggered
      if FreeFallCondition then
         -- set outputs
         DO_1_Pt.Set;
         DO_2_Pt.Set;
         DO_3_Pt.Set;
         DO_4_Pt.Set;
         Display.Display ('X');
      else
         Display.Display ('0');
      end if;


      -- Read analogue pin (could be 0,1,2,3,4, or 10)
      -- Value := MicroBit.IOs.Analog (2);
      -- Console.Put_Line ("Value : " & Value'Image);

      -- Set output
      -- if Value > Analog_Value(200) then
      --    MicroBit.IOs.Set (12, True);
      -- else
      --    MicroBit.IOs.Set (12, False);
      -- end if;

      Time.Sleep (5);  -- 200 samples / s
   end loop;
end Main;
