ltp_305_0.2.0_f11f3114/src/ltp_305.ads

  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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
--
--  Copyright 2021 (C) Jeremy Grosser
--
--  SPDX-License-Identifier: BSD-3-Clause
--
--  This is a driver for the Pimoroni PIM526 LED Dot Matrix Breakout using an
--  IS31FL3730 I2C LED matrix driver to control two LTP-305 5x7 matrices.
--  https://shop.pimoroni.com/products/led-dot-matrix-breakout
--
--  The breakout board includes pullups on the SDA and SCL pins, internal
--  pullups are unnecessary.
--
with Ada.Unchecked_Conversion;
with Interfaces;
with HAL.I2C;

package LTP_305 is

   use HAL;
   Default_Address : constant HAL.I2C.I2C_Address := 16#61# * 2;

   subtype Display_Column is Integer range 0 .. 4;
   subtype Display_Row is Integer range 0 .. 6;
   type Display_Matrix is array (Display_Column, Display_Row) of Boolean;

   type Matrix_Bits is new Interfaces.Unsigned_64;
   subtype Matrix_Array is HAL.UInt8_Array (1 .. 8);

   type Command is (Mode,
                    Matrix_R,
                    Update,
                    Options,
                    Matrix_L,
                    Brightness,
                    Reset)
      with Size => 8;

   for Command use (
       Mode       => 16#00#,
       Matrix_R   => 16#01#,
       Update     => 16#0C#,
       Options    => 16#0D#,
       Matrix_L   => 16#0E#,
       Brightness => 16#19#,
       Reset      => 16#FF#
    );

   type Outputs is (Matrix_1, Matrix_2, Both)
      with Size => 2;

   for Outputs use
      (Matrix_1 => 2#00#,
       Matrix_2 => 2#01#,
       Both     => 2#11#);

   type Matrix_Modes is (Matrix_8x8, Matrix_7x9, Matrix_6x10, Matrix_5x11)
      with Size => 2;

   for Matrix_Modes use
      (Matrix_8x8  => 2#00#,
       Matrix_7x9  => 2#01#,
       Matrix_6x10 => 2#10#,
       Matrix_5x11 => 2#11#);

   type Config_Register is record
      Shutdown    : Boolean := False;
      Reserved    : Boolean := False;
      Output      : Outputs := Both;
      Audio_In    : Boolean := False;
      Matrix_Mode : Matrix_Modes := Matrix_8x8;
   end record
      with Size => 8;

   for Config_Register use record
      Shutdown    at 0 range 7 .. 7;
      Reserved    at 0 range 5 .. 6;
      Output      at 0 range 3 .. 4;
      Audio_In    at 0 range 2 .. 2;
      Matrix_Mode at 0 range 0 .. 1;
   end record;

   function Convert is new Ada.Unchecked_Conversion
      (Source => Matrix_Bits,
       Target => Matrix_Array);

   function Convert is new Ada.Unchecked_Conversion
      (Source => Config_Register,
       Target => HAL.UInt8);

   -----------------------------------------------------------------------
   --  Initializes the matrix, must be called before anything else
   -----------------------------------------------------------------------
   procedure Initialize (This    : HAL.I2C.Any_I2C_Port;
                         Address : HAL.I2C.I2C_Address);

   procedure Write_Byte_Data
      (This    : HAL.I2C.Any_I2C_Port;
       Address : HAL.I2C.I2C_Address;
       Cmd     : Command;
       B       : HAL.UInt8);

   procedure Write_Block_Data
      (This    : HAL.I2C.Any_I2C_Port;
       Address : HAL.I2C.I2C_Address;
       Cmd     : Command;
       Data    : HAL.UInt8_Array);

   function To_Left_Matrix
      (DM : Display_Matrix;
       DP : Boolean := False)
      return Matrix_Array;

   function To_Right_Matrix
      (DM : Display_Matrix;
       DP : Boolean := False)
      return Matrix_Array;

   -----------------------------------------------------------------------
   --  Writes the
   --    character with the Code from Font.Characters
   --  into the
   --    Location: left/right
   --  and sets the decimal point dependent on
   --    DP
   -----------------------------------------------------------------------
   procedure Write (This     : HAL.I2C.Any_I2C_Port;
                    Address  : HAL.I2C.I2C_Address;
                    Location : Command;
                    Code     : Integer;
                    DP       : Boolean);
end LTP_305;