<commit_before>with Ada.Real_Time; use Ada.Real_Time;
with Ada.Unchecked_Conversion;
with System;
with Interfaces; use Interfaces;

package body ST7735R is

   ---------------------------
   --  Register definitions --
   ---------------------------

   type MADCTL is record
      Reserved1, Reserved2 : Boolean;
      MH  : Horizontal_Refresh_Order;
      RGB : RGB_BGR_Order;
      ML  : Vertical_Refresh_Order;
      MV  : Boolean;
      MX  : Column_Address_Order;
      MY  : Row_Address_Order;
   end record with Size => 8, Bit_Order => System.Low_Order_First;

   for MADCTL use record
      Reserved1 at 0 range 0 .. 0;
      Reserved2 at 0 range 1 .. 1;
      MH        at 0 range 2 .. 2;
      RGB       at 0 range 3 .. 3;
      ML        at 0 range 4 .. 4;
      MV        at 0 range 5 .. 5;
      MX        at 0 range 6 .. 6;
      MY        at 0 range 7 .. 7;
   end record;

   function To_Byte is new Ada.Unchecked_Conversion (MADCTL, Byte);

   procedure Write_Command (LCD : ST7735R_Device;
                            Cmd : Byte);
   procedure Write_Command (LCD  : ST7735R_Device;
                            Cmd  : Byte;
                            Data : HAL.Byte_Array);

   procedure Write_Data (LCD : ST7735R_Device;
                         Data : Byte);
   pragma Unreferenced (Write_Data);
   procedure Write_Data (LCD  : ST7735R_Device;
                         Data : HAL.Byte_Array);
   procedure Write_Data (LCD  : ST7735R_Device;
                         Data : HAL.Short_Array);

   procedure Set_Command_Mode (LCD : ST7735R_Device);
   procedure Set_Data_Mode (LCD : ST7735R_Device);
   procedure Start_Transaction (LCD : ST7735R_Device);
   procedure End_Transaction (LCD : ST7735R_Device);

   ----------------------
   -- Set_Command_Mode --
   ----------------------

   procedure Set_Command_Mode (LCD : ST7735R_Device) is
   begin
      LCD.RS.Clear;
   end Set_Command_Mode;

   -------------------
   -- Set_Data_Mode --
   -------------------

   procedure Set_Data_Mode (LCD : ST7735R_Device) is
   begin
      LCD.RS.Set;
   end Set_Data_Mode;

   -----------------------
   -- Start_Transaction --
   -----------------------

   procedure Start_Transaction (LCD : ST7735R_Device) is
   begin
      LCD.CS.Clear;
   end Start_Transaction;

   ---------------------
   -- End_Transaction --
   ---------------------

   procedure End_Transaction (LCD : ST7735R_Device) is
   begin
      LCD.CS.Set;
   end End_Transaction;

   -------------------
   -- Write_Command --
   -------------------

   procedure Write_Command (LCD : ST7735R_Device;
                            Cmd : Byte)
   is
      Status : SPI_Status;
   begin
      Start_Transaction (LCD);
      Set_Command_Mode (LCD);
      LCD.Port.Transmit (SPI_Data_8b'(1 => Cmd),
                         Status);
      End_Transaction (LCD);
      if Status /= Ok then
         --  No error handling...
         raise Program_Error;
      end if;
   end Write_Command;

   -------------------
   -- Write_Command --
   -------------------

   procedure Write_Command (LCD  : ST7735R_Device;
                            Cmd  : Byte;
                            Data : HAL.Byte_Array)
   is
   begin
      Write_Command (LCD, Cmd);
      Write_Data (LCD, Data);
   end Write_Command;

   ----------------
   -- Write_Data --
   ----------------

   procedure Write_Data (LCD : ST7735R_Device;
                         Data : Byte)
   is
      Status : SPI_Status;
   begin
      Start_Transaction (LCD);
      Set_Data_Mode (LCD);
      LCD.Port.Transmit (SPI_Data_8b'(1 => Data),
                         Status);
      End_Transaction (LCD);
      if Status /= Ok then
         --  No error handling...
         raise Program_Error;
      end if;
   end Write_Data;

   ----------------
   -- Write_Data --
   ----------------

   procedure Write_Data (LCD  : ST7735R_Device;
                         Data : HAL.Byte_Array)
   is
      Status : SPI_Status;
   begin
      Start_Transaction (LCD);
      Set_Data_Mode (LCD);
      for Elt of Data loop
         LCD.Port.Transmit (SPI_Data_8b'(1 => Elt),
                            Status);
         if Status /= Ok then
            --  No error handling...
            raise Program_Error;
         end if;
      end loop;
      End_Transaction (LCD);
   end Write_Data;

   ----------------
   -- Write_Data --
   ----------------

   procedure Write_Data (LCD  : ST7735R_Device;
                         Data : HAL.Short_Array)
   is
      B1, B2 : Byte;
      Status : SPI_Status;
   begin
      Start_Transaction (LCD);
      Set_Data_Mode (LCD);
      for Elt of Data loop
         B1 := Byte (Elt and 16#FF#);
         B2 := Byte (Interfaces.Shift_Right (Elt, 8));
         LCD.Port.Transmit (SPI_Data_8b'(B1, B2),
                            Status);
         if Status /= Ok then
            --  No error handling...
            raise Program_Error;
         end if;
      end loop;
      End_Transaction (LCD);
   end Write_Data;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (LCD : in out ST7735R_Device) is
   begin
      LCD.Initialized := True;

      LCD.RST.Clear;
      delay until Clock + Milliseconds (100);
      LCD.RST.Set;
      delay until Clock + Milliseconds (100);

      --  Sleep Exit
      Write_Command (LCD, 16#11#);

      delay until Clock + Milliseconds (100);
   end Initialize;<commit_msg>ST7735R: Fix output format (byte swap)<commit_after>with Ada.Real_Time; use Ada.Real_Time;
with Ada.Unchecked_Conversion;
with System;
with Interfaces; use Interfaces;

package body ST7735R is

   ---------------------------
   --  Register definitions --
   ---------------------------

   type MADCTL is record
      Reserved1, Reserved2 : Boolean;
      MH  : Horizontal_Refresh_Order;
      RGB : RGB_BGR_Order;
      ML  : Vertical_Refresh_Order;
      MV  : Boolean;
      MX  : Column_Address_Order;
      MY  : Row_Address_Order;
   end record with Size => 8, Bit_Order => System.Low_Order_First;

   for MADCTL use record
      Reserved1 at 0 range 0 .. 0;
      Reserved2 at 0 range 1 .. 1;
      MH        at 0 range 2 .. 2;
      RGB       at 0 range 3 .. 3;
      ML        at 0 range 4 .. 4;
      MV        at 0 range 5 .. 5;
      MX        at 0 range 6 .. 6;
      MY        at 0 range 7 .. 7;
   end record;

   function To_Byte is new Ada.Unchecked_Conversion (MADCTL, Byte);

   procedure Write_Command (LCD : ST7735R_Device;
                            Cmd : Byte);
   procedure Write_Command (LCD  : ST7735R_Device;
                            Cmd  : Byte;
                            Data : HAL.Byte_Array);

   procedure Write_Data (LCD : ST7735R_Device;
                         Data : Byte);
   pragma Unreferenced (Write_Data);
   procedure Write_Data (LCD  : ST7735R_Device;
                         Data : HAL.Byte_Array);
   procedure Write_Data (LCD  : ST7735R_Device;
                         Data : HAL.Short_Array);

   procedure Set_Command_Mode (LCD : ST7735R_Device);
   procedure Set_Data_Mode (LCD : ST7735R_Device);
   procedure Start_Transaction (LCD : ST7735R_Device);
   procedure End_Transaction (LCD : ST7735R_Device);

   ----------------------
   -- Set_Command_Mode --
   ----------------------

   procedure Set_Command_Mode (LCD : ST7735R_Device) is
   begin
      LCD.RS.Clear;
   end Set_Command_Mode;

   -------------------
   -- Set_Data_Mode --
   -------------------

   procedure Set_Data_Mode (LCD : ST7735R_Device) is
   begin
      LCD.RS.Set;
   end Set_Data_Mode;

   -----------------------
   -- Start_Transaction --
   -----------------------

   procedure Start_Transaction (LCD : ST7735R_Device) is
   begin
      LCD.CS.Clear;
   end Start_Transaction;

   ---------------------
   -- End_Transaction --
   ---------------------

   procedure End_Transaction (LCD : ST7735R_Device) is
   begin
      LCD.CS.Set;
   end End_Transaction;

   -------------------
   -- Write_Command --
   -------------------

   procedure Write_Command (LCD : ST7735R_Device;
                            Cmd : Byte)
   is
      Status : SPI_Status;
   begin
      Start_Transaction (LCD);
      Set_Command_Mode (LCD);
      LCD.Port.Transmit (SPI_Data_8b'(1 => Cmd),
                         Status);
      End_Transaction (LCD);
      if Status /= Ok then
         --  No error handling...
         raise Program_Error;
      end if;
   end Write_Command;

   -------------------
   -- Write_Command --
   -------------------

   procedure Write_Command (LCD  : ST7735R_Device;
                            Cmd  : Byte;
                            Data : HAL.Byte_Array)
   is
   begin
      Write_Command (LCD, Cmd);
      Write_Data (LCD, Data);
   end Write_Command;

   ----------------
   -- Write_Data --
   ----------------

   procedure Write_Data (LCD : ST7735R_Device;
                         Data : Byte)
   is
      Status : SPI_Status;
   begin
      Start_Transaction (LCD);
      Set_Data_Mode (LCD);
      LCD.Port.Transmit (SPI_Data_8b'(1 => Data),
                         Status);
      End_Transaction (LCD);
      if Status /= Ok then
         --  No error handling...
         raise Program_Error;
      end if;
   end Write_Data;

   ----------------
   -- Write_Data --
   ----------------

   procedure Write_Data (LCD  : ST7735R_Device;
                         Data : HAL.Byte_Array)
   is
      Status : SPI_Status;
   begin
      Start_Transaction (LCD);
      Set_Data_Mode (LCD);
      for Elt of Data loop
         LCD.Port.Transmit (SPI_Data_8b'(1 => Elt),
                            Status);
         if Status /= Ok then
            --  No error handling...
            raise Program_Error;
         end if;
      end loop;
      End_Transaction (LCD);
   end Write_Data;

   ----------------
   -- Write_Data --
   ----------------

   procedure Write_Data (LCD  : ST7735R_Device;
                         Data : HAL.Short_Array)
   is
      B1, B2 : Byte;
      Status : SPI_Status;
   begin
      Start_Transaction (LCD);
      Set_Data_Mode (LCD);
      for Elt of Data loop
         B1 := Byte (Interfaces.Shift_Right (Elt, 8));
         B2 := Byte (Elt and 16#FF#);
         LCD.Port.Transmit (SPI_Data_8b'(B1, B2),
                            Status);
         if Status /= Ok then
            --  No error handling...
            raise Program_Error;
         end if;
      end loop;
      End_Transaction (LCD);
   end Write_Data;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (LCD : in out ST7735R_Device) is
   begin
      LCD.Initialized := True;

      LCD.RST.Clear;
      delay until Clock + Milliseconds (100);
      LCD.RST.Set;
      delay until Clock + Milliseconds (100);

      --  Sleep Exit
      Write_Command (LCD, 16#11#);

      delay until Clock + Milliseconds (100);
   end Initialize;
{'fraction_non_alphanumeric': 0.15480246799889494, 'fraction_numerical': 0.02578506308131504, 'mean_word_length': 1.327475353621946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3857508', 'n_tokens_mistral': 3436, 'n_tokens_neox': 3165, 'n_words': 830}