1 /*
2  *  parallelport_win32.h
3  *  PHD Guiding
4  *
5  *  Created by Bret McKee
6  *  Copyright (c) 2013 Bret McKee
7  *  All rights reserved.
8  *
9  *  This source code is distributed under the following "BSD" license
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions are met:
12  *    Redistributions of source code must retain the above copyright notice,
13  *     this list of conditions and the following disclaimer.
14  *    Redistributions in binary form must reproduce the above copyright notice,
15  *     this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *    Neither the name of Bret McKee, Dad Dog Development,
18  *     Craig Stark, Stark Labs nor the names of its
19  *     contributors may be used to endorse or promote products derived from
20  *     this software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include "phd.h"
37 
38 #ifdef __WINDOWS__
39 
40 /* ----Prototypes of Inp and Out32--- */
41 short _stdcall Inp32(short PortAddress);
42 void _stdcall Out32(short PortAddress, short data);
43 
ParallelPortWin32(void)44 ParallelPortWin32::ParallelPortWin32(void)
45 {
46     m_portAddr = 0;
47 }
48 
~ParallelPortWin32(void)49 ParallelPortWin32::~ParallelPortWin32(void)
50 {
51 }
52 
53 struct ParallelPortChooser : public wxDialog
54 {
55     ParallelPortChooser();
56 };
57 
ChooseParallelPort(const wxString & dflt)58 wxString ParallelPortWin32::ChooseParallelPort(const wxString& dflt)
59 {
60     wxArrayString ports;
61     ports.Add("LPT1 - 0x3BC");
62     ports.Add("LPT2 - 0x378");
63     ports.Add("LPT3 - 0x278");
64 
65     wxString customPort = pConfig->Global.GetString("/CustomParallelPort", wxEmptyString);
66     if (!customPort.IsEmpty())
67         ports.Add(customPort);
68 
69     wxDialog dlg(NULL, wxID_ANY, _("Select Parallel Port"));
70     wxSizer *sz1 = new wxBoxSizer(wxVERTICAL);
71 
72     wxListBox *portlb = new wxListBox(&dlg, wxID_ANY, wxDefaultPosition, wxDefaultSize, ports);
73     portlb->SetStringSelection(dflt);
74     sz1->Add(portlb);
75 
76     wxWindow *label = new wxStaticText(&dlg, wxID_ANY, _("Custom Port Address:"));
77     wxTextCtrl *customtxt = new wxTextCtrl(&dlg ,wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(70, -1));
78 
79     wxSizer *sz2 = new wxBoxSizer(wxHORIZONTAL);
80     sz2->Add(label, wxSizerFlags(0).Border(wxALL, 10));
81     sz2->Add(customtxt, wxSizerFlags(0).Border(wxALL, 10));
82 
83     sz1->Add(sz2);
84 
85     sz1->Add(dlg.CreateButtonSizer(wxOK | wxCANCEL),
86         wxSizerFlags(0).Right().Border(wxALL, 10));
87 
88     dlg.SetSizerAndFit(sz1);
89 
90     wxString choice;
91 
92     if (dlg.ShowModal() == wxID_OK)
93     {
94         wxString custom = customtxt->GetValue();
95         if (!custom.IsEmpty())
96         {
97             long val;
98             if (custom.ToLong(&val, 16) && val != 0)
99             {
100                 choice = wxString::Format("Custom - 0x%x", val);
101                 pConfig->Global.SetString("/CustomParallelPort", choice);
102             }
103         }
104         else
105         {
106             choice = portlb->GetStringSelection();
107         }
108     }
109 
110     return choice;
111 }
112 
Connect(const wxString & portName)113 bool ParallelPortWin32::Connect(const wxString& portName)
114 {
115     bool bError = false;
116 
117     try
118     {
119         wxString address = portName.AfterLast(' ');
120         long addr;
121 
122         if (!address.ToLong(&addr, 16))
123         {
124             throw ERROR_INFO("unable to convert [" + address + "] to a port number");
125         }
126 
127         m_portAddr = addr;
128 
129         Debug.AddLine(wxString::Format("parallel port %s assigned address 0x%x", portName, m_portAddr));
130     }
131     catch (const wxString& Msg)
132     {
133         POSSIBLY_UNUSED(Msg);
134         bError = true;
135     }
136 
137     return bError;
138 }
139 
Disconnect(void)140 bool ParallelPortWin32::Disconnect(void)
141 {
142     bool bError = false;
143 
144     try
145     {
146         m_portAddr = 0;
147     }
148     catch (const wxString& Msg)
149     {
150         POSSIBLY_UNUSED(Msg);
151         bError = true;
152     }
153 
154     return bError;
155 }
156 
ReadByte(BYTE * pData)157 bool ParallelPortWin32::ReadByte(BYTE *pData)
158 {
159     bool bError = false;
160 
161     try
162     {
163         if (m_portAddr == 0)
164         {
165             throw ERROR_INFO("mPortAddr == 0");
166         }
167         *pData = Inp32(m_portAddr);
168     }
169     catch (const wxString& Msg)
170     {
171         POSSIBLY_UNUSED(Msg);
172         bError = true;
173     }
174 
175     return bError;
176 }
177 
WriteByte(UINT8 data)178 bool ParallelPortWin32::WriteByte(UINT8 data)
179 {
180     bool bError = false;
181 
182     try
183     {
184         if (m_portAddr == 0)
185         {
186             throw ERROR_INFO("mPortAddr == 0");
187         }
188 
189         Out32(m_portAddr, data);
190     }
191     catch (const wxString& Msg)
192     {
193         POSSIBLY_UNUSED(Msg);
194         bError = true;
195     }
196 
197     return bError;
198 }
199 
200 #endif // __WINDOWS__
201