static void turnoutCmd(char address, char cmd, TurnoutTable *turnout_state) { int i = 0, row = 0, col = 0; if (address >= 1 && address <= 18) { i = address - 1; row = i / 3 + 1; col = 9 * (i % 3) + 8; } else if (address >= 153 && address <= 156) { i = address - 135; row = 8; col = 9 * (address - 153) + 8; } else { assert(0); } int mask = 1 << i; char *colour; char rep; // don't repeat commands if (cmd == CURVE) { if ((*turnout_state & mask) == 0) { colour = YELLOW; rep = 'C'; *turnout_state |= mask; } else { return; } } else if (cmd == STRAIGHT) { if ((*turnout_state & mask) == 0) { return; } else { colour = GREEN; rep = 'S'; *turnout_state &= ~mask; } } else { assert(0); } struct String s; sinit(&s); sputc(&s, cmd); sputc(&s, address); sputc(&s, CLEAR_SOLENOID); tioPrint(&s); sinit(&s); sputstr(&s, CURSOR_SAVE); vtPos(&s, SWITCH_ROW + row, col); sputstr(&s, colour); sputc(&s, rep); sputstr(&s, RESET); sputstr(&s, CURSOR_RESTORE); mioPrint(&s); }
void drawTrackLayoutGraph(char track) { char *trackA = "\033[2;44H" "-- Track A --" "\033[4;24H" "---------12--11----------------------------------\\" "\033[5;24H" "-------4/ / ---------13--------10-----------\\ \\" "\033[6;24H" " 14--/ \\ | / \\ \\" "\033[7;24H" " / \\ |/155 \\--9" "\033[8;24H" " | 156\\| |" "\033[9;24H" " | |\\154 |" "\033[10;24H" " \\ 153/| \\ /--8" "\033[11;24H" " 15--\\ / | \\ / /" "\033[12;24H" "--------\\ \\ ----------S---------S-----------/ /" "\033[13;24H" "---------1\\ \\---------6------------7------------/" "\033[14;24H" "-----------2\\ \\ /" "\033[15;24H" "-------------3-----------18--------5------------"; char *trackB = "\033[2;44H" "-- Track B --" "\033[4;24H" " ------------5--------18-----------3-------------" "\033[5;24H" " / \\ \\2-----------" "\033[6;24H" " /------------7------------6---------\\ \\1---------" "\033[7;24H" " / /-----------17--------16--------- \\ \\" "\033[8;24H" " / / \\ | / \\--15 \\-\\" "\033[9;24H" " 8--/ \\ |/153 \\ -\\" "\033[10;24H" " | 154\\| | |" "\033[11;24H" " | |\\156 | |" "\033[12;24H" " 9--\\ 155/| \\ / /-/" "\033[13;24H" " \\ \\ / | \\ /--14 /" "\033[14;24H" " \\ \\-----------10--------13--------- / /4-------" "\033[15;24H" " \\----------------------------------11--12---------"; assert(STR_MAX_LEN > strlen(trackA)); assert(STR_MAX_LEN > strlen(trackB)); String s; sinit(&s); sputstr(&s, VT_CURSOR_SAVE); vt_pos(&s, VT_TRACK_GRAPH_ROW, VT_TRACK_GRAPH_COL); sputstr(&s, VT_RESET); sputstr(&s, track == 'a' ? trackA : trackB); sputstr(&s, VT_RESET); sputstr(&s, VT_CURSOR_RESTORE); PutString(COM2, &s); }
static void turnoutServer(void) { const char *rows[] = { "+--------+--------+--------+", "| 1: # | 2: # | 3: # |", "| 4: # | 5: # | 6: # |", "| 7: # | 8: # | 9: # |", "| 10: # | 11: # | 12: # |", "| 13: # | 14: # | 15: # |", "| 16: # | 17: # | 18: # |", "+--------+--------+--------+--------+", "| 153: # | 154: # | 155: # | 156: # |", "+--------+--------+--------+--------+" }; struct String s; for (unsigned int i = 0; i < sizeof(rows) / sizeof(rows[0]); i++) { sinit(&s); sputstr(&s, CURSOR_SAVE); vtPos(&s, SWITCH_ROW + i, 1); sputstr(&s, rows[i]); sputstr(&s, CURSOR_RESTORE); mioPrint(&s); } // set to all zeros (straight) so curve commands actually get sent TurnoutTable turnout_state = 0; for (int i = 1; i <= 18; ++i) { turnoutCmd(i, CURVE, &turnout_state); } for (int i = 153; i <= 156; ++i) { turnoutCmd(i, CURVE, &turnout_state); } bool quit = false; int tid; struct TurnoutMessage request; while (!quit) { int len = Receive(&tid, (char *) &request, sizeof(struct TurnoutMessage)); assert(len == sizeof(struct TurnoutMessage)); switch (request.cmd) { case CMD_TURNOUT_STRAIGHT: turnoutCmd(request.address, STRAIGHT, &turnout_state); break; case CMD_TURNOUT_CURVE: turnoutCmd(request.address, CURVE, &turnout_state); break; case CMD_TURNOUT_QUERY: // already responded break; case CMD_TURNOUT_QUIT: quit = true; break; } Reply(tid, (char *) &turnout_state, sizeof(int)); } Exit(); }