Modula-2 : Digital power supply

Many years ago I wanted to create my own custom made digitally controlled power supply. Send the required voltage via COM or LPT port to the digital power supply and that's it! This program is an attempt to create the used interface. Never finished... Here's the source:

MODULE Power01;

FROM    VgaLib      IMPORT  WinData, SetVGA, SetText, ChrWid, Center,
                            SetColour, MakeBox, EraseBox, FillBox,
                            PlotChar, WriteNumber, DrawH, PlotText,
                            ClickBar;
FROM    SYSTEM      IMPORT  ADDRESS, ASSEMBLER;
FROM    Mouse       IMPORT  ShowMouse, MouseRecord, GetMouseStatus,
                            InitMouse, SetMouseCursor, HideMouse,
                            ConfineMouse;
FROM    Keyboard    IMPORT  KeyPressed, GetKey;
FROM    InOut       IMPORT  WriteString, WriteCard;
FROM    barith      IMPORT  MulDiv;

VAR MainScreen, TopWin,
    Slider, PlotWin, TopLin,
    BotLin, MousWin         : WinData;
    Inimini                 : MouseRecord;
    StopCondition           : CARDINAL;
    Spanningen, Stromen     : ARRAY [0..2] OF CARDINAL;
    Clickers                : ARRAY [0..20] OF CARDINAL;


PROCEDURE ShowVal (InWin : WinData; Value, Decimals : CARDINAL);
(*
    Print Value and show Decimals digits only.  *)

VAR Num, Index, Digits  : CARDINAL;
    Store               : ARRAY [0..5] OF CHAR;
    Digit               : CHAR;

BEGIN
    Num := Value;
    FOR Index := 0 TO HIGH (Store) DO
        Store [Index] := ' ';
    END;
    Index := 0;
    WHILE Num > 0 DO
        Store [Index] := CHR ((Num MOD 10) + ORD ('0'));
        Num := Num DIV 10;
        INC (Index);
    END;
    Digits := Index;
    FOR Index := 3 - Decimals TO Digits DO
        IF Index # Decimals THEN
            PlotChar (InWin, Store [Index]);
        ELSE
            PlotChar (InWin, ",");
        END;
    END;
END ShowVal;


PROCEDURE W8;           (* Wait for keypress    *)

VAR k   : CHAR;

BEGIN
    REPEAT
        GetMouseStatus (Inimini);
    UNTIL (Inimini.Status = 3) OR KeyPressed ();
    IF KeyPressed () THEN
        GetKey (k);
    END;
END W8;


PROCEDURE InitWins;

BEGIN
(*  MainScreen is a dummy for the all around framework.     *)

    MainScreen.TopX   :=   0;   MainScreen.TopY   :=   0;
    MainScreen.Width  := 640;   MainScreen.Height := 480;
    MainScreen.CurX   :=   0;   MainScreen.CurY   :=   0;
    MainScreen.DeltaX :=   0;   MainScreen.DeltaY :=   0;
    MainScreen.Indent :=   5;
    MainScreen.TexCol := 0AX;   MainScreen.BoxCol := 0EX;
    MainScreen.BckCol :=  2X;   MainScreen.MnuCol :=  4X;

(*  TopLin describes the upper status bar for action keys.  *)

    TopLin.TopX     :=   5;     TopLin.TopY     :=   4;
    TopLin.Width    := 620;     TopLin.Height   :=  20;
    TopLin.CurX     :=   0;     TopLin.CurY     :=   0;
    TopLin.DeltaX   :=   0;     TopLin.DeltaY   :=   0;
    TopLin.Indent   :=   4;
    TopLin.TexCol   := 0CX;     TopLin.BoxCol   := 02X;
    TopLin.BckCol   :=  5X;     TopLin.MnuCol   := 0CX;

(*  TopWin describes the uppermost window for the parameters.   *)

    TopWin.TopX     :=  10;     TopWin.TopY     :=  24;
    TopWin.Width    := 620;     TopWin.Height   := 100;
    TopWin.CurX     :=   0;     TopWin.CurY     :=   0;
    TopWin.DeltaX   :=   0;     TopWin.DeltaY   :=   0;
    TopWin.Indent   :=   4;
    TopWin.TexCol   := 0EX;     TopWin.BoxCol   :=  3X;
    TopWin.BckCol   :=  5X;     TopWin.MnuCol   := 14X;

