Lab5

Dependencies:   TSI

Fork of TSI_sample by William Marsh

Committer:
WilliamMarshQMUL
Date:
Thu Feb 22 16:59:24 2018 +0000
Revision:
4:d54e74fbf82c
Parent:
1:e6ffa08ad8bf
Child:
5:6601376ae1b8
Remove unnecessary include "rtos.h"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WilliamMarshQMUL 0:4374caa1ef17 1 #include "mbed.h"
WilliamMarshQMUL 0:4374caa1ef17 2 #include "TSISensor.h"
WilliamMarshQMUL 0:4374caa1ef17 3
WilliamMarshQMUL 0:4374caa1ef17 4 // Example program for lab 5
WilliamMarshQMUL 0:4374caa1ef17 5 // -------------------------
WilliamMarshQMUL 0:4374caa1ef17 6 // A value is read from the touch sensor and use
WilliamMarshQMUL 0:4374caa1ef17 7 // to control two LEDs
WilliamMarshQMUL 0:4374caa1ef17 8 // The value is also output to the serial interface
WilliamMarshQMUL 0:4374caa1ef17 9
WilliamMarshQMUL 0:4374caa1ef17 10 Serial pc(USBTX, USBRX); // tx, rx
WilliamMarshQMUL 0:4374caa1ef17 11 DigitalOut redLED(LED_RED);
WilliamMarshQMUL 0:4374caa1ef17 12 DigitalOut greenLED(LED_GREEN);
WilliamMarshQMUL 0:4374caa1ef17 13 TSISensor tsi;
WilliamMarshQMUL 0:4374caa1ef17 14
WilliamMarshQMUL 0:4374caa1ef17 15 Thread redThread ; // thread for red LED
WilliamMarshQMUL 0:4374caa1ef17 16 Thread greenThread ; // thread for green LED
WilliamMarshQMUL 0:4374caa1ef17 17
WilliamMarshQMUL 0:4374caa1ef17 18 void red_thread() { // method to run in thread
WilliamMarshQMUL 0:4374caa1ef17 19 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 20 Thread::signal_wait(0x1);
WilliamMarshQMUL 0:4374caa1ef17 21 redLED = false ; // turn on
WilliamMarshQMUL 0:4374caa1ef17 22 Thread::wait(5000);
WilliamMarshQMUL 0:4374caa1ef17 23 redLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 24 redThread.signal_clr(0x1) ;
WilliamMarshQMUL 0:4374caa1ef17 25 // Signal are automatically cleared by wait_signal but
WilliamMarshQMUL 0:4374caa1ef17 26 // the signal might have been set again while LED on
WilliamMarshQMUL 0:4374caa1ef17 27 }
WilliamMarshQMUL 0:4374caa1ef17 28 }
WilliamMarshQMUL 0:4374caa1ef17 29
WilliamMarshQMUL 0:4374caa1ef17 30 void green_thread() { // method to run in thread
WilliamMarshQMUL 0:4374caa1ef17 31 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 32 Thread::signal_wait(0x1);
WilliamMarshQMUL 0:4374caa1ef17 33 greenLED = false ; // turn on
WilliamMarshQMUL 0:4374caa1ef17 34 Thread::wait(5000);
WilliamMarshQMUL 0:4374caa1ef17 35 greenLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 36 greenThread.signal_clr(0x1) ;
WilliamMarshQMUL 0:4374caa1ef17 37 // Signal are automatically cleared by wait_signal but
WilliamMarshQMUL 0:4374caa1ef17 38 // the signal might have been set again while LED on
WilliamMarshQMUL 0:4374caa1ef17 39 }
WilliamMarshQMUL 0:4374caa1ef17 40 }
WilliamMarshQMUL 0:4374caa1ef17 41
WilliamMarshQMUL 0:4374caa1ef17 42 int main(void) {
WilliamMarshQMUL 0:4374caa1ef17 43 redLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 44 greenLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 45 redThread.start(&red_thread) ; // start the red thread
WilliamMarshQMUL 0:4374caa1ef17 46 greenThread.start(&green_thread) ; // start the green thread
WilliamMarshQMUL 0:4374caa1ef17 47
WilliamMarshQMUL 0:4374caa1ef17 48 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 49 uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39
WilliamMarshQMUL 0:4374caa1ef17 50 // When no touch --> 0
WilliamMarshQMUL 0:4374caa1ef17 51 // Left --> low value Right --> high value
WilliamMarshQMUL 0:4374caa1ef17 52 pc.printf("%d", d) ;
WilliamMarshQMUL 1:e6ffa08ad8bf 53 pc.putc(' ') ;
WilliamMarshQMUL 0:4374caa1ef17 54 if (d == 10) redThread.signal_set(0x1) ;
WilliamMarshQMUL 0:4374caa1ef17 55 if (d == 20) greenThread.signal_set(0x1) ;
WilliamMarshQMUL 1:e6ffa08ad8bf 56 Thread::wait(200); // This polling rate is too slow - increase it
WilliamMarshQMUL 1:e6ffa08ad8bf 57 // The slower rate maks it easier to output on the terminal
WilliamMarshQMUL 0:4374caa1ef17 58 }
WilliamMarshQMUL 0:4374caa1ef17 59 }