no description

Fork of Middleware by Roberto Herrera

Committer:
Jorge_Beltran
Date:
Mon Dec 07 01:50:01 2015 +0000
Revision:
1:32a08ca33b00
Parent:
0:d1ff330c5128
SensorsForGusProject

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RoHe 0:d1ff330c5128 1 /*
RoHe 0:d1ff330c5128 2
RoHe 0:d1ff330c5128 3 Thread.cpp
RoHe 0:d1ff330c5128 4
RoHe 0:d1ff330c5128 5 Only to improve the clarity of main .cpp
RoHe 0:d1ff330c5128 6 This don't have any new classes
RoHe 0:d1ff330c5128 7
RoHe 0:d1ff330c5128 8 All the multiple task are here
RoHe 0:d1ff330c5128 9 for Smart Room project asked by
RoHe 0:d1ff330c5128 10 Gustavo Torres
RoHe 0:d1ff330c5128 11
RoHe 0:d1ff330c5128 12 Last update by RoHe 16/11/2015
RoHe 0:d1ff330c5128 13
RoHe 0:d1ff330c5128 14 */
RoHe 0:d1ff330c5128 15 #include "Threads.h"
RoHe 0:d1ff330c5128 16
RoHe 0:d1ff330c5128 17 #include "Initial.h"
RoHe 0:d1ff330c5128 18 #include "rtos.h"
RoHe 0:d1ff330c5128 19 #include "Ether.h"
RoHe 0:d1ff330c5128 20 #include "EthernetInterface.h"
RoHe 0:d1ff330c5128 21
RoHe 0:d1ff330c5128 22 static void sw2_press(void)
RoHe 0:d1ff330c5128 23 {
RoHe 0:d1ff330c5128 24 ledGREEN= !ledGREEN;
RoHe 0:d1ff330c5128 25
RoHe 0:d1ff330c5128 26 }
RoHe 0:d1ff330c5128 27
RoHe 0:d1ff330c5128 28 static void led_thread(void const *argument)
RoHe 0:d1ff330c5128 29 {
RoHe 0:d1ff330c5128 30 while (true) {
RoHe 0:d1ff330c5128 31 if(_isConnectedServer1 == true && _isConnectedServer2 == true) {
RoHe 0:d1ff330c5128 32 //Color GREEN
RoHe 0:d1ff330c5128 33 ledRED=1;//OFF
RoHe 0:d1ff330c5128 34 ledBLUE=1;//OFF
RoHe 0:d1ff330c5128 35 ledGREEN = !ledGREEN;
RoHe 0:d1ff330c5128 36 } else if(_isConnectedServer1 == true && _isConnectedServer2 == false) {
RoHe 0:d1ff330c5128 37 //Color BLUE
RoHe 0:d1ff330c5128 38 ledGREEN=1;//OFF
RoHe 0:d1ff330c5128 39 ledRED = 1; //OFF
RoHe 0:d1ff330c5128 40 ledBLUE = !ledBLUE;
RoHe 0:d1ff330c5128 41 } else if(_isConnectedServer1 == false && _isConnectedServer2 == true) {
RoHe 0:d1ff330c5128 42 //Color YELLOW = RED and GREEN
RoHe 0:d1ff330c5128 43 ledBLUE =1; //OFF
RoHe 0:d1ff330c5128 44 ledRED = !ledRED;
RoHe 0:d1ff330c5128 45 ledGREEN = !ledGREEN;
RoHe 0:d1ff330c5128 46 } else if(_isConnectedServer1 == false && _isConnectedServer2 == false) {
RoHe 0:d1ff330c5128 47 //Color RED
RoHe 0:d1ff330c5128 48 ledGREEN=1;//OFF
RoHe 0:d1ff330c5128 49 ledBLUE =1; //OFF
RoHe 0:d1ff330c5128 50 ledRED = !ledRED;
RoHe 0:d1ff330c5128 51 }
RoHe 0:d1ff330c5128 52 Thread::wait(1000);
RoHe 0:d1ff330c5128 53 }
RoHe 0:d1ff330c5128 54 }
RoHe 0:d1ff330c5128 55
RoHe 0:d1ff330c5128 56 /*
RoHe 0:d1ff330c5128 57 static bool conect_Server(void)
RoHe 0:d1ff330c5128 58 {
RoHe 0:d1ff330c5128 59 ////////////////////// Body of the funtion////////////////////////
RoHe 0:d1ff330c5128 60 while (socketTCP1.connect(ECHO_SERVER_ADDRESS1, ECHO_SERVER_PORT1) < 0) {
RoHe 0:d1ff330c5128 61 wait(0.5);
RoHe 0:d1ff330c5128 62 return false;
RoHe 0:d1ff330c5128 63 }
RoHe 0:d1ff330c5128 64 return true;
RoHe 0:d1ff330c5128 65 ////////////////////// end if the funtion////////////////////////
RoHe 0:d1ff330c5128 66 }
RoHe 0:d1ff330c5128 67 */
Jorge_Beltran 1:32a08ca33b00 68 static void messsageFromClient(void const *argument)
RoHe 0:d1ff330c5128 69 {
RoHe 0:d1ff330c5128 70
RoHe 0:d1ff330c5128 71 while (true) {
Jorge_Beltran 1:32a08ca33b00 72 pc.printf("Wait for new connection...\n\r");
Jorge_Beltran 1:32a08ca33b00 73 TCPSocketConnection client;
Jorge_Beltran 1:32a08ca33b00 74 serverTCP.accept(client);
Jorge_Beltran 1:32a08ca33b00 75 client.set_blocking(false, 1500); // Timeout after (1.5)s
Jorge_Beltran 1:32a08ca33b00 76
Jorge_Beltran 1:32a08ca33b00 77 pc.printf("Connection from: %s\n\r", client.get_address());
Jorge_Beltran 1:32a08ca33b00 78 char buffer[256];
Jorge_Beltran 1:32a08ca33b00 79 while (true) {
Jorge_Beltran 1:32a08ca33b00 80 int n = client.receive(buffer, sizeof(buffer));
Jorge_Beltran 1:32a08ca33b00 81 if (n <= 0) break;
Jorge_Beltran 1:32a08ca33b00 82
Jorge_Beltran 1:32a08ca33b00 83 // print received message to terminal
Jorge_Beltran 1:32a08ca33b00 84 buffer[n] = '\0';
Jorge_Beltran 1:32a08ca33b00 85 pc.printf("Received message from Client :'%s'\n\r",buffer);
Jorge_Beltran 1:32a08ca33b00 86
Jorge_Beltran 1:32a08ca33b00 87 // print reversed message to terminal
Jorge_Beltran 1:32a08ca33b00 88 pc.printf("Sending message to Client: '%s'\n\r",buffer);
Jorge_Beltran 1:32a08ca33b00 89
Jorge_Beltran 1:32a08ca33b00 90 // Echo received message back to client
Jorge_Beltran 1:32a08ca33b00 91 client.send_all(buffer, n);
Jorge_Beltran 1:32a08ca33b00 92 if (n <= 0) break;
Jorge_Beltran 1:32a08ca33b00 93 }
Jorge_Beltran 1:32a08ca33b00 94 client.close();
RoHe 0:d1ff330c5128 95 Thread::wait(500);
RoHe 0:d1ff330c5128 96 }
RoHe 0:d1ff330c5128 97
RoHe 0:d1ff330c5128 98 }
RoHe 0:d1ff330c5128 99