(*  PlotWin describes the chart recorder window.            *)

    PlotWin.TopX    :=  10;     PlotWin.TopY    := 205;
    PlotWin.Width   := 620;     PlotWin.Height  := 245;
    PlotWin.CurX    :=   0;     PlotWin.CurY    :=   0;
    PlotWin.DeltaX  :=   0;     PlotWin.DeltaY  :=   0;
    PlotWin.Indent  :=   0;
    PlotWin.TexCol  := 0DX;     PlotWin.BoxCol  :=  7X;
    PlotWin.BckCol  := 02X;     PlotWin.MnuCol  := 0FX;

(*  BotLin describes the Copyright notice.                  *)

    BotLin.TopX     :=   5;     BotLin.TopY     := 460;
    BotLin.Width    := 620;     BotLin.Height   :=  20;
    BotLin.CurX     :=   0;     BotLin.CurY     :=   0;
    BotLin.DeltaX   :=   0;     BotLin.DeltaY   :=   0;
    BotLin.Indent   :=   4;
    BotLin.TexCol   := 06X;     BotLin.BoxCol   := 02X;
    BotLin.BckCol   :=  5X;     BotLin.MnuCol   := 02X;

(*  MousWin describes the window for the mouse coordinates  *)

    MousWin.TopX    :=  10;     MousWin.TopY    := 135;
    MousWin.Width   :=  50;     MousWin.Height  :=  60;
    MousWin.CurX    :=   0;     MousWin.CurY    :=   0;
    MousWin.DeltaX  :=   0;     MousWin.DeltaY  :=   0;
    MousWin.Indent  :=   4;
    MousWin.TexCol  :=  7X;     MousWin.BoxCol  :=  7X;
    MousWin.BckCol  :=  1X;     MousWin.MnuCol  := 0EX;

(*  Slider describes the window for the Mouse Slidebar      *)

    Slider.TopX     :=  80;     Slider.TopY     := 135;
    Slider.Width    := 550;     Slider.Height   :=  60;
    Slider.CurX     :=   0;     Slider.CurY     :=   0;
    Slider.DeltaX   :=   0;     Slider.DeltaY   :=   0;
    Slider.Indent   :=   4;
    Slider.TexCol   := 0DX;     Slider.BoxCol   := 0FX;
    Slider.BckCol   :=  7X;     Slider.MnuCol   := 0EX;

END InitWins;


PROCEDURE SetUpScreen;

BEGIN
    InitMouse (Inimini);
    InitWins;
    SetVGA;
        MakeBox (MainScreen);
        MakeBox (TopWin);
        MakeBox (PlotWin);
        MakeBox (MousWin);
        MakeBox (Slider);
    TopLin.CurX := 10;
    TopLin.CurY :=  0;
(*    Center (TopLin, "JasPower Main 5_Volt 12_Volt MaxAmp Quit");  *)
    ClickBar (TopLin, "JasPower Main 5_Volt 12_Volt MaxAmp Quit", Clickers);
    BotLin.CurX := 20;
    BotLin.CurY :=  0;
    Center (BotLin, "Copyrights Jan_Verhoeven 5012_GH_272 Tilburg aklasse@tip.nl");
END SetUpScreen;


PROCEDURE SetUpArrays;

VAR m   : CARDINAL;

BEGIN
    FOR m := 0 TO 2 DO
        Spanningen [m] := m * 500;
        Stromen [m] := 200;
    END;
END SetUpArrays;


PROCEDURE RefreshTopWin;

VAR m, Power, Resist        : CARDINAL;

