-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
469 lines (323 loc) · 13.1 KB
/
main.cpp
File metadata and controls
469 lines (323 loc) · 13.1 KB
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
#include <unistd.h>
#include "Controller.h"
#include "StateManager/StateManager.h"
#include <iostream>
#include <vector>
constexpr int PIN_SHUTDOWN_PROG{ 21 };
int main() {
IdleState idlestate;
StateManager manager;
Controller controller(&manager);
manager.SetState(&idlestate);
manager.LoadCurrentState();
controller.Render();
for(int i = 0; i < 10; i++)//(gpioRead(PIN_SHUTDOWN_PROG) != 1) // loop for ever
{
if(controller.HandleTouch())
controller.Render();
//usleep(10);
}
return 0;
}
// #include <iostream>
// #include <unistd.h>
// #include <thread>
// #include <mutex>
// #include <atomic>
// #include <ctime>
// #include <iomanip>
// #include "OpenFoodFact.h"
// #include "Camera.h"
// constexpr int PIN_BEEPER{ 13 };
// constexpr int PIN_ACTIVATE_CAM{ 6 };
// constexpr int PIN_SHUTDOWN_PROG{ 21 };
// bool cam_activated{ false };
// bool product_found{ false };
// const char* date_format{ "%d/%m/%Y %T" };
// Camera camera{}; // Allocate the camera memory
// // Create our two permanents objects
// OpenFoodFact db{}; // Link to the database
// LCDScreen* lcd; // LCD screen
// // Open the database named food.db
// SQLite sqlite("food.db");
// // Get the table name
// const std::string table_name{ "table4" };
// std::atomic<bool> stopped(false); // Atomic flag to control the threads
// ListProduct* list;
// String* drawTime;
// enum class ActionMode{
// REST_MODE,
// CAM_MODE,
// PRODUCT_MODE,
// DATE_MODE
// };
// std::atomic<ActionMode> action{ActionMode::REST_MODE};
// std::string prev_barcode{};
// cv::Mat ConvertCamToLCD(const cv::Mat& frame);
// void AddItemsFromDB(SQLite& sqlite, const std::string& table_name, ListProduct& list);
// void LCDPrinting(std::atomic<bool>& stopped);
// void GPIOInteraction(std::atomic<bool>& stopped);
// int main()
// {
// // Initialize the pigpio library
// if (gpioInitialise() < 0)
// {
// std::cerr << "pigpio failed" << std::endl;
// }
// // Set Mode Pins
// if (gpioSetMode(PIN_BEEPER, PI_OUTPUT) != 0 ||
// gpioSetMode(PIN_ACTIVATE_CAM, PI_INPUT) != 0 ||
// gpioSetMode(PIN_SHUTDOWN_PROG, PI_INPUT != 0))
// {
// std::cerr << "Failed to set mode on one or more pins" << std::endl;
// }
// // Create the table if it is not already created
// row col_names{"product_names", "dates"};
// row col_types{"String", "String"};
// sqlite.createTable(table_name, col_names, col_types);
// lcd = new LCDScreen();
// drawTime = new String("",
// WIDTH / 2 - 20 / 2 * Font::width_font,
// 1,
// 20, // TODO: Not accurate for the letter
// 1,
// lcd,
// nullptr,
// 1);
// drawTime->SetName("TimeString");
// // Add the time text
// lcd->AddComponents(drawTime);
// // Create a list to be displayed on the LCD screen
// list = new ListProduct(2, lcd, nullptr, 2);
// list->SetPos(Position{10, 20});
// list->SetSize(Size{WIDTH - 20, HEIGHT - 30});
// list->SetName("LIST");
// list->Invisible();
// // Add the list to the LCD
// lcd->AddComponents(list);
// Loading* loading{ new Loading(200, 120, lcd, nullptr) };
// loading->SetName("Loading");
// lcd->AddComponents(loading);
// AddItemsFromDB(sqlite, table_name, *list); // Add saved products
// std::thread gpioInteraction(GPIOInteraction, std::ref(stopped));
// std::thread lcdPrinting(LCDPrinting, std::ref(stopped));
// usleep(3 * 1000 * 1000); // wait 3 seconds to stabilize all
// while(gpioRead(PIN_SHUTDOWN_PROG) != HIGH) // loop for ever
// {
// usleep(10);
// }
// std::cout << "Terminate" << std::endl;
// stopped = true;
// lcdPrinting.join();
// gpioInteraction.join();
// std::cout << "ok1" << std::endl;
// delete lcd; // Use it before terminate pigpio
// std::cout << "ok2" << std::endl;
// gpioTerminate();
// return 0;
// }
// double CompareDate(const std::string& datetime){
// const std::string SQLiteDateFormat{ "%d/%m/%Y" }; // Format used in the sqlite database and my application
// std::tm tmProduct{}; // time struct
// std::istringstream ss(datetime);
// ss >> std::get_time(&tmProduct, SQLiteDateFormat.c_str());
// if(ss.fail()){
// std::cout << "Conversion datetime failed" << std::endl;
// }else{
// std::time_t productDate{ mktime(&tmProduct) };
// double diffTime{ std::difftime(productDate, std::time(nullptr)) };
// std::cout << diffTime << std::endl;
// return diffTime;
// }
// return 0;
// }
// cv::Mat ConvertCamToLCD(const cv::Mat& frame){
// cv::Mat resizedImage{};
// resize(frame, resizedImage, cv::Size(WIDTH, HEIGHT), 0, 0, cv::INTER_CUBIC);
// return resizedImage;
// }
// void AddItemsFromDB(SQLite& sqlite, const std::string& table_name, ListProduct& list){
// // Vector that stores the row of the SQLite3 database
// // Currently holding product names and peremption date
// std::vector<row> products_name{ sqlite.getProductNames(table_name) };
// for(int i = 0; i < products_name.size(); i++){
// // Get product datetime
// double diffTime{ CompareDate(products_name[i][1]) };
// if(diffTime < 0){ // The date is already passed
// list.AddItem(std::vector<std::string>{products_name[i]}, RED);
// }else if(diffTime < 172800){ // The date is almost 2 days
// list.AddItem(std::vector<std::string>{products_name[i]}, YELLOW);
// }else{
// list.AddItem(std::vector<std::string>{products_name[i]}, WHITE);
// }
// //list.AddItem(std::vector<std::string>{products_name[i]});
// }
// }
// ActionMode GetAction(){
// return action.load();
// }
// void SetAction(ActionMode act){
// action.store(act);
// }
// void CheckUserGPIOInput(){
// int input{ gpioRead(PIN_ACTIVATE_CAM) };
// static int lastInput = LOW;
// static auto lastDebounceTime = std::chrono::steady_clock::now();
// const std::chrono::milliseconds debounceDelay(50); // 50ms debounce delay
// if (input != lastInput) {
// lastDebounceTime = std::chrono::steady_clock::now();
// }
// if ((std::chrono::steady_clock::now() - lastDebounceTime) > debounceDelay) {
// if (input == HIGH && GetAction() == ActionMode::REST_MODE) {
// if (camera.OpenCamera()) {
// std::cerr << "create" << std::endl;
// }
// std::cout << "wait" << std::endl;
// usleep(1000 * 1000); // wait 1 second
// SetAction(ActionMode::CAM_MODE);
// }
// }
// lastInput = input;
// }
// void GPIOInteraction(std::atomic<bool>& stopped){
// while(!stopped){
// usleep(10);
// ActionMode act = GetAction();
// switch(act){
// case ActionMode::REST_MODE:{
// CheckUserGPIOInput();
// break;
// }
// case ActionMode::CAM_MODE:{
// int input{ gpioRead(PIN_ACTIVATE_CAM) };
// if(input != LOW && input != HIGH){
// std::cout << "Bad input" << std::endl;
// }
// if(input == LOW){
// if(camera.readFrame())
// {
// std::string barcode{ camera.getBarcode() };
// if(barcode != "" && barcode == prev_barcode){
// gpioWrite(PIN_BEEPER, HIGH);
// usleep(200 * 1000); // wait 200 ms
// gpioWrite(PIN_BEEPER, LOW);
// std::cout << "Codebar is: ";
// std::cout << prev_barcode << std::endl;
// SetAction(ActionMode::PRODUCT_MODE);
// usleep(500); // wait 500 ms
// std::cout << "found" << std::endl;
// camera.CloseCamera();
// prev_barcode = "";
// }
// prev_barcode = barcode;
// }
// }
// else if(input == HIGH){
// SetAction(ActionMode::REST_MODE); // Before to turn off the child thread before the camera
// usleep(1000 * 1000); // wait 1 second
// camera.CloseCamera();
// std::cerr << "Reset" << std::endl;
// usleep(1000 * 1000); // wait 1 second
// }
// break;
// }
// case ActionMode::PRODUCT_MODE:{
// CheckUserGPIOInput();
// break;
// }
// }
// }
// }
// void DateProcessing(std::atomic<bool>& stopped, DateEntry*& dateEntry, Numpad*& numpad){
// std::cout << "ok1" << std::endl;
// while(!numpad->EntryTerminate()){
// // lcd->TouchScreen(); // Check if the screen has been touched
// // lcd->DrawComponents();
// }
// std::cout << "Date = " << dateEntry->GetString() << std::endl;
// db.makeRequest(prev_barcode.c_str());
// std::vector<row> vec_data{};
// row data{};
// data.push_back(db.getProductName());
// data.push_back(dateEntry->GetString());
// vec_data.push_back(data);
// sqlite.insertValues(table_name, vec_data);
// double diffTime{ CompareDate(dateEntry->GetString()) };
// if(diffTime < 0){ // The date is already passed
// list->AddItem(data, RED);
// }else if(diffTime < 172800){ // The date is almost 2 days
// list->AddItem(data, YELLOW);
// }else{
// list->AddItem(data, WHITE);
// }
// //list->AddItem(data);
// dateEntry->Invisible();
// numpad->Invisible();
// dateEntry->Deactivate();
// numpad->Deactivate();
// usleep(1000 * 1000); // wait 1s before deleting them to avoir lcd->DrawComponents to use them
// lcd->RemoveComponent(3);
// lcd->RemoveComponent(4);
// list->Visible();
// list->Activate();
// usleep(500 * 1000);
// }
// void LCDPrinting(std::atomic<bool>& stopped){
// lcd->DrawComponents();
// // time object
// std::time_t time;
// while(!stopped){
// usleep(10);
// ActionMode act = GetAction();
// switch(act){
// case ActionMode::REST_MODE:{
// // Get the actual time
// time = std::time(nullptr);
// char timeText[20];
// std::strftime(timeText, sizeof(timeText), date_format, std::localtime(&time));
// drawTime->SetString(timeText); // Update the time string
// lcd->TouchScreen();
// lcd->DrawComponents();
// break;
// }
// case ActionMode::CAM_MODE:{
// const cv::Mat frame{ camera.getFrame() };
// if(frame.empty())
// continue;
// cv::Mat image{ ConvertCamToLCD(frame) };
// unsigned char* data{ image.data };
// lcd->DrawFrame(data, image.total() * image.elemSize());
// break;
// }
// case ActionMode::PRODUCT_MODE:{
// for(Object* child: lcd->GetComponents()){
// if(child){
// if(child->GetName() == "LIST"){
// ListProduct* list{ dynamic_cast<ListProduct*>(child) };
// list->Invisible();
// list->Deactivate();
// }
// }
// }
// DateEntry* dateEntry{ new DateEntry(WIDTH / 2 - 4 * 8, HEIGHT / 4 - 2 * 6, 10 * Font::width_font, 2 * 2 * 6, lcd, nullptr, 3) };
// dateEntry->SetName("DateEntry");
// Numpad* numpad{ new Numpad(0, HEIGHT / 2, WIDTH, HEIGHT / 2, lcd, nullptr, 4) };
// numpad->SetName("Numpad");
// numpad->LinkTextEntry(dateEntry);
// lcd->AddComponents(dateEntry);
// lcd->AddComponents(numpad);
// SetAction(ActionMode::REST_MODE);
// std::thread dateProcess(DateProcessing, std::ref(stopped), std::ref(dateEntry), std::ref(numpad));
// dateProcess.detach();
// // DateProcessing(std::ref(stopped), dateEntry, numpad);
// if(dateEntry){
// std::cout << "still there" << std::endl;
// }else{
// std::cout << "Deleted" << std::endl;
// }
// lcd->DrawComponents();
// break;
// }
// }
// }
// }