BBS_BBB_Ada_17b6af3a/src-linux/bbs-embed-pwm.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
--
--  Author: Brent Seidel
--  Date: 9-Aug-2024
--
--  This file is part of bbs_embed.
--  Bbs_embed is free software: you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation, either version 3 of the License, or (at your
--  option) any later version.
--
--  bbs_embed is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
--  Public License for more details.
--
--  You should have received a copy of the GNU General Public License along
--  with bbs_embed. If not, see <https://www.gnu.org/licenses/>.--
--
package body BBS.embed.PWM is
	--
   function pwm_new return PWM is
   begin
      return new PWM_record;
   end;
   --
   procedure configure(self : not null access PWM_record'class;
                       pin : string; index : pwm_range) is
      temp : Ada.Text_IO.File_Type;
   begin
      --
      -- Set pin function
      --
      Ada.Text_IO.Open(temp, Ada.Text_IO.Out_File, pin);
      Ada.Text_IO.Put_Line(temp, "pwm");
      Ada.Text_IO.Close(temp);
      --
      -- Setup the record
      --
      self.PWM_index := index;
      self.period := 0;
      self.high_time := 0;
   end;
   --
   procedure enable(self : not null access PWM_record'class; state : boolean) is
      temp : Ada.Text_IO.File_Type;
   begin
      Ada.Text_IO.Open(temp, Ada.Text_IO.Out_File, pwm_ctrl(self.pwm_index).all & "/enable");
      if state then
         Ada.Text_IO.Put_Line(temp, "1");
      else
         Ada.Text_IO.Put_Line(temp, "0");
      end if;
      Ada.Text_IO.Close(temp);
   end;
   --
   procedure set_period(self : not null access PWM_record'class; period : nanoseconds) is
      temp : Ada.Text_IO.File_Type;
      value : constant string := Ada.Strings.Fixed.Trim(integer'Image(integer(period)),
                                                          Ada.Strings.Left);
   begin
      Ada.Text_IO.Open(temp, Ada.Text_IO.Out_File, pwm_ctrl(self.pwm_index).all & "/period");
      Ada.Text_IO.Put_Line(temp, value);
      self.period := period;
      Ada.Text_IO.Close(temp);
   end;
   --
   procedure set_period(self : not null access PWM_record'class; period : Duration) is
      temp : Ada.Text_IO.File_Type;
      value : constant string := Ada.Strings.Fixed.Trim(integer'Image(integer(period*1_000_000_000.0)),
                                                          Ada.Strings.Left);
   begin
      Ada.Text_IO.Open(temp, Ada.Text_IO.Out_File, pwm_ctrl(self.pwm_index).all & "/period");
      Ada.Text_IO.Put_Line(temp, value);
      self.period := nanoseconds(period*1_000_000_000.0);
      Ada.Text_IO.Close(temp);
   end;
   --
   procedure set_high(self : not null access PWM_record'class; high : nanoseconds) is
      temp : Ada.Text_IO.File_Type;
      value : constant string := Ada.Strings.Fixed.Trim(integer'Image(integer(high)),
                                                          Ada.Strings.Left);
   begin
      Ada.Text_IO.Open(temp, Ada.Text_IO.Out_File, pwm_ctrl(self.pwm_index).all & "/duty_cycle");
      Ada.Text_IO.Put_Line(temp, value);
      self.high_time := high;
      Ada.Text_IO.Close(temp);
   end;
   --
   procedure set_high(self : not null access PWM_record'class; high : Duration) is
      temp : Ada.Text_IO.File_Type;
      value : constant string := Ada.Strings.Fixed.Trim(integer'Image(integer(high*1_000_000_000.0)),
                                                          Ada.Strings.Left);
   begin
      Ada.Text_IO.Open(temp, Ada.Text_IO.Out_File, pwm_ctrl(self.pwm_index).all & "/duty_cycle");
      Ada.Text_IO.Put_Line(temp, value);
      self.high_time := nanoseconds(high*1_000_000_000.0);
      Ada.Text_IO.Close(temp);
   end;
   --
   procedure set_rate(self : not null access PWM_record'class; rate : BBS.units.freq_hz) is
      temp : Ada.Text_IO.File_Type;
      period : constant nanoseconds := nanoseconds(1_000_000_000.0/float(rate));
      value : constant string := Ada.Strings.Fixed.Trim(integer'Image(integer(period)),
                                                          Ada.Strings.Left);
   begin
      Ada.Text_IO.Open(temp, Ada.Text_IO.Out_File, pwm_ctrl(self.pwm_index).all & "/period");
      Ada.Text_IO.Put_Line(temp, value);
      self.period := period;
      Ada.Text_IO.Close(temp);
   end;
   --
   procedure set_duty(self : not null access PWM_record'class; duty : float) is
      temp : Ada.Text_IO.File_Type;
      high : constant nanoseconds := nanoseconds(float(self.period)*duty/100.0);
      value : constant string := Ada.Strings.Fixed.Trim(integer'Image(integer(high)),
                                                          Ada.Strings.Left);
   begin
      Ada.Text_IO.Open(temp, Ada.Text_IO.Out_File, pwm_ctrl(self.pwm_index).all & "/duty_cycle");
      Ada.Text_IO.Put_Line(temp, value);
      self.high_time := high;
      Ada.Text_IO.Close(temp);
   end;
   --
end;