void RegisterList::writeCVBitMain(char *s) volatile{ byte b[6]; // save space for checksum byte int cab; int cv; int bNum; int bValue; byte nB=0; if(sscanf(s,"%d %d %d %d",&cab,&cv,&bNum,&bValue)!=4) return; cv--; bValue=bValue%2; bNum=bNum%8; if(cab>127) b[nB++]=highByte(cab) | 0xC0; // convert train number into a two-byte address b[nB++]=lowByte(cab); b[nB++]=0xE8+(highByte(cv)&0x03); // any CV>1023 will become modulus(1024) due to bit-mask of 0x03 b[nB++]=lowByte(cv); b[nB++]=0xF0+bValue*8+bNum; loadPacket(0,b,nB,4); } // RegisterList::writeCVBitMain()
void RegisterList::setFunction(char *s) volatile{ byte b[5]; // save space for checksum byte int cab; int fByte, eByte; int nParams; byte nB=0; nParams=sscanf(s,"%d %d %d",&cab,&fByte,&eByte); if(nParams<2) return; if(cab>127) b[nB++]=highByte(cab) | 0xC0; // convert train number into a two-byte address b[nB++]=lowByte(cab); if(nParams==2){ // this is a request for functions FL,F1-F12 b[nB++]=(fByte | 0x80) & 0xBF; // for safety this guarantees that first nibble of function byte will always be of binary form 10XX which should always be the case for FL,F1-F12 } else { // this is a request for functions F13-F28 b[nB++]=(fByte | 0xDE) & 0xDF; // for safety this guarantees that first byte will either be 0xDE (for F13-F20) or 0xDF (for F21-F28) b[nB++]=eByte; } loadPacket(0,b,nB,4,1); } // RegisterList::setFunction()
void RegisterList::writeCVBit(char *s) volatile{ byte bWrite[4]; int bNum,bValue; int c,d,base; int cv, callBack, callBackSub; if(sscanf(s,"%d %d %d %d %d",&cv,&bNum,&bValue,&callBack,&callBackSub)!=5) // cv = 1-1024 return; cv--; // actual CV addresses are cv-1 (0-1023) bValue=bValue%2; bNum=bNum%8; bWrite[0]=0x78+(highByte(cv)&0x03); // any CV>1023 will become modulus(1024) due to bit-mask of 0x03 bWrite[1]=lowByte(cv); bWrite[2]=0xF0+bValue*8+bNum; loadPacket(0,resetPacket,2,1); loadPacket(0,bWrite,3,4); loadPacket(0,resetPacket,2,1); loadPacket(0,idlePacket,2,10); c=0; d=0; base=0; for(int j=0;j<ACK_BASE_COUNT;j++) base+=analogRead(CURRENT_MONITOR_PIN_PROG); base/=ACK_BASE_COUNT; bitClear(bWrite[2],4); // change instruction code from Write Bit to Verify Bit loadPacket(0,resetPacket,2,3); // NMRA recommends starting with 3 reset packets loadPacket(0,bWrite,3,5); // NMRA recommends 5 verfy packets loadPacket(0,resetPacket,2,1); // forces code to wait until all repeats of bRead are completed (and decoder begins to respond) for(int j=0;j<ACK_SAMPLE_COUNT;j++){ c=(analogRead(CURRENT_MONITOR_PIN_PROG)-base)*ACK_SAMPLE_SMOOTHING+c*(1.0-ACK_SAMPLE_SMOOTHING); if(c>ACK_SAMPLE_THRESHOLD) d=1; } if(d==0) // verify unsuccessful bValue=-1; INTERFACE.print("<r"); INTERFACE.print(callBack); INTERFACE.print("|"); INTERFACE.print(callBackSub); INTERFACE.print("|"); INTERFACE.print(cv+1); INTERFACE.print(" "); INTERFACE.print(bNum); INTERFACE.print(" "); INTERFACE.print(bValue); INTERFACE.print(">"); } // RegisterList::writeCVBit()
void RegisterList::setAccessory(char *s) volatile{ byte b[3]; // save space for checksum byte int aAdd; // the accessory address (0-511 = 9 bits) int aNum; // the accessory number within that address (0-3) int activate; // flag indicated whether accessory should be activated (1) or deactivated (0) following NMRA recommended convention if(sscanf(s,"%d %d %d",&aAdd,&aNum,&activate)!=3) return; b[0]=aAdd%64+128; // first byte is of the form 10AAAAAA, where AAAAAA represent 6 least signifcant bits of accessory address b[1]=((((aAdd/64)%8)<<4) + (aNum%4<<1) + activate%2) ^ 0xF8; // second byte is of the form 1AAACDDD, where C should be 1, and the least significant D represent activate/deactivate loadPacket(0,b,2,4,1); } // RegisterList::setAccessory()
void TRXFile::loadPackets(Common::SeekableReadStream &trx, std::vector<Packet> &packets) { for (std::vector<Packet>::iterator p = packets.begin(); p != packets.end(); ++p) { trx.seek(p->offset); uint32 type = trx.readUint32BE(); if (type != p->type) throw Common::Exception("Packet type mismatch (0x%08X vs 0x%08)", type, p->type); p->size = trx.readUint32LE(); if ((uint)(trx.size() - trx.pos()) < p->size) throw Common::Exception("Size of 0x%8X packet too big (%d)", p->type, p->size); loadPacket(trx, *p); } }
void RegisterList::writeTextPacket(char *s) volatile{ int nReg; byte b[6]; int nBytes; volatile RegisterList *regs; nBytes=sscanf(s,"%d %x %x %x %x %x",&nReg,b,b+1,b+2,b+3,b+4)-1; if(nBytes<2 || nBytes>5){ // invalid valid packet INTERFACE.print("<mInvalid Packet>"); return; } loadPacket(nReg,b,nBytes,0,1); } // RegisterList::writeTextPacket()
void RegisterList::setThrottle(char *s) volatile{ byte b[5]; // save space for checksum byte int nReg; int cab; int tSpeed; int tDirection; byte nB=0; if(sscanf(s,"%d %d %d %d",&nReg,&cab,&tSpeed,&tDirection)!=4) return; if(nReg<1 || nReg>maxNumRegs) return; if(cab>127) b[nB++]=highByte(cab) | 0xC0; // convert train number into a two-byte address b[nB++]=lowByte(cab); b[nB++]=0x3F; // 128-step speed control byte if(tSpeed>=0) b[nB++]=tSpeed+(tSpeed>0)+tDirection*128; // max speed is 126, but speed codes range from 2-127 (0=stop, 1=emergency stop) else{ b[nB++]=1; tSpeed=0; } loadPacket(nReg,b,nB,0,1); INTERFACE.print("<t "); INTERFACE.print(nReg); INTERFACE.print(" "); INTERFACE.print(cab); INTERFACE.print(" "); INTERFACE.print(tSpeed); INTERFACE.print(" "); INTERFACE.print(tDirection); INTERFACE.print(">"); speedTable[nReg]=tDirection==1?tSpeed:-tSpeed; } // RegisterList::setThrottle()
void RegisterList::readCV(char *s) volatile{ byte bRead[4]; int bValue; int c,d,base; int cv, callBack, callBackSub; if(sscanf(s,"%d %d %d",&cv,&callBack,&callBackSub)!=3) // cv = 1-1024 return; cv--; // actual CV addresses are cv-1 (0-1023) bRead[0]=0x78+(highByte(cv)&0x03); // any CV>1023 will become modulus(1024) due to bit-mask of 0x03 bRead[1]=lowByte(cv); bValue=0; for(int i=0;i<8;i++){ c=0; d=0; base=0; for(int j=0;j<ACK_BASE_COUNT;j++) base+=analogRead(CURRENT_MONITOR_PIN_PROG); base/=ACK_BASE_COUNT; bRead[2]=0xE8+i; loadPacket(0,resetPacket,2,3); // NMRA recommends starting with 3 reset packets loadPacket(0,bRead,3,5); // NMRA recommends 5 verfy packets loadPacket(0,resetPacket,2,1); // forces code to wait until all repeats of bRead are completed (and decoder begins to respond) for(int j=0;j<ACK_SAMPLE_COUNT;j++){ c=(analogRead(CURRENT_MONITOR_PIN_PROG)-base)*ACK_SAMPLE_SMOOTHING+c*(1.0-ACK_SAMPLE_SMOOTHING); if(c>ACK_SAMPLE_THRESHOLD) d=1; } bitWrite(bValue,i,d); } c=0; d=0; base=0; for(int j=0;j<ACK_BASE_COUNT;j++) base+=analogRead(CURRENT_MONITOR_PIN_PROG); base/=ACK_BASE_COUNT; bRead[0]=0x74+(highByte(cv)&0x03); // set-up to re-verify entire byte bRead[2]=bValue; loadPacket(0,resetPacket,2,3); // NMRA recommends starting with 3 reset packets loadPacket(0,bRead,3,5); // NMRA recommends 5 verfy packets loadPacket(0,resetPacket,2,1); // forces code to wait until all repeats of bRead are completed (and decoder begins to respond) for(int j=0;j<ACK_SAMPLE_COUNT;j++){ c=(analogRead(CURRENT_MONITOR_PIN_PROG)-base)*ACK_SAMPLE_SMOOTHING+c*(1.0-ACK_SAMPLE_SMOOTHING); if(c>ACK_SAMPLE_THRESHOLD) d=1; } if(d==0) // verify unsuccessful bValue=-1; INTERFACE.print("<r"); INTERFACE.print(callBack); INTERFACE.print("|"); INTERFACE.print(callBackSub); INTERFACE.print("|"); INTERFACE.print(cv+1); INTERFACE.print(" "); INTERFACE.print(bValue); INTERFACE.print(">"); } // RegisterList::readCV()