' Raspberry Pi LPC1114 I/O Processor Expansion Board SPI Agent Firmware ' Pulse Width Modulation output test program ' Copyright (C)2015-2018, Philip Munts, President, Munts AM Corp. ' ' Redistribution and use in source and binary forms, with or without ' modification, are permitted provided that the following conditions are met: ' ' * Redistributions of source code must retain the above copyright notice, ' this list of conditions and the following disclaimer. ' ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ' IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ' ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE ' LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR ' CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF ' SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ' INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN ' CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ' ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ' POSSIBILITY OF SUCH DAMAGE. ' Layout the GUI IsTablet = LDFile.Exists("C:\ProgramData\MuntsTechnologies\tablet.txt") GraphicsWindow.Title = "Raspberry Pi LPC1114 PWM Output Test" GraphicsWindow.BrushColor = "black" If IsTablet Then ' Layout the GUI for Windows tablet LDUtilities.GWState = 2 W = GraphicsWindow.Width H = GraphicsWindow.Height GraphicsWindow.FontSize = 32 GraphicsWindow.DrawText(W/2 - 300, 50, "Raspberry Pi LPC1114 I/O Processor") GraphicsWindow.DrawText(W/2 - 300, 100, "Expansion Board SPI Agent Firmware") GraphicsWindow.DrawText(W/2 - 300, 150, "Pulse Width Modulation Test") GraphicsWindow.FontSize = 30 GraphicsWindow.DrawText(W/2 - 270, 250, "Server name:") TextBoxServerName = Controls.AddTextBox(W/2 - 60, 250) ButtonConnect = Controls.AddButton("Connect", W/2 + 120, 250) GraphicsWindow.DrawText(W/2 - 270, 320, "Frequency:") TextBoxFrequency = Controls.AddTextBox(W/2 - 60, 320) Controls.SetTextBoxText(TextBoxFrequency, "100") ButtonChange = Controls.AddButton("Change", W/2 + 120, 320) Label1 = Shapes.AddText("PWM1") Label2 = Shapes.AddText("PWM2") Label3 = Shapes.AddText("PWM3") Label4 = Shapes.AddText("PWM4") Controls.Move(Label1, W/2 - 360, 400) Controls.Move(Label2, W/2 - 160, 400) Controls.Move(Label3, W/2 + 40, 400) Controls.Move(Label4, W/2 + 240, 400) Slider1 = LDControls.AddSlider(25, 100, "V") Slider2 = LDControls.AddSlider(25, 100, "V") Slider3 = LDControls.AddSlider(25, 100, "V") Slider4 = LDControls.AddSlider(25, 100, "V") Shapes.Zoom(Slider1, 2.0, 2.0) Shapes.Zoom(Slider2, 2.0, 2.0) Shapes.Zoom(Slider3, 2.0, 2.0) Shapes.Zoom(Slider4, 2.0, 2.0) Controls.Move(Slider1, W/2 - 320, 500) Controls.Move(Slider2, W/2 - 120, 500) Controls.Move(Slider3, W/2 + 80, 500) Controls.Move(Slider4, W/2 + 270, 500) Else ' Layout the GUI for Windows Desktop GraphicsWindow.Width = 400 GraphicsWindow.Height = 300 GraphicsWindow.FontSize = 18 GraphicsWindow.DrawText(30, 10, "Raspberry Pi LPC1114 I/O Processor") GraphicsWindow.DrawText(30, 30, "Expansion Board SPI Agent Firmware") GraphicsWindow.DrawText(30, 50, "Pulse Width Modulation Test") GraphicsWindow.FontSize = 14 GraphicsWindow.DrawText(30, 84, "Server name:") TextBoxServerName = Controls.AddTextBox(130, 80) ButtonConnect = Controls.AddButton("Connect", 300, 78) GraphicsWindow.DrawText(30, 124, "Frequency:") TextBoxFrequency = Controls.AddTextBox(130, 120) Controls.SetTextBoxText(TextBoxFrequency, "100") ButtonChange = Controls.AddButton("Change", 300, 118) Label1 = Shapes.AddText("PWM1") Label2 = Shapes.AddText("PWM2") Label3 = Shapes.AddText("PWM3") Label4 = Shapes.AddText("PWM4") Controls.Move(Label1, 70, 160) Controls.Move(Label2, 140, 160) Controls.Move(Label3, 210, 160) Controls.Move(Label4, 270, 160) Slider1 = LDControls.AddSlider(25, 100, "V") Slider2 = LDControls.AddSlider(25, 100, "V") Slider3 = LDControls.AddSlider(25, 100, "V") Slider4 = LDControls.AddSlider(25, 100, "V") Controls.Move(Slider1, 82, 180) Controls.Move(Slider2, 152, 180) Controls.Move(Slider3, 222, 180) Controls.Move(Slider4, 292, 180) GraphicsWindow.Show() Endif HideSliders() ' Preload server name from command line argument If Program.ArgumentCount = 1 Then Controls.SetTextBoxText(TextBoxServerName, Program.GetArgument(1)) Endif ' Register the event handler subroutines Controls.ButtonClicked = ButtonHandler LDControls.SliderChanged = SliderHandler ' This subroutine hides the frequency change controls Sub HideFrequency Controls.HideControl(ButtonChange) LDControls.TextBoxReadOnly(TextBoxFrequency, "True") EndSub ' This subroutine reveals the frequency change controls Sub RevealFrequency Controls.ShowControl(ButtonChange) LDControls.TextBoxReadOnly(TextBoxFrequency, "False") EndSub ' This subroutine hides the slider controls Sub HideSliders Controls.HideControl(Label1) Controls.HideControl(Label2) Controls.HideControl(Label3) Controls.HideControl(Label4) Controls.HideControl(Slider1) Controls.HideControl(Slider2) Controls.HideControl(Slider3) Controls.HideControl(Slider4) EndSub ' This subroutine reveals the slider controls Sub RevealSliders Controls.ShowControl(Label1) Controls.ShowControl(Label2) Controls.ShowControl(Label3) Controls.ShowControl(Label4) Controls.ShowControl(Slider1) Controls.ShowControl(Slider2) Controls.ShowControl(Slider3) Controls.ShowControl(Slider4) EndSub ' This subroutine resets the slider controls to all off Sub ResetSliders LDControls.SliderValue(Slider1, 0) LDControls.SliderValue(Slider2, 0) LDControls.SliderValue(Slider3, 0) LDControls.SliderValue(Slider4, 0) SPIAgent.PWM1 = 0 SPIAgent.PWM2 = 0 SPIAgent.PWM3 = 0 SPIAgent.PWM3 = 0 EndSub ' This subroutine changes the PWM output pulse rate Sub ChangeFrequency SPIAgent.PWM1_Frequency = Controls.GetTextBoxText(TextBoxFrequency) If SPIAgent.error <> 0 Then GraphicsWindow.ShowMessage("PWM1 Frequency is invalid", "ERROR:") EndIf SPIAgent.PWM2_Frequency = Controls.GetTextBoxText(TextBoxFrequency) If SPIAgent.error <> 0 Then GraphicsWindow.ShowMessage("PWM2 Frequency is invalid", "ERROR:") EndIf SPIAgent.PWM3_Frequency = Controls.GetTextBoxText(TextBoxFrequency) If SPIAgent.error <> 0 Then GraphicsWindow.ShowMessage("PWM3 Frequency is invalid", "ERROR:") EndIf SPIAgent.PWM4_Frequency = Controls.GetTextBoxText(TextBoxFrequency) If SPIAgent.error <> 0 Then GraphicsWindow.ShowMessage("PWM4 Frequency is invalid", "ERROR:") EndIf EndSub ' This subroutine handles button presses Sub ButtonHandler c = Controls.GetButtonCaption(Controls.LastClickedButton) If c = "Connect" Then SPIAgent.Open(Controls.GetTextBoxText(TextBoxServerName)) If SPIAgent.error <> 0 Then GraphicsWindow.ShowMessage("SPIAgent.Open() returned error " + SPIAgent.error, "ERROR:") Goto ButtonHandlerDone Endif ChangeFrequency() HideFrequency() Controls.SetButtonCaption(ButtonConnect, "Disconnect") ResetSliders() RevealSliders() ElseIf c = "Disconnect" Then SPIAgent.Close() ResetSliders() HideSliders() RevealFrequency() Controls.SetButtonCaption(ButtonConnect, "Connect") Elseif c = "Change" Then ChangeFrequency() EndIf ButtonHandlerDone: EndSub ' This subroutine handles slider changes Sub SliderHandler s = LDControls.LastSlider If s = Slider1 Then SPIAgent.PWM1 = LDControls.SliderGetValue(Slider1) ElseIf s = Slider2 Then SPIAgent.PWM2 = LDControls.SliderGetValue(Slider2) ElseIF s = Slider3 Then SPIAgent.PWM3 = LDControls.SliderGetValue(Slider3) ElseIF s = Slider4 Then SPIAgent.PWM4 = LDControls.SliderGetValue(Slider4) EndIF EndSub