duck.c 2.31 KB
Newer Older
jp7g21's avatar
jp7g21 committed
1
2
3
4
5
6
7
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <stdlib.h>
#include "../libduck/libduck.h"

jp7g21's avatar
jp7g21 committed
8
#define SHAKE_ANGLE 90
jp7g21's avatar
jp7g21 committed
9
10
#define SHAKE_T 300

jp7g21's avatar
jp7g21 committed
11
void shake(int motor, int number, int min, int max, int delay)
jp7g21's avatar
jp7g21 committed
12
{
jp7g21's avatar
jp7g21 committed
13
14
15
16
17
18
19
20
21
22
23
     int delay1 = (delay * (-min)) / (max - min);
     int delay2 = delay - delay1;
     
     duck_set_position(motor, min);
     duck_delay(delay1);
     
     for (int j=0; j < number-1; j++) {
	  duck_set_position(motor, max);
	  duck_delay(delay);
	  duck_set_position(motor, min);
	  duck_delay(delay);
jp7g21's avatar
jp7g21 committed
24
     }
jp7g21's avatar
jp7g21 committed
25
26
27

     duck_set_position(motor, 0);
     duck_delay(delay2);
jp7g21's avatar
jp7g21 committed
28
29
}

jp7g21's avatar
stuff    
jp7g21 committed
30
31
int read_mode = 0;

jp7g21's avatar
jp7g21 committed
32
33
34
35
36
37
int main(int argc, char **argv)
{
     int option;
     const char *fname = DEFAULT_DUCK_FNAME;
     char buf[100];
     
jp7g21's avatar
stuff    
jp7g21 committed
38
     while ((option=getopt(argc, argv, "+t:dr")) != -1) {
jp7g21's avatar
jp7g21 committed
39
	  switch (option) {
jp7g21's avatar
jp7g21 committed
40
41
42
	  case 'd':
	       duck_debug_mode = 1;
	       break;
jp7g21's avatar
stuff    
jp7g21 committed
43
44
45
	  case 'r':
	       read_mode = 1;
	       break;
jp7g21's avatar
jp7g21 committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	  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) {
jp7g21's avatar
stuff    
jp7g21 committed
69
	       if (argv[i+1] && argv[i+2] && argv[i+3]) {
jp7g21's avatar
jp7g21 committed
70
71
		    int motor = atoi(argv[i+1]);
		    int deg_per_sec = atoi(argv[i+2]);
jp7g21's avatar
stuff    
jp7g21 committed
72
73
74
		    int ms = atoi(argv[i+3]);
		    duck_set_velocity(motor, deg_per_sec, ms);
		    i += 3;
jp7g21's avatar
jp7g21 committed
75
76
77
78
	       }
	  } else if (strcmp(argv[i], "headshake") == 0) {
	       if (argv[i+1]) {
		    int numshakes = atoi(argv[i+1]);
jp7g21's avatar
jp7g21 committed
79
		    shake(MOTOR_3, numshakes, -90, 90, 250);
jp7g21's avatar
jp7g21 committed
80
81
82
83
84
		    i++;
	       }
	  } else if (strcmp(argv[i], "nod") == 0) {
	       if (argv[i+1]) {
		    int numnods = atoi(argv[i+1]);
jp7g21's avatar
jp7g21 committed
85
86
87
88
89
90
91
		    shake(MOTOR_2, numnods, -90, 0, 250);
		    i++;
	       }
	  } else if (strcmp(argv[i], "facepalm") == 0) {
	       if (argv[i+1]) {
		    int numnods = atoi(argv[i+1]);
		    shake(MOTOR_2, numnods, -90, 0, 250);
jp7g21's avatar
jp7g21 committed
92
93
94
95
		    i++;
	       }
	  }
     }
jp7g21's avatar
stuff    
jp7g21 committed
96
     if (read_mode) {
jp7g21's avatar
jp7g21 committed
97
98
	  read_duck_to_stdout();
     }
jp7g21's avatar
stuff    
jp7g21 committed
99
     close_duck();
jp7g21's avatar
jp7g21 committed
100
101
102
     return 0;
}