void main(void) { OSInit(); TMOD = (TMOD & 0XF0) | 0X01; TL0 = 0x0; TH0 = 0x0; TR0 = 1; ET0 = 1; TF0 = 0; PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); PC_DispStr(15, 0, "Small RTOS(51), the Real-Time Kernel(For Keil c51)", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); PC_DispStr(35, 1, "ChenMingJi", DISP_FGND_WHITE); PC_DispStr(35, 3, "EXAMPLE #2", DISP_FGND_WHITE); OSTaskCreate(TaskA, NULL, 0); OSTaskCreate(TaskB, NULL, 1); OSTaskCreate(TaskC, NULL, 2); OSTaskCreate(TaskD, NULL, 3); OSTaskCreate(TaskE, NULL, 4); while(1) { PCON = PCON | 0x01; /* CPU进入休眠状态 */ } }
void DisplayTitle ( void ) { PC_DispStr(20, 0, "uC/OS-II, The Real-Time Kernel's Shell", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); PC_DispStr(30, 1, "Yu Ren on 2005/09/20", DISP_FGND_WHITE); PC_DispStr( 0, 22, "#Tasks : xxxxx CPU Usage: xxx %", DISP_FGND_WHITE); PC_DispStr( 0, 23, "#Task switch/sec: xxxxx", DISP_FGND_WHITE); PC_DispStr(28, 24, "<-PRESS 'ESC' TO QUIT->", DISP_FGND_WHITE + DISP_BLINK); }
void DisplayMessage ( char *sMsg ) { PWINDOWS ptr = GetActiveWindow(); memset(sLine, 0x20, ptr->ex - ptr->sx); sLine[ptr->ex - ptr->sx] = '\0'; PC_DispStr(1, EndY - 1, sLine, ptr->color); // PC_DispClrLine(EndY - 1, DISP_FGND_WHITE + DISP_BGND_BLUE); PC_DispStr(1, EndY - 1, sMsg, ptr->color); }
void DoHelp ( void ) { char buffer[82]; PWINDOWS ptr = GetActiveWindow(); ClearWindow(); memset(buffer, 0x00, 82); sprintf(buffer, "StartAll -- resume all tasks"); PC_DispStr(ptr->cx, ptr->cy, buffer, ptr->color); ptr->cy ++; sprintf(buffer, "StopAll -- suspend all tasks"); PC_DispStr(ptr->cx, ptr->cy, buffer, ptr->color); // printf("Test printf functions!\n"); }
void Task (void *pdata) { FP32 x; FP32 y; FP32 angle; FP32 radians; char s[81]; INT8U ypos; ypos = *(INT8U *)pdata + 7; //pdata points to an 8 bit integer containing the task priority //to make each task calculate different angles, offset each task by 36 degrees angle = (FP32)(*(INT8U *)pdata) * (FP32)36.0; for (;;) { //sin() and cos() assumes radians instead of degrees, and thus the conversion radians = (FP32)2.0 * (FP32)3.141592 * angle / (FP32)360.0; x = cos(radians); y = sin(radians); sprintf(s, " %2d %8.3f %8.3f %8.3f", *(INT8U *)pdata, angle, x, y); PC_DispStr(0, ypos, s, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); if (angle >= (FP32)360.0) { angle = (FP32)0.0; } else { angle += (FP32)0.01; } OSTimeDly(1); } }
void DisplayPrompt ( void ) { PWINDOWS ptr = GetActiveWindow(); PC_DispStr(1, ptr->cy, sCmdPrompt, ptr->color); DisplayCursor (strlen(sCmdPrompt) + 1, ptr->cy); }
/* ********************************************************************************************************* * STATISTIC TASK HOOK ********************************************************************************************************* */ void OSTaskStatHook (void) { char s[80]; INT8U i; INT32U total; INT8U pct; total = 0L; /* Totalize TOT. EXEC. TIME for each task */ for (i = 0; i < 7; i++) { total += TaskUserData[i].TaskTotExecTime; DispTaskStat(i); /* Display task data */ } if (total > 0) { for (i = 0; i < 7; i++) { /* Derive percentage of each task */ pct = (INT8U) (100 * TaskUserData[i].TaskTotExecTime / total); sprintf(s, "%3d %%", pct); PC_DispStr(62, (INT8U) (i + 11), s, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); } } if (total > 1000000000L) { /* Reset total time counters at 1 billion */ for (i = 0; i < 7; i++) { TaskUserData[i].TaskTotExecTime = 0L; } } }
void DisplayStatus ( void ) { char s[100]; sprintf(s, "%5d", OSTaskCtr); /* Display #tasks running */ PC_DispStr(18, 22, s, DISP_FGND_BLUE + DISP_BGND_CYAN); sprintf(s, "%3d", OSCPUUsage); /* Display CPU usage in % */ PC_DispStr(36, 22, s, DISP_FGND_BLUE + DISP_BGND_CYAN); sprintf(s, "%5d", OSCtxSwCtr); /* Display #context switches per second */ PC_DispStr(18, 23, s, DISP_FGND_BLUE + DISP_BGND_CYAN); OSCtxSwCtr = 0; sprintf(s, "V%3.2f", (float)OSVersion() * 0.01); /* Display version number as Vx.yy */ PC_DispStr(75, 24, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); PC_GetDateTime(s); /* Get and display date and time */ PC_DispStr(0, 24, s, DISP_FGND_BLUE + DISP_BGND_CYAN); }
void Task (void *pdata) { unsigned int rand_num; INT8U err; INT8U pos_x, pos_y = 0; FILE* fp; //used for storing information to data.dat char str_send[10]; char str_sum[10]; char filename[25]; static int value[10]; //values send used for calculate the average for (;;) { GetDate(filename); strcat(filename, "Sd.dat"); fp = fopen(filename,"a"); OSSemPend(RandomSem, 0, &err); rand_num = random(500); OSSemPost(RandomSem); sprintf(str_send,"%5d",rand_num); OSQPost(MSGQ,str_send); pos_x = *(INT8U*)pdata - '0'; fprintf(fp,"%s",str_send); PC_DispStr(pos_x*6+5, pos_y + 2, str_send, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); value[pos_x] = (int)rand_num; //if this group of value are gotten, sum and average it if (pos_x == 9) { memset(str_sum,0,sizeof(str_sum)); sprintf(str_sum,"%6.2f",(float)sum(value,10)/10.0); //show sum col PC_DispStr(68, 2 + pos_y, str_sum, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); fprintf(fp,"%8s\n%6s",str_sum," "); memset(value,0,sizeof(value)); } if (pos_y == 7) pos_y = 0; OSTimeDlyHMSM(0,0,1,0); pos_y ++; ///start next row fclose(fp); } }
void TaskRTC(void *pdata) { INT32U clk; char s[24]; pdata = pdata; while(1) { PC_GetDateTime(&s[0]); PC_DispStr(X_INTER1+16, Y_END+1, s, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK); OSTimeDly(1); //delay here for the kernel to switch to another task } }
void TaskClk (void *pdata) { char s[40]; pdata = pdata; for (;;) { PC_GetDateTime(s); PC_DispStr(58, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); OSTimeDlyHMSM(0, 0, 0, 500); } }
void main (void) { int far *bootend; int result; int *test1, *test2; msgIndex = tickCnt = 0; init_queue(&osqueue1); init_queue(&osqueue2); bootend = MK_FP(0x7c0, 510); result = *bootend; if(result == 0xAA55) isFunnyOS = 1; //test1 = osMalloc(10); //strcpy(test1, "testmallcabcdefghi"); //test2 = osMalloc(20); //strcpy(test2, "test2malloc"); PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); /* Clear the screen */ PC_DispStr( 0, 0, " Funny OS Demo ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); //PC_DispChar( 10, 0, '0' + isFunnyOS , DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); //PC_DispStr( 15, 0, test1, DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); //PC_DispStr( 15, 1, test2, DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); //PC_DispChar( 14, 0, *bootend , DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); PC_SetTickRate(18); /* Reprogram tick rate */ //PC_TickISR = &TickISR; PC_TickISR = PC_VectGet(0x8); PC_VectSet(0x8, &TickISR); //asm {int 08h}; // ptask1 = &testTask1; // ptask2 = &testTask2; task[0].ptask = &gameSnake;//&testTask1; task[0].stack_SS = 0x9000; task[0].stack_SP = 0x100; taskCount++; task[1].ptask = &gameTetris;//&task_bubble_sort;//&testTask2; task[1].stack_SS = 0x8000; task[1].stack_SP = 0x100; taskCount++; task[2].ptask = &taskReadKeyBoard; //&task_select_sort; task[2].stack_SS = 0x7000; task[2].stack_SP = 0x100; taskCount++; task[3].ptask = &task_quick_sort; task[3].stack_SS = 0x6000; task[3].stack_SP = 0x100; //taskCount++; for(;;); }
void TaskClk (void *pdata) { char s[40]; pdata = pdata; for (;;) { PC_GetDateTime(s); PC_DispStr(60, 23, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); //display current date and time once a 500ms OSTimeDlyHMSM(0, 0, 0, 500); } }
void DispTaskStat (INT8U id) { char s[80]; sprintf(s, "%-18s %05u %5u %10ld", TaskUserData[id].TaskName, TaskUserData[id].TaskCtr, TaskUserData[id].TaskExecTime, TaskUserData[id].TaskTotExecTime); PC_DispStr(0, (INT8U) (id + 11), s, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); }
void Task1 (void *pdata) { char *msg; INT8U err; pdata = pdata; for (;;) { msg = (char *)OSQPend(MsgQueue, 0, &err); PC_DispStr(70, 13, msg, DISP_FGND_YELLOW + DISP_BGND_BLUE); OSTimeDlyHMSM(0, 0, 0, 100); } }
void Task1 (void *pdata) { char *msg; INT8U err; pdata = pdata; for (;;) { //returns the pointer to the message, MsgQueue is the queue being created using MsgQueue = OSQCreate(&MsgQueueTbl[0], MSG_QUEUE_SIZE); msg = (char *)OSQPend(MsgQueue, 0, &err); //wait forever for a message to arrive PC_DispStr(70, 13, msg, DISP_FGND_YELLOW + DISP_BGND_BLUE); //display the message when it arrives OSTimeDlyHMSM(0, 0, 0, 100); //delayed for 100ms to allow you to see the message received } }
static void TaskStartDisp (void) { char s[80]; sprintf(s, "%5d", OSTaskCtr); /* Display #tasks running */ PC_DispStr(18, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); #if OS_TASK_STAT_EN > 0 sprintf(s, "%3d", OSCPUUsage); /* Display CPU usage in % */ PC_DispStr(36, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); #endif sprintf(s, "%5d", OSCtxSwCtr); /* Display #context switches per second */ PC_DispStr(18, 23, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); #ifdef __WIN32__ sprintf(s, "uCOS-II V%1d.%02d WIN32 V%1d.%02d", OSVersion() / 100, OSVersion() % 100, OSPortVersion() / 100, OSPortVersion() % 100); /* Display uC/OS-II's version number */ #endif #ifdef __LINUX__ sprintf(s, "uCOS-II V%1d.%02d LINUX V%1d.%02d", OSVersion() / 100, OSVersion() % 100, OSPortVersion() / 100, OSPortVersion() % 100); /* Display uC/OS-II's version number */ #endif PC_DispStr(52, 23, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); }
static void TaskStartDisp (void) { char s[80]; sprintf(s, "%5d", OSTaskCtr); /* Display #tasks running */ PC_DispStr(18, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); #if OS_TASK_STAT_EN > 0 sprintf(s, "%3d", OSCPUUsage); /* Display CPU usage in % */ PC_DispStr(36, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); #endif sprintf(s, "%5d", OSCtxSwCtr); /* Display #context switches per second */ PC_DispStr(18, 23, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); sprintf(s, "V%4.2f", (float)OSVersion() * 0.01); /* Display uC/OS-II's version number */ PC_DispStr(75, 24, s, DISP_FGND_YELLOW + DISP_BGND_BLUE); switch (_8087) { /* Display whether FPU present */ case 0: PC_DispStr(71, 22, " NO FPU ", DISP_FGND_YELLOW + DISP_BGND_BLUE); break; case 1: PC_DispStr(71, 22, " 8087 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE); break; case 2: PC_DispStr(71, 22, "80287 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE); break; case 3: PC_DispStr(71, 22, "80387 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE); break; } }
static void TaskStartDispInit(void) { PC_DispStr( 0, 0, " RTOS_Final_Project:Traffic lights Student name:Vu Nguyen ID:1005157 ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); PC_DispStr( 0, 1, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 2, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 3, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 4, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 5, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 6, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 7, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 8, "__________________________________|_______________________|_____________________", DISP_FGND_RED + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 9, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 10, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 11, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 12, "__________________________________|_______________________|_____________________", DISP_FGND_RED + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 13, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 14, " '$' represents a pedestrian | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 15, " 'X' represents car 1 | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 16, " 'Y' represents car 2 | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 17, " 'Z' represents car 3 | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 18, " 'R' represents red light | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 19, " 'Y' represents yellow light | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 20, " 'G' represents green light | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 21, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 22, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 23, " | | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 24, " <-PRESS 'ESC' TO QUIT-> ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK); }
static void TaskStartDispInit (void) { /* 1111111111222222222233333333334444444444555555555566666666667777777777 */ /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ PC_DispStr( 0, 0, " uC/OS-II, The Real-Time Kernel ", DISP_FGND_WHITE + DISP_BGND_RED); #ifdef __WIN32__ PC_DispStr( 0, 1, " Original version by Jean J. Labrosse, 80x86-WIN32 port by Werner Zimmermann ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); #endif #ifdef __LINUX__ PC_DispStr( 0, 1, " Original version by Jean J. Labrosse, 80x86-LINUX port by Werner Zimmermann ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); #endif PC_DispStr( 0, 2, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 3, " EXAMPLE #3 ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 4, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 5, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 6, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 7, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 8, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 9, "Task Name Counter Exec.Time(uS) Tot.Exec.Time(uS) %Tot. ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 10, "----------------- ------- ------------- ----------------- ----- ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 11, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 12, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 13, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 14, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 15, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 16, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 17, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 18, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 19, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 20, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 21, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 22, "#Tasks : CPU Usage: % ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 23, "#Task switch/sec: ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 24, " <-PRESS 'ESC' TO QUIT-> ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); /* 1111111111222222222233333333334444444444555555555566666666667777777777 */ /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ }
void TaskStart ( void *data ) { PWINDOWS ptr = GetActiveWindow(); UBYTE i; char sCmd[MAX_X]; WORD key; WORD index, pos = 0x00; data = data; /* Prevent compiler warning */ OS_ENTER_CRITICAL(); PC_VectSet(0x08, OSTickISR); /* Install uC/OS-II's clock tick ISR */ PC_SetTickRate(OS_TICKS_PER_SEC); /* Reprogram tick rate */ OS_EXIT_CRITICAL(); PC_DispChar(0, 0, ' ', 0x00); PC_DispStr (8, 16, "Determining CPU's capacity ...", DISP_FGND_WHITE); CreateWindow (0, StartY, MAX_X, EndY, DISP_FGND_WHITE, DISP_BGND_BLUE, "[ Prompt Window ]"); CreateTaskWindows(); OSStatInit(); /* Initialize uC/OS-II's statistics */ for (i = 0; i < N_TASKS; i ++) { /* Create N_TASKS identical tasks */ TaskData[i] = '0' + i; /* Each task will display its own letter */ OSTaskCreate(Task, (void *)&TaskData[i], (void *)&TaskStk[i][TASK_STK_SIZE - 1], i + 1); } DisplayTitle(); ClearWindow(); memset(sCmd, 0x00, MAX_X); DisplayPrompt(); for (;;) { DisplayStatus(); // display status of tasks if (PC_GetKey(&key) == TRUE) { /* See if key has been pressed */ if (key == 0x1B) { /* Yes, see if it's the ESCAPE key */ PC_DOSReturn(); /* Return to DOS */ } else if (key == 0x08) // backspace { pos --; pos = pos <= 0x00 ? 0x00 : pos; sCmd[pos] = '\0'; PC_DispChar(ptr->cx + pos + strlen(sCmdPrompt), ptr->cy, ' ', ptr->color); DisplayCursor(ptr->cx + pos + strlen(sCmdPrompt), ptr->cy); } else if (key == 0x0d || key == 0x0a) // enter { if (pos) { index = ParseCommand(sCmd); HandleCommand(index); } memset(sCmd, 0x00, MAX_X); pos = 0x00; ptr->cy ++; if (ptr->cy == ptr->ey - 1) ClearWindow(); DisplayPrompt(); } else if ((key >= '0' && key <= '9') || (key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z')) { if (pos < MAX_X) { sCmd[pos] = (char)key; PC_DispChar(ptr->cx + pos + strlen(sCmdPrompt), ptr->cy, key, ptr->color); DisplayCursor (ptr->cx + pos + strlen(sCmdPrompt) + 1, ptr->cy); } pos ++; } } OSTimeDlyHMSM(0, 0, 0, 200); /* Wait 200 ms */ } }
void TaskTrafficLightsController(void *pdata) { /* * The traffic lights controller is implemented using finite state machine (FSM) * There are 4 states in the FSM: green_red state, yellow_red state, red_green state, and red_flashingGreen state * The controller is simulated based on the real life traffic lights in Finland * However, the small difference is that it controls based on the sensors located at 4 different locations before each of the * crossing lines. */ void *carMsg = ""; void *pedMsg = ""; TtrafLightState currentState = green_red; TtrafLightState nextState = yellow_red; pdata = pdata; while(1) { switch(currentState) { case green_red: carLights = green; pedLights = red; displayCarLights(green); displayPedLights(red); carMsg = OSMboxAccept(CarMbox); pedMsg = OSMboxAccept(PedMbox); if (pedMsg != "PED_REQUEST") { currentState = currentState; } else { nextState = yellow_red; currentState = nextState; } break; case yellow_red: carLights = yellow; pedLights = red; displayCarLights(yellow); displayPedLights(red); pedMsg = OSMboxAccept(PedMbox); carMsg = OSMboxAccept(CarMbox); if (carMsg != "CAR_REQUEST" || carMsg == "CAR_FINISH") { nextState = red_green; currentState = nextState; } else { currentState = currentState; } break; case red_green: carLights = red; pedLights = green; displayCarLights(red); displayPedLights(green); pedMsg = OSMboxAccept(PedMbox); carMsg = OSMboxAccept(CarMbox); if (carMsg != "CAR_REQUEST") { currentState = currentState; } else { nextState = red_flashingGreen; currentState = nextState; } break; case red_flashingGreen: carLights = red; pedLights = flashingGreen; displayCarLights(red); displayPedLights(flashingGreen); carMsg = OSMboxAccept(CarMbox); pedMsg = OSMboxAccept(PedMbox); if (pedMsg == "PED_REQUEST" || pedMsg != "PED_FINISH") { currentState = currentState; } else { nextState = green_red; currentState = nextState; } break; default: PC_DispStr(X_START+1, Y_END-1, "in default state", DISP_FGND_YELLOW + DISP_BGND_LIGHT_GRAY); carLights = red; pedLights = green; displayCarLights(red); displayPedLights(green); pedMsg = OSMboxAccept(PedMbox); carMsg = OSMboxAccept(CarMbox); nextState == red_flashingGreen; currentState = nextState; break; } OSTimeDly(1); //delay here for the kernel to switch to another task } }
/* ********************************************************************************************************* * PRINTINTNUMBER ********************************************************************************************************* */ void PntIntNum(INT16U number, INT8U x, INT8U y) { char str_p[10]; sprintf(str_p,"%5d", number); PC_DispStr(x, y, str_p, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); }
static void TaskStartDispInit (void) { /* 1111111111222222222233333333334444444444555555555566666666667777777777 */ /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ PC_DispStr( 0, 0, " Feng Wendi NO.2012011686 Class: CS1206 ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); PC_DispStr( 0, 1, "Source:(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (AVG) ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 2, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 3, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 4, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 5, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 6, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 7, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 8, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 9, "--------------------------------------------------------------------------------", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 10, "Recei: ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 11, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 12, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 13, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 14, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 15, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 16, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 17, "Times: | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 18, "Max : | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 19, "Min : | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 20, "Avg : | ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 21, " --------------", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 22, "#Tasks : CPU Usage: % ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 23, "#Task switch/sec: ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 24, " <-PRESS 'q' TO QUIT 'b' TO Suspend 'r' TO Resume-> ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK); /* 1111111111222222222233333333334444444444555555555566666666667777777777 */ /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ }
/* ********************************************************************************************************* * PRINTFLOATNUMBER ********************************************************************************************************* */ void PntFltNum(float num, INT8U x, INT8U y) { char str_p[10]; sprintf(str_p,"%6.1f", num); PC_DispStr(x, y, str_p, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); }
/* ********************************************************************************************************* * CLEARGRAPHICAREA ********************************************************************************************************* */ void RemoveGraph(void) { INT8U i; char* white = " "; for (i = 0; i < 4; i++) PC_DispStr(67, 17+i, white, DISP_BGND_LIGHT_GRAY); }
static void TaskStartDispInit (void) { /* 1111111111222222222233333333334444444444555555555566666666667777777777 */ /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ PC_DispStr( 0, 0, " uC/OS-II, The Real-Time Kernel ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK); PC_DispStr( 0, 1, " Jean J. Labrosse ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 2, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 3, " EXAMPLE #4 ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 4, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 5, "TaskPrio Angle cos(Angle) sin(Angle) ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 6, "-------- ----- ---------- ---------- ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 7, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 8, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 9, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 10, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 11, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 12, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 13, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 14, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 15, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 16, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 17, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 18, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 19, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 20, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 21, " ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 22, "#Tasks : CPU Usage: % ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 23, "#Task switch/sec: ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); PC_DispStr( 0, 24, " <-PRESS 'ESC' TO QUIT-> ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK); /* 1111111111222222222233333333334444444444555555555566666666667777777777 */ /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ }
/* ********************************************************************************************************* * RECEIVETASK ********************************************************************************************************* */ void ReceiveTask(void *pdata) { INT8U err; INT8U pos_x = 0, pos_y = 0; INT8U font_color; char *str_receiv; INT16U recev; float result; char avg[10]; char attention[2]; INT16U i = 0; ///used for row control char str_print[10]; INT16U number = 0; //used for calculating averages in receiving data. FILE* fp; char filename[25]; INT16U data_iter = 0; //data_iter/10 is the row and data_iter%10 is the col static INT16U times[10]; //used for count the times received static INT16U data[10]; //used for record all the data. static INT16U max[10]; static INT16U min[10]; static INT16U sumr[10]; pdata = pdata; memset(min,1000,sizeof(min)); //initialize the min array for (;;) { GetDate(filename); strcat(filename,"Rv.dat"); fp = fopen(filename, "a"); str_receiv = OSQPend(MSGQ,20,&err); //request message pos_x = i; i ++; i %= 10; if (pos_y/10 == 7) { pos_y = 0; PC_DispClrScr(DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); TaskStartDispInit(); } PC_DispStr(5+6*pos_x, 10 + pos_y/10, str_receiv, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); fprintf(fp, "%s", str_receiv); recev = atoi(str_receiv); if (data_iter == 70) { data_iter = 0; } data[data_iter%10] = recev; PntIntNum((++ times[data_iter%10]),5+6*pos_x, 17); if (times[9] == 100) { TaskSuspend(); } max[data_iter%10] = (max[data_iter%10] < recev) ? recev : max[data_iter%10]; PntIntNum(max[data_iter%10],5+6*pos_x, 18); min[data_iter%10] = (min[data_iter%10] > recev) ? recev : min[data_iter%10]; PntIntNum(min[data_iter%10],5+6*pos_x, 19); sumr[data_iter%10] += recev; PntFltNum((float)sumr[data_iter%10]/(float)times[data_iter%10],5+6*pos_x,20); DrawGraph(sumr[data_iter%10]/times[data_iter%10], pos_x); data_iter ++; number += recev; result = (float)number/10.0; sprintf(avg,"%6.2f",result); attention[0] = Classify((unsigned int)result); attention[1] = 0; //the string ending strcat(avg,attention); font_color = 0x00 + ((*attention) - 'A'); PC_DispStr(68, 10 + pos_y/10, avg, font_color + DISP_BGND_LIGHT_GRAY); if (pos_y%10 == 9) { fprintf(fp, "%8s\n%6s",avg," "); memset(avg,0,sizeof(avg)); number = 0; RemoveGraph(); } OSTimeDlyHMSM(0, 0, 0, 100); pos_y ++; fclose(fp); } }