STATEMENT: We are currently undecided on the release of this document.


Schematics

The Amplifier Schematic

Isolating Solenoid Schematic

Bill Of Materials

Part Soursce Unit Cost Quantity Total Cost
MicroStickII Lab Stock $10 1 $10
White Breadboard Lab Stock $6 2 $12
Power Supply Lab Stock $2 1 $2
TFT LCD Lab Stock $10 1 $10
Lab Speakers Lab Stock $2 1 $2
Solenoid Sparkfun $4.95 1 $4.95
FET Lab Stock $1.50 1 $1.50
Diode Lab Stock $0.25 1 $0.25
Isolator (4N35) Lab Stock $0.60 1 $0.60
Audio Amp (LM386) Lab Stock $0.98 1 $0.98
Resistors Lab Stock $0.25 8 $2.00
Capacitors Lab Stock $0.25 5 $1.25
Potentiometer Lab Stock $1 1 $1
Piezo Microphone/td> Lab Stock $2 1 $2
Jumper Cables Lab Stock $0.10 3 $0.30
Pushbutton Lab Stock $0.70 1 $0.70
Total $51.53


References

http://www.learnmorsecode.com/
Microphone Amplifying Circuitry on Learn about electronics
Cornell ECE4760 Homepage


Work Distribution

Brendon Jackson Cheuk Tse Joseph Featherston
Created Morse tree header file Built microphone amplifier circuit Implemented IIR filtering
Implemented Morse code decoding Helped implement Morse code decoding Created Matlab program for testing system
Helped integrate button input with Morse code decoding Helped integrate button input with Morse code decoding Tested IIR filter and general microphone Morse code input
Built solenoid circuit Integrated microphone input with Morse code decoding Parts ordering/component selection
Responsible for site design Implemented final TFT screen code Helped write Implementation section of lab report
Wrote Introduction section of lab report Helped write Implementation section of lab report Wrote Testing section of lab report
Wrote High Level Design section of lab report Wrote Takeaway (Conclusion) section of lab report
Edited code in Appendix


Program Listing

Download all the code
Go to the testing program


Main.c

  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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#include "config.h"
#include "pt_cornell_1_2_1.h"
#include <math.h>

#include "tft_master.h"
#include "tft_gfx.h"
#include <stdlib.h>
#include <stdint.h>

#include <plib.h>
#include <xc.h>

#include "morseTree.h"


char buffer[60];
char buzz_buffer[60];
char butt_buffer[60];

static struct pt pt_physical, pt_sound, pt_lcd;

static volatile int adc;

int sys_time_seconds;

//init password
    char password[5] = "BRUCE";

float filtered;
char output;
float printout;

//RING BUFFER for IIR FILTER 
#define RB_SIZE 5
typedef struct{
    char zero;
    float data[RB_SIZE];
}ringbuf;

inline void RB_INIT(ringbuf *buffer) {
    static int k;
    buffer->zero = 0;
    for(k = 0; k<RB_SIZE; k++){
        buffer->data[k] = 0;
    }
}

inline void RB_ADD(ringbuf *buffer, float a) {
    buffer->zero++;
    buffer->zero = buffer->zero == RB_SIZE ? 0 : buffer->zero;
    buffer->data[buffer->zero] = a;
}
//index is [0,RB_SIZE-1] and refers to number of samples in the past
inline float RB_GET(ringbuf *buffer, char index){  
    return buffer->data[(buffer->zero-index+RB_SIZE)%RB_SIZE];
}
//IIR Filter

 float input_coef[RB_SIZE] = {.0000630,0,-.000126,0,0.000063};
 float output_coef[RB_SIZE] = {1.0,-3.7573,5.5117,-3.7243,.9825};

 
ringbuf inputs;
ringbuf outputs;

