// Test DCC-Decoder #include "DCC_Decoder.h" // DCC-Decoder: byte Speed = 0; byte dccRichtung = 0; byte dccCount = 0; // DCC-Adresse: byte dccAdresse = 0; // DCC-Decoder #define kDCC_INTERRUPT 0 typedef struct { int count; byte validBytes; byte data[6]; } DCCPacket; // The DCC-Decoder object and global data int gPacketCount = 0; int gIdlePacketCount = 0; int gLongestPreamble = 0; DCCPacket gPackets[25]; // ALL packets are sent to the RawPacket handler. // Returning true indicates that packet was handled. // DCC library starts watching for next preamble. // Returning false and library continue parsing // packet and finds another handler to call. boolean RawPacket_Handler(byte byteCount, byte* packetBytes) { // Bump global packet count ++gPacketCount; int thisPreamble = DCC.LastPreambleBitCount(); if( thisPreamble > gLongestPreamble ) { gLongestPreamble = thisPreamble; } // Walk table and look for a matching packet for( int i=0; i<(int)(sizeof(gPackets)/sizeof(gPackets[0])); ++i ) { if( gPackets[i].validBytes ) { // Not an empty slot. Does this slot match this packet? If so, bump count. if( gPackets[i].validBytes==byteCount ) { char isPacket = true; for( int j=0; j 2) { // Fartrichtung festlegen // 0= Rck, 1=Vrw } } #define LED 5 // Kontrollled void serialAvailable() { digitalWrite(LED, HIGH); // LED eingeschaltet // digitalWrite(LED, LOW); // LED ausgeschaltet } void setup() { Serial.begin(9600); Serial.setTimeout(200); //Timeout auf 100ms // DCC-Decoder DCC.SetRawPacketHandler(RawPacket_Handler); DCC.SetIdlePacketHandler(IdlePacket_Handler); DCC.SetupMonitor( kDCC_INTERRUPT ); getAnzeige(); pinMode(LED, OUTPUT); digitalWrite(LED, LOW); // LED ausgeschaltet } void GetAndResetTable() { char buffer60Bytes[60]; for( int i=0; i<(int)(sizeof(gPackets)/sizeof(gPackets[0])); ++i ) { if( gPackets[i].validBytes > 0 ) { //Serial.println( DCC.MakePacketString(buffer60Bytes, gPackets[i].validBytes, &gPackets[i].data[0]) ); if (gPackets[i].data[0] == dccAdresse ) { dccCount = gPackets[i].count; Speed = gPackets[i].data[2]; dccRichtung = bitRead(Speed, 7); Speed &= B01111111; //Direction-Bit (Bit 7) abschneiden //Serial.println(gPackets[i].data[2], DEC); } } gPackets[i].validBytes = 0; gPackets[i].count = 0; } gPacketCount = 0; gIdlePacketCount = 0; gLongestPreamble = 0; } void loop() { // Proforma warten (nachträglich eingefügt): delay(100); getAnzeige(); //nach jedem Durchlauf aufrufen, falls sich auch die Fahrtrichtung geändert hat if (Serial.available()) { serialAvailable(); } }