-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (106 loc) · 2.83 KB
/
main.cpp
File metadata and controls
115 lines (106 loc) · 2.83 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
//
// Created by renbin jiang on 2022/2/27.
//
#include <string.h>
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include "alarm_timer.h"
alarmTimer *alarm = nullptr;
int addMinus(char *ch, unsigned int &num) {
if (!ch || num < 0) {
std::cout << "[" << __func__ << "] ch is NULL or num is zero!!!" << std::endl;
return -1;
}
switch (*ch) {
case 'w':
num += 1;
break;
case 's':
if (!num) return -1;
num -= 1;
break;
default:
std::cout << "ch is illegal!!!!" << std::endl;
break;
}
return 0;
}
int inputHandle(char *ch) {
if (!ch) {
std::cout << "[" << __func__ << "] ch is NULL!!!" << std::endl;
return -1;
}
unsigned int h, m, s;
unsigned int ws;
switch (*ch) {
case 'h': {
alarm->getCurrentTime(h, m, s);
std::cout << "cur hours:" << h << std::endl;
std::cout << "please input hours:";
std::cin >> ws;
std::cout << "set hours:" << ws << std::endl;
alarm->setTimerHour(ws);
break;
} break;
case 'm': {
alarm->getCurrentTime(h, m, s);
std::cout << "cur minute:" << m << std::endl;
std::cout << "please input minute:";
std::cin >> ws;
std::cout << "set minute:" << ws << std::endl;
alarm->setTimerMinute(ws);
break;
} break;
case 's': {
alarm->getCurrentTime(h, m, s);
std::cout << "cur second:" << s << std::endl;
std::cout << "please input second:";
std::cin >> ws;
std::cout << "set second:" << ws << std::endl;
alarm->setTimerSecond(ws);
break;
} break;
default:
break;
}
return 0;
}
void timerThread() {
while (true) {
if (alarm->setTimer()) break;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void help() {
std::cout << "\n---------------------\n"
<< "input 'q' : exit\n"
<< "input 'h' : hours\n"
<< "input 'm' : minute\n"
<< "input 's' : seconds\n"
<< "---------------------\n"
<< std::endl;
}
int main() {
alarm = alarmTimer::get();
alarm->showCurrentTime();
help();
char ch;
// Ctrl + z end
while (true) {
std::cout << "input:";
std::cin >> ch;
std::cout << "ch = " << ch << std::endl;
if (inputHandle(&ch)) {
break;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
if (ch && !strcmp(&ch, "q")) break;
}
std::thread timerFunc(timerThread);
if (timerFunc.joinable()) {
timerFunc.join();
}
return 0;
}