float iir_filt(float new_value, float *input_coef, float *output_coef, ringbuf *inputs, ringbuf *outputs){
    static int i;
    static float sum;
    
    RB_ADD(inputs, new_value);
    sum = 0;
    for(i=0;i<RB_SIZE;i++){
        sum += input_coef[i]*RB_GET(inputs, i);
    }
    printout = RB_GET(outputs, 2);
    //printout = sum;
    for(i=1;i<RB_SIZE;i++){
        sum -= output_coef[i]*RB_GET(outputs, i-1);
    }
    RB_ADD(outputs, sum);
    return sum;
}

#define THRESHOLD 20
char filter_threshold(float value){
    static char count = 0;
    static char state = 0;
    
    if(state){
        if(value < THRESHOLD && value > -THRESHOLD){
            count++;
            if(count > 5){
               count = 0;
               state = 0;
            }
        }
        else{
            count=0;
        }
    }
    else{
        if(value > THRESHOLD || value < -THRESHOLD){
            count++;
            if(count > 5){
               count = 0;
               state = 1;
            }
        }
        else{
            count = 0;
        }
    }


//lights up LED if state = 1
    if(state){
        mPORTASetBits(BIT_0);
    }
    else{
        mPORTAClearBits(BIT_0);
    }
    return state;
}

#define MAX_SYMBOLS 50
int time1;
int time2;
int micOn;
int micLastCapture;
volatile unsigned int miclengths[MAX_SYMBOLS];
int micNextLength = 0;
char micDotDash[55];
char micPass[5];
#define DOT     '.'
#define DASH    '-'
#define CHAREND '|'
#define LOWDOT  ' '
#define NULLIN  '0'


int adc_state = 0;
#define LOW         0
#define MAYBE_HIGH  1
#define HIGH        2
#define MAYBE_LOW   3


//adc input capture handler
void __ISR(_ADC_VECTOR, ipl3) ADCHandler(void)
{
    
    adc = ReadADC10(0);
    filtered = iir_filt( (float) adc, input_coef, output_coef, &inputs, &outputs);
    output = filter_threshold(filtered);
    
//FSM for IIR filter output
    switch(adc_state){
        case LOW:
            if(output){
                adc_state = MAYBE_HIGH;
                time1 = PT_GET_TIME();
            }else{
                adc_state = LOW;
            }
            break;
        case MAYBE_HIGH:
            if(output){
                adc_state = MAYBE_HIGH;
                if(PT_GET_TIME()-time1 > 30){
                    adc_state = HIGH;
                    miclengths[micNextLength] = PT_GET_TIME() - micLastCapture;
                    micNextLength++;
                    micLastCapture = PT_GET_TIME();
                }
            }else{
                adc_state = LOW;
            }
            break;
        case HIGH:
            if(output){
                adc_state = HIGH;
            }else{
                adc_state = MAYBE_LOW;
                time2 = PT_GET_TIME();
            }
            
            break;
        case MAYBE_LOW:
            if(output){
                adc_state = HIGH;
            }else{
                adc_state = MAYBE_LOW;
                if(PT_GET_TIME()-time2 > 30){
                    adc_state = LOW;
                    micOn = PT_GET_TIME();
                    miclengths[micNextLength] = PT_GET_TIME() - micLastCapture;
                    micNextLength++;
                    micLastCapture = PT_GET_TIME();
                }
            }
            break;
        default:
            adc_state = LOW;
    }

    mAD1ClearIntFlag();
}

//distinguish between dots and dash for a given char and stores in array
void micReadMorse(){
    static int k = 0;
    static int j = 0;
    static int val;
    int rat ;
    micDotDash[0] = LOWDOT;
    
    k=1;
    j=0;
    
    int min = dotLength(miclengths);
    int max = 3*min;
    
    while(k<MAX_SYMBOLS){
        val = miclengths[k];
        if (val == 0){
            micDotDash[k] = '\0';
            break;
        }        
        //much higher tolerance
        if (k%2 == 1){
            if (val < 2*min){
                micDotDash[k] = DOT;
            }
            else{
                micDotDash[k] = DASH;
            }
        }
        else{
            if (val < 2*min){
                micDotDash[k] = LOWDOT;
            }
            else{
                micDotDash[k] = CHAREND;
            }
        }
        k++;
    }    
}

void micConvertingToString(int i, int j,  t_treeNode *a){
    if (i >= MAX_SYMBOLS)
        return;
    switch (micDotDash[i]){
        case LOWDOT: micConvertingToString(i+1, j, a); break;
        case CHAREND: micPass[j] = a->let; micConvertingToString(i+1, j+1, &treeHead); break;
        case DOT: micConvertingToString(i+1, j, a->dot); break;
        case DASH: micConvertingToString(i+1, j, a->dash); break;
        case NULLIN: micConvertingToString(i+1, j, &treeNull); break;
        case '\0':micPass[j] = '\0'; return;
    }
}


//morse stuff
volatile unsigned int lengths[MAX_SYMBOLS];
volatile char next_length = 0;

volatile unsigned int capture_buffer[16];
volatile unsigned int capture_period, capture, last_capture;
volatile unsigned int high, low, sb = 0;
int letter_read;
volatile unsigned int minPulse = 0;

char dotDash[55];
char pass[10];



#define MIN_PULSE 100
#define BORDER 50000
#define TIMEOUT 500000

//calculates dot length from input buffers
//averages out the shorter lengths
int dotLength(int array[]){
    int i, j, avg;
    avg = 99999999;
    j = 0;
    for (i = 1; i < MAX_SYMBOLS; i++){
        if (array[i] != 0 && array[i] < 2*avg){
            avg = avg*j + array[i];
            j++;
            avg = avg/j;
        }
        if (array[i] != 0 && array[i] < avg/2){
            avg = array[i];
            j = 1;
        }
    }
    if (j == 1 ){
        return 0;
    }
    return avg;
}


//distinguish between dots and dash for a given char and stores in array
void readMorse(){
    static int k = 0;
    static int j = 0;
    static int val;
    int rat ;
    dotDash[0] = LOWDOT;
    
    k=1;
    j=0;
    
    int min = dotLength(lengths);
    int max = 3*min;
    
    while(k<MAX_SYMBOLS){
        val = lengths[k];
        if (val == 0){
            dotDash[k] = '\0';
            break;
        }
        //much higher tolerance
        if (k%2 == 1){
            if (val < 2*min){
                dotDash[k] = DOT;
            }
            else{
                dotDash[k] = DASH;
            }
        }
        else{
            if (val < 2*min){
                dotDash[k] = LOWDOT;
            }
            else{
                dotDash[k] = CHAREND;
            }
        }
        k++;
    }    
}

void convertingToString(int i, int j,  t_treeNode *a){
    if (i >= MAX_SYMBOLS)
        return;
    switch (dotDash[i]){
        case LOWDOT: convertingToString(i+1, j, a); break;
        case CHAREND: pass[j] = a->let; convertingToString(i+1, j+1, &treeHead); break;
        case DOT: convertingToString(i+1, j, a->dot); break;
        case DASH: convertingToString(i+1, j, a->dash); break;
        case NULLIN: convertingToString(i+1, j, &treeNull); break;
        case '\0':pass[j] = '\0'; return;
    }
}

void __ISR(_INPUT_CAPTURE_1_VECTOR, ipl3) C1Handler(void)
{
    ReadCapture1(capture_buffer);
    capture = capture_buffer[0];
    capture_period = capture-last_capture;
    if(capture_period > 15000){
        lengths[next_length] = capture_period;
        next_length++;
        last_capture = capture;
    }    
    mIC1ClearIntFlag();

}


#define RESET           0
#define WAIT_SOUND      1
#define PASS_ENTRY      2
#define CHECK           3
#define CORRECT         4

int but_state = RESET;
int sound_state = RESET;
int micCorrect = 0;


//controls microphone input 
static PT_THREAD(protothread_sound(struct pt *pt)) {

    PT_BEGIN(pt);
    static int timeout, i, last_length;
    while (1) {
        switch(sound_state){
            case RESET: 
                micNextLength = 0;
                memset(&miclengths[0], '\0', sizeof(miclengths));
                memset(&micDotDash[0], '\0', sizeof(micDotDash));
                memset(&micPass[0], '\0', sizeof(micPass));
                sound_state = WAIT_SOUND;   
                break;
            case WAIT_SOUND:
                if(miclengths[1] != 0 && but_state == 1){
                    sound_state = PASS_ENTRY;
                    timeout = PT_GET_TIME();
                    last_length = micNextLength;
                }
                break;
            case PASS_ENTRY:
                if(micNextLength != last_length){
                    timeout = PT_GET_TIME();
                    last_length = micNextLength;
                }
                micReadMorse();
                micConvertingToString(0,0,&treeHead);
                int done = 1;
                for(i = 0; i < 5 ; i ++){
                    if (micPass[i] == '\0'){
                        done = 0;
                    }
                }
                if(PT_GET_TIME() - timeout > 2000){
                
                    sound_state = CHECK;
                    miclengths[micNextLength] = 3*dotLength(miclengths);
                    micReadMorse();
                    micConvertingToString(0,0,&treeHead);
                }
                break;
                
            case CHECK:
                micCorrect = 1;
                for(i = 0; i < 5 ; i ++){
                    if (micPass[i] != password[i]){
                        micCorrect = 0;
                    }
                }
                if(micCorrect){
                    sound_state = CORRECT;
                    timeout = PT_GET_TIME();
                }else{
                    tft_setCursor(0, 40);
                    sprintf(buzz_buffer,"\nPlease try again.");
                    tft_writeString(buzz_buffer);
                    sound_state = RESET;
                }
                break;  
                
            case CORRECT:
                mPORTASetBits(BIT_1);
                sound_state = CORRECT;

                PT_YIELD_TIME_msec(5000);
                mPORTAClearBits(BIT_1);
                sound_state = RESET;
                break;
                
            default: 
                sound_state = RESET;
                break;
        }
          PT_YIELD_TIME_msec(30) ; 
    }

    PT_END(pt);
}



int butCorrect = 0;
char new_pass[5];
#define WAIT_BUT    1
#define WAIT_NEW    4
#define NEW_PASS    5
#define WAIT_CONF   6
#define CONFIRM     7
#define CHECK_CHG   8
#define SUCCESS     9
#define FAIL        10


//controls FSM for button input
static PT_THREAD(protothread_physical(struct pt *pt)) {
    
    PT_BEGIN(pt);
    
    static int timeout, i, last_length;
    while (1) {
        switch(but_state){
            case RESET: 
                next_length = 0;
                memset(&lengths[0], '\0', sizeof(lengths));
                memset(&dotDash[0], '\0', sizeof(dotDash));
                memset(&pass[0], '\0', sizeof(pass));
                but_state = WAIT_BUT;   
                break;
            case WAIT_BUT:
                if(lengths[1] != 0 && sound_state == WAIT_SOUND){
                    but_state = PASS_ENTRY;
                    timeout = PT_GET_TIME();
                    last_length = next_length;
                }
                break;
            case PASS_ENTRY:
                if(next_length != last_length){
                    timeout = PT_GET_TIME();
                    last_length = next_length;
                }
                readMorse();
                convertingToString(0,0,&treeHead);
                int done = 1;
                for(i = 0; i < 5 ; i ++){
                    if (pass[i] == '\0'){
                        done = 0;
                    }
                }
                if(PT_GET_TIME() - timeout > 4000){
                //if(done){ 
                    but_state = CHECK;
                    lengths[next_length] = 3*dotLength(lengths);
                    readMorse();
                    convertingToString(0,0,&treeHead);
                }
                break;
                
            case CHECK:
                butCorrect = 1;
                for(i = 0; i < 5 ; i ++){
                    if (pass[i] != password[i]){
                        butCorrect = 0;
                    }
                }
                if(butCorrect){
                    next_length = 0;
                    memset(&lengths[0], '\0', sizeof(lengths));
                    memset(&dotDash[0], '\0', sizeof(dotDash));
                    memset(&pass[0], '\0', sizeof(pass));
                    but_state = WAIT_NEW;                    
                }else{
                    tft_setCursor(0, 40);
                    sprintf(butt_buffer,"\nPlease try again.");
                    tft_writeString(butt_buffer);
                    but_state = RESET;
                }
                break; 
            case WAIT_NEW:
                if(lengths[1] != 0){
                    but_state = NEW_PASS;
                    timeout = PT_GET_TIME();
                    last_length = next_length;
                }
            break;
            
            case NEW_PASS:
                if(next_length != last_length){
                    timeout = PT_GET_TIME();
                    last_length = next_length;
                }
                readMorse();
                convertingToString(0,0,&treeHead);
                done = 1;
                
                if(PT_GET_TIME() - timeout > 4000){
                //if(done){ 
                    lengths[next_length] = 3*dotLength(lengths);
                    readMorse();
                    convertingToString(0,0,&treeHead);
                    for(i = 0; i < 5 ; i ++){
                        new_pass[i]=pass[i];
                    }
                    next_length = 0;
                    memset(&lengths[0], '\0', sizeof(lengths));
                    memset(&dotDash[0], '\0', sizeof(dotDash));
                    memset(&pass[0], '\0', sizeof(pass));
                    but_state = WAIT_CONF;
                    
                }
                break;
            case WAIT_CONF:
                if(lengths[1] != 0){
                    but_state = CONFIRM;
                    timeout = PT_GET_TIME();
                    last_length = next_length;
                }
            break;
            
            case CONFIRM:
                if(next_length != last_length){
                    timeout = PT_GET_TIME();
                    last_length = next_length;
                }
                readMorse();
                convertingToString(0,0,&treeHead);
                done = 1;
                
                if(PT_GET_TIME() - timeout > 2000){
                //if(done){ 
                    lengths[next_length] = 3*dotLength(lengths);
                    readMorse();
                    convertingToString(0,0,&treeHead);
                    but_state = CHECK_CHG;
                    
                }
                break;
            case CHECK_CHG:
                butCorrect = 1;
                for(i = 0; i < 5 ; i ++){
                    if (pass[i] != new_pass[i]){
                        butCorrect = 0;
                    }
                }
                if(butCorrect){
                    but_state = SUCCESS;                    
                }else{
                    but_state = FAIL;
                }
                break; 
                
            case SUCCESS:
                but_state = SUCCESS;
                for(i = 0; i < 5 ; i ++){
                    password[i]=new_pass[i];
                }
                tft_setCursor(0, 80);
                tft_fillRect(0, 90, 320, 20, ILI9340_BLACK);
                sprintf(buffer,"\nPassword: ");
                tft_writeString(buffer);
                for(i = 0; i <5; i++){
                    sprintf(buffer,"%c", password[i]);
                    tft_writeString(buffer);
                }
                PT_YIELD_TIME_msec(5000);
                but_state = RESET;
                break;
            case FAIL:
                but_state = FAIL;               
                PT_YIELD_TIME_msec(5000);
                but_state = RESET;
                break;
                
            default: 
                but_state = RESET;
                break;
        }
        PT_YIELD_TIME_msec(30) ; 
    }
    PT_END(pt);
}

int lcd_state = 0;
#define INIT    0
#define WAIT    1
#define BUZZING 2
#define BUTTON  3

