void com_save(char *text[], char *path) { //删除旧有文件 unlink(path); //新建文件并打开 int fd = open(path, O_WRONLY|O_CREATE); if (fd == -1) { printf(1, "save failed, file can't open:\n"); setProgramStatus(SHELL); exit(); } if (text[0] == NULL) { close(fd); return; } //写数据 write(fd, text[0], strlen(text[0])); int i = 1; for (; text[i] != NULL; i++) { printf(fd, "\n"); write(fd, text[i], strlen(text[i])); } close(fd); printf(1, "saved successfully\n"); changed = 0; return; }
void dearmVehicle(){ int i = 1; for (i = 1; i <= NUM_CHANNELS; i++){ setPWM(i, MIN_PWM); } setProgramStatus(UNARMED); p_priority numPacket = PRIORITY1; while (!vehicleArmed){ imuCommunication(); asm("CLRWDT"); writeDatalink(numPacket%3); //TODO: Change this for multiple packets readDatalink(); inboundBufferMaintenance(); outboundBufferMaintenance(); Delay(200); asm("CLRWDT"); numPacket = (numPacket + 1) % 3; } setProgramStatus(MAIN_EXECUTION); }
void armVehicle(int delayTime){ setProgramStatus(ARMING); asm("CLRWDT"); Delay(delayTime); asm("CLRWDT"); setPWM(ROLL_OUT_CHANNEL, 0); setPWM(PITCH_OUT_CHANNEL, 0); setPWM(THROTTLE_OUT_CHANNEL, MIN_PWM); setPWM(YAW_OUT_CHANNEL, 0); setPWM(FLAP_OUT_CHANNEL, MIN_PWM); asm("CLRWDT"); Delay(delayTime); asm("CLRWDT"); }
void initialization(int* outputSignal){ setPWM(THROTTLE_OUT_CHANNEL, MIN_PWM); p_priority numPacket = PRIORITY1; setProgramStatus(UNARMED); // char str[20]; // sprintf(str,"AM:%d, PM:%d",sizeof(AMData), sizeof(PMData)); // debug(str); while (!vehicleArmed){ imuCommunication(); checkDMA(); asm("CLRWDT"); writeDatalink(numPacket%3); readDatalink(); inboundBufferMaintenance(); outboundBufferMaintenance(); asm("CLRWDT"); Delay(200); asm("CLRWDT"); numPacket = (numPacket + 1) % 3; } }
void com_exit(char *text[], char *path) { //询问是否保存 if (changed == 1) { printf(1, "save the file? y/n\n"); char input[MAX_LINE_LENGTH] = {}; gets(input, MAX_LINE_LENGTH); input[strlen(input)-1] = '\0'; if (strcmp(input, "y") == 0) com_save(text, path); } //释放内存 int i = 0; for (; text[i] != NULL; i++) { free(text[i]); text[i] = 0; } //退出 setProgramStatus(SHELL); exit(); }
int main(int argc, char *argv[]) { setProgramStatus(EDITOR); if (argc == 1) { printf(1, "please input the command as [editor file_name]\n"); setProgramStatus(SHELL); exit(); } //存放文件内容 char *text[MAX_LINE_NUMBER] = {}; text[0] = malloc(MAX_LINE_LENGTH); memset(text[0], 0, MAX_LINE_LENGTH); //存储当前最大的行号,从0开始。即若line_number == x,则从text[0]到text[x]可用 int line_number = 0; //尝试打开文件 int fd = open(argv[1], O_RDONLY); //如果文件存在,则打开并读取里面的内容 if (fd != -1) { char buf[BUF_SIZE] = {}; int len = 0; while ((len = read(fd, buf, BUF_SIZE)) > 0) { int i = 0; int next = 0; int is_full = 0; while (i < len) { //拷贝"\n"之前的内容 for (i = next; i < len && buf[i] != '\n'; i++) ; strcat_n(text[line_number], buf+next, i-next); //必要时新建一行 if (i < len && buf[i] == '\n') { if (line_number >= MAX_LINE_NUMBER - 1) is_full = 1; else { line_number++; text[line_number] = malloc(MAX_LINE_LENGTH); memset(text[line_number], 0, MAX_LINE_LENGTH); } } if (is_full == 1 || i >= len - 1) break; else next = i + 1; } if (is_full == 1) break; } close(fd); } //输出文件内容 show_text(text); //输出帮助 com_help(text); //处理命令 char input[MAX_LINE_LENGTH] = {}; while (1) { printf(1, "\nplease input command:\n"); memset(input, 0, MAX_LINE_LENGTH); gets(input, MAX_LINE_LENGTH); int len = strlen(input); input[len-1] = '\0'; len --; //寻找命令中第一个空格 int pos = MAX_LINE_LENGTH - 1; int j = 0; for (; j < 8; j++) { if (input[j] == ' ') { pos = j + 1; break; } } //ins if (input[0] == 'i' && input[1] == 'n' && input[2] == 's') { if (input[3] == '-') com_ins(text, atoi(&input[4]), &input[pos]); else com_ins(text, line_number+1, &input[pos]); //插入操作需要更新行号 line_number = get_line_number(text); } //mod else if (input[0] == 'm' && input[1] == 'o' && input[2] == 'd') { if (input[3] == '-') com_mod(text, atoi(&input[4]), &input[pos]); else com_mod(text, line_number + 1, &input[pos]); } //del else if (input[0] == 'd' && input[1] == 'e' && input[2] == 'l') { if (input[3] == '-') com_del(text, atoi(&input[4])); else com_del(text, line_number + 1); //删除操作需要更新行号 line_number = get_line_number(text); } else if (strcmp(input, "show") == 0) { auto_show = 1; printf(1, "enable show current contents after text changed.\n"); } else if (strcmp(input, "hide") == 0) { auto_show = 0; printf(1, "disable show current contents after text changed.\n"); } else if (strcmp(input, "help") == 0) com_help(text); else if (strcmp(input, "save") == 0 || strcmp(input, "CTRL+S\n") == 0) com_save(text, argv[1]); else if (strcmp(input, "exit") == 0) com_exit(text, argv[1]); else { printf(1, "invalid command.\n"); com_help(text); } } setProgramStatus(SHELL); exit(); }