OrganProject

Check-in [41508259dd]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added the code to fire the LED's. Breadboard now demonstrates stop selection end-to-end
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:41508259dd6d4a74c7db5282c8dce16d36e55521
User & Date: 200002852 2009-08-24 02:45:22
Context
2009-09-01
02:48
System integration to card reader. Reallocated pins for a more sensible layout on the RBBB in the card reader. Changed LED timings to reflect the hypothesis that there are two distinct clock phases, not a quadrature signal. Changed terminal code to pull the PS/2 connector pins actively HIGH momentarily before attaching a pullup resistor. This makes a cleaner transition on the cable and avoids spurious interrupts at the LED controller. check-in: 95f796cc4d user: 200002852 tags: trunk
2009-08-24
02:45
Added the code to fire the LED's. Breadboard now demonstrates stop selection end-to-end check-in: 41508259dd user: 200002852 tags: trunk
2009-08-23
03:03
Added serial communications (over a PS/2 [IBM AT] keyboard cable) between the LED control module and the terminal check-in: ac332e3e60 user: 200002852 tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ledcontrol/ledcontrol.pde.

3
4
5
6
7
8
9



10
11
12
13
14
15
16
...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
...
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

extern "C" void __cxa_pure_virtual() {
  while (1) {}
}

volatile unsigned long now;




/******************************************************************************
 *                                                                            *
 *                                  PS/2 PORT                                 *
 *                                                                            *
 ******************************************************************************/

/* Definitions */
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

void
ps2Init() {

  /* Set the interface to 'idle' and the input buffer to 'empty'.

  ps2State = PS2_IDLE;
  ps2InputPending = 0;
  ps2InputAddIndex = 0;
  ps2InputRemoveIndex = 0;

  /* Set the pins to high impedance with a pull-up resistor. */
................................................................................
  pinMode(PS2_DATA_PIN, INPUT);
  digitalWrite(PS2_DATA_PIN, HIGH);

  /* Associate the interrupt service routine. */

  attachInterrupt(PS2_CLOCK_INTR, ps2Interrupt, FALLING);
}























































void





































































































































































































































































































































































































setup(void)
{
  now = millis();
  Serial.begin(9600);
  ps2Init();
  delay(5);
  interrupts();


}


void
loop(void)
{
  byte b;
  now = millis();

  if (ps2GetByte(&b)) {
    Serial.print(b, BYTE);
  } else if (ps2LastError) {
    Serial.print("\nGot error: ");
    Serial.print((int) ps2LastError, HEX);
    Serial.print(" byte=");
    Serial.println((int)b, HEX);
  }


}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 2
 * End:
 */







>
>
>







 







|







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
>

>




<

<
<
<
<
<
<
<
<
<
>
>








3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
...
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729

730









731
732
733
734
735
736
737
738
739
740

extern "C" void __cxa_pure_virtual() {
  while (1) {}
}

volatile unsigned long now;

#define WAVETABLE_LEN	16
byte wavetable[16];		/* Current wavetable */

/******************************************************************************
 *                                                                            *
 *                                  PS/2 PORT                                 *
 *                                                                            *
 ******************************************************************************/

/* Definitions */
................................................................................
 *
 *-----------------------------------------------------------------------------
 */

void
ps2Init() {

  /* Set the interface to 'idle' and the input buffer to 'empty'. */

  ps2State = PS2_IDLE;
  ps2InputPending = 0;
  ps2InputAddIndex = 0;
  ps2InputRemoveIndex = 0;

  /* Set the pins to high impedance with a pull-up resistor. */
................................................................................
  pinMode(PS2_DATA_PIN, INPUT);
  digitalWrite(PS2_DATA_PIN, HIGH);

  /* Associate the interrupt service routine. */

  attachInterrupt(PS2_CLOCK_INTR, ps2Interrupt, FALLING);
}
 
/*
 ******************************************************************************
 *                                                                            *
 *			LED SYSTEM					      *
 *                                                                            *
 ******************************************************************************/