//controls TFT
static PT_THREAD(protothread_lcd(struct pt *pt)) {
    
    PT_BEGIN(pt);
    static int i;
    static int button_prev, buzz_prev;
    while (1) {
        
        //tft_fillRoundRect(0,0, 320, 240, 1, ILI9340_BLACK);// x,y,w,h,radius,color
        switch(lcd_state){
            case INIT:
                tft_setTextColor(ILI9340_WHITE);  
                tft_setTextSize(2);
                tft_fillRect(0, 10, 320, 40, ILI9340_BLACK);
                tft_setCursor(0, 10);
                sprintf(buffer,"Please enter your password");
                tft_writeString(buffer);
                tft_setCursor(0, 80);
                tft_fillRect(0, 90, 320, 20, ILI9340_BLACK);
                sprintf(buffer,"\nPassword: ");
                tft_writeString(buffer);
                for(i = 0; i <5; i++){
                    sprintf(buffer,"%c", password[i]);
                    tft_writeString(buffer);
                }
                lcd_state = WAIT;
                break;
            case WAIT:
                if(but_state > 1){
                    lcd_state = BUTTON;
                } else if(sound_state > 1){
                    lcd_state = BUZZING;
                } else{
                    lcd_state = WAIT;
                }
                break;
                
            case BUZZING:
                if(sound_state <= 1){
                    lcd_state = INIT;
                }else{
                    lcd_state = BUZZING;
                }
                if(sound_state != buzz_prev && sound_state != 0){
                    tft_fillRect(0, 55, 320, 35, ILI9340_BLACK);
                }
                buzz_prev = sound_state;
                tft_fillRect(174, 35, 165, 20, ILI9340_BLACK);
                tft_setCursor(0, 20);
                sprintf(buzz_buffer,"\nYou've entered: ");
                tft_writeString(buzz_buffer);
                for(i = 0; i <5; i++){
                    sprintf(buzz_buffer,"%c", micPass[i]);
                    tft_writeString(buzz_buffer);
                }
                switch(sound_state){
                    case CORRECT:
                        tft_setCursor(0, 40);
                        sprintf(buzz_buffer,"\nCorrect. Please enter.");
                        tft_writeString(buzz_buffer);
                        break;

                    default: 
                        break;
                }   
                
                break;
                
            case BUTTON:
                if(but_state <= 1){
                    lcd_state = INIT;
                }else{
                    lcd_state = BUTTON;
                }
                if(but_state != button_prev && but_state != 0){
                    tft_fillRect(0, 55, 320, 35, ILI9340_BLACK);
                }
                button_prev = but_state;
                tft_fillRect(174, 35, 165, 20, ILI9340_BLACK);
                tft_setCursor(0, 20);
                sprintf(butt_buffer,"\nYou've entered: ");
                tft_writeString(butt_buffer);
                for(i = 0; i <5; i++){
                    sprintf(butt_buffer,"%c", pass[i]);
                    tft_writeString(butt_buffer);
                }
                
                switch(but_state){
                    case PASS_ENTRY:
                        break;

                    case CHECK:
                        break; 
                    case WAIT_NEW:
                        tft_setCursor(0, 40);
                        sprintf(butt_buffer,"\nCorrect. Please enter new \npassword.");
                        tft_writeString(butt_buffer);
                        break;

                    case NEW_PASS:
                        break;
                    case WAIT_CONF:
                        tft_setCursor(0, 40);
                        sprintf(butt_buffer,"\nPlease reenter new \npassword to confirm.");
                        tft_writeString(butt_buffer);
                    break;

                    case CONFIRM:
                        break;
                    case CHECK_CHG:
                        break; 

                    case SUCCESS:
                        tft_setCursor(0, 40);
                        sprintf(butt_buffer,"\nPassword change successful");
                        tft_writeString(butt_buffer);

                        break;
                    case FAIL:
                        tft_setCursor(0, 40);
                        sprintf(butt_buffer,"\nPassword change failed");
                        tft_writeString(butt_buffer);
                        break;

                    default: 
                        break; 
                    }
                
                break;
                    
            default:
                lcd_state = WAIT;
                break;
        }        
        

        PT_YIELD_TIME_msec(67) ; 
    }    
    PT_END(pt);
}

