bme280_1.0.0_85a38919/examples/bme280_put/main.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
--  SPDX-FileCopyrightText: 2023 Max Reznik <reznikmm@gmail.com>
--
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
----------------------------------------------------------------

with Ada.Real_Time;
with Ada.Text_IO;

with Ravenscar_Time;

with STM32.Board;
with STM32.Device;
with STM32.Setup;

with BME280.I2C;

procedure Main is
   use type Ada.Real_Time.Time;

   package BME280_I2C is new BME280.I2C
     (I2C_Port    => STM32.Device.I2C_1'Access,
      I2C_Address => 16#76#);

   Next : Ada.Real_Time.Time := Ada.Real_Time.Clock;

   Ok          : Boolean;
   Calib       : BME280.Calibration_Constants;
   Measurement : BME280.Measurement;
   Temp        : BME280.Deci_Celsius;
   Humi        : BME280.Relative_Humidity;
   Press       : BME280.Pressure_Pa;

begin
   STM32.Board.Initialize_LEDs;
   STM32.Setup.Setup_I2C_Master
     (Port        => STM32.Device.I2C_1,
      SDA         => STM32.Device.PB9,
      SCL         => STM32.Device.PB8,
      SDA_AF      => STM32.Device.GPIO_AF_I2C1_4,
      SCL_AF      => STM32.Device.GPIO_AF_I2C1_4,
      Clock_Speed => 400_000);

   --  Look for BME280 chip
   if not BME280_I2C.Sensor.Check_Chip_Id then
      Ada.Text_IO.Put_Line ("BME280 not found.");
      raise Program_Error;
   end if;

   --  Reset BME280
   BME280_I2C.Sensor.Reset (Ravenscar_Time.Delays, Ok);
   pragma Assert (Ok);

   --  Read calibration data into Clib
   BME280_I2C.Sensor.Read_Calibration (Calib, Ok);

   --  Consigure IRR filter and minimal incativity delay
   BME280_I2C.Sensor.Configure
     (Standby    => 0.5,
      Filter     => BME280.X16,
      SPI_3_Wire => False,
      Success    => Ok);
   pragma Assert (Ok);

   --  Enable cycling of measurements with given oversamplig
   BME280_I2C.Sensor.Start
     (Mode        => BME280.Normal,
      Humidity    => BME280.X1,
      Pressure    => BME280.X16,
      Temperature => BME280.X2,
      Success     => Ok);

   --  Wait for the first measurement
   Ravenscar_Time.Delays.Delay_Milliseconds
     (BME280.Max_Measurement_Time
        (Humidity    => BME280.X1,
         Pressure    => BME280.X16,
         Temperature => BME280.X2) / 1000 + 1);

   loop
      STM32.Board.Toggle (STM32.Board.D1_LED);

      --  Read raw values from the sensor
      BME280_I2C.Sensor.Read_Measurement (Measurement, Ok);

      if Ok then
         --  Decode temperature, humidity and pressure
         Temp := BME280.Temperature (Measurement, Calib);
         Humi := BME280.Humidity (Measurement, Temp, Calib);
         Press := BME280.Pressure (Measurement, Temp, Calib);

         Ada.Text_IO.Put_Line
           ("T=" & Temp'Image &
              " H=" & Humi'Image &
              " P=" & Press'Image);
      end if;

      Next := Next + Ada.Real_Time.Milliseconds (500);
      delay until Next;
   end loop;
end Main;