00001 #include <j2k/Fred/Standard.hpp>
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 UCHAR getSpeedHex( int spd )
00041 {
00042
00043
00044
00045 if ( spd < 0 ) return 0x01;
00046
00047
00048
00049
00050 if ( spd == 0 ) return 0x00;
00051
00052 assert( spd <= 28 );
00053
00054
00055
00056
00057 register UCHAR even = ( (spd & 0x01) ? 0x00 : 0x10 );
00058
00059
00060
00061
00062
00063 register UCHAR val = spd;
00064
00065
00066 val++;
00067 val >>= 1;
00068 val++;
00069
00070 return even + val;
00071 }
00072
00073 char* setSpeed( UCHAR trainid, int speed, BOOL clockwise, BOOL lights )
00074 {
00075 char buf[13];
00076 memset( buf, 0, 13 );
00077
00078 register UCHAR checksum = trainid;
00079 register UCHAR command = 0x40;
00080
00081 if ( clockwise ) command += 0x20;
00082 if ( lights ) command += 0x10;
00083
00084 command += getSpeedHex( speed );
00085
00086
00087 checksum ^= command;
00088
00089 sprintf( buf, "Q %02X %02X %02X%c%c", trainid, command, checksum, 13, 0 );
00090
00091 printf( "Train #%d is going at speed %d ", trainid, speed );
00092
00093 if ( clockwise ) {
00094 printf( "in the forward direction " );
00095 } else {
00096 printf( "in the reverse direction " );
00097 }
00098
00099 if ( lights ) {
00100 printf( "with the lights ON \n" );
00101 } else {
00102 printf( "with the lights OFF \n" );
00103 }
00104
00105 printf( "\t%s\n", buf );
00106 fflush( stdout );
00107
00108 return strdup( buf );
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 void test()
00140 {
00141 UCHAR trainid = 7;
00142
00143 for( int l = 0; l <= 1; l++ )
00144 for( int d = 0; d <= 1; d++ )
00145 for( int i = -1; i <= 14; i++ )
00146 {
00147 char* b = setSpeed( trainid, i, d==1, l==1 );
00148 printf( "Command: %s", b );
00149 }
00150 }
00151
00152
00153 int main()
00154 {
00155 test();
00156
00157 return 0;
00158 }
00159