aboutsummaryrefslogtreecommitdiff
path: root/demokit/demokit.pde
blob: 69e75bca616da057eabe128fd85b4bf0a9cd29bc (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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include <Max3421e.h>
#include <Usb.h>
#include <Wire.h>
#include <Servo.h>

#define USB_ACCESSORY_VENDOR_ID 0x18D1
#define USB_ACCESSORY_PRODUCT_ID 0x2D00

#define USB_ACCESSORY_ADB_PRODUCT_ID 0x2D01
#define ACCESSORY_STRING_MANUFACTURER 0
#define ACCESSORY_STRING_MODEL 1
#define ACCESSORY_STRING_DESCRIPTION 2
#define ACCESSORY_STRING_VERSION 3
#define ACCESSORY_STRING_URI 4
#define ACCESSORY_STRING_SERIAL 5

#define ACCESSORY_GET_PROTOCOL 51
#define ACCESSORY_SEND_STRING 52
#define ACCESSORY_START 53


#define  LED3_RED       2
#define  LED3_GREEN     4
#define  LED3_BLUE      3

#define  LED2_RED       5
#define  LED2_GREEN     7
#define  LED2_BLUE      6

#define  LED1_RED       8
#define  LED1_GREEN     10
#define  LED1_BLUE      9

#define  SERVO1         11
#define  SERVO2         12
#define  SERVO3         13

#define  TOUCH          14

#define  RELAY1         A0
#define  RELAY2         A1

#define  LIGHT_SENSOR   A2
#define  TEMP_SENSOR    A3

#define  BUTTON1        A6
#define  BUTTON2        A7
#define  BUTTON3        A8

#define  JOY_SWITCH     A9      // pulls line down when pressed
#define  JOY_nINT       A10     // active low interrupt input
#define  JOY_nRESET     A11     // active low reset output


MAX3421E Max;
USB Usb;
Servo servos[3];


void setup();
void loop();

uint8_t usbBuff[256];


void init_buttons()
{
	pinMode( BUTTON1, INPUT );
	pinMode( BUTTON2, INPUT );
	pinMode( BUTTON3, INPUT );

	digitalWrite( BUTTON1, HIGH );  // enable the internal pullups
	digitalWrite( BUTTON2, HIGH );
	digitalWrite( BUTTON3, HIGH );
}


void init_relays()
{
	pinMode( RELAY1, OUTPUT );
	pinMode( RELAY2, OUTPUT );
}


void init_leds()
{
	digitalWrite( LED1_RED,   1 );
	digitalWrite( LED1_GREEN, 1 );
	digitalWrite( LED1_BLUE,  1 );

	pinMode( LED1_RED,    OUTPUT );
	pinMode( LED1_GREEN,  OUTPUT );
	pinMode( LED1_BLUE,   OUTPUT );

	digitalWrite( LED2_RED,   1 );
	digitalWrite( LED2_GREEN, 1 );
	digitalWrite( LED2_BLUE,  1 );

	pinMode( LED2_RED,    OUTPUT );
	pinMode( LED2_GREEN,  OUTPUT );
	pinMode( LED2_BLUE,   OUTPUT );

	digitalWrite( LED3_RED,   1 );
	digitalWrite( LED3_GREEN, 1 );
	digitalWrite( LED3_BLUE,  1 );

	pinMode( LED3_RED,    OUTPUT );
	pinMode( LED3_GREEN,  OUTPUT );
	pinMode( LED3_BLUE,   OUTPUT );
}

void init_joystick( int threshold );

void setup()
{
	Serial.begin( 115200 );
	Serial.print("\r\nStart");

	init_leds();
	init_relays();
	init_buttons();
	init_joystick( 5 );      // initialize with thresholding enabled, dead zone of 5 units  


	servos[0].attach(SERVO1);
	servos[0].write(90);
	servos[1].attach(SERVO2);
	servos[1].write(90);
	servos[2].attach(SERVO3);
	servos[2].write(90);

	Max.powerOn();
	delay( 200 );
}

bool isAndroidVendor(USB_DEVICE_DESCRIPTOR *desc)
{
	return desc->idVendor == 0x18d1 || desc->idVendor == 0x22B8;
}

bool isAccessoryDevice(USB_DEVICE_DESCRIPTOR *desc)
{
	return desc->idProduct == 0x2D00 || desc->idProduct == 0x2D01;
}

int getProtocol(byte addr)
{
        uint16_t protocol = -1;
        Usb.ctrlReq(addr, 0, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE,
		    ACCESSORY_GET_PROTOCOL, 0, 0, 0, 2, (char *)&protocol);
        return protocol;
}

void sendString(byte addr, int index, char *str)
{
	Usb.ctrlReq(addr, 0, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE,
		    ACCESSORY_SEND_STRING, 0, 0, index, strlen(str) + 1, str);

}

bool switchDevice(byte addr)
{
        int protocol = getProtocol(addr);
        if (protocol == 1)
                Serial.print("device supports protcol 1\n");
        else {
                Serial.print("could not read device protocol version\n");
                return false;
        }
	sendString(addr, ACCESSORY_STRING_MANUFACTURER, "Google, Inc.");
	sendString(addr, ACCESSORY_STRING_MODEL, "DemoKit");
	sendString(addr, ACCESSORY_STRING_DESCRIPTION, "DemoKit test board");
	sendString(addr, ACCESSORY_STRING_VERSION, "1.0");
	sendString(addr, ACCESSORY_STRING_URI, "http://www.android.com");
	sendString(addr, ACCESSORY_STRING_SERIAL, "0000000012345678");

	Usb.ctrlReq(addr, 0, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE,
		    ACCESSORY_START, 0, 0, 0, 0, NULL);
        return true;
}

bool findEndpoints(byte addr, EP_RECORD *inEp, EP_RECORD *outEp)
{
	int len;
	byte err;
	uint8_t *p;

	err = Usb.getConfDescr(addr, 0, 4, 0, (char *)usbBuff);
	if (err) {
		Serial.print("Can't get config descriptor length\n");
		return false;
	}

	len = usbBuff[2] | ((int)usbBuff[3] << 8);
	Serial.print("Config Desc Length: ");
	Serial.println(len, DEC);
	if (len > sizeof(usbBuff)) {
		Serial.print("config descriptor too large\n");
		/* might want to truncate here */
		return false;
	}

	err = Usb.getConfDescr(addr, 0, len, 0, (char *)usbBuff);
	if (err) {
		Serial.print("Can't get config descriptor\n");
		return false;
	}

	p = usbBuff;
	inEp->epAddr = 0;
	outEp->epAddr = 0;
	while (p < (usbBuff + len)){
		uint8_t descLen = p[0];
		uint8_t descType = p[1];
		USB_ENDPOINT_DESCRIPTOR *epDesc;
		EP_RECORD *ep;

		switch (descType) {
		case USB_DESCRIPTOR_CONFIGURATION:
			Serial.print("config desc\n");
			break;

		case USB_DESCRIPTOR_INTERFACE:
			Serial.print("interface desc\n");
			break;

		case USB_DESCRIPTOR_ENDPOINT:
			epDesc = (USB_ENDPOINT_DESCRIPTOR *)p;
			if (!inEp->epAddr && (epDesc->bEndpointAddress & 0x80))
				ep = inEp;
			else if (!outEp->epAddr)
				ep = outEp;
			else
				ep = NULL;

			if (ep) {
				ep->epAddr = epDesc->bEndpointAddress & 0x7f;
				ep->Attr = epDesc->bmAttributes;
				ep->MaxPktSize = epDesc->wMaxPacketSize;
				ep->sndToggle = bmSNDTOG0;
				ep->rcvToggle = bmRCVTOG0;
			}
			break;

		default:
			Serial.print("unkown desc type ");
			Serial.println( descType, HEX);
			break;
		}

		p += descLen;
	}

	return inEp->epAddr && outEp->epAddr;
}

EP_RECORD ep_record[ 8 ];  //endpoint record structure for the mouse


void doAndroid(void)
{
	byte err;
	byte idle;
	byte b1, b2, b3, c;
	EP_RECORD inEp, outEp;
	byte count = 0;

	if (findEndpoints(1, &inEp, &outEp)) {

		ep_record[inEp.epAddr] = inEp;
		if (outEp.epAddr != inEp.epAddr)
			ep_record[outEp.epAddr] = outEp;

		Serial.print("inEp: ");
		Serial.println(inEp.epAddr, HEX);
		Serial.print("outEp: ");
		Serial.println(outEp.epAddr, HEX);

		ep_record[0] = *(Usb.getDevTableEntry(0,0));
		Usb.setDevTableEntry(1, ep_record);

		err = Usb.setConf( 1, 0, 1 );
		if (err)
			Serial.print("Can't set config to 1\n");

		Usb.setUsbTaskState( USB_STATE_RUNNING );

		b1 = digitalRead(BUTTON1);
		b2 = digitalRead(BUTTON2);
		b3 = digitalRead(BUTTON3);
		c = captouched();

		while(1) {
			int len = Usb.newInTransfer(1, inEp.epAddr, sizeof(usbBuff),
						    (char *)usbBuff, 1);
			int i;
			byte b;
			byte msg[3];
			msg[0] = 0x1;

			if (len > 0) {
				// XXX: assumes only one command per packet
				Serial.print(usbBuff[0], HEX);
				Serial.print(":");
				Serial.print(usbBuff[1], HEX);
				Serial.print(":");
				Serial.println(usbBuff[2], HEX);
				if (usbBuff[0] == 0x2) {
					if (usbBuff[1] == 0x0)
						analogWrite( LED1_RED, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x1)
						analogWrite( LED1_GREEN, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x2)
						analogWrite( LED1_BLUE, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x3)
						analogWrite( LED2_RED, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x4)
						analogWrite( LED2_GREEN, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x5)
						analogWrite( LED2_BLUE, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x6)
						analogWrite( LED3_RED, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x7)
						analogWrite( LED3_GREEN, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x8)
						analogWrite( LED3_BLUE, 255 - usbBuff[2]);
					else if (usbBuff[1] == 0x10)
						servos[0].write(map(usbBuff[2], 0, 255, 0, 180));
					else if (usbBuff[1] == 0x11)
						servos[1].write(map(usbBuff[2], 0, 255, 0, 180));
					else if (usbBuff[1] == 0x12)
						servos[2].write(map(usbBuff[2], 0, 255, 0, 180));
				} else if (usbBuff[0] == 0x3) {
					if (usbBuff[1] == 0x0)
						digitalWrite( RELAY1, usbBuff[2] ? HIGH : LOW );
					else if (usbBuff[1] == 0x1)
						digitalWrite( RELAY2, usbBuff[2] ? HIGH : LOW );

				}

//				for (i = 0; i < len; i++)
//				Serial.print('\n');
			}

			b = digitalRead(BUTTON1);
			if (b != b1) {
				msg[1] = 0;
				msg[2] = b ? 0 : 1;
				Usb.outTransfer(1, outEp.epAddr, 3, (char *)msg);
				b1 = b;
			}

			b = digitalRead(BUTTON2);
			if (b != b2) {
				msg[1] = 1;
				msg[2] = b ? 0 : 1;
				Usb.outTransfer(1, outEp.epAddr, 3, (char *)msg);
				b2 = b;
			}

			b = digitalRead(BUTTON3);
			if (b != b3) {
				msg[1] = 2;
				msg[2] = b ? 0 : 1;
				Usb.outTransfer(1, outEp.epAddr, 3, (char *)msg);
				b3 = b;
			}

			if ((count++ % 16) == 0) {
				uint16_t val;
				int x, y;

				val = analogRead(TEMP_SENSOR);
				msg[0] = 0x4;
				msg[1] = val >> 8;
				msg[2] = val & 0xff;
				Usb.outTransfer(1, outEp.epAddr, 3, (char *)msg);

				val = analogRead(LIGHT_SENSOR);
				msg[0] = 0x5;
				msg[1] = val >> 8;
				msg[2] = val & 0xff;
				Usb.outTransfer(1, outEp.epAddr, 3, (char *)msg);

				read_joystick(&x, &y);
				msg[0] = 0x6;
				msg[1] = constrain(x, -128, 127);
				msg[2] = constrain(y, -128, 127);
				Usb.outTransfer(1, outEp.epAddr, 3, (char *)msg);

				char c0 = captouched();
				if (c0 != c) {
					msg[0] = 0x1;
					msg[1] = 3;
					msg[2] = c0 ? 0 : 1;
					Usb.outTransfer(1, outEp.epAddr, 3, (char *)msg);
					c = c0;
				}
			}

			delay(10);

		}

	}

}


void loop()
{
	USB_DEVICE_DESCRIPTOR *devDesc = (USB_DEVICE_DESCRIPTOR *) usbBuff;
	byte err;

	Max.Task();
	Usb.Task();
	if( Usb.getUsbTaskState() >= USB_STATE_CONFIGURING ) {
		Serial.print("\nDevice addressed... ");
		Serial.print("Requesting device descriptor.");

		err = Usb.getDevDescr(1, 0, 0x12, (char *) devDesc);
		if (err) {
			Serial.print("\nDevice descriptor cannot be retrieved. Program Halted\n");
			while(1);
		}

		if (isAndroidVendor(devDesc)) {
			Serial.print("found android device\n");

			if (isAccessoryDevice(devDesc)) {
				Serial.print("found android acessory device\n");
				doAndroid();
			} else {
				Serial.print("found possible device. swithcing to serial mode\n");
				switchDevice(1);
			}
		}

		while (Usb.getUsbTaskState() != USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) {
			Max.Task();
			Usb.Task();


		}

		Serial.print("detached\n");

	}

}

// ==============================================================================
// Austria Microsystems i2c Joystick

/*
  If a threshold is provided, the dead zone will be programmed such that interrupts will not
  be generated unless the threshold is exceeded.

  Note that if you use that mode, you will have to use passage of time with no new interrupts
  to detect that the stick has been released and has returned to center.
  
  If you need to explicitly track return to center, pass 0 as the threshold.  "Center" will
  still bounce around a little 
*/


void init_joystick( int threshold )
{
  byte status = 0;
  
  pinMode( JOY_SWITCH, INPUT );
  digitalWrite( JOY_SWITCH, HIGH );    // enable the internal pullup
  
  pinMode( JOY_nINT, INPUT );
  digitalWrite( JOY_nINT, HIGH );      // enable the internal pullup

  pinMode( JOY_nRESET, OUTPUT );

  digitalWrite( JOY_nRESET, 1 );
  delay(1);
  digitalWrite( JOY_nRESET, 0 );
  delay(1);
  digitalWrite( JOY_nRESET, 1 );

  Wire.begin();
  
  do {
    status = read_joy_reg( 0x0f );        // XXX need timeout
  } while ((status & 0xf0) != 0xf0);
  
  write_joy_reg( 0x2e, 0x86 );            // invert magnet polarity setting, per datasheet

  calibrate_joystick( threshold );        // calibrate & set up dead zone area  
}


int offset_X, offset_Y;

void calibrate_joystick( int dz )
{
  char iii;
  int x_cal = 0;
  int y_cal = 0;

  write_joy_reg( 0x0f, 0x00 );          // Low Power Mode, 20ms auto wakeup
                                        // INTn output enabled
                                        // INTn active after each measurement
                                        // Normal (non-Reset) mode
  delay(1);

  read_joy_reg( 0x11 );                 // dummy read of Y_reg to reset interrupt

  for( iii = 0; iii != 16; iii++ ) {    // read coords 16 times & average 
    while( !joystick_interrupt() )      // poll for interrupt
      ;
    x_cal += read_joy_reg( 0x10 );      // X pos
    y_cal += read_joy_reg( 0x11 );      // Y pos
  }
  
  offset_X = -(x_cal>>4);               // divide by 16 to get average
  offset_Y = -(y_cal>>4);
  
  //sprintf(msgbuf, "offsets = %d, %d\n", offset_X, offset_Y);
  //Serial.print(msgbuf);
  
  write_joy_reg( 0x12,  dz - offset_X );  // Xp, LEFT threshold for INTn
  write_joy_reg( 0x13, -dz - offset_X );  // Xn, RIGHT threshold for INTn
  write_joy_reg( 0x14,  dz - offset_Y );  // Yp, UP threshold for INTn
  write_joy_reg( 0x15, -dz - offset_Y );  // Yn, DOWN threshold for INTn

  if ( dz )                             // dead zone threshold detect requested?
    write_joy_reg( 0x0f, 0x04 );          // Low Power Mode, 20ms auto wakeup
                                          // INTn output enabled
                                          // INTn active when movement exceeds dead zone
                                          // Normal (non-Reset) mode
}


void read_joystick( int *x, int *y )
{
  *x = read_joy_reg( 0x10 ) + offset_X;
  *y = read_joy_reg( 0x11 ) + offset_Y;  // reading Y clears the interrupt
}

char joystick_interrupt()
{
  return ( digitalRead( JOY_nINT ) == 0 ); 
}


#define  JOY_I2C_ADDR    0x40

char read_joy_reg( char reg_addr )
{
  char c;
  
  Wire.beginTransmission( JOY_I2C_ADDR );
  Wire.send( reg_addr );
  Wire.endTransmission();
  
  Wire.requestFrom( JOY_I2C_ADDR, 1 );
  
  while(Wire.available())
    c = Wire.receive();
  
  return c;
}

void write_joy_reg( char reg_addr, char val )
{
  Wire.beginTransmission( JOY_I2C_ADDR );
  Wire.send( reg_addr );
  Wire.send( val );
  Wire.endTransmission();  
}

/* Capacitive touch technique from Mario Becker, Fraunhofer IGD, 2007 http://www.igd.fhg.de/igd-a4 */

char captouched() 
{
  char iii, jjj, retval;
  
  retval = 0;
  
  for( jjj = 0; jjj != 10; jjj++ ) {
    delay( 10 );
  
    pinMode( TOUCH, INPUT );
    digitalWrite( TOUCH, HIGH );
  
    for ( iii = 0; iii <  16; iii++ )
      if( digitalRead( TOUCH ) )
        break;
      
    digitalWrite( TOUCH, LOW );
    pinMode( TOUCH, OUTPUT );
  
    retval += iii;
  }
  
  return retval;
}