----------------------------------------------------------------------------
--
--                              -*- Mode: Ada -*-
--
-- Filename        : gyro.adb
-- Description     : All you need to read the laser range finder on the B14
-- Author          : Uwe R. Zimmer
-- Created On      : Wed Oct 15 16:04:34 1997
-- Last Modified By:
-- Last Modified On:
-- Update Count    :
-- Status          :
--
----------------------------------------------------------------------------

----------------------------------------------------------------------------
--
-- Imports
--
----------------------------------------------------------------------------

with Metrics;        use Metrics;
with Ada.Real_Time;  use Ada.Real_Time;
with Interfaces.C;   use Interfaces.C;
with Gyro_driver;    use Gyro_driver;

----------------------------------------------------------------------------
--
-- Package Laser
--
----------------------------------------------------------------------------

package body Gyro is

----------------------------------------------------------------------------
--
-- Variables global for package Laser
--
----------------------------------------------------------------------------

   GyroInitialized   : Boolean := False;

   NoOfClients       : Natural := 0;
   NoOfSuspends      : Natural := 0;

----------------------------------------------------------------------------
--
-- Protected Monitor GyroInterface;
--
----------------------------------------------------------------------------

   protected body GyroInterface is


      procedure InitGyro is

      begin
         if not GyroInitialized then
            GyroDriverInterface.InitGyroDriver;
            GyroMonitor.BlockTask;
            GyroInitialized := True;
         end if;
         NoOfClients := NoOfClients + 1;
      end InitGyro;


      entry ShutdownGyro when GyroInitialized is

      begin
         NoOfClients := NoOfClients - 1;
         if NoOfClients = 0 then
            GyroDriverInterface.ShutdownGyroDriver;
            GyroInitialized := False;
         end if;
      end ShutdownGyro;


      entry SuspendSensing when GyroInitialized is

      begin
         NoOfSuspends := NoOfSuspends + 1;
         if NoOfSuspends >= NoOfClients then
            GyroDriverInterface.SuspendGyroDriver;
         end if;
      end SuspendSensing;


      entry ResumeSensing when GyroInitialized is

      begin
         NoOfSuspends := NoOfSuspends - 1;
         if NoOfSuspends < NoOfClients then
            GyroDriverInterface.ResumeGyroDriver;
         end if;
      end ResumeSensing;


      entry GetCurrentGyroReading (Reading : out GyroReading)
         when GyroInitialized is

      begin
         GyroDriverInterface.GetMostRecentGyroData (Reading);
      end GetCurrentGyroReading;


      entry GetCurrentGyroTimeStamp (TimeStamp: out Time)
      when GyroInitialized is

      begin
         GyroDriverInterface.GetMostRecentGyroTimeStamp (TimeStamp);
      end GetCurrentGyroTimeStamp;


      entry GetAllGyroDataSince     (TimeStamp    :  in Time;
                                     Readings     : out GyroReadingArray;
                                     NoOfReadings : out Natural)
      when GyroInitialized is

      begin
         GyroDriverInterface.GetAllGyroSince
           (TimeStamp, Readings, NoOfReadings);
      end GetAllGyroDataSince;


      entry WaitforNewGyroData when GyroInitialized is

      begin
         requeue GyroMonitor.BlockTask;
      end WaitforNewGyroData;


   end GyroInterface;

end Gyro;