None

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
henryeherman
Date:
Wed Dec 01 03:28:28 2010 +0000
Commit message:
A

Changed in this revision

com.cpp Show annotated file Show diff for this revision Revisions of this file
com.h Show annotated file Show diff for this revision Revisions of this file
handler.cpp Show annotated file Show diff for this revision Revisions of this file
handler.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
touchbutton.cpp Show annotated file Show diff for this revision Revisions of this file
touchbutton.h Show annotated file Show diff for this revision Revisions of this file
touchmachine.cpp Show annotated file Show diff for this revision Revisions of this file
touchmachine.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6ae7b9747a06 com.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/com.cpp	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,2 @@
+#include "com.h"
+Serial com(USBTX,USBRX);
\ No newline at end of file
diff -r 000000000000 -r 6ae7b9747a06 com.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/com.h	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,5 @@
+//#ifndef COM_H
+//#define COM_H
+#include "mbed.h"
+extern Serial com;
+//#endif
\ No newline at end of file
diff -r 000000000000 -r 6ae7b9747a06 handler.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/handler.cpp	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,126 @@
+#include "handler.h"
+#include "com.h"
+
+
+
+Handler::Handler(char *nm) {
+    name = nm;
+    init();
+    stringState=NOTREADY;
+}
+
+
+void Handler::init() {
+    #ifdef DEBUGHAND
+    com.printf("INIT HANDLER\r\n");
+    #endif
+    state = WAITINGON_S;
+    resetMatch();
+}
+
+void Handler::resetMatch() {
+    #ifdef DEBUGHAND
+    com.printf("RESET MATCH STRING\r\n");
+    #endif
+    matchString[0]='\0';
+    pmatchString = matchString;
+}
+
+
+void Handler::run() {
+    int c;
+    if (!com.readable()) {
+        return;
+    }
+    c = com.getc();
+    
+    #ifdef DEBUGHAND
+    com.printf("READ CHAR, %c\r\n", c);
+    #endif
+    
+    switch (state) {
+        case WAITINGON_S:
+            recievedStart(c);
+            break;
+        case RECIEVING_MATCH:
+            processChar(c);
+            break;
+        default:
+            //should not get here
+            hostError();
+            break;
+
+    }
+}
+
+void Handler::processChar(char c) {
+    switch (c) {
+        case ONECHR:
+        case ZEROCHR:
+            recieveMatchString(c);
+            #ifdef DEBUGHAND
+            com.printf("REC 1 or 0\r\n");
+            #endif
+            break;
+        case '\r':
+        case ' ':
+        case '\n':
+            //do nothing
+            break;
+        case ENDCHAR:
+            #ifdef DEBUGHAND
+            com.printf("REC E\r\n");
+            #endif
+            matchStringRecieved();
+            break;
+        default:
+            hostError();
+    }
+}
+
+
+void Handler::hostError() {
+    com.printf(HOSTERROR);
+    init();
+}
+
+void Handler::recieveMatchString(char c) {
+    *pmatchString = c;
+    pmatchString++;
+}
+
+void Handler::recievedStart(char c) {
+    if (c==STARTCHAR) {
+        #ifdef DEBUGHAND
+        com.printf("REC S\r\n");
+        #endif
+        state = RECIEVING_MATCH;
+        stringState = NOTREADY;
+        resetMatch();
+    }
+}
+
+void Handler::matchStringRecieved() {
+    #ifdef DEBUGHAND
+    com.printf("COMPLETE\r\n");
+    #endif
+    *pmatchString=NULL;
+    strcpy(completeMatchString, matchString);
+    com.printf("ARMED-%s\r\n",matchString);
+    init();
+    stringState=READY;
+}
+
+
+
+bool Handler::isReady() {
+    if (stringState==READY) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+char *Handler::getMatchString() {
+    return completeMatchString;
+}
diff -r 000000000000 -r 6ae7b9747a06 handler.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/handler.h	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,42 @@
+
+#ifndef HANDLER_H
+#define HANDLER_H
+#include "mbed.h"
+
+#define MATCHSIZE 128
+#define STARTCHAR 'S'
+#define ENDCHAR   'E'
+#define ONECHR '1'
+#define ZEROCHR '0'
+#define HOSTERROR "HOST ERROR\r\n"
+
+//#define DEBUGHAND
+
+class Handler {
+
+public:
+    Handler(char* nm);
+    void run();
+    bool isReady();
+    char *getMatchString();
+    void init();
+
+private:
+    char *name;
+    char matchString[MATCHSIZE];
+    char completeMatchString[MATCHSIZE];
+    char *pmatchString;
+    enum SerialStates { WAITINGON_S, RECIEVING_MATCH};
+    SerialStates state;
+    enum StringStates { READY, NOTREADY};
+    StringStates stringState;
+    int value;
+    void resetMatch();
+    void recieveMatchString(char c);
+    void waitForStart(char c);
+    void matchStringRecieved();
+    void hostError();
+    void processChar(char c);
+    void recievedStart(char c);
+};
+#endif
\ No newline at end of file
diff -r 000000000000 -r 6ae7b9747a06 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,11 @@
+#include "mbed.h"
+#include "touchmachine.h"
+
+
+
+
+int main() {
+    TouchMachine touchMachine("Touch 1");
+    touchMachine.run();
+}
+
diff -r 000000000000 -r 6ae7b9747a06 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
diff -r 000000000000 -r 6ae7b9747a06 touchbutton.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/touchbutton.cpp	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,97 @@
+#include "mbed.h"
+#include "touchbutton.h"
+#include "com.h"
+
+#ifdef DEBUGTOUCHLED
+DigitalOut debugled(LED1);
+#endif
+
+float TouchButton::difference() {
+    return previousValue - currentValue;
+}
+
+TouchButton::TouchButton(PinName ain, PinName din, PinName dout, float threshold, int debounceTime, char *nm) :   sensor(ain), charger(din), ground(dout) {
+
+    setThreshold(threshold);
+    setDebounceTime(debounceTime);
+    AnalogIn sensor(ain);
+    DigitalIn charger(din);
+    DigitalOut ground(dout);
+    ground.write(0);
+    timer.start();
+    sample();
+    storeValue();
+    strcpy(name,nm);
+#ifdef DEBUGTOUCH
+        com.printf("INIT BUTTON %s\r\n",name);
+#endif
+}
+
+void TouchButton::storeValue() {
+    previousValue=currentValue;
+}
+
+void TouchButton::setThreshold(float level) {
+    analogThreshold = level;
+}
+
+void TouchButton::setDebounceTime(int t) {
+    timeout_ms = t;
+}
+
+bool TouchButton::checkState() {
+    float diff;
+#ifdef DEBUGTOUCH
+        com.printf("CHECK BUTTON\r\n");
+#endif
+ 
+    charge();
+#ifdef DEBUGTOUCH
+        com.printf("CHARGE BUTTON\r\n");
+#endif    
+    sample();
+#ifdef DEBUGTOUCH
+        com.printf("CHARGE & SAMPLE BUTTON %s\r\n",name);
+        com.printf("Value:%3.2f\r\n", currentValue);
+#endif
+
+
+    diff=previousValue - currentValue;
+    storeValue();
+    if (diff > analogThreshold && timer.read_ms() > timeout_ms) {
+        timer.reset();
+#ifdef DEBUGTOUCH
+        com.printf("TOUCH %s\r\n", name);
+#endif
+#ifdef DEBUGTOUCHLED
+        //Toggle LED to show touch was registered
+        if (debugled.read()==1)
+            debugled=0;
+        else
+            debugled=1;
+#endif
+        return true;
+    } else {
+        return false;
+    }
+
+}
+
+float TouchButton::sample() {
+
+    float sum = 0;
+    for (int i=0; i<NUMSAMP;i++) {
+        float value = sensor.read();
+        sum = sum + value;
+    }
+    currentValue = sum/NUMSAMP;
+    return currentValue;
+
+}
+
+void TouchButton::charge() {
+    charger.mode(PullUp);
+    charger.mode(PullNone);
+    wait(.005);
+
+}
diff -r 000000000000 -r 6ae7b9747a06 touchbutton.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/touchbutton.h	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,32 @@
+
+#ifndef TOUCHBUTTON_H
+#define TOUCHBUTTON_H
+#define NUMSAMP 10
+//#define DEBUGTOUCH
+#define DEBUGTOUCHLED
+class TouchButton {
+
+public:
+    TouchButton(PinName ain, PinName din, PinName dout, float threshold, int debounceTime, char *nm);
+    void setThreshold(float level);
+    void setDebounceTime(int t);
+    bool checkState();
+    float sample();
+    void charge();
+
+private:
+    char name[10];
+    float currentValue;
+    float previousValue;
+    AnalogIn sensor;
+    DigitalIn charger;
+    DigitalOut ground;
+    Timer timer;
+    Timer debounceTimer;
+    float analogThreshold;
+    int timeout_ms;
+    void storeValue();
+    float difference();
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 6ae7b9747a06 touchmachine.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/touchmachine.cpp	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,211 @@
+#include "touchmachine.h"
+#include "mbed.h"
+#include "com.h"
+TouchButton touch0(p20,p19,p18, THRESHOLD, TIMER_THRESHOLD,"0");
+TouchButton touch1(p17,p16,p15, THRESHOLD, TIMER_THRESHOLD,"1");
+Handler handler("Serial");
+
+void runser() {
+    handler.run();
+}
+
+TouchMachine::TouchMachine(char *c) {
+#ifdef DEBUGTM
+    com.printf("INIT TM\r\n");
+#endif    
+    touchState=WAITING;
+    t0 = &touch0;
+    t1 = &touch1;
+    hand = &handler;
+    com.attach(runser);
+    
+};
+
+void TouchMachine::check() {
+#ifdef DEBUGTM
+    com.printf("CHECK BUTTONS\r\n");
+#endif    
+    t0state = t0->checkState();
+    t1state = t1->checkState();
+    if (t0state && t1state) {
+    #ifdef DEBUGTM1
+        com.printf("ERR-CHK\r\n");
+    #endif
+        touchState=ERROR;
+    }else if (t0state) {
+    #ifdef DEBUGTM1
+        com.printf("ZERO-CHK\r\n");
+    #endif
+        touchState=ZERO;
+    } else if(t1state) {
+    #ifdef DEBUGTM1
+        com.printf("ONE-CHK\r\n");
+    #endif    
+        touchState=ONE;
+    } else {
+    #ifdef DEBUGTM
+        com.printf("WAIT-CHK\r\n");
+    #endif    
+        touchState=WAITING;
+    }
+}
+
+bool TouchMachine::isMatch() {
+    if (touchState==MATCH) {
+#ifdef DEBUGTM1
+        com.printf("MATCH-TM\r\n");
+#endif    
+        return true;
+    }
+    return false;
+}
+
+bool TouchMachine::isOne() {
+    if (touchState==ONE) {
+#ifdef DEBUGTM
+        com.printf("ONE-TM\r\n");
+#endif    
+        return true;
+    }
+    return false;
+}
+
+
+bool TouchMachine::isZero() {
+    if (touchState==ZERO) {
+#ifdef DEBUGTM
+        com.printf("ZERO-TM\r\n");
+#endif    
+        return true;
+    }
+    return false;
+}
+
+bool TouchMachine::isWaiting() {
+    if (touchState==WAITING) {
+#ifdef DEBUGTM
+        com.printf("WAITING-TM\r\n");
+#endif    
+        return true;
+    }
+    return false;
+}
+
+bool TouchMachine::isError() {
+    if (touchState==ERROR) {
+#ifdef DEBUGTM
+        com.printf("ERROR-TM\r\n");
+#endif    
+        return true;
+    }
+    return false;
+}
+
+void TouchMachine::setError() {
+    touchState=ERROR;
+}
+
+void TouchMachine::setMatch() {
+    touchState=MATCH;
+}
+
+void TouchMachine::resetTouch() {
+#ifdef DEBUGTM1
+    com.printf("RESET FOR TOUCH\r\n");
+#endif    
+    touchState=WAITING;
+    loadMatchStr();
+}
+
+char TouchMachine::getMatchChar(){
+    if(*pmatchstr!=NULL){
+    return *pmatchstr++;
+    }
+      return NULL;
+}
+
+char TouchMachine::nextMatchChar(){
+    return *(pmatchstr);
+}
+
+void TouchMachine::loadMatchStr(){
+    pmatchstr=hand->getMatchString();
+#ifdef DEBUGTM1
+    com.printf("LOAD MATCH STR-%s\r\n",pmatchstr);
+#endif
+}
+
+void TouchMachine::touchError() {
+    com.printf("TOUCH ERROR\r\n");
+}
+
+void TouchMachine::touchMatch() {
+    com.printf("***MATCH***\r\n");
+}
+
+void TouchMachine::run() {    
+    while(1) {
+        if (hand->isReady()) {
+            resetTouch();
+            loadMatchStr();
+            checkTouchForMatch();
+        }    
+     }
+}
+
+void TouchMachine::checkTouchForMatch() {
+    char c;
+    while(( isWaiting() || isOne() || isZero())&& hand->isReady()) {
+            check();
+            if(isOne() || isZero()) {
+                
+                c = getMatchChar();
+                #ifdef DEBUGTM1
+                    com.printf("CHR TO MATCH %c\r\n",c);
+                #endif
+                switch(c) {
+                case NULL:
+                    setMatch();
+                    break;                    
+                case ZEROCHR:
+                    if(!isZero()) 
+                        setError();
+                     
+                    break;
+                case ONECHR:
+                    if(!isOne()) 
+                        setError();
+                    
+                default:
+                    break;
+                    
+               
+                }
+            }
+            
+            
+            if(isError()) {
+                touchError();
+                resetTouch();
+            }
+            checkMatch();
+            if(isMatch()) {
+                touchMatch();
+                resetTouch();
+            }
+            waitForRelease();
+
+    }
+}
+
+void TouchMachine::waitForRelease() {
+    while(!isWaiting() && hand->isReady()) {
+        check();
+    }
+}
+
+void TouchMachine::checkMatch() {
+ if(!isError() && nextMatchChar()==NULL) {
+               setMatch();
+  }
+ }
\ No newline at end of file
diff -r 000000000000 -r 6ae7b9747a06 touchmachine.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/touchmachine.h	Wed Dec 01 03:28:28 2010 +0000
@@ -0,0 +1,49 @@
+#ifndef TOUCHMACHINE_H
+#define TOUCHMACHINE_H
+
+#include "mbed.h"
+#include "touchbutton.h"
+#include "handler.h"
+#include "com.h"
+#define THRESHOLD .45
+#define TIMER_THRESHOLD 50
+//#define DEBUGTM
+//#define DEBUGTM1
+
+
+
+class TouchMachine {
+    public:
+    TouchMachine(char *c);
+    void check();
+    void run();
+    private:
+        char *pmatchstr;
+        TouchButton *t0;
+        TouchButton *t1;
+        Handler *hand;
+        bool t0state;
+        bool t1state;
+        enum States {ERROR, WAITING, ONE, ZERO, MATCH};
+        States touchState;
+        bool isOne();
+        bool isZero();
+        bool isWaiting();
+        bool isError();
+        bool isMatch();
+        void setMatch();
+        char getNextMatchChar();
+        char nextMatchChar();
+        void setError();
+        void resetTouch();
+        char getMatchChar();
+        void loadMatchStr();
+        void touchError();
+        void touchMatch();
+        void checkTouchForMatch();
+        void waitForRelease();
+        void checkMatch();
+};
+
+
+#endif
\ No newline at end of file