#define LED_CARD_PRESENT_PIN	8 /* Row 12 of card = 'card present' */
#define LED_PH1_PIN	       13 /* Row 8 of card = clock phase 1 */
#define LED_PH2_PIN	       14 /* Row 9 of card = clock phase 2 */
#define LED_COUNT 	       10 /* Number of LEDs being controlled */

#define LED_CLOCK_PERIOD_MS     4 /* Number of milliseconds per clock
				   * period (six clock periods per data
				   * byte) */

/* PROGRAM MEMORY */

const byte ledPins[] PROGMEM = {
  7, 6, 5, 9, 10, 11, 12,	/* 7 data bits */
  LED_PH2_PIN, LED_PH1_PIN,	/* 2 clock bits */
  LED_CARD_PRESENT_PIN		/* Card present */
};
#define LED_PH1_INDEX	8
#define LED_PH2_INDEX   7

enum LedState {
  LED_IDLE,
  LED_LAMPS_LIGHTING,		/* TODO - If coming out of zleep */
  LED_CARD_ENTERING_0,
  LED_CARD_ENTERING_1,
  LED_CARD_ENTERING_2,
  LED_CARD_ENTERING_3,
  LED_CARD_LEAVING_0,
  LED_CARD_LEAVING_1,
  LED_CARD_LEAVING_2,
  LED_CARD_LEAVING_3,
  LED_CARD_EXITING,
} ledState;			/* State of the stop loading process */
byte ledWaveIndex;		/* Index in the wave table of the current
				 * card column */
unsigned long ledLastTime;	/* Time of the last state transition */
unsigned long ledDelay;		/* Time until the next state transition */
 
/*
 *-----------------------------------------------------------------------------
 *
 * ledOff --
 *
 *	Turn off a given LED
 *
 *-----------------------------------------------------------------------------
 */

void
ledOff(byte n)			/* Number of the LED to extinguish */
{
  byte pin = pgm_read_byte(ledPins + n);
  pinMode(pin, INPUT);
  digitalWrite(pin, LOW);	/* High impedance, no pullup */
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * ledOn --
 *
 *	Turn on a given LED
 *
 *-----------------------------------------------------------------------------
 */

void
ledOn(byte n)			/* Number of the LED to light */
{
  byte pin = pgm_read_byte(ledPins + n);
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);	/* Low impedance, pull to ground */
}
 

/*
 *-----------------------------------------------------------------------------
 *
 * ledInit --
 *
 *	Turn on all the LEDs.
 *
 * The idle state of all ON allows for a card to be read in manually.
 *
 *-----------------------------------------------------------------------------
 */

