🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #5144

Re: Another delphi translation please!

#5144
From: Bruce Knowles
Date: Mon, 26 Sep 2005 11:26
91 lines
3124 bytes
HI,

How does the following code suit you?

Regards

Bruce Knowles

// try to find all the ds1820's on this bus -- Private Method ??
int __fastcall TxSensorBus::FindROMCode(TxROMCode *RomCode, int *done, int 
*lastDiscrep, int nCmd)
{
    char val;
    int  i;
    int  b;
    int  sb;
    int  bitPos;
    int  discrep     =  0;
    int  Result      = -1;

    Reset();

    if (!SendCommand(nCmd)) return(Result);

    for (bitPos = 1; bitPos <= 64; bitPos++) {
        b = bitPos - 1;
        i = (int)(b / 8);

        // Search for the bit
        sb = WriteBit(1) << 1;

        if (sb >= 0) sb |= WriteBit(1);

        if (sb == -1) return(Result); // Error

        // Get the Sernumbit
        val = RomCode[i] & (1 << ((int)(b / 8)));

        if (sb == 3) { // 11
            return(0);
        }

        if (sb == 0) {
            if (bitPos == *lastDiscrep) {
                // Set the SernumBit on
                RomCode[i] |= (1 << ((int)(b / 8)));
            } else {
                if (bitPos > *lastDiscrep) {
                    // Set the SernumBit off
                    RomCode[i] &= ~(1 << ((int)(b / 8)));
                    discrep = bitPos;
                } else {
                    if (val == 0) discrep = bitPos;
                }
            }
        } else {
            if (sb & 0xFE) {
                // Set the SernumBit on
                RomCode[i] |= (1 << ((int)(b / 8)));
            } else {
                // Set the SernumBit off
                RomCode[i] &= ~(1 << ((int)(b / 8)));
            }
        }

        val   = RomCode[i] & (1 << ((int)(b / 8)));
        val >>= ((int)(b / 8));

        if (WriteBit(val) != val) return(Result);
    }

    *lastDiscrep = discrep;

    if (discrep == 0) *done = 1;

    return(1);
}

void __fastcall TxSensorBus::EnumSensors(int nCmd, TEnumSensorsProc *Proc, 
void *pParam)
{
    TxROMCode RomCode = (TxROMCode)0;
    int       done      = 0;
    int  lastDiscrep  = 0;

    while (done == 0 && (FindROMCode(&RomCode, &done, &lastDiscrep, nCmd) == 
1)) {
        Assert(ValidateCRC(RomCode, 8));
        Proc(pParam, RomCode);
    }
} 
Technical Details
Message-ID: <4337da80@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <43370a81@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6ea8b2c4d2578842
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Bruce Knowles" <knowle_b@mtn.co.za>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <43370a81@newsgroups.borland.com>
Subject: Re: Another delphi translation please!
Date: Mon, 26 Sep 2005 13:26:10 +0200
Lines: 93
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 196.11.239.43
Message-ID: <4337da80@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1127733888 196.11.239.43 (26 Sep 2005 04:24:48 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail