duck.c 1.7 KB
Newer Older
jp7g21's avatar
jp7g21 committed
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <stdlib.h>
#include "../libduck/libduck.h"

#define SHAKE_ANGLE 45
#define SHAKE_T 300

void shake(int motor, int number)
{
     for (int j=0; j < number; j++) {
	  duck_set_position(motor, -SHAKE_ANGLE);
	  duck_delay(SHAKE_T);
	  duck_set_position(motor, SHAKE_ANGLE);
	  duck_delay(2 * SHAKE_T);
	  duck_set_position(motor, 0);
	  duck_delay(SHAKE_T);
     }
}

int main(int argc, char **argv)
{
     int option;
     const char *fname = DEFAULT_DUCK_FNAME;
     char buf[100];
     
     while ((option=getopt(argc, argv, "t:")) != -1) {
	  switch (option) {
	  case 't':
	       strncpy(buf, optarg, 100);
	       fname = buf;
	       break;
	  default:
	       printf("Usage: duck [-t TTYNAME] command\n");
	       exit(EXIT_FAILURE);
	       break;
	  }
     }

     open_duck(fname);
     configure_duck();
     
     for (int i=optind; argv[i]; i++) {
	  if (strcmp(argv[i], "set") == 0) {
	       if (argv[i+1] && argv[i+2]) {
		    int motor = atoi(argv[i+1]);
		    int angle = atoi(argv[i+2]);
		    duck_set_position(motor, angle);
		    i += 2;
	       }
	  } else if (strcmp(argv[i], "vel") == 0) {
	       if (argv[i+1] && argv[i+2]) {
		    int motor = atoi(argv[i+1]);
		    int deg_per_sec = atoi(argv[i+2]);
		    duck_set_velocity(motor, deg_per_sec);
		    i += 2;
	       }
	  } else if (strcmp(argv[i], "headshake") == 0) {
	       if (argv[i+1]) {
		    int numshakes = atoi(argv[i+1]);
		    shake(MOTOR_1, numshakes);
		    i++;
	       }
	  } else if (strcmp(argv[i], "nod") == 0) {
	       if (argv[i+1]) {
		    int numnods = atoi(argv[i+1]);
		    shake(MOTOR_2, numnods);
		    i++;
	       }
	  }
     }
     return 0;
}