//TC 1: Delayed message sending & no preemption void proc1(void) { MSG_BUF* envelope = NULL; char* msg = "SE 350"; uart1_put_string("\n\r"); uart1_put_string("G003_test: START\n\r"); uart1_put_string("G003_test: total "); uart1_put_char(total + 48); uart1_put_string(" tests\n\r"); prev_pid = 1; envelope = (MSG_BUF*)request_memory_block(); strcpy(envelope->mtext, msg); delayed_send(1, envelope, 5000); envelope = (MSG_BUF*)receive_message(NULL); if (strcmp(envelope->mtext, msg) == 0) { prev_success = 1; } else { prev_success = 0; } release_memory_block(envelope); if (prev_success) { uart1_put_string("G003_test: test "); uart1_put_char(1 + 48); uart1_put_string(" OK\n\r"); pass = pass + 1; } else { uart1_put_string("G003_test: test "); uart1_put_char(1 + 48); uart1_put_string(" FAIL\n\r"); } //TC 2: Blocked on receive & preemption envelope = (MSG_BUF*)receive_message(NULL); prev_success = 1; // Checks if you ever get here. release_memory_block(envelope); if (prev_success) { uart1_put_string("G003_test: test "); uart1_put_char(2 + 48); uart1_put_string(" OK\n\r"); pass = pass + 1; } else { uart1_put_string("G003_test: test "); uart1_put_char(2 + 48); uart1_put_string(" FAIL\n\r"); } while (1) { release_processor(); } }
/** 设置上台参数 **/ void setReady() { while(1) { do { uart1_put_str("\n\n\r上台参数:"); uart1_put_str("\n\r1.设置速度"); uart1_put_str("\n\r2.设置上台时延"); uart1_put_str("\n\r0.返回"); uart1_put_str("\n\r-> "); select = uart1_get_char(); uart1_put_char(select); } while( select != '1' && select != '2' && select != '3' && select != '0'); if(select == '0') break; switch(select) { case '1': // 设置左速度 uart1_put_str("\n\n\r设置上台速度:"); do { uart1_put_str("\n\r速度左(-9999~09999): "); for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; READYSPEEDL = atoi(buf); } while(READYSPEEDL < -9999 || READYSPEEDL > 9999); do { uart1_put_str("\n\r速度右(-9999~09999): "); for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; READYSPEEDR = atoi(buf); } while(READYSPEEDR < -9999 || READYSPEEDR > 9999); uart1_put_str("\n\r设置成功!"); break; case '2': // 设置上台时延 do { uart1_put_str("\n\r上台时延(0000~9999): "); for(i=0; i<4; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[4] = '\0'; MSGOUP = atoi(buf); } while( MSGOUP > 9999 ||MSGOUP < 0 ); uart1_put_str("\n\r设置成功!"); break; default: break; } } }
/** * Prints five numbers and then releases a memory block */ void test_proc_p1_b_2(void) { int i = 0; int ret_val = 20; void* p_mem_blk; p_mem_blk = request_memory_block(); set_process_priority(PID_P2, MEDIUM); while ( 1) { if ( i != 0 && i % 5 == 0 ) { uart1_put_string("\n\r"); ret_val = release_memory_block(p_mem_blk); #ifdef DEBUG_0 printf("proc2: ret_val=%d\n", ret_val); #endif /* DEBUG_0 */ if ( ret_val == -1 ) { break; } } uart1_put_char('0' + i % 10); i++; } uart1_put_string("proc2: end of testing\n\r"); set_process_priority(PID_P2, LOWEST); while ( 1 ) { release_processor(); } }
/** * @brief: a process that prints 4x5 numbers */ void test_proc_p1_a_2(void) { int i = 0; int ret_val = 20; int counter = 0; while ( 1) { if ( i != 0 && i % 5 == 0 ) { uart1_put_string("\n\r"); counter++; if ( counter == 4 ) { ret_val = set_process_priority(PID_P1, HIGH); break; } else { ret_val = release_processor(); } #ifdef DEBUG_0 printf("proc2: ret_val=%d\n", ret_val); #endif /* DEBUG_0 */ } uart1_put_char('0' + i % 10); i++; } uart1_put_string("proc2 end of testing\n\r"); while ( 1 ) { release_processor(); } }
/** 设置推棋子参数 **/ void setPush() { while(1) { do { uart1_put_str("\n\n\r推棋参数:"); uart1_put_str("\n\r1.设置速度"); uart1_put_str("\n\r2.设置记忆周期"); uart1_put_str("\n\r0.返回"); uart1_put_str("\n\r-> "); select = uart1_get_char(); uart1_put_char(select); } while( select != '1' && select != '2' && select != '3' && select != '0'); if(select == '0') break; switch(select) { case '1': // 设置推棋子速度 do{ uart1_put_str("\n\n\r设置推棋子速度(0000~9999):"); for(i=0; i<4; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[4] = '\0'; SPEEDPUSH = atoi(buf); } while(SPEEDPUSH < 0 || SPEEDPUSH > 9999); uart1_put_str("\n\r设置成功!"); break; case '2': // 设置推棋子记忆周期 do { uart1_put_str("\n\r推棋子记忆周期(0000~9999): "); for(i=0; i<4; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[4] = '\0'; MEMORYCYCLE_PUSH = atoi(buf); } while( MEMORYCYCLE_PUSH > 9999 ||MEMORYCYCLE_PUSH < 0 ); uart1_put_str("\n\r设置成功!"); break; default: break; } } }
//TC 2: Blocked on receive & preemption void proc2(void) { MSG_BUF* envelope = 0; char* msg1 = "SE 350"; char* msg2 = "LAB"; envelope = (MSG_BUF*)request_memory_block(); strcpy(envelope->mtext, msg1); send_message(1, envelope); //TC 3: IPC envelope = (MSG_BUF*)request_memory_block(); strcpy(envelope->mtext, msg2); send_message(3, envelope); envelope = (MSG_BUF*)receive_message(NULL); if (strcmp(envelope->mtext, msg2) == 0) { prev_success = 1; } else { prev_success = 0; } release_memory_block(envelope); if (prev_success) { uart1_put_string("G003_test: test "); uart1_put_char(3 + 48); uart1_put_string(" OK\n\r"); pass = pass + 1; } else { uart1_put_string("G003_test: test "); uart1_put_char(3 + 48); uart1_put_string(" FAIL\n\r"); } set_process_priority(4, HIGH); while (1) { release_processor(); } }
/** 设置角落权值 **/ void setCornerRation() { do { uart1_put_str("\n\n\r设置角落权值(0~9):"); buf[0] = uart1_get_char(); uart1_put_char(buf[0]); buf[1] = '\0'; CORNERRATION = atoi(buf); } while( CORNERRATION < 0 || CORNERRATION > 9); uart1_put_str("\n\r设置成功!"); }
/** * @brief: c UART0 IRQ Handler */ void c_UART0_IRQHandler(void) { uint8_t IIR_IntId; // Interrupt ID from IIR LPC_UART_TypeDef *pUart = (LPC_UART_TypeDef *)LPC_UART0; #ifdef DEBUG_0 uart1_put_string("Entering c_UART0_IRQHandler\n\r"); #endif // DEBUG_0 /* Reading IIR automatically acknowledges the interrupt */ IIR_IntId = (pUart->IIR) >> 1 ; // skip pending bit in IIR if (IIR_IntId & IIR_RDA) { // Receive Data Avaialbe /* read UART. Read RBR will clear the interrupt */ g_char_in = pUart->RBR; #ifdef DEBUG_0 uart1_put_string("Reading a char = "); uart1_put_char(g_char_in); uart1_put_string("\n\r"); #endif // DEBUG_0 g_buffer[12] = g_char_in; // nasty hack g_send_char = 1; } else if (IIR_IntId & IIR_THRE) { /* THRE Interrupt, transmit holding register becomes empty */ if (*gp_buffer != '\0' ) { g_char_out = *gp_buffer; #ifdef DEBUG_0 //uart1_put_string("Writing a char = "); //uart1_put_char(g_char_out); //uart1_put_string("\n\r"); // you could use the printf instead printf("Writing a char = %c \n\r", g_char_out); #endif // DEBUG_0 pUart->THR = g_char_out; gp_buffer++; } else { #ifdef DEBUG_0 uart1_put_string("Finish writing. Turning off IER_THRE\n\r"); #endif // DEBUG_0 pUart->IER ^= IER_THRE; // toggle the IER_THRE bit pUart->THR = '\0'; g_send_char = 0; gp_buffer = g_buffer; } } else { /* not implemented yet */ #ifdef DEBUG_0 uart1_put_string("Should not get here!\n\r"); #endif // DEBUG_0 return; } }
void proc5(void) { //END of TC 3d: all processes except proc5 are blocked if (prev_pid == 4 && prev_success) { uart1_put_string("G003_test: test "); uart1_put_char(3 + 48); uart1_put_string(" OK\n\r"); pass = pass + 1; } else { uart1_put_string("G003_test: test "); uart1_put_char(3 + 48); uart1_put_string(" FAIL\n\r"); } uart1_put_string("G003_test: "); uart1_put_char(pass + 48); uart1_put_string("/"); uart1_put_char(total + 48); uart1_put_string(" tests OK\n\r"); uart1_put_string("G003_test: "); uart1_put_char((total - pass) + 48); uart1_put_string("/"); uart1_put_char(total + 48); uart1_put_string(" tests FAIL\n\r"); uart1_put_string("G003_test: END\n\r"); while (1) { mem[index++] = request_memory_block(); } }
/** 设置记忆周期 **/ void setMemoryCycle() { do { uart1_put_str("\n\n\r设置记忆周期(0000~9999ms):"); for(i=0; i<4; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[4] = '\0'; MEMORYCYCLE_INIT = atoi(buf); } while( MEMORYCYCLE_INIT < 0 || MEMORYCYCLE_INIT > 9999); uart1_put_str("\n\r设置成功!"); }
/** 设置状态周期 **/ void setStateCycle() { do { uart1_put_str("\n\n\r设置状态周期(000~999ms):"); for(i=0; i<3; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[3] = '\0'; STATECYCLE = atoi(buf); } while( STATECYCLE < 0 || STATECYCLE > 999); uart1_put_str("\n\r设置成功!"); }
/** * Prints five uppercase letters and request a memory block. */ void test_proc_p1_b_1(void) { int i = 0; void* p_mem_blk; while ( 1 ) { if ( i != 0 && i % 5 == 0 ) { uart1_put_string("\n\r"); p_mem_blk = request_memory_block(); #ifdef DEBUG_0 printf("proc1: p_mem_blk=0x%x\n", p_mem_blk); #endif /* DEBUG_0 */ } uart1_put_char('A' + i % 26); i++; } }
//TC 4: KCD and CRT void proc4(void) { MSG_BUF* msg = NULL; msg = (MSG_BUF*)request_memory_block(); msg->mtype = KCD_REG; msg->mtext[0] = '%'; msg->mtext[1] = 'T'; msg->mtext[2] = 'E'; msg->mtext[3] = 'S'; msg->mtext[4] = 'T'; msg->mtext[4] = '\0'; send_message(PID_KCD, msg); msg = (MSG_BUF*)request_memory_block(); msg->mtype = CRT_DISPLAY; strcpy(msg->mtext, "Input %TEST and press enter:\n\r"); send_message(PID_CRT, msg); msg = receive_message(NULL); if (strcmp(msg->mtext, "%TEST") == 0) { prev_success = 1; } else { prev_success = 0; } release_memory_block(msg); if (prev_success) { uart1_put_string("G003_test: test "); uart1_put_char(4 + 48); uart1_put_string(" OK\n\r"); pass = pass + 1; } else { uart1_put_string("G003_test: test "); uart1_put_char(4 + 48); uart1_put_string(" FAIL\n\r"); } uart1_put_string("G003_test: "); uart1_put_char(pass + 48); uart1_put_string("/"); uart1_put_char(total + 48); uart1_put_string(" tests OK\n\r"); uart1_put_string("G003_test: "); uart1_put_char((total - pass) + 48); uart1_put_string("/"); uart1_put_char(total + 48); uart1_put_string(" tests FAIL\n\r"); uart1_put_string("G003_test: END\n\r"); while (1) { release_processor(); } }
/** * @brief call back function for printf * NOTE: first paramter p is not used for now. */ void putc(void *p, char c) { if (p != NULL) { if (active_uart == 0) { uart0_put_string("putc: first parameter needs to be NULL"); } else { uart1_put_string("putc: first parameter needs to be NULL"); } } else { if (active_uart == 0) { uart0_put_char(c); } else { uart1_put_char(c); } } }
/** * @brief: a process that prints 5x6 numbers * and then yields the cpu. */ void proc2(void) { int i = 0; int ret_val = 20; int x = 0; while ( 1) { if ( i != 0 && i%5 == 0 ) { uart1_put_string("\n\r"); if ( i%30 == 0 ) { ret_val = release_processor(); #ifdef DEBUG_0 printf("proc2: ret_val=%d\n", ret_val); #endif /* DEBUG_0 */ } for ( x = 0; x < 500000; x++); // some artifical delay } uart1_put_char('0' + i%10); i++; } }
void print_char_debug(char message) { #ifdef DEBUG_0 uart1_put_char(message); #endif }
void proc6(void) { // TC 2a: change itself to be lower than the max: PASS if prev pid is 1 if (prev_pid != 1) { prev_success = 0; // At here, priority of the procs: // 1: LOW // 2, 3, 4, 5: LOWEST // 6: MEDIUM } //TC 2b: set itself to a higher one with currently being the higeest -> still itself prev_pid = 6; set_process_priority(6, HIGH); //TC 2b: set itself to a higher one with currently being the higeest -> still itself: PASS if prev_pid is 6 if (prev_pid != 6) { prev_success = 0; // At here, priority of the procs: // 1: LOW // 2, 3, 4, 5: LOWEST // 6: HIGH } //TC 2c: set itself to be the same as the currently highest other than itself -> switch to that highest prev_pid = 6; set_process_priority(6, LOW); // TC 2d: change a proc s.t. B=A to B>A, A is current if (prev_pid != 1) { prev_success = 0; // At here, priority of the procs: // 1: LOW // 2, 3, 4, 5: LOWEST // 6: HIGH } // TC 2e: change a proc s.t. B<A to B=A, A is current, then B should be run prev_pid = 6; set_process_priority(1, HIGH); // TC 2g: change a proc s.t. B<A to B>A, A is current, then jump to B if (prev_pid != 1) { prev_success = 0; // At here, priority of the procs: // 1: MEDIUM // 2, 3, 4, 5: LOWEST // 6: HIGH } // TC 2h: change itself to be the same priority of itself: no change prev_pid = 6; set_process_priority(6, HIGH); // Finish TC 2 if (prev_pid == 6 && prev_success) { uart1_put_string("G003_test: test "); uart1_put_char(2 + 48); uart1_put_string(" OK\n\r"); pass = pass + 1; } else { uart1_put_string("G003_test: test "); uart1_put_char(2 + 48); uart1_put_string(" FAIL\n\r"); } // END of TC 2 prev_success = 1; // Start of TC 3 // TC 3a: null process operations // 1: MEDIUM // 2, 3, 4, 5: LOWEST // 6: HIGH if (set_process_priority(0, LOW) != RTX_ERR) { prev_success = 0; } set_process_priority(1, LOW); set_process_priority(2, MEDIUM); // TC 3b: allocate all memory blocks, free one, return // 1: LOW // 2: MEDIUM // 3, 4, 5: LOWEST // 6: HIGH, will be blocked while (prev_pid == 6) { mem[index++] = request_memory_block(); } // proc 6 should be preempted and go to 2 // TC 3b: proc_6 is return from blocked queue to ready queue and is the highest priority process if (prev_pid != 2) { prev_success = 0; } prev_pid = 6; // TC 3c: with 2 procs at highest priority, block both procs, then release memory in another proc to return to proc_6 set_process_priority(3, HIGH); // jumps to proc_3 // 1: LOW // 2: MEDIUM // 3: HIGH, blocked // 4, 5: LOWEST // 6: HIGH, will be blocked prev_pid = 6; mem[index++] = request_memory_block(); // blocks proc_6, goes to proc_2 }
void proc1(void) { // Start of TC 1 //TC 1: allocation and deallocation void* test_blk1 = NULL; void* test_blk2 = NULL; int status1 = 1; int status2 = 1; uart1_put_string("\n\r"); uart1_put_string("G003_test: START\n\r"); uart1_put_string("G003_test: total "); uart1_put_char(total + 48); uart1_put_string(" tests\n\r"); test_blk1 = request_memory_block(); test_blk2 = request_memory_block(); if (test_blk1 == test_blk2 || test_blk1 == NULL || test_blk2 == NULL) { prev_success = 0; } status1 = release_memory_block(test_blk1); status2 = release_memory_block(test_blk2); if (status1 != 0 || status2 != 0) { prev_success = 0; } // End of TC 1 if (prev_success) { uart1_put_string("G003_test: test "); uart1_put_char(1 + 48); uart1_put_string(" OK\n\r"); pass = pass + 1; } else { uart1_put_string("G003_test: test "); uart1_put_char(1 + 48); uart1_put_string(" FAIL\n\r"); } prev_success = 1; // Start of TC 2 // TC 2a: change itself to be lower than the max prev_pid = 1; set_process_priority(1, LOW); // set_process_priority(1, HIGH); if (prev_pid != 6) { prev_success = 0; } // At here, priority of the procs: // 1: LOW // 2, 3, 4, 5: LOWEST // 6: LOW // TC 2d: change a proc s.t. B=A to B>A, A is current prev_pid = 1; set_process_priority(6, HIGH); // should go to 6 // TC 2e: change a proc s.t. B<A to B=A, A is current, then B should be run if (prev_pid != 6) { prev_success = 0; } // At here, priority of the procs: // 1: HIGH // 2, 3, 4, 5: LOWEST // 6: HIGH // TC 2f: change a proc s.t. B=A to B<A, A is current, then still A prev_pid = 1; set_process_priority(6, LOW); // TC 2f: change a proc s.t. B=A to B<A, A is current, then still A if (prev_pid != 1) { prev_success = 0; } // At here, priority of the procs: // 1: HIGH // 2, 3, 4, 5: LOWEST // 6: LOW // TC 2g: change a proc s.t. B<A to B>A, A is current, then jump to B // First, change itself to be MEDIUM set_process_priority(1, MEDIUM); if (prev_pid != 1) { prev_success = 0; } prev_pid = 1; // Then, change 6 to HIGH set_process_priority(6, HIGH); // If TC 2 ever reaches here, TC 2 fails if (prev_pid == 1) { uart1_put_string("G003_test: test "); uart1_put_char(2 + 48); uart1_put_string(" FAIL\n\r"); } // TC 3d: Block everything and preempt to null process after starvation if (prev_pid != 3) { prev_success = 0; } // 1: MEDIUM // 2: LOW // 3: HIGH, blocked // 4, 5: LOWEST // 6: HIGH, blocked prev_pid = 1; mem[index++] = request_memory_block(); }
/** 设置电机 **/ void setMotor() { while(1) { do { uart1_put_str("\n\n\r电机选项:"); uart1_put_str("\n\r1.设置电机ID"); uart1_put_str("\n\r2.设置最大速度"); uart1_put_str("\n\r3.设置转弯速度"); uart1_put_str("\n\r4.设置安全参数"); uart1_put_str("\n\r5.设置攻击参数"); uart1_put_str("\n\r0.返回"); uart1_put_str("\n\r-> "); select = uart1_get_char(); uart1_put_char(select); } while( select != '1' && select != '2' && select != '3' && select != '4' && select != '5' && select != '0'); if( select == '0') break; switch(select) { case '1': // 设置电机ID uart1_put_str("\n\n\r设置电机ID:"); do { uart1_put_str("\n\r电机左(00~99): "); for(i=0; i<2; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[2] = '\0'; NODE_L = atoi(buf); } while( NODE_L < 0 || NODE_L > 99); do { uart1_put_str("\n\r电机右(00~99): "); for(i=0; i<2; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[2] = '\0'; NODE_R = atoi(buf); } while(NODE_R < 0 || NODE_R > 99); uart1_put_str("\n\r设置成功!"); break; case '2': // 设置最大速度 uart1_put_str("\n\n\r设置最大速度(00000 ~ 20000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; TOPSPEED = atoi(buf); } while(TOPSPEED < 0 || TOPSPEED > 20000); uart1_put_str("\n\r设置成功!"); break; case '3': // 设置转弯速度 uart1_put_str("\n\n\r设置转弯速度(00000 ~ 20000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; TURNSPEED = atoi(buf); } while(TURNSPEED < 0 || TURNSPEED > 20000); uart1_put_str("\n\r设置成功!"); break; case '4': // 设置安全参数 uart1_put_str("\n\n\r设置安全参数:"); uart1_put_str("\n\r安全速度(00000~20000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; SAFESPEED = atoi(buf); } while(SAFESPEED < 0 || SAFESPEED > 20000); uart1_put_str("\n\r安全加、减速度(00000~10000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; SAFERATION = atoi(buf); } while(SAFERATION < 0 || SAFERATION > 10000); uart1_put_str("\n\r设置成功!"); break; case '5': // 设置攻击参数 uart1_put_str("\n\n\r设置攻击参数:"); uart1_put_str("\n\r一档速度(00000~20000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; ATTACKGEAR1 = atoi(buf); } while(ATTACKGEAR1 < 0 || ATTACKGEAR1 > 20000); uart1_put_str("\n\r二档速度(00000~20000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; ATTACKGEAR2 = atoi(buf); } while(ATTACKGEAR2 < 0 || ATTACKGEAR2 > 20000); uart1_put_str("\n\r三档速度(00000~20000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; ATTACKGEAR3 = atoi(buf); } while(ATTACKGEAR3 < 0 || ATTACKGEAR3 > 20000); uart1_put_str("\n\r四档速度(00000~20000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; ATTACKGEAR4 = atoi(buf); } while(ATTACKGEAR4 < 0 || ATTACKGEAR4 > 20000); uart1_put_str("\n\r攻击加、减速度(00000~50000):"); do { for(i=0; i<5; i++) { buf[i] = uart1_get_char(); uart1_put_char(buf[i]); } buf[5] = '\0'; ATTACKRATION = atoi(buf); } while(ATTACKRATION < 0 || ATTACKRATION > 50000); uart1_put_str("\n\r设置成功!"); break; } } }
/* * 对战程序,包括根据状态做出动作、走向擂台中心、陷入边缘危机处理 * 调用方法:int main() { init_device(); fight(); } */ void fight() { // 开启检测边缘中断,使用上升沿触发 cli(); EICRB |= (1<<ISC71) |(1<<ISC70) |(1<<ISC61) |(1<<ISC60) |(1<<ISC51) |(1<<ISC50) |(1<<ISC41) |(1<<ISC40) ; EIMSK |=(1<<INT7) |(1<<INT6) |(1<<INT5) |(1<<INT4); sei(); while(1) { txData(); printData(); uart1_put_char(0x0d); switch(vehicle.locEnemy) { case NO_ENEMY: // 未发现敌人 noEnemy(); break; CASE_FACE_ENEMY // 面对敌人 fight_FACE_ENEMY(); break; CASE_BACK_ENEMY // 背对敌人 fight_BACK_ENEMY(); break; CASE_LEFT_ENEMY // 左侧敌人 fight_LEFT_ENEMY(); break; CASE_RIGHT_ENEMY // 右侧敌人 fight_RIGHT_ENEMY(); break; CASE_FRRI_ENEMY // 右前敌人 fight_FRRI_ENEMY(); break; CASE_FRLE_ENEMY // 左前敌人 fight_FRLE_ENEMY(); break; CASE_BALE_ENEMY // 左后敌人 fight_BALE_ENEMY(); break; CASE_BARI_ENEMY // 右后敌人 fight_BARI_ENEMY(); break; default: // 汇总当多个方位检查到敌人的情况 if(sensor.io_f == 0) vehicle.locEnemy = FACE_ENEMY; else if(sensor.io_fr == 0) vehicle.locEnemy = FRRI_ENEMY; else if(sensor.io_fl == 0) vehicle.locEnemy = FRLE_ENEMY; else if(sensor.io_l == 0) vehicle.locEnemy = LEFT_ENEMY; else if(sensor.io_r == 0) vehicle.locEnemy == RIGHT_ENEMY; else if(sensor.io_bl == 0) vehicle.locEnemy = BALE_ENEMY; else if(sensor.io_br == 0) vehicle.locEnemy = BARI_ENEMY; else if(sensor.io_b == 0) vehicle.locEnemy = BACK_ENEMY; break; } } }
/** * @brief: c UART0 IRQ Handler */ void uart_iprocess(void) { uint8_t IIR_IntId; // Interrupt ID from IIR LPC_UART_TypeDef *pUart = (LPC_UART_TypeDef *)LPC_UART0; __disable_irq(); #ifdef DEBUG_0 //uart1_put_string("Entering c_UART0_IRQHandler\n\r"); #endif // DEBUG_0 /* Reading IIR automatically acknowledges the interrupt */ IIR_IntId = (pUart->IIR) >> 1 ; // skip pending bit in IIR if (IIR_IntId & IIR_RDA) { // Receive Data Avaialbe /* read UART. Read RBR will clear the interrupt */ // Perform a 'context switch' PCB *old_proc = gp_current_process; gp_current_process = k_get_process(PID_UART_IPROC); g_char_in = pUart->RBR; #ifdef DEBUG_0 uart1_put_string("Reading a char = "); uart1_put_char(g_char_in); uart1_put_string("\n\r"); #endif // DEBUG_0 // process the character. If it is a hotkey, call the corresponding function // If we are reading, fall through to default and add to the command buffer switch (g_char_in) { case '\r': case '\n': if (g_is_reading) { // We've finished reading a command, send it to the KCD process MSG_BUF *message; g_is_reading = 0; strcpy("\n\r", (char *)(g_in_buffer + g_in_index)); if (is_memory_available()) { message = k_request_memory_block(); message->mtype = DEFAULT; strcpy((char *)g_in_buffer, message->mtext); k_send_message(PID_KCD, message); } g_in_index = 0; } break; case '%': if (!g_is_reading) { // Start reading a command g_is_reading = 1; g_in_index = 1; g_in_buffer[0] = '%'; break; } #if defined(DEBUG_0) && defined(_DEBUG_HOTKEYS) case READY_Q_COMMAND: if (!g_is_reading) { print_ready(); break; } case MEMORY_Q_COMMAND: if (!g_is_reading) { print_mem_blocked(); break; } case RECEIVE_Q_COMMAND: if (!g_is_reading) { print_receive_blocked(); break; } case MESSAGE_COMMAND: if (!g_is_reading) { print_messages(); break; } #endif /* DEBUG HOTKEYS */ default: if (g_is_reading) { // TODO: check bounds g_in_buffer[g_in_index++] = g_char_in; } } gp_current_process = old_proc; } else if (IIR_IntId & IIR_THRE) { /* THRE Interrupt, transmit holding register becomes empty */ // Check for messages and load the buffer // Perform a 'context switch' to the i-process MSG_BUF *message; PCB *old_proc = gp_current_process; gp_current_process = k_get_process(PID_UART_IPROC); // Don't block waiting for a message while (is_message(PID_UART_IPROC)) { int sender = PID_CRT; char *c; // Receive message and copy it to the buffer message = k_receive_message(&sender); c = message->mtext; //dprintf("Copying message to buffer: %s\n\r", message->mtext); if (*c != '\0') { do { g_out_buffer[g_out_end] = *c; g_out_end = (g_out_end + 1) % OUTPUT_BUFF_SIZE; c++; } while (g_out_end != g_out_start && *c != '\0'); } k_release_memory_block(message); } // Check if there is something in the circular buffer if (g_out_start != g_out_end) { g_char_out = g_out_buffer[g_out_start]; pUart->THR = g_char_out; g_out_start = (g_out_start + 1) % OUTPUT_BUFF_SIZE; } else { // nothing to print, disable the THRE interrupt pUart->IER ^= IER_THRE; // toggle (disable) the IER_THRE bit } gp_current_process = old_proc; } else { /* not implemented yet */ #ifdef DEBUG_0 //uart1_put_string("Should not get here!\n\r"); #endif // DEBUG_0 __enable_irq(); return; } __enable_irq(); }
void menu() { myuart_init(); _delay_ms(1000); uart1_put_str("\n\n\r设置模式启动,正在读取信息..."); //从EEPROM中读取参数 eeprom_rOw_par(EEP_R); uart1_put_str("\n\r读取成功!"); while(1) { buf[0] = 'n'; buf[1] = 'n'; buf[2] = 'n'; buf[3] = 'n'; buf[4] = 'n'; do { uart1_put_str("\n\n\r您要进行哪项操作?"); uart1_put_str("\n\r1.打印当前信息"); uart1_put_str("\t2.设置推棋参数"); uart1_put_str("\t3.进行灰度采样"); uart1_put_str("\t4.设置角落权值"); uart1_put_str("\n\r5.设置状态周期"); uart1_put_str("\t6.设置记忆周期"); uart1_put_str("\t7.设置上台参数"); uart1_put_str("\t8.设置电机参数"); uart1_put_str("\n\rl.重读"); uart1_put_str("\tw.保存"); uart1_put_str("\n\r-> "); select = uart1_get_char(); uart1_put_char(select); } while( select != '1' && select != '2' && select != '3' && select != '4' && select != '5' && select != '6' && select != '7' && select != '8' && select != 'l' && select != 'w' ); switch(select) { case '1': // 打印当前信息 printMessage(); break; case '2': // 设置推棋子参数 setPush(); break; case '3': // 灰度采样 refresh_stage_AD(); break; case '4': // 设置角落权值 setCornerRation(); break; case '5': // 设置状态周期 setStateCycle(); break; case '6': // 设置记忆周期 setMemoryCycle(); break; case '7': // 设置上台参数 setReady(); break; case '8': // 设置电机参数 setMotor(); break; case 'w': // 保存 uart1_put_str("\n\n\r正在保存..."); eeprom_rOw_par(EEP_W); uart1_put_str("\n\r保存成功!"); break; case 'l': // 重新读取 uart1_put_str("\n\n\r正在重新读取信息..."); eeprom_rOw_par(EEP_R); uart1_put_str("\n\r读取成功!"); break; } } }
void uart1_put_str(char* str) { int i = 0; while(str[i] != '\0') uart1_put_char(str[i++]); }