Lab5

Dependencies:   TSI

Fork of TSI_sample by William Marsh

main.cpp

Committer:
Bossman
Date:
2018-03-02
Revision:
5:6601376ae1b8
Parent:
4:d54e74fbf82c

File content as of revision 5:6601376ae1b8:

#include "mbed.h"
#include "TSISensor.h"
// Example program for lab 5
// -------------------------
//  A value is read from the touch sensor and use
//    to control two LEDs
//  The value is also output to the serial interface
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut redLED(LED_RED);
DigitalOut greenLED(LED_GREEN);
DigitalOut blueLED(LED_BLUE);
DigitalOut led(PTD5);
TSISensor tsi;
Thread redThread(osPriorityNormal,1000) ; // thread for red LED
Thread greenThread(osPriorityNormal,1000) ; // thread for green LED
Thread blueThread(osPriorityNormal,1000) ; // thread for blue LED
Thread whiteThread(osPriorityNormal,1000) ; // thread for mix LED

void red_thread()    // methbd to run in thread
{
    while (true) {
        Thread::signal_wait(0x1);
        redLED = !redLED; // turn on
        redThread.signal_clr(0x1) ;
        // Signal are automatically cleared by wait_signal but
        // the signal might have been set again while LED on
    }
}
void blue_thread()    // methbd to run in thread
{
    while (true) {
        Thread::signal_wait(0x1);
        blueLED = !blueLED;
        blueThread.signal_clr(0x1) ;
        // Signal are automatically cleared by wait_signal but
        // the signal might have been set again while LED on
    }
}
void green_thread()    // methbd to run in thread
{
    while (true) {
        Thread::signal_wait(0x1);
        greenLED = !greenLED;
        greenThread.signal_clr(0x1) ;
        // Signal are automatically cleared by wait_signal but
        // the signal might have been set again while LED on
    }
}
void white_thread()    // methbd to run in thread
{
    while (true) {
        Thread::signal_wait(0x1);
        led=!led;
        whiteThread.signal_clr(0x1) ;
        // Signal are automatically cleared by wait_signal but
        // the signal might have been set again while LED on
    }
}

int main(void)
{
    int pos=1;
    redLED = 1; // turn off
    greenLED = 1; // turn off
    blueLED = 1;
    led=1;
    redThread.start(&red_thread) ; // start the red thread
    greenThread.start(&green_thread) ; // start the green thread
    blueThread.start(&blue_thread) ; // start the blue thread
    whiteThread.start(&white_thread) ; // start the mixcolor thread

    while (true) {
        uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
        // When no touch --> 0
        // Left --> low value  Right --> high value
        pc.printf("%d", d) ;
        pc.putc(' ') ;
        if ((d>3) &&(d<9)) {
            if (pos==1) {
                redThread.signal_set(0x1) ;
                pos=0;
            }
        } else if ((d>13) &&(d<19)) {
            if (pos==1) {
                greenThread.signal_set(0x1) ;
                pos=0;
            }
        } else if ((d>23) &&(d<29)) {
            if (pos==1) {
                blueThread.signal_set(0x1) ;
                pos=0;
            }
        } else if (d>33) {
            if (pos==1) {
                whiteThread.signal_set(0x1) ;
                pos=0;
            }
        } else if(d == 0) { // touch off
            pos=1;
        }
        Thread::wait(200);  // This polling rate is too slow - increase it
        // The slower rate maks it easier to output on the terminal
    }
}