chiark / gitweb /
hrm, should work ha ha
[trains.git] / hostside / nmra-packets.h
1 /*
2  * strange macros - do not use directly, usually
3  */
4   
5 NMRA(speed28, Aint(addr,0) Aint(speed,1) Aint(reverse,2), {
6   /* 0<=speed<=28 or <=126; reverse: 0 forwards, non-0 backwards */
7   int adj;
8   ADDR;
9   nmra_errchk(cn, speed, speed>=0 && speed<=28);
10   *c++= 0x40 | (reverse ? 0 : 0x20);
11   if (speed) {
12     adj= speed + 3;
13     *c |= adj & 1 ? 0x10 : 0;
14     *c |= adj >> 1;
15   }
16   c++;
17 })
18 NMRA(estop1, Aint(addr,0), {
19   /* Baseline Speed and direction Forwards E-Stop(I) S9.2 B table l.56 */
20   ADDR;
21   *c++= 0x71;
22 })
23 NMRA(speed126, Aint(addr,0) Aint(speed,1) Aint(reverse,2), {
24   /* Advanced Operations 128 Speed Step Control
25    * (actually speeds 0..126) RP9.2.1 C l.200- */
26   ADDR;
27   nmra_errchk(cn, speed, speed>=0 && speed<=126);
28   *c++= 0x3f;
29   *c++= (speed ? speed + 1 : 0) | (reverse ? 0 : 0x80);
30 })
31
32 NMRA(estop, Anone, {
33   /* Baseline Broadcast stop Forwards(I) Emergency S9.2 B l.98- */
34   CONST(0x00, 0x71);
35 })
36 NMRA(reset, Anone, {
37   /* Baseline Decoder Reset S9.2 B l.77- */
38   CONST(0x00, 0x00);
39 })
40 NMRA(idle, Anone, {
41   /* Baseline Idle S9.2 B l.87- */
42   CONST(0xff, 0x00);
43 })
44
45 /* For functions:
46  * bit 0 is FL aka F0; bits 1-12 are F1-F12; do not call with bits
47  * outside 0-12 set; bits in 0-12 but not relevant for the relevant
48  * command are ignored. */
49 NMRA(funcs0to4, Aint(addr,0) Abitmap(bitmap,1), {
50   /* Function Group One RP9.2.1 C l.234- */
51   FUNCS(0x80 | ((bitmap >> 1) & 0x0f) | ((bitmap << 4 & 0x10)));
52 })
53 NMRA(funcs5to9, Aint(addr,0) Abitmap(bitmap,1), {
54   /* Function Group Two RP9.2.1 C l.246- */
55   FUNCS(0xa0 | ((bitmap >> 5) & 0x0f));
56 })
57 NMRA(funcs9to12, Aint(addr,0) Abitmap(bitmap,1), {
58   /* Function Group Two RP9.2.1 C l.246- */
59   FUNCS(0xb0 | ((bitmap >> 9) & 0x0f));
60 })
61 NMRA(cvwrite, Aint(addr,0) Aint(cv,1) Abyte(value,2), {
62   /* Configuration Variable Access Long Form RP9.2.1 C l.286- */
63   int adj;
64   ADDR;
65   nmra_errchk(cn, cv, cv>=1 && cv<=1024);
66   adj= cv - 1;
67   *c++= 0xec | (adj >> 8);
68   *c++= adj;
69   *c++= value;
70 })
71
72 #undef Aint
73 #undef Abitmap
74 #undef Abyte
75 #undef Anone
76 #undef NMRA