// === Main  ======================================================

void main(void) {

    // === config threads ==========
    // turns OFF UART support and debugger pin, unless defines are set
    PT_setup();

    // == Configure Input Capture==
    OpenTimer23(T23_ON | T23_SOURCE_INT | T23_PS_1_256, 0xffffffff);
    
    OpenCapture1(IC_ON | IC_INT_1CAPTURE | IC_EVERY_EDGE | IC_CAP_32BIT);
    OpenCapture5(IC_ON | IC_INT_1CAPTURE | IC_EVERY_EDGE | IC_CAP_32BIT);
    PPSInput(3,IC1,RPB13);
    PPSInput(3,IC5,RPB13);
    ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_3);

    mT2ClearIntFlag();
    // init the threads
    PT_INIT(&pt_physical);
    PT_INIT(&pt_sound);
    PT_INIT(&pt_lcd);

   

    // init the display
    tft_init_hw();
    tft_begin();
    tft_fillScreen(ILI9340_BLACK);
    //240x320 vertical display
    tft_setRotation(1); // Use tft_setRotation(1) for 320x240
    
    RB_INIT(&inputs);
    RB_INIT(&outputs);
    //------------------------------------------------------------
    // Set up ADC for 1953.125Hz sampling
        //off by factor of 2??? 3.827
    // 7656Hz
    //------------------------------------------------------------

    SetChanADC10(ADC_CH0_POS_SAMPLEA_AN9 | ADC_CH0_NEG_SAMPLEA_NVREF);
    
    #define CONFIG1 ADC_MODULE_ON | ADC_FORMAT_INTG32 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
    #define CONFIG2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
    #define CONFIG3 ADC_CONV_CLK_PB | ADC_SAMPLE_TIME_28 | ADC_CONV_CLK_32Tcy
    #define CONFIG_PORT ENABLE_AN9_ANA
    #define CONFIG_SCAN SKIP_SCAN_ALL
    OpenADC10(CONFIG1, CONFIG2, CONFIG3, CONFIG_PORT, CONFIG_SCAN);
    
        
    //config interrupt
    ConfigIntADC10( ADC_INT_ON | ADC_INT_PRI_3 | ADC_INT_SUB_PRI_3);
    EnableADC10();


    
    mPORTASetPinsDigitalOut(BIT_0);
    mPORTASetPinsDigitalOut(BIT_1);

    
   // === setup system wide interrupts  ====================
    INTEnableSystemMultiVectoredInt();
    // round-robin scheduler for threads
    while (1) {
        PT_SCHEDULE(protothread_physical(&pt_physical));
        PT_SCHEDULE(protothread_sound(&pt_sound));
        PT_SCHEDULE(protothread_lcd(&pt_lcd));
    }
} // main

// === end  ======================================================


Play_sound.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[Y,FS]=wavread('sample.wav');
[buzz, buzz_fs] = audioread('buzz.wav');
buzz = buzz(60000:80000);
buzz= repmat(buzz,20,1);

t = [1:81920];
x = sin(t*2*pi/8192*420);

%player = audioplayer(Y,FS);
%player = audioplayer(buzz,buzz_fs);
player = audioplayer(x,8192);

player.play

fig = figure;
set(fig,'KeyPressFcn',{@keyDownListener,player})
set(fig, 'KeyReleaseFcn', {@keyUpListener,player});


KeyDownListener.m

1
2
3
4
5
6
7
8
function keyDownListener(src,event,player)
%UNTITLED3 Summary of this function goes heres
%   Detailed explanation goes here
if(player.isplaying == false)
    player.resume
end

end


KeyUpListener.m

1
2
3
4
5
function keyUpListener(src,event,player)
%UNTITLED3 Summary of this function goes heres
%   Detailed explanation goes here
player.pause
end