blinkenlights_0.1.0_339e553e/lamp-test/lamp_test.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
with Ada.Text_IO;
with BBS;
use type BBS.uint16;
use type BBS.uint32;
with BBS.embed.i2c;
with i2c;

--
--  This is a routine to test the simulator LEDs and switches.
--
procedure lamp_test is
   ad_data  : BBS.uint32;
   ctl_data : BBS.uint16;
   res      : i2c.result;
   err      : BBS.embed.i2c.err_code;
   --
   --  Set all LEDS, mirror the switches, then turn LEDs off
   --
   procedure mirror is
   begin
     --
     --  Set all the LEDs on and wait for 5 seconds.
     --
     i2c.set_addr_data(16#FFFF_FFFF#, res);
     Ada.Text_IO.Put_Line("Set Addr/Data LEDs result is " & i2c.result'Image(res));
     i2c.set_ctrl(16#FFFF#, res);
     Ada.Text_IO.Put_Line("Set Mode/Ctl LEDs result is " & i2c.result'Image(res));
     delay 5.0;
     --
     --  Read the switch values
     --
     i2c.read_addr_data(ad_data, res);
     Ada.Text_IO.Put_Line("Read Addr/Data switches result is " & i2c.result'Image(res));
     Ada.Text_IO.Put_Line("  Addr/Data switch value is " & ad_data'Image);
     i2c.read_ctrl(ctl_data, res);
     Ada.Text_IO.Put_Line("Read control switches result is " & i2c.result'Image(res));
     Ada.Text_IO.Put_Line("  Control switch value is " & ctl_data'Image);
     --
     --  Set the LEDs to the switch values and wait for 5 seconds
     --
     i2c.set_addr_data(ad_data, res);
     Ada.Text_IO.Put_Line("Set Addr/Data LEDs result is " & i2c.result'Image(res));
     i2c.set_ctrl(ctl_data, res);
     Ada.Text_IO.Put_Line("Set Mode/Ctl LEDs result is " & i2c.result'Image(res));
     delay 5.0;
     --
     --  Turn the LEDs off.
     --
     i2c.set_addr_data(0, res);
     Ada.Text_IO.Put_Line("Set Addr/Data LEDs result is " & i2c.result'Image(res));
     i2c.set_ctrl(0, res);
     Ada.Text_IO.Put_Line("Set Mode/Ctl LEDs result is " & i2c.result'Image(res));
   end;
   --
   --  Sweep LEDs
   --
   procedure sweep is
     addr_lsw : BBS.uint32 := 1;
     addr_msw : BBS.uint32 := 1;
     mode_ctl : BBS.uint16 := 1;
   begin
     loop
       i2c.set_addr_data(addr_msw * 16#0001_0000# + addr_lsw, res);
       i2c.set_ctrl(mode_ctl, res);
       delay 0.1;
       if addr_lsw >= 16#8000# then
         addr_lsw := 1;
         addr_msw := 1;
         mode_ctl := 1;
       else
         addr_lsw := addr_lsw * 2;
         addr_msw := addr_msw * 2;
         mode_ctl := mode_ctl * 2;
       end if;
     end loop;
   end;
begin
   i2c.init_i2c;
   --
   -- Configure LED drivers
   --
   if i2c.MCP23017_found(i2c.LED_LSW) then
      i2c.MCP23017_info(i2c.LED_LSW).set_dir(16#0000#, err);
      Ada.Text_IO.Put_Line("LED least significant word configured");
   end if;
   if i2c.MCP23017_found(i2c.LED_MSW) then
      i2c.MCP23017_info(i2c.LED_MSW).set_dir(16#0000#, err);
      Ada.Text_IO.Put_Line("LED most significant word configured");
   end if;
   if i2c.MCP23017_found(i2c.LED_CTRL) then
      i2c.MCP23017_info(i2c.LED_CTRL).set_dir(16#0000#, err);
      Ada.Text_IO.Put_Line("LED mode and control configured");
   end if;
   --
   --  Select pattern for testing
   --
   if True then
     mirror;
   else
     sweep;
   end if;
end lamp_test;