Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832 LM75B mbed EthernetInterface mbed-rtos
slave.cpp
00001 #include "slave.h" 00002 #include "EthernetInterface.h" 00003 #include <stdio.h> 00004 #include <ctype.h> 00005 00006 #define MAX_PACKET_SIZE 512 00007 00008 namespace ProjectOne{ 00009 00010 const char* Slave::MASK = "255.255.255.0"; 00011 const char* Slave::GATEWAY = "192.168.0.1"; 00012 00013 EthernetInterface eth_slave; 00014 UDPSocket sock_slave; 00015 Endpoint master; 00016 00017 Slave::Slave(int slave_id, Temperature *temperature, Potentiometer *potentiometer, LCD *lcd, RGB *rgb, BuzzerMusic *buzzerMusic) 00018 { 00019 slaveId = slave_id; 00020 slaveTemp = temperature; 00021 slavePot = potentiometer; 00022 slaveLcd = lcd; 00023 slaveRgb = rgb; 00024 slaveBuzzerMusic = buzzerMusic; 00025 CurrentSlaveState = STATE_INIT; 00026 } 00027 00028 void Slave::handleIncomingFrame(void){ 00029 char in_buffer[MAX_PACKET_SIZE]; 00030 char ack_id[4]; 00031 char response_array[512]; 00032 switch(CurrentSlaveState){ 00033 case STATE_INIT: 00034 { 00035 printf("----------------\r\n"); 00036 printf("Initiating slave\r\n"); 00037 printf("----------------\r\n"); 00038 00039 memset(&ip_address[0], 0, sizeof(ip_address)); 00040 00041 ip_address[0] = '1'; 00042 ip_address[1] = '9'; 00043 ip_address[2] = '2'; 00044 ip_address[3] = '.'; 00045 ip_address[4] = '1'; 00046 ip_address[5] = '6'; 00047 ip_address[6] = '8'; 00048 ip_address[7] = '.'; 00049 ip_address[8] = '0'; 00050 ip_address[9] = '.'; 00051 00052 int temp[3]; 00053 int i = 0; 00054 do{ 00055 temp[i] = slaveId % 10; 00056 slaveId /= 10; 00057 i++; 00058 }while(slaveId !=0); 00059 00060 char tempi[4]; 00061 sprintf(tempi, "%ld", temp[2]); 00062 ip_address[10] = *tempi; 00063 sprintf(tempi, "%ld", temp[1]); 00064 ip_address[11] = *tempi; 00065 sprintf(tempi, "%ld", temp[0]); 00066 ip_address[12] = *tempi; 00067 00068 ip_address[13] = '\0'; 00069 00070 eth_slave.init(ip_address, MASK, GATEWAY); 00071 eth_slave.connect(); 00072 00073 sock_slave.bind(4000); 00074 00075 command_info[0] = 0; 00076 00077 CurrentSlaveState = STATE_WAIT_FOR_FRAME; 00078 break; 00079 } 00080 case STATE_WAIT_FOR_FRAME: 00081 { 00082 printf("Waiting for frame!\r\n"); 00083 int n = sock_slave.receiveFrom(master, in_buffer, sizeof(in_buffer)); 00084 in_buffer[n] = '\0'; 00085 CurrentSlaveState = STATE_HANDLE_FRAME; 00086 break; 00087 } 00088 case STATE_HANDLE_FRAME: 00089 { 00090 printf("Received message from server: '%s'\r\n", in_buffer); 00091 00092 char *p = strtok(in_buffer, " "); 00093 command_info[0] = p; 00094 char *q = strtok(NULL, " "); 00095 command_info[1] = q; 00096 char *s = strtok(NULL, '\0'); 00097 command_info[2] = s; 00098 00099 ack_id[0] = ip_address[10]; 00100 ack_id[1] = ip_address[11]; 00101 ack_id[2] = ip_address[12]; 00102 ack_id[3] = '\0'; 00103 00104 CurrentSlaveState = STATE_HANDLE_REQUEST; 00105 break; 00106 } 00107 case STATE_HANDLE_REQUEST: 00108 { 00109 slaveLcd->clearLcd(); 00110 slaveRgb->turnOnLed(""); 00111 temperatureValue = 0.0; 00112 potentiometerValue = 0.0; 00113 00114 response.clear(); 00115 memset(&response_array[0], 0, sizeof(response_array)); 00116 00117 if(strcmp(command_info[0], "PUT") == 0){ 00118 if(strcmp(command_info[1], "/LCD") == 0){ 00119 slaveLcd->displayChars(command_info[2]); 00120 response.append("ACK").append(" ").append("2.04").append(" ").append(ack_id).append(""); 00121 } 00122 else if(strcmp(command_info[1], "/LED") == 0){ 00123 string kleurtje(command_info[2]); 00124 slaveRgb->turnOnLed(kleurtje); 00125 response.append("ACK").append(" ").append("2.04").append(" ").append(ack_id).append(""); 00126 } 00127 else if(strcmp(command_info[1], "/BUZZER") == 0){ 00128 string indexes(command_info[2]); 00129 slaveBuzzerMusic->playMusic(indexes); 00130 response.append("ACK").append(" ").append("2.04").append(" ").append(ack_id).append(""); 00131 } 00132 else 00133 { 00134 response.append("ACK").append(" ").append("4.0").append(" ").append(ack_id); 00135 } 00136 00137 } 00138 else if(strcmp(command_info[0], "GET") == 0){ 00139 if(strcmp(command_info[1], "/temperature") == 0){ 00140 temperatureValue = slaveTemp->readTemperature(); 00141 char temp[] = ""; 00142 sprintf(temp, "%.3f", temperatureValue); 00143 response.append("ACK").append(" ").append("2.05").append(" ").append(ack_id).append(" ").append(temp); 00144 } 00145 else if(strcmp(command_info[1], "/potentiometer") == 0){ 00146 potentiometerValue = slavePot->getPotValue(); 00147 char temp[] = ""; 00148 sprintf(temp, "%.3f", potentiometerValue); 00149 response.append("ACK").append(" ").append("2.05").append(" ").append(ack_id).append(" ").append(temp); 00150 } 00151 else{ 00152 response.append("ACK").append(" ").append("4.0").append(" ").append(ack_id); 00153 } 00154 } 00155 else{ 00156 response.append("ACK").append(" ").append("4.0").append(" ").append(ack_id); 00157 } 00158 CurrentSlaveState = STATE_SEND_ACKNOWLEDGEMENT; 00159 break; 00160 } 00161 case STATE_SEND_ACKNOWLEDGEMENT: 00162 { 00163 strcpy(response_array, response.c_str()); 00164 sock_slave.sendTo(master,(char *)response_array, 512); 00165 00166 response.clear(); 00167 00168 CurrentSlaveState = STATE_WAIT_FOR_FRAME; 00169 break; 00170 } 00171 00172 } 00173 } 00174 00175 00176 }
Generated on Sat Jul 23 2022 22:59:36 by
1.7.2