/
main.cpp
562 lines (459 loc) · 16.2 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
#include <windows.h>
#include <time.h>
#include <process.h>
#include <math.h>
#include <vector.h>
#include "GUI/gui.h"
#include "resource.h"
#include "scale.h"
#define WM_UPDATE_READING 0x0400
#define WM_SCALE_CONNECTED 0x0401
#define WM_SCALE_DISCONNECTED 0x0402
//Declare Windows procedure
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
//Declare Custom functions
void CreateWindowMenu(Window);
void HandleMenu(HWND, DWORD);
void GetScaleReading(void*);
void ScaleConnect(void*);
void Meal(void*);
void GetSaveFile(HWND);
bool SaveMeal();
void AddCurrentValue();
int CheckAndSave();
void ToggleScaleMenu(bool);
//Create menus/popup-menus
Menu mTitle(0);
Menu mFile(1);
Menu mScale(1);
//Initialize the controls
Window cWindow;
Button cButton;
ListBox cListBox;
EditBox cEditBox;
//Initialize the labels
Label cLabelDelay;
EditBox cEditDelay;
Label cLabelDate;
ComboBox cComboDate;
Label cLabelRecord;
ComboBox cComboRecord;
Label cLabelSensitivity;
ComboBox cComboSensitivity;
class MealItem {
private:
struct tm* timestamp;
float value;
LPCSTR unit;
MealItem(struct tm* timestamp, float value, LPCSTR unit) : timestamp(timestamp), value(value), unit(unit) {}
public:
static MealItem create(float value, LPCSTR unit) {
time_t rwtm;
time(&rwtm);
MealItem item(new tm(*localtime(&rwtm)), value, unit);
return item;
}
struct tm* getTime() { return timestamp; }
float getValue() { return value; }
LPCSTR getUnit() { return unit; }
~MealItem() {}
};
//Data to store the meal
std::vector<MealItem> vMealItems;
int mCompare = 1;
float mSensitivity = 0;
//Option variables
bool bMealActive;
UINT iMealWait;
UINT uLastStable;
float fLastWeight;
//For saving
char cSaveName[300] = "\0";
bool bSaved = 0;
//Initialize scale
Scale* scale;
HANDLE hScaleThread;
//Entry point of the program
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
//Create a window
cWindow = Window(WindowProcedure, hThisInstance, "MealTrackApp", nCmdShow);
cWindow.Create("MealTrack - Untitled", 462, 375);
//Actually create the button with the window as its parent
RECT rEditBox = {16, 280, 272, 24};
cEditBox = EditBox(cWindow, rEditBox, "Disconnected");
cEditBox.SetReadOnly(1);
//Create the Button
RECT rButton = {304, 280, 128, 24};
cButton = Button(cWindow, rButton, "Start Meal", IDR_START_BUTTON);
cButton.SetEnabled(0);
//Create the listbox
RECT rListBox = {16, 16, 272, 272};
cListBox = ListBox(cWindow, rListBox, "MealListBox");
//Meal wait box
RECT rLabelDelay = {304, 16, 128, 16};
RECT rEditDelay = {304, 32, 128, 24};
cLabelDelay = Label(cWindow, rLabelDelay, "Meal wait (seconds)");
cEditDelay = EditBox(cWindow, rEditDelay, "10");
//Create Date format box
RECT rLabelDate = {304, 64, 128, 16};
RECT rComboDate = {304, 80, 128, 24};
cLabelDate = Label(cWindow, rLabelDate, "Date format");
cComboDate = ComboBox(cWindow, rComboDate, "ComboBoxDate");
cComboDate.AddItem("12 Hour AM/PM");
cComboDate.AddItem("24 Hour");
cComboDate.SetDefaultItem(1);
//Record format box
RECT rLabelRecord = {304, 112, 128, 16};
RECT rComboRecord = {304, 128, 128, 24};
cLabelRecord = Label(cWindow, rLabelRecord, "Record change type");
cComboRecord = ComboBox(cWindow, rComboRecord, "ComboBoxRecord");
cComboRecord.AddItem("Increases");
cComboRecord.AddItem("Decreases");
cComboRecord.AddItem("Both");
cComboRecord.SetDefaultItem(1);
//Record format box
RECT rLabelSensitivity = {304, 160, 128, 16};
RECT rComboSensitivity = {304, 176, 128, 24};
cLabelSensitivity = Label(cWindow, rLabelSensitivity, "Sensitivity");
cComboSensitivity = ComboBox(cWindow, rComboSensitivity, "ComboBoxSensitivity");
cComboSensitivity.AddItem("0.01 g");
cComboSensitivity.AddItem("0.02 g");
cComboSensitivity.AddItem("0.03 g");
cComboSensitivity.AddItem("0.04 g");
cComboSensitivity.AddItem("0.05 g");
cComboSensitivity.AddItem("0.06 g");
cComboSensitivity.AddItem("0.07 g");
cComboSensitivity.AddItem("0.08 g");
cComboSensitivity.AddItem("0.09 g");
cComboSensitivity.SetDefaultItem(2);
//Custom function to creeate window
CreateWindowMenu(cWindow);
//Message loop
MSG msg;
while (cWindow.GetMessage(&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// The program return-value is 0 - The value that PostQuitMessage() gave
return msg.wParam;
}
// This function is called by the Windows function DispatchMessage()
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT mMsg, WPARAM wParam, LPARAM lParam)
{
switch (mMsg)
{
case WM_CREATE:
break;
case WM_SIZE:
break;
case WM_COMMAND:
//If handle is the window, handle the menu
if (hWnd == cWindow.GetHandle())
HandleMenu(hWnd, LOWORD(wParam));
if (LOWORD(wParam) == IDR_START_BUTTON) {
//Check if we have a connection to the scale
if (scale != NULL && scale->IsConnected()) {
//Check if we just finished a meal as well, if we didn't, start one
if (!bMealActive) { //If the meal is not active, start it or reset it
if (vMealItems.size() == 0) {
_beginthread(Meal, 0, (void*)hWnd);
} else {
vMealItems.clear();
HandleMenu(hWnd, ID_FILE_NEW);
}
} else { //Otherwise, stop it
if (strlen(cSaveName)<=1) GetSaveFile(hWnd);
bMealActive = 0;
}
}
}
break;
case WM_CLOSE:
if (strlen(cSaveName) > 1 && !SaveMeal()) {
if (CheckAndSave() == -1) break;
} else if (!bSaved && vMealItems.size() > 0) {
switch(MessageBox(hWnd, "You haven't saved the current meal, would you like to?","Hold Up!", MB_YESNOCANCEL | MB_ICONINFORMATION)) {
case IDYES:
if (CheckAndSave() == -1) break;
case IDNO:
if (scale) delete scale;
DestroyWindow(hWnd);
hWnd = NULL;
break;
case IDCANCEL:
break;
default:
break;
}
} else {
if (hWnd != NULL) {
if (scale) delete scale;
DestroyWindow(hWnd);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_UPDATE_READING:
//Destroy any spare messages if scale is null
if (scale == NULL) break;
//Retreive the information and print it on the button
char tBuf[32];
sprintf(tBuf, "Connected on %s - %.2f %s %s", scale->GetName(), scale->GetWeight(), scale->GetUnit(), (scale->IsStable()?"":"(unstable)"));
cEditBox.SetText(tBuf);
hScaleThread = (HANDLE)_beginthread(GetScaleReading, 0, (void*) hWnd);
break;
case WM_SCALE_CONNECTED:
ToggleScaleMenu(1);
break;
case WM_SCALE_DISCONNECTED:
ToggleScaleMenu(0);
cEditBox.SetText("Disconnected");
break;
default:
return DefWindowProc (hWnd, mMsg, wParam, lParam);
}
return 0;
}
void CreateWindowMenu(Window w) {
//File Menu
mFile.AddItem(ID_FILE_NEW, "&New");
mFile.AddItem(ID_FILE_SAVE, "&Save");
mFile.AddBreak();
mFile.AddItem(ID_FILE_EXIT, "E&xit");
//Scale Menu
mScale.AddItem(ID_SCALE_CONNECT, "&Connect");
mScale.AddItem(ID_SCALE_ZERO, "&Zero", MF_DISABLED);
//Add the sub menu's to the title menu
mTitle.AddSubMenu(mFile, "&File");
mTitle.AddSubMenu(mScale, "&Scale");
//Set the window's menu to the title menu
w.SetWindowMenu(mTitle);
}
void ToggleScaleMenu(bool scaleIsActive) {
if (scaleIsActive) { //If scale is active
mScale.EditItem(ID_SCALE_CONNECT, "&Disconnect");
mScale.EditItem(ID_SCALE_ZERO, "&Zero", 0);
cButton.SetEnabled(1);
} else {
mScale.EditItem(ID_SCALE_CONNECT, "&Connect");
mScale.EditItem(ID_SCALE_ZERO, "&Zero", MF_DISABLED);
cButton.SetEnabled(0);
}
}
void HandleMenu(HWND hWnd, DWORD opt) {
switch(opt) {
case ID_SCALE_CONNECT:
cEditBox.SetText("Trying to communicate with the scale...");
mScale.EditItem(ID_SCALE_CONNECT, "&Connecting...", MF_DISABLED);
_beginthread(ScaleConnect, 0, (void*) hWnd);
break;
case ID_FILE_NEW:
if (!bSaved && vMealItems.size() > 0) {
switch(MessageBox(hWnd, "You haven't saved the current meal, would you like to?","Hold Up!", MB_YESNOCANCEL | MB_ICONINFORMATION)) {
case IDYES:
if (CheckAndSave()==-1) break;
case IDNO:
cListBox.Clear();
vMealItems.clear();
strcpy(cSaveName, "");
cWindow.SetTitle("MealTrack - Untitled");
cButton.SetText("Start Meal");
break;
case IDCANCEL:
default:
break;
}
} else {
cListBox.Clear();
vMealItems.clear();
strcpy(cSaveName, "");
cWindow.SetTitle("MealTrack - Untitled");
cButton.SetText("Start Meal");
}
break;
case ID_FILE_SAVE:
GetSaveFile(hWnd);
if (vMealItems.size() >= 1) CheckAndSave();
char tmp[313];
sprintf(tmp, "MealTrack - %s", strrchr(cSaveName,'\\') + 1);
cWindow.SetTitle(tmp);
break;
case ID_FILE_EXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
case ID_SCALE_ZERO:
if (scale != NULL) scale->Zero();
break;
}
}
void GetScaleReading(void* args) {
//Acquire a reading from the scale
if (scale != NULL) {
scale->GetReading();
SendMessage((HWND)args, WM_UPDATE_READING, 0, 0);
}
}
void ScaleConnect(void* args) {
//If we aren't connected, connect!
if (scale == NULL) {
//Create a new scale and attempt to connect
scale = new Scale("COM1");
if (!scale->IsConnected()) {
delete scale;
scale = new Scale();
}
if (!scale->IsConnected()) {
char err[256];
sprintf(err, "Failed to connect! ERROR: %i", scale->GetError());
mScale.EditItem(ID_SCALE_CONNECT, "&Connect");
cEditBox.SetText(err);
delete scale;
scale = NULL;
return;
}
//Begin the thread which sends a message to the Window's event and loops it
hScaleThread = (HANDLE)_beginthread(GetScaleReading, 0, args);
//Switch the menu to allow for disconnect & other features
SendMessage((HWND)args, WM_SCALE_CONNECTED, 0, 0);
} else {
//Deactivate the meal
bMealActive = 0;
//Delete the scale which will disconnect it
delete scale;
scale = NULL;
SendMessage((HWND)args, WM_SCALE_DISCONNECTED, 0, 0);
}
}
void Meal(void* args) {
//Disable buttons/settings
cButton.SetText("End Meal");
cEditDelay.SetEnabled(0);
cComboDate.SetEnabled(0);
cComboRecord.SetEnabled(0);
cComboSensitivity.SetEnabled(0);
mScale.EditItem(ID_SCALE_ZERO, "&Zero", MF_DISABLED);
//Get Variables
char* tmpWait = new char[255];
cEditDelay.GetText(tmpWait);
iMealWait = atoi(tmpWait);
if (iMealWait==0) iMealWait = 10;
mCompare = cComboRecord.GetSelectedItem();
mSensitivity = ((float)cComboSensitivity.GetSelectedItem()+1.0)/100.0;
//Initialize the first weight
AddCurrentValue();
//Activate the meal
bMealActive = true;
while(bMealActive) {
//So if the scale is not stable, set the current time to when the scale was last stable
if (!scale->IsStable()) uLastStable = time(0);
if (time(0) - uLastStable >= iMealWait) {
if (scale!=NULL && scale->IsConnected()) {
if (fabs(scale->GetWeight() - fLastWeight) >= mSensitivity) {
if ((mCompare == 0 || mCompare == 2) && scale->GetWeight() > fLastWeight) AddCurrentValue();
if ((mCompare == 1 || mCompare == 2) && scale->GetWeight() < fLastWeight) AddCurrentValue();
}
}
}
//Sleep for .1 second
Sleep(100);
}
//Check and see if the person saved the file
CheckAndSave();
//Enable them
cButton.SetText("New Meal");
cEditDelay.SetEnabled(1);
cComboDate.SetEnabled(1);
cComboRecord.SetEnabled(1);
cComboSensitivity.SetEnabled(1);
mScale.EditItem(ID_SCALE_ZERO, "&Zero", 0);
}
void AddCurrentValue() {
//For temporary strings
char tmp[128], tmp2[128];
time_t rwtm;
//Retrieve the current time
time(&rwtm);
//Get the current values from the scale and computer
fLastWeight = scale->GetWeight();
MealItem item = MealItem::create(fLastWeight, scale->GetUnit());
vMealItems.push_back(item);
//Convert time to string (12/24 hour format)
if (cComboDate.GetSelectedItem()==0) strftime(tmp2, 128, "%I:%M:%S %p", item.getTime());
else strftime(tmp2, 128, "%H:%M:%S", item.getTime());
//Print to the textbox
sprintf(tmp, "%s: %.2f %s", tmp2, item.getValue(), item.getUnit());
cListBox.AddItem(tmp);
}
void GetSaveFile(HWND hWnd) {
OPENFILENAME ofName;
ZeroMemory(&ofName, sizeof(ofName));
ofName.lStructSize = sizeof(ofName);
ofName.hwndOwner = hWnd;
ofName.lpstrFile = cSaveName;
ofName.lpstrFile[0] = '\0';
ofName.nMaxFile = sizeof(cSaveName);
ofName.lpstrFilter = "Comma Seperated Values\0*.csv\0";
ofName.nFilterIndex = 1;
ofName.lpstrFileTitle = NULL;
ofName.nMaxFileTitle = 0;
ofName.lpstrInitialDir = NULL;
GetSaveFileName(&ofName);
}
bool SaveMeal() {
if (cSaveName==NULL || strlen(cSaveName)<=1) return 0;
if (vMealItems.size() <=0) return 0;
if (strstr(cSaveName,".csv") == NULL) strcat(cSaveName,".csv");
//Open the file
FILE* fSave = fopen(cSaveName, "w+");
if (fSave == NULL) return 0;
char tmp[313];
strftime(tmp, 128, "%m/%d/%Y", vMealItems[0].getTime());
if (fprintf(fSave, "MEAL START: %s,,\n", tmp) == 0) {
fclose(fSave);
return 0;
}
fprintf(fSave, "Time,Mass,Unit\n");
std::vector<MealItem>::iterator mealIterator = vMealItems.begin();
while(mealIterator != vMealItems.end()) {
if (cComboDate.GetSelectedItem()==0) strftime(tmp, 128, "%I:%M:%S %p", mealIterator->getTime());
else strftime(tmp, 128, "%H:%M:%S", mealIterator->getTime());
fprintf(fSave, "%s,%.2f,%s\n", tmp, mealIterator->getValue(), mealIterator->getUnit());
}
strftime(tmp, 128, "%m/%d/%Y", vMealItems[vMealItems.size()-1].getTime());
fprintf(fSave, "MEAL END: %s,,\n", tmp);
fclose(fSave);
sprintf(tmp, "MealTrack - %s", strrchr(cSaveName,'\\') + 1);
cWindow.SetTitle(tmp);
bSaved = 1;
return(1);
}
int CheckAndSave() {
//Get the filename if we don't have one
if (strlen(cSaveName)<=1) GetSaveFile(cWindow.GetHandle());
//Save the meal & make sure it saved
bool okay = 0;
while(!okay) {
if (!SaveMeal()) {
switch(MessageBox(cWindow.GetHandle(), "Failed to save the file, do you wish to try again?", "Error", MB_YESNOCANCEL | MB_ICONERROR)) {
case IDYES:
GetSaveFile(cWindow.GetHandle());
break;
case IDNO:
return(0);
break;
case IDCANCEL:
return(-1);
break;
default:
break;
}
} else {
okay = 1;
}
}
return(1);
}