// Lab01/2 - for third task void tWrite(void){ while(1){ beLazy(100); char stringBufferPot[50]; char stringBufferTemp[50]; char stringBufferKey[50]; semTake(writeSem, WAIT_FOREVER); sprintf(stringBufferTemp, "Temperature: %d ",AInTemp); //sprintf(stringBufferPot, "Potentiomenter: %d ",AInPot); sprintf(stringBufferKey, "Gedrueckte Taste: %d ", key); if(key == 49){ sprintf(stringBufferPot, "Potentiometer: %d ",AInPot); } else if(key == 50){ //calculation of the voltage based on the input value 58...4095 float k = (5.0/4037); float d = (float)(-k*58); float calc = (float)(k* AInPot+d); sprintf(stringBufferPot, "Potentiometer: %f V ",calc); } else if (key == 51){ sprintf(stringBufferPot, "Potentiometer: %x ", AInPot); } else{ sprintf(stringBufferPot, "Error - key 1,2 or 3 should be used"); } writeToDisplay(0,0, stringBufferPot); writeToDisplay(1,0, stringBufferTemp); writeToDisplay(2,0, stringBufferKey); semGive(writeSem); } }
// for third task void tWrite(void){ while(1){ beLazy(100); char stringBufferPot[50]; char stringBufferTemp[50]; sprintf(stringBufferTemp, "Temperature: %d ",AInTemp); sprintf(stringBufferPot, "Potentiomenter: %d ",AInPot); writeToDisplay(0,0, stringBufferPot); writeToDisplay(1,0, stringBufferTemp); } }
int timer (void) { int t; char output[32]; while(1){ time_t t; struct tm * timeinfo; time ( &t ); timeinfo = localtime ( &t ); semTake (writeDisplay, WAIT_FOREVER); sprintf(output,"Current local time and date: %s", asctime (timeinfo)); writeToDisplay (10, 2, output ); semGive (writeDisplay); beLazy (3000); } }
void tTime(void){ while(1){ time_t timedate; beLazy(3000); count +=3; char stringBufferTime[50]; char stringBufferCount[50]; timedate = time(NULL); semTake(writeSem, WAIT_FOREVER); sprintf(stringBufferTime, "Aktuelle Zeit: %s", ctime(&timedate)); sprintf(stringBufferCount, "Count: %d ",count); writeToDisplay(4,0,stringBufferCount); writeToDisplay(5,0,stringBufferTime); semGive(writeSem); } }
VOID tcpServerWorkTask(int sFd, /* server's socket file descriptor */ char * address, /* client's socket address */ u_short port /* client's socket port */ ) { char clientRequest[200]; //struct request clientRequest; /* request/message from client */ int nRead; /* number of bytes read */ static char replyMsg[] = "Gruess Gott in Vorarlberg\n"; float potiVolt; /* read client request, display message */ writeToDisplay (14, 2, "tcpServerWorkTask" ); write(sFd, replyMsg, sizeof(replyMsg)); while ((nRead = fioRdString(sFd, (char *) &clientRequest, sizeof(clientRequest)))> 0) { //printf("MESSAGE FROM CLIENT (Internet Address %s, port %d):\n%s\n",address, port, clientRequest.message); char output[64]; switch (clientRequest[0]) { case '1': sprintf(output,"Poti Dezimal = %d ", globalPoti); write(sFd, output, 64); break; case '2': potiVolt=(float)(globalPoti*5)/4095; sprintf(output,"Poti Volt = %f ", potiVolt); write(sFd, output, 64); break; case '3': sprintf(output,"Poti Hex = %x ", globalPoti); write(sFd, output, 64); break; default: write(sFd, "Wrong input", 11); } } if (nRead == ERROR) /* error from read() */ perror("read"); close(sFd); /* close server socket connection */ }
STATUS tcpServer(void) { struct sockaddr_in serverAddr; /* server's socket address */ struct sockaddr_in clientAddr; /* client's socket address */ int sockAddrSize; /* size of socket address structure */ int sFd; /* socket file descriptor */ int newFd; /* socket descriptor from accept */ int ix = 0; /* counter for work task names */ char workName[16]; /* name of work task */ /* set up the local address */ sockAddrSize = sizeof(struct sockaddr_in); bzero((char *) &serverAddr, sockAddrSize); serverAddr.sin_family = AF_INET; serverAddr.sin_len = (u_char) sockAddrSize; serverAddr.sin_port = htons(SERVER_PORT_NUM); serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* create a TCP-based socket */ if ((sFd = socket(AF_INET, SOCK_STREAM, 0)) == ERROR) { perror("socket"); return (ERROR); } /* bind socket to local address */ if (bind(sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) { perror("bind"); close(sFd); return (ERROR); } /* create queue for client connection requests */ if (listen(sFd, 4) == ERROR) { perror("listen"); close(sFd); return (ERROR); } /* accept new connect requests and spawn tasks to process them */ FOREVER { writeToDisplay (22, 2, "tcpServer established " ); socklen_t len = sizeof(clientAddr); if ((newFd = accept(sFd, (struct sockaddr *) &clientAddr, &len)) == ERROR) { perror("accept"); close(sFd); return (ERROR); } sprintf(workName, "tTcpWork%d", ix++); if (taskSpawn(workName, SERVER_WORK_PRIORITY, 0, SERVER_STACK_SIZE, (FUNCPTR) tcpServerWorkTask, newFd, (int) inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), 0, 0, 0, 0, 0, 0, 0) == ERROR) { /* if taskSpawn fails, close fd and return to top of loop */ perror("taskSpawn"); close(newFd); } } }
int showInputs (void) { char output[64]; float potiVolt; float tempVolt; while(1){ semTake (writeDisplay, WAIT_FOREVER); sprintf(output,"GlobalKey = %c ", globalKey); writeToDisplay (3, 2, output ); switch(globalKey){ case '1': writeToDisplay (8, 2, " "); sprintf(output,"Temperature = %d ", globalTemp); writeToDisplay (5, 2, output ); sprintf(output,"Poti = %d ", globalPoti); writeToDisplay (6, 2, output ); break; case '2': writeToDisplay (8, 2, " "); potiVolt=(float)(globalPoti*5)/4095; tempVolt=(float)(globalTemp*10)/4095; sprintf(output,"Temperature = %f ", tempVolt); writeToDisplay (5, 2, output ); sprintf(output,"Poti = %f ", potiVolt); writeToDisplay (6, 2, output ); break; case '3': writeToDisplay (8, 2, " "); sprintf(output,"Temperature = %x ", globalTemp); writeToDisplay (5, 2, output ); sprintf(output,"Poti = %x ", globalPoti); writeToDisplay (6, 2, output ); break; default: writeToDisplay (8, 2, "Wrong User Input" ); } semGive (writeDisplay); beLazy (100); } }