HW2 implemented using busy loop

Committer:
the729
Date:
Wed Dec 01 02:53:33 2010 +0000
Revision:
0:9db2667ac0d3

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
the729 0:9db2667ac0d3 1 #include "mbed.h"
the729 0:9db2667ac0d3 2
the729 0:9db2667ac0d3 3 //DigitalOut led1(LED1);
the729 0:9db2667ac0d3 4 //DigitalOut led2(LED2);
the729 0:9db2667ac0d3 5
the729 0:9db2667ac0d3 6 Serial pc(USBTX, USBRX);
the729 0:9db2667ac0d3 7
the729 0:9db2667ac0d3 8 #define BTN0 p15
the729 0:9db2667ac0d3 9 #define BTN1 p20
the729 0:9db2667ac0d3 10 #define MAX_LENGTH 32
the729 0:9db2667ac0d3 11
the729 0:9db2667ac0d3 12 typedef unsigned int uint32_t;
the729 0:9db2667ac0d3 13 typedef unsigned char uint8_t;
the729 0:9db2667ac0d3 14
the729 0:9db2667ac0d3 15 void precharge(PinName p)
the729 0:9db2667ac0d3 16 {
the729 0:9db2667ac0d3 17 DigitalIn prec(p);
the729 0:9db2667ac0d3 18 prec.mode(PullUp);
the729 0:9db2667ac0d3 19 prec.mode(PullNone);
the729 0:9db2667ac0d3 20 }
the729 0:9db2667ac0d3 21
the729 0:9db2667ac0d3 22 uint8_t read_cap()
the729 0:9db2667ac0d3 23 {
the729 0:9db2667ac0d3 24 precharge(BTN0);
the729 0:9db2667ac0d3 25 precharge(BTN1);
the729 0:9db2667ac0d3 26 wait_ms(5);
the729 0:9db2667ac0d3 27 float s0 = AnalogIn(BTN0);
the729 0:9db2667ac0d3 28 float s1 = AnalogIn(BTN1);
the729 0:9db2667ac0d3 29 //pc.printf("%f \r", s0);
the729 0:9db2667ac0d3 30 return ((s0>0.5)?0:1) + ((s1>0.5)?0:2);
the729 0:9db2667ac0d3 31 }
the729 0:9db2667ac0d3 32
the729 0:9db2667ac0d3 33 int main()
the729 0:9db2667ac0d3 34 {
the729 0:9db2667ac0d3 35 enum {
the729 0:9db2667ac0d3 36 IDLE=0,
the729 0:9db2667ac0d3 37 HOST_INPUT,
the729 0:9db2667ac0d3 38 USER_INPUT
the729 0:9db2667ac0d3 39 } state;
the729 0:9db2667ac0d3 40 uint8_t c = 0;
the729 0:9db2667ac0d3 41 uint32_t data = 0;
the729 0:9db2667ac0d3 42 uint8_t datalength = 0;
the729 0:9db2667ac0d3 43
the729 0:9db2667ac0d3 44 state = IDLE;
the729 0:9db2667ac0d3 45 while (1) {
the729 0:9db2667ac0d3 46 switch(state) {
the729 0:9db2667ac0d3 47 case IDLE:
the729 0:9db2667ac0d3 48 pc.putc(c = pc.getc());
the729 0:9db2667ac0d3 49 if (c == 'S') {
the729 0:9db2667ac0d3 50 datalength = 0;
the729 0:9db2667ac0d3 51 state = HOST_INPUT;
the729 0:9db2667ac0d3 52 } else {
the729 0:9db2667ac0d3 53 pc.printf("HOST ERROR. You are supposed to input S. Try again. \r\n");
the729 0:9db2667ac0d3 54 }
the729 0:9db2667ac0d3 55 break;
the729 0:9db2667ac0d3 56 case HOST_INPUT:
the729 0:9db2667ac0d3 57 pc.putc(c = pc.getc());
the729 0:9db2667ac0d3 58 if (c == '0' || c == '1') {
the729 0:9db2667ac0d3 59 datalength ++;
the729 0:9db2667ac0d3 60 if (datalength > MAX_LENGTH) {
the729 0:9db2667ac0d3 61 pc.printf("\r\nLength execeeds maximum. \r\n");
the729 0:9db2667ac0d3 62 } else {
the729 0:9db2667ac0d3 63 data = (data << 1) | (c-'0');
the729 0:9db2667ac0d3 64 }
the729 0:9db2667ac0d3 65 } else if (c == 'E') {
the729 0:9db2667ac0d3 66 pc.printf("\r\nInput End. Tap the caps now. \r\n");
the729 0:9db2667ac0d3 67 if (!datalength) {
the729 0:9db2667ac0d3 68 pc.printf("Zero length. Try again. \r\n");
the729 0:9db2667ac0d3 69 state = IDLE;
the729 0:9db2667ac0d3 70 break;
the729 0:9db2667ac0d3 71 }
the729 0:9db2667ac0d3 72 data <<= (32-datalength);
the729 0:9db2667ac0d3 73 state = USER_INPUT;
the729 0:9db2667ac0d3 74 } else {
the729 0:9db2667ac0d3 75 pc.printf("\r\nHOST ERROR. \r\n");
the729 0:9db2667ac0d3 76 }
the729 0:9db2667ac0d3 77 break;
the729 0:9db2667ac0d3 78 case USER_INPUT:
the729 0:9db2667ac0d3 79 c = read_cap();
the729 0:9db2667ac0d3 80 if (!c) break;
the729 0:9db2667ac0d3 81 wait_ms(10);
the729 0:9db2667ac0d3 82 if (c != read_cap()) break;
the729 0:9db2667ac0d3 83 while(read_cap());
the729 0:9db2667ac0d3 84 if (c == 3) {
the729 0:9db2667ac0d3 85 pc.printf("\r\nTOUCH ERROR. Touch only one. \r\n");
the729 0:9db2667ac0d3 86 break;
the729 0:9db2667ac0d3 87 }
the729 0:9db2667ac0d3 88 c -= 1;
the729 0:9db2667ac0d3 89 pc.putc(c + '0');
the729 0:9db2667ac0d3 90 if (c == (data >> 31)) {
the729 0:9db2667ac0d3 91 datalength--;
the729 0:9db2667ac0d3 92 if (!datalength) {
the729 0:9db2667ac0d3 93 pc.printf("\r\nMATCH! \r\n");
the729 0:9db2667ac0d3 94 state = IDLE;
the729 0:9db2667ac0d3 95 }
the729 0:9db2667ac0d3 96 data <<= 1;
the729 0:9db2667ac0d3 97 } else {
the729 0:9db2667ac0d3 98 pc.printf("\r\nTOUCH ERROR. \r\n");
the729 0:9db2667ac0d3 99 }
the729 0:9db2667ac0d3 100 break;
the729 0:9db2667ac0d3 101 }
the729 0:9db2667ac0d3 102 }
the729 0:9db2667ac0d3 103 }