HW2 implemented using protothread

Dependencies:   mbed

Committer:
the729
Date:
Wed Dec 01 02:53:56 2010 +0000
Revision:
0:e2cc8ae74a24

        

Who changed what in which revision?

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