From: ian Date: Sun, 2 Jan 2005 17:45:07 +0000 (+0000) Subject: encoding bugfixes X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ijackson/git?a=commitdiff_plain;h=d1aec7cefec536ed607c03066232478c7f3d35df;p=trains.git encoding bugfixes --- diff --git a/hostside/nmra.c b/hostside/nmra.c index a0d3b81..443f235 100644 --- a/hostside/nmra.c +++ b/hostside/nmra.c @@ -7,32 +7,33 @@ void nmra_transmit(const Byte *nmra_packet, int length) { Byte encoded[COMMAND_ENCODED_MAX], *encp; unsigned working; - int working_bits; + int working_qty; assert(length <= NMRA_PACKET_MAX); encp= encoded; - working= 0xfffc; /* 16-bit temp register. Top working_bits bits */ - working_bits= 14; /* are some data bits to encode, rest are clear. */ + working= 0xfffc; /* 16-bit temp register. Top working_qty bits */ + working_qty= 14; /* are some data bits to encode, rest are clear. */ for (;;) { - assert(working_bits >= 0); - if (working_bits < 7) { + assert(working_qty >= 0); + if (working_qty < 7) { if (length > 0) { /* plonk new data bits just to right of old data bits */ - working |= (*nmra_packet++ << (8-working_bits)); + working |= (*nmra_packet++ << (8-working_qty)); length--; - working_bits += 8; - } else if (!working_bits) { + working_qty += 8; + } else if (!working_qty) { /* all done */ break; } else { /* pad with exactly enough 1 bits to make up the encoded byte */ - working |= 0xffU << (8-working_bits); - working_bits= 7; + working |= 0xffU << (8-working_qty); + working_qty= 7; } } assert(encp < encoded + COMMAND_ENCODED_MAX); - *encp++= (working_bits >> 8) & 0xfe; /* top 7 bits, shifted left one */ - working_bits -= 7; + *encp++= (working >> 8) & 0xfe; /* top 7 bits, shifted left one */ + working <<= 7; + working_qty -= 7; } assert(encp > encoded); encp[-1] |= 0x01; /* `end of command' bit */