#define ledWake ledInit
#define ledRemoveCard ledInit
void
ledInit()
{
  byte n;
  for (n = 0; n < LED_COUNT; ++n) {
    ledOn(n);
  }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * ledInsertCard --
 *
 *	Turn all the LEDs off as if a card had been inserted.
 *
 *-----------------------------------------------------------------------------
 */

#define ledZleep ledInsertCard
void
ledInsertCard()
{
  byte n;
  for (n = 0; n < LED_COUNT; ++n) {
    ledOff(n);
  }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * ledDataByte --
 *
 *	Write a data byte into LEDs 0-7, and light the PHASE 2 LED.
 *
 *-----------------------------------------------------------------------------
 */

void
ledDataByte(byte b)
{
  byte i;
  b |= 0x80;			/* Light the PHASE 2 led */
  for (i = 0; i <= 7; ++i) {
    if (b & 1) {
      ledOn(i);
    } else {
      ledOff(i);
    }
    b >>= 1;
  }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * ledSetStop --
 *
 *	Set a stop on the organ by blinking the LEDs as if a card had
 *	been inserted and withdrawn.
 *
 *-----------------------------------------------------------------------------
 */

void
ledSetStop()
{
  ledInsertCard();		/* Turn all the lights off as if a card
				 * had been inserted */
  ledState = LED_CARD_ENTERING_0;
  ledWaveIndex = 0;
  ledLastTime = now;
  ledDelay = 24 * LED_CLOCK_PERIOD_MS;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * ledPoll --
 *
 *	Test the time and perform the next action for the LEDs.
 *
 *-----------------------------------------------------------------------------
 */

void
ledPoll()
{
  if (ledState != LED_IDLE
      && (now - ledLastTime) > ledDelay) {
    ledDelay = LED_CLOCK_PERIOD_MS;
    switch(ledState) {
    case LED_LAMPS_LIGHTING:
      ledInsertCard();
      ledState = LED_CARD_ENTERING_1;
      ledDelay = LED_CLOCK_PERIOD_MS * 24;
      break;
    case LED_CARD_ENTERING_0:
      ledDataByte(wavetable[ledWaveIndex++]);
      ledState = LED_CARD_ENTERING_1;
      break;
    case LED_CARD_ENTERING_1:
      ledOn(LED_PH1_INDEX);
      ledState = LED_CARD_ENTERING_2;
      break;
    case LED_CARD_ENTERING_2:
      ledDataByte(0); ledOff(LED_PH2_INDEX);
      ledState = LED_CARD_ENTERING_3;
      break;
    case LED_CARD_ENTERING_3:
      ledOff(LED_PH1_INDEX);
      if (ledWaveIndex == 16) {
	ledState = LED_CARD_LEAVING_0;
	ledDelay = LED_CLOCK_PERIOD_MS * 24;
      } else {
	ledState = LED_CARD_ENTERING_0;
	ledDelay = LED_CLOCK_PERIOD_MS * 3;
      }
      break;
    case LED_CARD_LEAVING_0:
      ledOn(LED_PH1_INDEX);
      ledState = LED_CARD_LEAVING_1;
      break;
    case LED_CARD_LEAVING_1:
      ledDataByte(wavetable[--ledWaveIndex]);
      ledState = LED_CARD_LEAVING_2;
      break;
    case LED_CARD_LEAVING_2:
      ledOff(LED_PH1_INDEX);
      ledState = LED_CARD_LEAVING_3;
      break;
    case LED_CARD_LEAVING_3:
      ledDataByte(0); ledOff(LED_PH2_INDEX);
      if(ledWaveIndex == 0) {
	ledState = LED_CARD_EXITING;
	ledDelay = LED_CLOCK_PERIOD_MS * 24;
      } else {
	ledState = LED_CARD_LEAVING_0;
	ledDelay = LED_CLOCK_PERIOD_MS * 3;
      }
      break;
    case LED_CARD_EXITING:
      ledRemoveCard();
      ledState = LED_IDLE;
      break;
    case LED_IDLE:
      /* can't happen */
      break;
    }
    ledLastTime = now;
  }
}
		   
/******************************************************************************
 *                                                                            *
 *			COMMAND SYSTEM					      *
 *                                                                            *
 ******************************************************************************/

#define CP_COMMAND_TIME_LIMIT	250 /* Milliseconds allowed for a complete
				     * command to arrive over the PS/2 port */

/* RAM */

/* State of command processing */

enum cpState {
  CP_IDLE,			/* ┴waiting another command */
  CP_TEST,			/* Seen 'T'est - a no-operation */
  CP_STOP,			/* Seen 'S'top - set the stop */
  CP_WAKE,			/* Seen 'W'ake - turn on the lights */
  CP_ZLEEP,			/* Seen 'Z'leep - turn off the lights */
  CP_ERROR,			/* Seen something else - an error */
} cpState;

unsigned long cpTimer;		/* Time the first byte of a command arrived */

byte cpWaveIndex;		/* Which byte of a wavetable are we on? */
byte cpHexit;			/* Which hexit within the byte are we on? */
byte cpWavetable[WAVETABLE_LEN];
				/* Wavetable being accumulated */
 
/*
 *-----------------------------------------------------------------------------
 *
 * cpInit --
 *
 *	Initialize command processing.
 *
 * Sets the command processing state to IDLE
 *
 *-----------------------------------------------------------------------------
 */

void
cpInit(void)
{
  cpState = CP_IDLE;
  cpTimer = now;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * cpAction --
 *
 *	Dispatch an action once a complete command has been received over
 *	the PS/2 interface.
 *
 *-----------------------------------------------------------------------------
 */

void
cpAction() {
  byte i;
  switch(cpState) {
  case CP_IDLE:
    break;
  case CP_TEST:
    break;
  case CP_STOP:
    Serial.print("Saw "); Serial.print(cpWaveIndex, DEC);
    Serial.println(" bytes of wavetable.");
    if (cpWaveIndex != WAVETABLE_LEN) {
      cpState = CP_IDLE;		/* Wavetable too short */
    } else {
      for (i = 0; i < WAVETABLE_LEN; ++i) {
	wavetable[i] = cpWavetable[i];
      }
      ledSetStop();
    }
    break;
  case CP_WAKE:
    Serial.println("Should wake up here."); break;
  case CP_ZLEEP:
    Serial.println("Should zleep here."); break;
  default:
    Serial.println("Unrecognized command."); break;
  }
  cpState = CP_IDLE;
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * cpAccumStop --
 *
 *	Accumulate one hex digit toward having a complete wavetable for a
 *	stop to be programmed.
 *
 *-----------------------------------------------------------------------------
 */

void
cpAccumStop(byte b)		/* Hex digit that has arrived */
{
  byte hexit = 0;

  /* Make sure wavetable has not overflowed. */
  if (cpWaveIndex >= WAVETABLE_LEN) {
    cpState = CP_ERROR;		/* Error out if the wavetable overflows */
  } else {

    /* Parse a hexadecimal digit */
    if (b >= '0' && b <= '9') {
      hexit = b - '0';
    } else if (b >= 'A' && b <= 'F') {
      hexit = b - 'A' + 0xA;
    } else if (b >= 'a' && b <= 'f') {
      hexit = b - 'a' + 0xa;
    } else {
      cpState = CP_ERROR;	/* Error out if an invalid hexit is seen */
    }

    /* Add the hexadecimal digit to the wavetable */
    if (cpState == CP_STOP) {
      if (cpHexit) {
	cpWavetable[cpWaveIndex] |= hexit;
	cpHexit = 0;
	cpWaveIndex++;
      } else if (hexit >= 0x8) {
	/* value exceeds 7 bits. */
	cpState = CP_ERROR;
      } else {
	cpWavetable[cpWaveIndex] = hexit << 4;
	cpHexit = 1;
      }
    }
  }
}
 
/*
 *-----------------------------------------------------------------------------
 *
 * cpPoll --
 *
 *	Polls the PS/2 port for a command.  When the newline following the
 *	command arrives, executes the command.
 *
 *-----------------------------------------------------------------------------
 */

void
cpPoll(void)
{
  byte b;
  if (ps2GetByte(&b)) {
    Serial.print(b, BYTE);
    if (b == '\n') {
      cpAction();
    } else if (cpState == CP_STOP) {
      cpAccumStop(b);
    } else if (cpState == CP_IDLE) {
      switch (b) {
      case 'T': 
	cpState = CP_TEST;
	break;
      case 'S':
	cpState = CP_STOP;
	cpWaveIndex = 0;
	cpHexit = 0;
	break;
      case 'W':
	cpState = CP_WAKE;
	break;
      case 'Z':
	cpState = CP_ZLEEP;
	break;
      default:
	cpState = CP_ERROR;
	break;
      }
      cpTimer = now;
    }
  } else if (ps2LastError) {
    /* Ignore comm errors for now */
    Serial.print("\nGot error: ");
    Serial.print((int) ps2LastError, HEX);
    Serial.print(" byte=");
    Serial.println((int)b, HEX);
    cpState = CP_IDLE;
  } else if (cpState != CP_IDLE && now > cpTimer + CP_COMMAND_TIME_LIMIT) {
    Serial.println("\nCommand timed out.");
    cpState = CP_IDLE;
  }
}

void
setup(void)
{
  now = millis();
  Serial.begin(9600);
  ps2Init();
  delay(5);
  interrupts();
  cpInit();
  ledInit();
}


void
loop(void)
{

  now = millis();









  cpPoll();
  ledPoll();
}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 2
 * End:
 */