BEGIN
    SetColour (TopWin.TexCol);
    TopWin.CurY :=   5;
    TopWin.CurX := 175;     PlotText (TopWin, "Spanning");
    TopWin.CurX := 275;     PlotText (TopWin, "Stroom");
    TopWin.CurX := 375;     PlotText (TopWin, "Vermogen");
    TopWin.CurX := 475;     PlotText (TopWin, "Weerstand");
    TopWin.CurX := 10;
    TopWin.CurY := 25;      PlotText (TopWin, "Hoofdspanning  : ");
    TopWin.CurX := 10;
    TopWin.CurY := 50;      PlotText (TopWin, "Regelbaar laag : ");
    TopWin.CurX := 10;
    TopWin.CurY := 75;      PlotText (TopWin, "Regelbaar hoog : ");

    TopWin.CurY :=  0;
    FOR m := 0 TO 2 DO
        TopWin.CurX := 185;
        INC (TopWin.CurY, 25);
        WriteNumber (TopWin, Spanningen [m], 5);
    END;

    TopWin.CurY :=   0;
    FOR m := 0 TO 2 DO
        TopWin.CurX := 285;
        INC (TopWin.CurY, 25);
        WriteNumber (TopWin, Stromen [m], 4);
    END;

    TopWin.CurY :=   0;
    FOR m := 0 TO 2 DO
        TopWin.CurX := 385;
        INC (TopWin.CurY, 25);
        Power := MulDiv (Stromen [m], Spanningen [m], 1000);
        WriteNumber (TopWin, Power, 5);
    END;

    TopWin.CurY :=   0;
    FOR m := 0 TO 2 DO
        TopWin.CurX := 485;
        INC (TopWin.CurY, 25);
        IF Stromen [m] > 0 THEN
            Resist := Spanningen [m] DIV Stromen [m];
            WriteNumber (TopWin, Resist, 4);
        ELSE
            PlotText (TopWin, 'Oneindig');
        END;
    END;
END RefreshTopWin;


PROCEDURE SetUpMouse;

BEGIN
    ShowMouse;
    IF Inimini.Buttons = 2 THEN
        StopCondition := 3;
    ELSE
        StopCondition := 7;
    END;
    SetMouseCursor (30, 30);
END SetUpMouse;


PROCEDURE ShowMouseXY;
(*
    Show current mouse data on screen.      *)

BEGIN
    SetColour (MousWin.TexCol);
    MousWin.CurX := MousWin.Indent;
    MousWin.CurY := 4;
    WriteNumber (MousWin, Inimini.MouseX, 4);
    MousWin.CurX := MousWin.Indent;
    MousWin.CurY := 23;
    WriteNumber (MousWin, Inimini.MouseY, 4);
    MousWin.CurX := MousWin.Indent;
    MousWin.CurY := 42;
    WriteNumber (MousWin, Inimini.Status, 4);
END ShowMouseXY;


PROCEDURE SlideBar (Min, Current, Max  : CARDINAL) : CARDINAL;
(*
    Draw a sliding bar inside the SLIDERbox.    *)

VAR Value, Position         : CARDINAL;

BEGIN
    HideMouse;
    FillBox (Slider);
    SetColour (Slider.TexCol);
    Slider.CurX := 10;
    Slider.CurY :=  5;
    WriteNumber (Slider, Min, 5);
    Slider.CurX := 240;
    WriteNumber (Slider, Current, 5);
    Slider.CurX := 470;
    WriteNumber (Slider, Max, 5);
    Slider.CurX := 10;
    Slider.CurY := 30;
    Slider.DeltaX := 500;
    DrawH (Slider, TRUE);
    ConfineMouse (90, 590, 166, 166);
    Position := MulDiv (500, Current - Min, Max - Min) + 90;
    SetMouseCursor (Position, 166);
    ShowMouse;
    REPEAT
        GetMouseStatus (Inimini);
        ShowMouseXY;
        Value := MulDiv (Inimini.MouseX - 90, Max - Min, 500) + Min;
        Slider.CurX := 240;
        Slider.CurY :=   5;
        SetColour (Slider.TexCol);
        WriteNumber (Slider, Value, 5);
    UNTIL Inimini.Status = 1;
    ConfineMouse (0, 639, 0, 479);
    HideMouse;      EraseBox (Slider);      ShowMouse;
    RETURN Value;
END SlideBar;


BEGIN
    SetUpArrays;
    SetUpScreen;
    SetUpMouse;
    RefreshTopWin;
    Spanningen [0] := SlideBar (0, 0, 20000);           W8;
    RefreshTopWin;
    Spanningen [1] := SlideBar (3000, 5000, 5500);      W8;
    RefreshTopWin;
    Spanningen [2] := SlideBar (10000, 12000, 12500);

    REPEAT
        GetMouseStatus (Inimini);
        ShowMouseXY;
        RefreshTopWin;
    UNTIL (Inimini.Status = StopCondition) OR (KeyPressed ());

    SetText;
END Power01.
   

Page created September 15, 2012 and