summaryrefslogtreecommitdiff
path: root/2003/i/buzz/src/busp/test_busp.cc
blob: 3ca65e5e935a8d6a5f031c642c85771bf62a0451 (plain)
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
// test_busp.cc
// buzz - Programme du robot Efrei Robotique I1-I2 2003
// Copyright (C) 2003 Nicolas Schodet
//
#include "busp.h"
#include "erreur/erreur.h"

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>

// Mode de controle des servos.
void testServos (void);

// Mode capteurs.
void testCapteurs (void);

int
main (int argc, char **argv)
{
    try
      {
	Busp busp;
	int addr, data;
	switch (argc)
	  {
	  case 3:
	    // Ecriture.
	    addr = strtol (argv[1], 0, 0);
	    data = strtol (argv[2], 0, 0);
	    busp.cli ();
	    busp.write (addr, data);
	    busp.sti ();
	    break;
	  case 2:
	    if (strcmp (argv[1], "irq") == 0)
	      {
		// Attend des interuptions.
		cout << "irq" << endl;
		char c;
		cin >> c;
	      }
	    else if (strcmp (argv[1], "servo") == 0)
	      {
		// Passe en mode contr�le servos.
		testServos ();
	      }
	    else if (strcmp (argv[1], "capteurs") == 0)
	      {
		// Passe en mode capteurs.
		testCapteurs ();
	      }
	    else if (strcmp (argv[1], "reset") == 0)
	      {
		// Ne fait rien, juste reset.
		busp.reset ();
		cout << "reset" << endl;
	      }
	    else if (strcmp (argv[1], "gps") == 0)
	      {
		// Lit une trame sur le GPS.
		busp.getGPS ().read ();
		busp.getGPS ().dump ();
	      }
	    else
	      {
		// Lecture.
		addr = strtol (argv[1], 0, 0);
		busp.cli ();
		busp.read (addr);
		busp.sti ();
	      }
	    break;
	  default:
	    if (argc >= 4 && strcmp (argv[1], "servo") == 0 && argc % 2 == 0)
	      {
		for (int i = 2; i < argc; i += 2)
		  {
		    busp.getServo ().moveTo (atoi (argv[i]),
					     atoi (argv[i + 1]));
		  }
	      }
	    if (argc >= 4 && strcmp (argv[1], "actionneurs") == 0 && argc % 2
		== 0)
	      {
		if (strcmp (argv[2], "all") == 0)
		  {
		    busp.getActionneurs ().setAll (atoi (argv[3]),
						   atoi (argv[4]));
		  }
		else
		  {
		    for (int i = 2; i < argc; i += 2)
		      {
			busp.getActionneurs ().set (atoi (argv[i]),
						    atoi (argv[i + 1]));
		      }
		  }
	      }
	    else
	      {
		cerr << argv[0] << ": teste le bus parall�le." << endl
		    << "\t" << argv[0] << " <adresse> [<donn�e>]" << endl
		    << "\t" << argv[0] << " reset" << endl
		    << "\t" << argv[0] << " irq" << endl
		    << "\t" << argv[0] << " capteurs" << endl
		    << "\t" << argv[0] << " gps" << endl
		    << "\t" << argv[0] << " actionneurs [<n> <val>...]" << endl
		    << "\t" << argv[0] << " actionneurs all <set> <reset>" <<
		    endl
		    << "\t" << argv[0] << " servo [<servo> <pos>...]" << endl;
	      }
	    return 1;
	  }
      }
    catch (const std::exception &e)
      {
	cerr << e.what ();
	return 1;
      }
    return 0;
}

// Mode de controle des servos.
void
testServos (void)
{
    char c;
    int diff, servo = 0;
    cout << "Mode servos\n"
	"0-9 : choix du servo.\n"
	"y : -1, h : +1, u : -5, j : +5, i : -20, k : +20\n"
	"r : reset, g<pos> : goto <pos>\n"
	"p<pos> : position pr�programm�e\n";
    while (!cin.eof ())
      {
	cin >> c;
	diff = 0;
	switch (c)
	  {
	  case '0':
	  case '1':
	  case '2':
	  case '3':
	  case '4':
	  case '5':
	  case '6':
	  case '7':
	    servo = c - '0';
	    break;
	  case 'y':
	    diff = -1;
	    break;
	  case 'h':
	    diff = 1;
	    break;
	  case 'u':
	    diff = -5;
	    break;
	  case 'j':
	    diff = 5;
	    break;
	  case 'i':
	    diff = -20;
	    break;
	  case 'k':
	    diff = 20;
	    break;
	  }
	if (diff)
	  {
	    Busp::getInstance ().getServo ().move (servo, diff);
	  }
	else if (c == 'g')
	  {
	    int pos;
	    cin >> pos;
	    Busp::getInstance ().getServo ().moveTo (servo, pos);
	  }
	else if (c == 'p')
	  {
	    int pos;
	    cin >> pos;
	    Busp::getInstance ().getServo ().position (pos);
	  }
	else if (c == 'r')
	  {
	    Busp::getInstance ().reset ();
	  }
      }
}

// Mode capteurs.
void
testCapteurs (void)
{
    while (1)
      {
	Busp::getInstance ().getCapteurs ().dump ();
	cout << '\r';
	cout.flush ();
	sleep (1);
      }
}