static void train_reserved_node_update(int index, DisplayData *data, track_node **reserved_nodes) { // Do a diff. int i; int diff = 0; for (i = 0; i < MAX_RESERVED_NODES; ++i) { if (data->reserved_nodes[i] != reserved_nodes[i]) { data->reserved_nodes[i] = reserved_nodes[i]; diff++; } } if (diff == 0) return; char command[256]; char *pos = &command[0]; char buf[TRAIN_COLUMN_WIDTH]; char *buf_pos = &buf[0]; pos += sprintf(pos, "\0337"); int offset = strlen(RESERVED_NODES_STRING); pos += sprintf(pos, "\033[%u;%uH", get_y(index, TRAIN_RESERVED_NODES_HEIGHT), get_x(index) * TRAIN_COLUMN_WIDTH + 1 + offset); unsigned int j; for (j = 0; j < MAX_RESERVED_NODES; ++j) { if (data->reserved_nodes[j] != 0) { buf_pos += sprintf(buf_pos, "%s ", data->reserved_nodes[j]->name); } } pos += sputw(pos, TRAIN_COLUMN_WRITABLE_WIDTH - offset, ' ', buf); pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); }
static void train_destination_update(int index, DisplayData *data, track_node *destination) { if (data->destination == destination) return; data->destination = destination; char command[256]; char *pos = &command[0]; char buf[TRAIN_COLUMN_WIDTH]; char *buf_pos = &buf[0]; pos += sprintf(pos, "\0337"); int offset = strlen(DESTINATION_STRING); pos += sprintf(pos, "\033[%u;%uH", get_y(index, TRAIN_DESTINATION_HEIGHT), get_x(index) * TRAIN_COLUMN_WIDTH + 1 + offset); if (data->destination) { buf_pos += sprintf(buf_pos, "%s", data->destination->name); } else { buf_pos += sprintf(buf_pos, "%s", "None"); } pos += sputw(pos, TRAIN_COLUMN_WRITABLE_WIDTH - offset, ' ', buf); pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); }
void switches_init() { int i; for (i = 0; i < NUM_SWITCHES + NUM_AUXILLARY_SWITCHES; ++i) { switches[i] = 0; } char command[1024]; char *pos = &command[0]; pos += sprintf(pos, "\0337\033[%u;%uH", SWITCH_TABLE_HEIGHT, 1); pos += sprintf(pos, "Switches: "); pos += sprintf(pos, "\033[%u;%uH", SWITCH_TABLE_HEIGHT + 1, 1); // Draw Regular Switches. for (i = 0; i < NUM_SWITCHES; ++i) { char num[4]; ui2a(i + 1, 10, num); pos += sputw(pos, 4, ' ', num); } // Draw Auxillary Switches. for (i = 0; i < AUXILLARY_SWITCH_COUNT; ++i) { char num[4]; ui2a(AUXILLARY_SWITCH_BASE + i, 10, num); pos += sputw(pos, 4, ' ', num); } pos += sprintf(pos, "\033[%u;%uH", SWITCH_TABLE_HEIGHT + 2, 1); // Draw States. for (i = 0; i < NUM_SWITCHES + AUXILLARY_SWITCH_COUNT; ++i) { pos += sputw(pos, 4, ' ', "?"); } pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); }
int switch_update(int number, unsigned char position) { int index = switch_to_index(number); if (index == -1) return -1; index = index * 4; char command[128]; char *pos = &command[0]; pos += sprintf(pos, "\0337\033[%u;%uH", SWITCH_TABLE_HEIGHT + 2, index + 1); pos += sputw(pos, 4, ' ', position == CURVED ? "C" : "S"); pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); return 0; }
static void train_error_update(int index, DisplayData *data, int error) { if (data->calibrated_error == error) return; data->calibrated_error = error; char command[128]; char *pos = &command[0]; pos += sprintf(pos, "\0337"); int offset = strlen(ERROR_STRING); pos += sprintf(pos, "\033[%u;%uH", get_y(index, TRAIN_ERROR_HEIGHT), get_x(index) * TRAIN_COLUMN_WIDTH + 1 + offset); char buf[TRAIN_COLUMN_WIDTH]; sprintf(buf, "%dmm", error / 1000); pos += sputw(pos, TRAIN_COLUMN_WRITABLE_WIDTH - offset, ' ', buf); pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); }
static void train_calibrated_velocity_update(int index, DisplayData *data, int velocity) { if (data->calibrated_velocity == velocity) return; data->calibrated_velocity = velocity; char command[128]; char *pos = &command[0]; pos += sprintf(pos, "\0337"); int offset = strlen(MEASURED_VELOCITY_STRING); pos += sprintf(pos, "\033[%u;%uH", get_y(index, TRAIN_MEASURED_VELOCITY_HEIGHT), get_x(index) * TRAIN_COLUMN_WIDTH + 1 + offset); char buf[TRAIN_COLUMN_WIDTH]; sprintf(buf, "%dum/tick", velocity); pos += sputw(pos, TRAIN_COLUMN_WRITABLE_WIDTH - offset, ' ', buf); pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); }
static void train_stopping_distance_update(int index, DisplayData *data, int stopping_distance) { if (data->stopping_distance == stopping_distance) return; data->stopping_distance = stopping_distance; char command[128]; char *pos = &command[0]; pos += sprintf(pos, "\0337"); int offset = strlen(STOPPING_DISTANCE_STRING); pos += sprintf(pos, "\033[%u;%uH", get_y(index, TRAIN_STOPPING_DISTANCE_HEIGHT), get_x(index) * TRAIN_COLUMN_WIDTH + 1 + offset); char stop_buf[TRAIN_COLUMN_WIDTH]; sprintf(stop_buf, "%dmm", stopping_distance / 1000); pos += sputw(pos, TRAIN_COLUMN_WRITABLE_WIDTH - offset, ' ', stop_buf); pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); }
static void train_edge_update(int index, DisplayData *data, track_edge *edge) { if (data->edge == edge) return; data->edge = edge; char command[128]; char *pos = &command[0]; pos += sprintf(pos, "\0337"); int offset = strlen(LANDMARK_STRING); pos += sprintf(pos, "\033[%u;%uH", get_y(index, TRAIN_LANDMARK_HEIGHT), get_x(index) * TRAIN_COLUMN_WIDTH + 1 + offset); char position[TRAIN_COLUMN_WIDTH]; if (!edge) { sprintf(position, "N/A"); } else { sprintf(position, "%s", edge->src->name); } pos += sputw(pos, TRAIN_COLUMN_WRITABLE_WIDTH - offset, ' ', position); pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); }
static void train_orientation_update(int index, DisplayData *data, enum TRAIN_ORIENTATION orientation) { if (data->orientation == orientation) return; data->orientation = orientation; char command[128]; char *pos = &command[0]; pos += sprintf(pos, "\0337"); int offset = strlen(ORIENTATION_STRING); pos += sprintf(pos, "\033[%u;%uH", get_y(index, TRAIN_ORIENTATION_HEIGHT), get_x(index) * TRAIN_COLUMN_WIDTH + 1 + offset); char position[TRAIN_COLUMN_WIDTH]; if (orientation == TRAIN_FORWARD) { sprintf(position, "F"); } else if (orientation == TRAIN_BACKWARD) { sprintf(position, "B"); } else { sprintf(position, "N/A"); } pos += sputw(pos, TRAIN_COLUMN_WRITABLE_WIDTH - offset, ' ', position); pos += sprintf(pos, "\0338"); Write(COM2, command, pos - command); }
int sformat ( char* dst, const char *fmt, va_list va ) { char* orig = dst; char bf[12]; char ch, lz; int w; while ( ( ch = *(fmt++) ) ) { if ( ch != '%' ){ *(dst++) = ch; } else { lz = ' '; w = 0; ch = *(fmt++); if( ch == '0' ){ lz = '0'; ch = *(fmt++); } switch ( ch ) { case '0': lz = 0; ch = *(fmt++); break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ch = sa2i( ch, &fmt, 10, &w ); break; } switch( ch ) { case 0: fmt--; break; case 'c': *(dst++) = va_arg( va, char ); break; case 's': dst = sputw( dst, w, 0, va_arg( va, char* ) ); break; case 'u': sui2a( va_arg( va, unsigned int ), 10, bf ); dst = sputw( dst, w, lz, bf ); break; case 'd': si2a( va_arg( va, int ), bf ); dst = sputw( dst, w, lz, bf ); break; case 'x': sui2a( va_arg( va, unsigned int ), 16, bf ); dst = sputw( dst, w, lz, bf ); break; case '%': *(dst++) = ch; break; } } } *(dst++) = '\0'; return dst - orig; }