fix deni

Dependencies:   TSI

Fork of TSI_sample by William Marsh

main.cpp

Committer:
dhenis
Date:
2018-03-01
Revision:
5:e79b003f2bc5
Parent:
4:d54e74fbf82c

File content as of revision 5:e79b003f2bc5:

#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 ledex1(D0);

TSISensor tsi;
//Thread redThread(osPriorityNormal, 1000);
//Thread greenThread(osPriorityNormal, 1000);


Thread outterleft(osPriorityNormal, 1000);
Thread innerleft(osPriorityNormal, 1000);
Thread innerlight(osPriorityNormal, 1000);
Thread outerright(osPriorityNormal, 1000); // this thread doesnt work, so I directly move it into main thread

//Thread redThread ; // thread for red LED
//Thread greenThread ; // thread for green LED
 
 bool red = true,green = true,blue = true,white = true;

 

void red_thread() {  // method to run in thread
    while (true) {
        
       
        
        Thread::signal_wait(0x1);
        
        
        redLED = !redLED ; // turn on 
        
        outterleft.signal_clr(0x1) ;


          // Signal are automatically cleared by wait_signal but
          // the signal might have been set again while LED on 
    }
}

void green_thread() {  // method to run in thread
    while (true) {
         
        Thread::signal_wait(0x1);
        
//        green = false ;
        greenLED = !greenLED ; // turn on 

        innerleft.signal_clr(0x1) ;
        


          // Signal are automatically cleared by wait_signal but
          // the signal might have been set again while LED on 
    }
}

void blue_thread() {  // method to run in thread
    while (true) {
      
         
        Thread::signal_wait(0x1);
//         blue = false;
        
         blueLED = !blueLED ; // turn on 
        innerlight.signal_clr(0x1) ;


        
          // Signal are automatically cleared by wait_signal but
          // the signal might have been set again while LED on 
    }
}

void white_thread() {  // method to run in thread
    while (true) {
         //
        Thread::signal_wait(0x1);
        
        
    ledex1 = !ledex1;
        //
//        blueLED = !blueLED ; // turn off 
//        greenLED = !greenLED; // turn off 
//        redLED = !redLED ; // turn off 
        
        outerright.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 position = 1; //checking state

    outterleft.start(&red_thread) ; // start the red thread
    innerleft.start(&green_thread) ; // start the green thread
    innerlight.start(&blue_thread) ; // start the green thread
    outerright.start(&white_thread) ; // start the green thread


        blueLED = true ; // turn off 
        greenLED = true; // turn off 
        redLED = true ; // turn off 
        
    ledex1 = false;
       
    while (true) {
//        uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
                                          // When no touch --> 0
                                          // Left --> low value  Right --> high value
        // signals are triggered by touching the sri
        // signal set on addres 0x1
        
        uint8_t pos = tsi.readDistance() ;  // Distance is between 0 and 39
        
//        
//         redLED = true;
//         greenLED = true;
//         blueLED = true;
         
      if(pos >3 and pos < 9){ // softkey outterleft
      
      
     //state entry 
//      redLED = !redLED ; // turn on 
        if(position == 1){
                
            outterleft.signal_set(0x1) ;
        
            position = 0;
        
        }
  
    
    }else if(pos ==3 or pos ==9){
    //state exit
        redLED = true; // turn of
//        outterleft.signal_set(0x1) ;
  
        
    }else if(pos >13 and pos < 19){// softkey innerleft
     //state entry 
  //      greenLED = !greenLED; // turn on 
  
          if(position == 1){
                
                innerleft.signal_set(0x1) ;
                
                 position = 0;
         }
    
    }else if(pos ==13 or pos ==19){
    //state exit
       greenLED = true; // turn of

 //       innerleft.signal_set(0x2) ;
 

    }else if(pos >23 and pos < 29){ // softkey innerlight
     //state entry 
 //       blueLED = !blueLED; // turn on 
        
          if(position == 1){
        
            innerlight.signal_set(0x1) ;
            
            position = 0;
         }
    
    }else if(pos ==23 or pos ==29){ 
    //state exit
        blueLED = true; // turn of
        
 
        
    }else if(pos >=34 and pos <40){ // softkye outerright
     //state entry 

    if(position == 1){
    
        ledex1 = !ledex1;
        
        position = 0;
    }


    }
    else if(pos ==33){
    //state exit
    
    ledex1 = false;


    }
    else if(pos == 0) {
        position = 1;
      
              
    }
    
    

         
        pc.printf("%d \n  \r", pos ) ;  
        pc.putc(' ') ;
//        if (d == 10) redThread.signal_set(0x1) ;
//        if (d == 20) greenThread.signal_set(0x1) ;
        
        Thread::wait(200);  // This polling rate is too slow - increase it
                            // The slower rate maks it easier to output on the terminal
    }
}