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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
//-----------------------------------------------------------------------------
// Copyright 2007 Jonathan Westhues
//
// This file is part of LDmicro.
//
// LDmicro 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.
//
// LDmicro 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 LDmicro. If not, see <http://www.gnu.org/licenses/>.
//------
//
// Miscellaneous utility functions that don't fit anywhere else. IHEX writing,
// verified memory allocator, other junk.
//-----------------------------------------------------------------------------
#include "linuxUI.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "ldmicro.h"
// We should display messages to the user differently if we are running
// interactively vs. in batch (command-line) mode.
BOOL RunningInBatchMode = FALSE;
// Allocate memory on a local heap
HANDLE MainHeap;
// Running checksum as we build up IHEX records.
static int IhexChecksum;
// Try to common a bit of stuff between the dialog boxes, since only one
// can be open at any time.
HWID OkButton;
HWID CancelButton;
BOOL DialogDone;
BOOL DialogCancel;
HFONT MyNiceFont;
HFONT MyFixedFont;
//-----------------------------------------------------------------------------
// printf-like debug function, to the Windows debug log.
//-----------------------------------------------------------------------------
void dbp(char *str, ...)
{
va_list f;
char buf[1024];
va_start(f, str);
vsprintf(buf, str, f);
OutputDebugString(buf);
OutputDebugString("\n");
}
//-----------------------------------------------------------------------------
// Wrapper for AttachConsole that does nothing running under <WinXP, so that
// we still run (except for the console stuff) in earlier versions.
//-----------------------------------------------------------------------------
#define ATTACH_PARENT_PROCESS ((DWORD)-1) // defined in WinCon.h, but only if
// _WIN32_WINNT >= 0x500
// BOOL AttachConsoleDynamic(DWORD base)
// {
// typedef BOOL WINAPI fptr_acd(DWORD base);
// fptr_acd *fp;
// HMODULE hm = LoadLibrary("kernel32.dll");
// if(!hm) return FALSE;
// fp = (fptr_acd *)GetProcAddress(hm, "AttachConsole");
// if(!fp) return FALSE;
// return fp(base);
// }
//-----------------------------------------------------------------------------
// For error messages to the user; printf-like, to a message box.
//-----------------------------------------------------------------------------
void Error(char *str, ...)
{
va_list f;
char buf[1024];
va_start(f, str);
vsprintf(buf, str, f);
va_end(f);
if(RunningInBatchMode) {
printf("print batch mode");
/* Only required for windows
* AttachConsoleDynamic(ATTACH_PARENT_PROCESS);
*/
// Indicate that it's an error, plus the output filename
printf("compile error ('%s'): \n", CurrentCompileFile);
// The error message itself and an extra newline to be safe.
printf("%s\n", buf);
} else {
//HWND h = GetForegroundWindow();
MessageBox(MainWindow, buf, _("LDmicro Error"), MB_OK, MB_ICONERROR);
}
}
//-----------------------------------------------------------------------------
// A standard format for showing a message that indicates that a compile
// was successful.
//-----------------------------------------------------------------------------
void CompileSuccessfulMessage(char *str)
{
if(RunningInBatchMode) {
char str[MAX_PATH+100];
printf("compiled okay, wrote '%s'\n", CurrentCompileFile);
//AttachConsoleDynamic(ATTACH_PARENT_PROCESS);
//HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
//DWORD written;
//riteFile(h, str, strlen(str), &written, NULL);
} else {
// MessageBox(MainWindow, str, _("Compile Successful"),
// MB_OK | MB_ICONINFORMATION);
}
}
//-----------------------------------------------------------------------------
// Check the consistency of the heap on which all the PLC program stuff is
// stored.
//-----------------------------------------------------------------------------
void CheckHeap(char *file, int line)
{
static unsigned int SkippedCalls;
static SDWORD LastCallTime;
SDWORD now = GetTickCount();
// It slows us down too much to do the check every time we are called;
// but let's still do the check periodically; let's do it every 70
// calls or every 20 ms, whichever is sooner.
if(SkippedCalls < 70 && (now - LastCallTime) < 20) {
SkippedCalls++;
return;
}
SkippedCalls = 0;
LastCallTime = now;
/*if(!HeapValidate(MainHeap, 0, NULL)) {
dbp("file %s line %d", file, line);
Error("Noticed memory corruption at file '%s' line %d.", file, line);
oops();
}*/
}
//-----------------------------------------------------------------------------
// Like malloc/free, but memsets memory allocated to all zeros. Also TODO some
// checking and something sensible if it fails.
//-----------------------------------------------------------------------------
void *CheckMalloc(size_t n)
{
ok();
void *p = HeapAlloc(MainHeap, HEAP_ZERO_MEMORY, n);
return p;
}
void CheckFree(void *p)
{
ok();
HeapFree(MainHeap, 0, p);
}
//-----------------------------------------------------------------------------
// Clear the checksum and write the : that starts an IHEX record.
//-----------------------------------------------------------------------------
void StartIhex(FILE *f)
{
fprintf(f, ":");
IhexChecksum = 0;
}
//-----------------------------------------------------------------------------
// Write an octet in hex format to the given stream, and update the checksum
// for the IHEX file.
//-----------------------------------------------------------------------------
void WriteIhex(FILE *f, BYTE b)
{
fprintf(f, "%02X", b);
IhexChecksum += b;
}
//-----------------------------------------------------------------------------
// Write the finished checksum to the IHEX file from the running sum
// calculated by WriteIhex.
//-----------------------------------------------------------------------------
void FinishIhex(FILE *f)
{
IhexChecksum = ~IhexChecksum + 1;
IhexChecksum = IhexChecksum & 0xff;
fprintf(f, "%02X\n", IhexChecksum);
}
//-----------------------------------------------------------------------------
// Create a window with a given client area.
//-----------------------------------------------------------------------------
QDialog* CreateWindowClient(char *windowName,
int x, int y, int width, int height, HWND parent)
{
QDialog* h = new QDialog(parent);
h->resize(width, height);
h->setWindowTitle(windowName);
return h;
}
//-----------------------------------------------------------------------------
// Set the font of a control to a pretty proportional font (typ. Tahoma).
//-----------------------------------------------------------------------------
void NiceFont(HWID h)
{
QFont qtfont = h->font();
qtfont.setFamily(MyNiceFont->lpszFace);
qtfont.setPixelSize(MyNiceFont->nHeight - 3);
qtfont.setFixedPitch(TRUE);
qtfont.setStyle(MyNiceFont->fdwItalic ? QFont::StyleItalic : QFont::StyleNormal);
qtfont.setWeight(MyNiceFont->fnWeight == FW_BOLD ? QFont::Bold : QFont::Normal);
h->setFont(qtfont);
// SendMessage(h, WM_SETFONT, (WPARAM)MyNiceFont, TRUE);
}
//-----------------------------------------------------------------------------
// Set the font of a control to a pretty fixed-width font (typ. Lucida
// Console).
//-----------------------------------------------------------------------------
void FixedFont(HWID h)
{
QFont qtfont = h->font();
qtfont.setFamily(MyFixedFont->lpszFace);
qtfont.setPixelSize(MyFixedFont->nHeight - 3);
qtfont.setFixedPitch(TRUE);
qtfont.setStyle(MyFixedFont->fdwItalic ? QFont::StyleItalic : QFont::StyleNormal);
qtfont.setWeight(MyFixedFont->fnWeight == FW_BOLD ? QFont::Bold : QFont::Normal);
h->setFont(qtfont);
// SendMessage(h, WM_SETFONT, (WPARAM)MyFixedFont, TRUE);
}
void SetFont(HWID h, HFONT f)
{
QFont qtfont = h->font();
qtfont.setFamily(f->lpszFace);
qtfont.setPixelSize(f->nHeight - 3);
qtfont.setFixedPitch(TRUE);
qtfont.setStyle(f->fdwItalic ? QFont::StyleItalic : QFont::StyleNormal);
qtfont.setWeight(f->fnWeight == FW_BOLD ? QFont::Bold : QFont::Normal);
h->setFont(qtfont);
}
//-----------------------------------------------------------------------------
// Create our dialog box class, used for most of the popup dialogs.
//-----------------------------------------------------------------------------
void MakeDialogBoxClass(void)
{
MyNiceFont = CreateFont(16, 0, 0, FW_REGULAR, FALSE, "Tahoma");
MyFixedFont = CreateFont(14, 0, 0, FW_REGULAR, FALSE, "Lucida Console");
}
//-----------------------------------------------------------------------------
// Map an I/O type to a string describing it. Used both in the on-screen
// list and when we write a text file to describe it.
//-----------------------------------------------------------------------------
char *IoTypeToString(int ioType)
{
switch(ioType) {
case IO_TYPE_DIG_INPUT: return _("digital in");
case IO_TYPE_DIG_OUTPUT: return _("digital out");
case IO_TYPE_INTERNAL_RELAY: return _("int. relay");
case IO_TYPE_UART_TX: return _("UART tx");
case IO_TYPE_UART_RX: return _("UART rx");
case IO_TYPE_PWM_OUTPUT: return _("PWM out");
case IO_TYPE_TON: return _("turn-on delay");
case IO_TYPE_TOF: return _("turn-off delay");
case IO_TYPE_RTO: return _("retentive timer");
case IO_TYPE_COUNTER: return _("counter");
case IO_TYPE_GENERAL: return _("general var");
case IO_TYPE_READ_ADC: return _("adc input");
default: return _("<corrupt!>");
}
}
//-----------------------------------------------------------------------------
// Get a pin number for a given I/O; for digital ins and outs and analog ins,
// this is easy, but for PWM and UART this is forced (by what peripherals
// are available) so we look at the characteristics of the MCU that is in
// use.
//-----------------------------------------------------------------------------
void PinNumberForIo(char *dest, PlcProgramSingleIo *io)
{
if(!dest) return;
if(!io) {
strcpy(dest, "");
return;
}
int type = io->type;
if(type == IO_TYPE_DIG_INPUT || type == IO_TYPE_DIG_OUTPUT
|| type == IO_TYPE_READ_ADC)
{
int pin = io->pin;
if(pin == NO_PIN_ASSIGNED) {
strcpy(dest, _("(not assigned)"));
} else {
sprintf(dest, "%d", pin);
}
} else if(type == IO_TYPE_UART_TX && Prog.mcu) {
if(Prog.mcu->uartNeeds.txPin == 0) {
strcpy(dest, _("<no UART!>"));
} else {
sprintf(dest, "%d", Prog.mcu->uartNeeds.txPin);
}
} else if(type == IO_TYPE_UART_RX && Prog.mcu) {
if(Prog.mcu->uartNeeds.rxPin == 0) {
strcpy(dest, _("<no UART!>"));
} else {
sprintf(dest, "%d", Prog.mcu->uartNeeds.rxPin);
}
} else if(type == IO_TYPE_PWM_OUTPUT && Prog.mcu) {
if(Prog.mcu->pwmNeedsPin == 0) {
strcpy(dest, _("<no PWM!>"));
} else {
sprintf(dest, "%d", Prog.mcu->pwmNeedsPin);
}
} else {
strcpy(dest, "");
}
}
|