-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
660 lines (568 loc) · 15.6 KB
/
main.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[] = _T("CodeBlocksWindowsApp");
#define PANELSIZE 12
#define MAX_BUTTONS 8
// 1 2 3 4 5 6 7 8 9 10 11 12
char * Note[12]={"FA#","SI","MI","LA","RE","SOL","DO","FA","SIb","MIb","SOL#","DO#"};
int ClefFA[12]={2,7,12,5,10,3,8,1,6,11,4,9};
static int ChordList[12][5] =
{
{1,9,12,2,3},
{2,10,1,5,4},
{3,11,2,6,5},
{4,12,3,7,6},
{5,1,4,8,7},
{6,2,5,9,8},
{7,3,6,10,9},
{8,4,7,11,10},
{9,5,8,12,11},
{10,6,9,1,12},
{11,7,10,2,1},
{12,8,11,3,2}
};
static int ShaftList[12][4] =
{
{12,17,19,0},
{3,5,10,0},
{8,13,15,0},
{1,8,20,0},
{4,9,11,16},
{14,19,0,0},
{5,7,12,0},
{10,15,17,0},
{6,18,23,0},
{6,11,13,18},
{4,16,21,0},
{7,9,14,0}
};
static int MajorKeys[MAX_BUTTONS][3] =
{
{-5,9,-12},
{-11,3,-6},
{-6,10,-1},
{-1,5,-8},
{-8,12,-3},
{-3,7,-10},
{-10,2,-5},
{-12,4,-7}
};
int X1, Y1;
int wSpace, hSpace;
class TShaft
{
DWORD conf;
int x,y,status;
int position;
int pinout;
char name[5];
public:
TShaft(int n1, int n2, int n3 = 0, int n4 = 0);
void Draw(HDC dc, int st);
void ReversePos();
int SetPos(int);
int Where();
int Select(int *,int *);
void GetPinFunction(int*,int*);
};
class TKey
{
DWORD Lconf,Rconf;
int x,y;
int status;
int position;
void MakeConfig(int n1, int n2, int n3, int n4);
public:
TKey(int n1, int n2, int n3, int n4 = 0);
int SetPos(int);
int MouseOver(POINTS p);
void Draw(HDC dc,int stat);
void GetKeyConfig(DWORD *lc, DWORD *rc){*lc =Lconf; *rc = Rconf;}
};
class ButtonPanel
{
HWND hWnd;
int initpos;
TShaft *shaft[PANELSIZE];
TKey *key[MAX_BUTTONS];
public:
ButtonPanel(HWND hwnd);
~ButtonPanel();
int Init();
int Find(int *index);
int GetNext(int i);
void OnPaint();
int CheckClick(POINTS p);
void OnKeyClick(int n, UINT msg);
};
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Code::Blocks Template Windows App"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
700, /* The programs width */
450, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static ButtonPanel *btnPanel = NULL;
switch (message) /* handle the messages */
{
case WM_CREATE:
btnPanel = new ButtonPanel(hwnd);
if(btnPanel->Init() < 0)
{
MessageBox(0, "Fail to build buttons panel","Error", MB_OK);
return -1;
}
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
{
POINTS p = MAKEPOINTS(lParam);
int n = btnPanel->CheckClick(p);
if(n != -1)
btnPanel->OnKeyClick(n, message);
break;
}
case WM_PAINT:
btnPanel->OnPaint();
break;
case WM_DESTROY:
delete btnPanel;
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
ButtonPanel::ButtonPanel(HWND hwnd): hWnd(hwnd)
{
ZeroMemory(shaft, (PANELSIZE * sizeof(TShaft*)));
ZeroMemory(key, (MAX_BUTTONS * sizeof(TKey*)));
// Locate the image in the screen
X1 = 40;
Y1 = 70;
wSpace = 21; // Space between keys
hSpace = 21; // Space between shafts
initpos = 2; // position of first key in buttons panel
}
ButtonPanel::~ButtonPanel()
{
for(int i = 0; i < MAX_BUTTONS; i++)
delete key[i];
for(int i = 0; i < PANELSIZE; i++)
delete shaft[i];
}
int ButtonPanel::Init()
{
int index[MAX_BUTTONS];
if(!Find(index))
{
MessageBox(0, "Impossible to sort keys","Error", MB_OK);
return -1;
}
//
for(int n = 0; n < MAX_BUTTONS; n++)
{
TKey *k = new TKey(MajorKeys[index[n]][0], MajorKeys[index[n]][1], MajorKeys[index[n]][2]);
k->SetPos(n + initpos + 1);
key[n] = k;
}
for(int n = 0; n < PANELSIZE; n++)
{
TShaft * tr = new TShaft(ShaftList[n][0], ShaftList[n][1], ShaftList[n][2], ShaftList[n][3]);
for(int i = 0; i < 2; i++)
{
int p = tr->Where();
if(p > 0 && p < 13 && shaft[p - 1] == 0)
{
tr->SetPos(p);
shaft[p - 1] = tr;
break;
}
else // reverse position and try again
{
if(i == 0)tr->ReversePos();
else
{
delete tr;
return -2;
}
}
}
}
return 0;
}
/*
* Sort the key list to correct order
* return true if sucessfull, false otherwise.
*/
int ButtonPanel::Find(int *index)
{
int value, done;
ZeroMemory(index, MAX_BUTTONS * sizeof(int));
for(int i = 0; i < MAX_BUTTONS; i++)
{
value = i;
done = 0;
*(index + 7) = value;
for (int n = 0; n < MAX_BUTTONS; n++)
{
value = GetNext(value);
if (value < 0) break;
else
{
done++;
*(index + (6 - n)) = value;
}
}
if (done == 7) return 1;
}
return 0;
}
int ButtonPanel::GetNext(int i)
{
// get first note of chord
int value = MajorKeys[i][0];
// find the chord index that match the third note
for(int t = 0; t < MAX_BUTTONS; t++)
{
if (i != t && value == MajorKeys[t][2])
return t;
}
return -1;
}
void ButtonPanel::OnPaint()
{
PAINTSTRUCT ps;
HDC dc = BeginPaint(hWnd, &ps);
int x1 = X1 - (wSpace / 2);
int w = 25 * wSpace + wSpace;
LOGBRUSH lb;
lb.lbStyle = BS_PATTERN;
HANDLE old = SelectObject(dc,CreatePen(PS_SOLID, 3, RGB(200, 100, 150)));
MoveToEx(dc, x1, Y1 - 10, NULL);
LineTo(dc, x1 + w, Y1 - 10);
LineTo(dc, x1 + w, Y1 + 275);
LineTo(dc, x1, Y1 + 275);
LineTo(dc, x1, Y1 - 10);
MoveToEx(dc, x1 + wSpace, Y1 - 10, NULL);
LineTo(dc, x1 + wSpace, Y1 + 275);
MoveToEx(dc, x1 + w - wSpace, Y1 - 10, NULL);
LineTo(dc, x1 + w - wSpace, Y1 + 275);
DeleteObject(SelectObject(dc,old));
for(int i = 0; i < MAX_BUTTONS; i++)
key[i]->Draw(dc, 0);
for(int n = 0; n < PANELSIZE; n++)
{
if(shaft[n])
shaft[n]->Draw(dc, 0);
}
old = SelectObject(dc, CreatePen(PS_SOLID, 3, RGB(200, 200, 200)));
for(int n = 0; n < 25; n++)
{
int xx = x1 + n * wSpace + wSpace;
MoveToEx(dc, xx - 2, Y1 - 10, NULL);
LineTo(dc, xx + 2 , Y1 - 10);
}
for(int n = 0; n < 25; n++)
{
int xx = x1 + n * wSpace + wSpace;
MoveToEx(dc, xx - 2, Y1 + 275, NULL);
LineTo(dc, xx + 2 , Y1 + 275);
}
DeleteObject(SelectObject(dc,old));
EndPaint(hWnd, &ps);
}
int ButtonPanel::CheckClick(POINTS p)
{
for(int n = 0; n < MAX_BUTTONS; n++)
{
if(key[n]->MouseOver(p))
return n;
}
return -1;
}
void ButtonPanel::OnKeyClick(int n, UINT msg)
{
DWORD Lconf, Rconf;
HDC dc = GetDC(hWnd);
key[n]->GetKeyConfig(&Lconf, &Rconf);
key[n]->Draw(dc, msg == WM_LBUTTONDOWN);
for(int n = 0; n < PANELSIZE; n++)
{
if((Lconf >> n) & 1)
shaft[n]->Draw(dc, msg == WM_LBUTTONDOWN);
if((Rconf >> n) & 1)
shaft[n]->Draw(dc, msg == WM_LBUTTONDOWN);
}
ReleaseDC(hWnd,dc);
}
TKey::TKey(int n1, int n2, int n3, int n4): position(0),
status(0)
{
MakeConfig(n1, n2, n3, n4);
}
void TKey::MakeConfig(int n1, int n2, int n3, int n4)
{
Lconf = Rconf = 0;
if (n1 > 0) Rconf |= (DWORD)1 << (n1 - 1);
else if (n1 < 0) Lconf |= (DWORD)1 << (abs(n1) - 1);
if (n2 > 0) Rconf |= (DWORD)1 << (n2 - 1);
else if (n2 < 0) Lconf |= (DWORD)1 << (abs(n2) - 1);
if (n3 > 0) Rconf |= (DWORD)1 << (n3 - 1);
else if (n3 < 0) Lconf |= (DWORD)1 << (abs(n3) - 1);
if (n4 > 0) Rconf |= (DWORD)1 << (n4 - 1);
else if (n4 < 0) Lconf |= (DWORD)1 << (abs(n4) - 1);
}
int TKey::SetPos(int p)
{
if (p < 0 || p > 12)
return 0;
position = p;
x = X1 + position * (wSpace * 2) + (wSpace / 2);
y = Y1 + 5;
return p;
}
void TKey::Draw(HDC dc, int stat)
{
int l = wSpace - 5;
if(status != stat)
{
status = stat;
HANDLE old = SelectObject(dc,CreatePen(PS_SOLID, 1, RGB(200, 200, 200)));
MoveToEx(dc, x, y - 40, NULL);
LineTo(dc, x, y + 12 * hSpace + 20);
for(int i = 0; i < PANELSIZE; i++)
{
if((Lconf >> i) & 1)
{
MoveToEx(dc, x - l, y + i * hSpace, NULL);
LineTo(dc, x, y + i * hSpace);
}
if((Rconf >> i) & 1)
{
MoveToEx(dc, x, y + i * hSpace, NULL);
LineTo(dc, x + l, y + i * hSpace);
}
}
y = stat ? (y + (hSpace - 10)) : (y - (hSpace - 10));
DeleteObject(SelectObject(dc,old));
}
MoveToEx(dc, x, y - 40, NULL);
LineTo(dc, x, y + 12 * hSpace + 20);
for(int i = 0; i < PANELSIZE; i++)
{
if((Lconf >> i) & 1)
{
MoveToEx(dc, x - l, y + i * hSpace, NULL);
LineTo(dc, x, y + i * hSpace);
}
if((Rconf >> i) & 1)
{
MoveToEx(dc, x, y + i * hSpace, NULL);
LineTo(dc, x + l, y + i * hSpace);
}
}
}
int TKey::MouseOver(POINTS p)
{
if(p.x > x - 15 && p.x < x + 15)
return 1;
return 0;
}
TShaft::TShaft(int n1, int n2, int n3, int n4)
{
conf = 0; name[0] = '\0'; status = 0;
if(n1)conf = (DWORD)1 << (n1 - 1);
if(n2)conf |= (DWORD)1 << (n2 - 1);
if(n3)conf |= (DWORD)1 << (n3 - 1);
if(n4)conf |= (DWORD)1 << (n4 - 1);
}
void TShaft::Draw(HDC dc, int st)
{
int yy;
if(st)
{
yy = y + 10;
SetTextColor(dc, RGB(240, 240, 240));
}
else
{
yy = y - 3;
SetTextColor(dc, RGB(0, 0, 0));
}
SetBkMode(dc, TRANSPARENT);
if ( status && st == 0)
{
HANDLE old = SelectObject(dc,CreatePen(PS_SOLID, 1, RGB(200, 200, 200)));
for(int i = 0; i < 25; i++)
{
if((conf >> i) & 1)
{
int xx = x + wSpace * (24 - i);
MoveToEx(dc, xx, y, NULL);
LineTo(dc, xx, y + 11);
}
}
DeleteObject(SelectObject(dc,old));
}
MoveToEx(dc, x, y, NULL);
LineTo(dc, x + 25 * wSpace, y);
for(int i = 0; i < 25; i++)
{
if((conf >> i) & 1)
{
int xx = x + wSpace * (24 - i);
MoveToEx(dc, xx, yy, NULL);
LineTo(dc, xx, y);
if(((i + 1) & 1) && pinout == (i >> 1) + 1)
{
int xx = x + (24 - i) * wSpace - (wSpace / 2) + 5;
int yy = Y1 + hSpace * 12 + 50;
TextOut(dc, xx, yy, Note[(i / 2)], lstrlen(Note[(i / 2)]));
}
}
}
status = st;
}
void TShaft::ReversePos()
{
DWORD aux=0;
for(int i = 0; i < 25; i++)
if((conf >> i) & 1)
aux += (DWORD)1 << (24 - i);
conf = aux;
}
int TShaft::SetPos(int p)
{
x = X1;
y = Y1 + p * hSpace;
position = p;
return p;
}
int TShaft::Where()
{
int in[4], out[4], p;
ZeroMemory(in, (4 * sizeof(int)));
ZeroMemory(out, (4 * sizeof(int)));
GetPinFunction(in, out);
if ((p = Select(in, out)))
return p;
return 0;
}
/**
return index clef FA note name
*/
int TShaft::Select(int *in, int *out)
{
int done;
int nPins = 0;
// Calculate number of pins in shaft
for (int n = 0; n < 25; n++)
{
if((conf >> n) & 1)
nPins++;
}
for(int n = 0; n < 4; n++)
{
if (out[n] == 0) break;
done = 0;
for (int i = 0; i < 4; i++)
{
if (in[i] == 0) break;
for (int z = 0; z < 5; z++)
{
if (out[n] == ChordList[in[i] - 1][z])
{
done++;
if (done == nPins)
{
pinout = out[n];
lstrcpy(name, Note[out[n] - 1]);
return ClefFA[out[n] - 1];
}
break;
}
}
}
}
return 0;
}
/**
Identify input and output pins
*/
void TShaft::GetPinFunction(int *_in, int *_out)
{
int *in, *out, note;
in = _in;
out = _out;
for (int i = 0; i < 25; i++)
{
if ((conf >> i) & 1)
{
note = (i >> 1) + (i & 1) + 1;
*in++ = note; // All pins are input (receive command from keys)
if (!(i & 1))
*out++ = note; // One odd pin is output (control for the valve)
}
}
}