HW2 implemented by interrupts

Dependencies:   mbed

Committer:
the729
Date:
Wed Dec 01 02:48:22 2010 +0000
Revision:
0:64a910e4a0d5

        

Who changed what in which revision?

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