-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
282 lines (258 loc) · 5.38 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <linux/power_ic.h>
#include <fcntl.h>
#define STANDARD 0
void standard_vibro();
void helicopter_vibro();
void ufo_vibro();
void shock_vibro();
void mosquito_vibro();
void train_vibro();
void pulse_vibro();
static struct modes {
const char *name_mode;
const int count_it;
void (*vibro_func)();
} vibro_mode[] = {
{ "standard", 3, standard_vibro },
{ "helicopter", 3, helicopter_vibro },
{ "ufo", 3, ufo_vibro },
{ "shock", 2, shock_vibro },
{ "mosquito", 6, mosquito_vibro },
{ "train", 3, train_vibro },
{ "pulse", 3, pulse_vibro }
};
void _delay(int msec)
{
usleep(msec * 1000);
}
void _vibrate(int duration)
{
int power_ic = open("/dev/" POWER_IC_DEV_NAME, O_RDWR);
ioctl(power_ic, POWER_IC_IOCTL_PERIPH_SET_VIBRATOR_ON,1);
usleep(duration * 1000);
ioctl(power_ic, POWER_IC_IOCTL_PERIPH_SET_VIBRATOR_ON,0);
close(power_ic);
}
int in_count_range_q(int cnt)
{
return (cnt < 2 || cnt > 10) ? 0 : 1;
}
int in_msec_range_q(int msec)
{
return (msec < 15 || msec > 60000) ? 0 : 1;
}
void print_error(int err)
{
switch (err) {
case 0: {
printf("ERROR: Wrong range!\n"
"\tThe msec should be in the range 15...60000\n");
break;
}
case 1: {
printf("ERROR: Wrong range!\n"
"\tThe count should be in the range 2...10\n");
break;
}
default: {
printf("ERROR: Unknown error!\n");
break;
}
}
print_help();
}
void print_help()
{
printf("Please use:\n"
"\tzPulse\n"
"\tzPulse [duration-in-msec]\n"
"\tzPulse [mode]\n"
"\tzPulse [mode] [count]\n"
"Available modes:\n"
"\t- standard\n"
"\t- helicopter\n"
"\t- ufo\n"
"\t- shock\n"
"\t- mosquito\n"
"\t- train\n"
"\t- pulse\n"
"Examples:\n"
"\tzPulse 15\n"
"\tzPulse 3000\n"
"\tzPulse mosquito\n"
"\tzPulse -ufo 6\n"
"\tzPulse --pulse 2\n");
}
int call_vibro_func(const char *arg)
{
int i;
int res = 0;
char *arg_dup = strdup(arg);
const char *_arg = strtok(arg_dup, "-");
const int modes_count = sizeof(vibro_mode) / sizeof(vibro_mode[0]);
for (i = 0; i < modes_count; ++i) {
if (_arg) {
if (!strcmp(_arg, vibro_mode[i].name_mode)) {
vibro_mode[i].vibro_func();
res = 1;
}
}
}
free(arg_dup);
return res;
}
int is_only_digits_arg_q(const char *arg)
{
int i;
const int size = strlen(arg);
for (i = 0; i < size; ++i) {
if (!isdigit(arg[i])) {
return 0;
}
}
return 1;
}
int main(int argc, char *argv[])
{
switch (argc) {
case 1: {
vibro_mode[STANDARD].vibro_func(); // Call standard vibro function
break;
}
case 2: {
if (!is_only_digits_arg_q(argv[1])) {
if (!call_vibro_func(argv[1])) {
print_help();
}
} else {
int msec = atoi(argv[1]);
if (!in_msec_range_q(msec)) {
print_error(0);
return 1;
} else {
_vibrate(msec);
}
}
break;
}
case 3: {
if (!is_only_digits_arg_q(argv[1]) && is_only_digits_arg_q(argv[2])) {
int i, j = 0;
int count = atoi(argv[2]);
if (!in_count_range_q(count)) {
print_error(1);
return 2;
}
for (i = 0; i < count; ++i) {
if (!call_vibro_func(argv[1])) {
++j;
}
_delay(350);
}
if (j) {
print_help();
}
}
else {
print_help();
}
break;
}
default: {
print_help();
break;
}
}
return 0;
}
void standard_vibro()
{
printf("Standard vibro\n");
_vibrate(500);
_vibrate(500);
}
void helicopter_vibro()
{
printf("Helicopter vibro\n");
_vibrate(75);
_delay(100);
_vibrate(100);
_delay(100);
_vibrate(75);
_delay(100);
_vibrate(100);
_delay(100);
_vibrate(75);
_delay(100);
_vibrate(100);
}
void ufo_vibro()
{
printf("Ufo vibro\n");
_vibrate(200);
_delay(300);
_vibrate(100);
_delay(300);
_vibrate(200);
}
void shock_vibro()
{
printf("Shock vibro\n");
_vibrate(35);
_delay(100);
_vibrate(200);
_delay(300);
_vibrate(35);
_delay(100);
_vibrate(200);
_delay(300);
_vibrate(35);
_delay(100);
_vibrate(200);
}
void mosquito_vibro()
{
printf("Mosquito vibro\n");
_vibrate(15);
_delay(150);
_vibrate(35);
_delay(150);
_vibrate(55);
_delay(150);
_vibrate(75);
_delay(150);
_vibrate(95);
_delay(150);
_vibrate(75);
_delay(150);
_vibrate(55);
_delay(150);
_vibrate(35);
_delay(150);
_vibrate(15);
}
void train_vibro()
{
printf("Train vibro\n");
_vibrate(75);
_vibrate(100);
_delay(100);
_vibrate(100);
_vibrate(75);
}
void pulse_vibro()
{
printf("Pulse vibro\n");
_vibrate(150);
_delay(150);
_vibrate(150);
_delay(500);
_vibrate(150);
_delay(150);
_vibrate(150);
}