Need Help Translating C

Working on a Raspberry Pi project today using a Thermocouple Amplifier from Adafruit and ran across this example code. It’s been way too long since I’ve done any C. Can someone help translate this into Xojo for me?

[code]double MAX6675::readCelsius(void) {

uint16_t v;

digitalWrite(cs, LOW);
_delay_ms(1);

v = spiread();
v <<= 8;
v |= spiread();

digitalWrite(cs, HIGH);

if (v & 0x4) {
// uh oh, no thermocouple attached!
return NAN;
//return -100;
}

v >>= 3;

return v*0.25;
}
[/code]

Should be along these lines - it’s forum code :slight_smile:

function readCelsius() as Double

  dim v as Uint16

  digitalWrite(cs, LOW) // no idea how cs and LOW are defined

  // wait 1 msec
  dim ms as double = microseconds
  while microseconds < ms + 1
  wend

  v = spiread() // no idea what spiread is or returns here
  v = Bitwise.ShiftLeft(v, 8)
  v = BitWise.BitOr(v, spiread())

  digitalWrite(cs, HIGH)  // no idea how cs and HIGH are defined

  if bitwise.BitAnd(v, &h4) > 0 then
    // uh oh, no thermocouple attached!
    return NAN
 end if   

 v = BitWise.ShiftRight(v, 3)
 
return v*0.25
end 

It looks like spiread returns a single byte. They’re loading 2 bytes into a 16-bit integer. It would also appear that it’s 13 bits of data followed by 3 bits of control information. The first of those 3 bits is the status of the thermocouple.

Norman’s code looks about right.

Thanks for sharing! What is the meaning of code like “MAX6675::” in C?

best
Thomas

That’s the class name.

max6675 is also the name of the chip that does the thermocouple amplifier

Thanks, Norman. This is what I came up with substituting the proper GPIO calls in. Not sure about the use the memory block though. At least it compiles but won’t be able to test until tomorrow (probably).

[code]Private Function readCelsius() As double
dim v as Uint16

GPIO.DigitalWrite(10, GPIO.LOW)

// wait 1 msec
GPIO.Delay(1)

dim m as new MemoryBlock(8)
dim iResult as integer = GPIO.SPIDataRW(0, m, 8)
if iResult = -1 then
//Result failed
return -1
end

v = m.Int8Value(0)
v = Bitwise.ShiftLeft(v, 8)

m = new MemoryBlock(8)
iResult = GPIO.SPIDataRW(0, m, 8)
if iResult = -1 then
//Result failed
return -1
end

dim v2 as integer
v2 = m.Int8Value(0)

v = BitWise.BitOr(v, v2)

GPIO.DigitalWrite(10, GPIO.HIGH)

if bitwise.BitAnd(v, &h4) > 0 then
// uh oh, no thermocouple attached!
return -1
end if

v = BitWise.ShiftRight(v, 3)

return v*0.25

End Function
[/code]

SPIDataRW is defined in the GPIO library as:

[code]Protected Function SPIDataRW(channel As Integer, data As CString, len As Integer) As Integer
// int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ;
// This performs a simultaneous write/read transaction over the selected SPI bus.
// Data that was in your buffer is overwritten by data returned from the SPI bus.
//
// That’s all there is in the helper library. It is possible to do simple
// read and writes over the SPI bus using the standard read() and write() system
// calls though – write() may be better to use for sending data to chains of
// shift registers, or those LED strings where you send RGB triplets of data.
// Devices such as A/D and D/A converters usually need to perform a concurrent
// write/read transaction to work.
// http://wiringpi.com/reference/spi-library/

#If TargetARM And TargetLinux Then
Soft Declare Function wpWiringPiSPIDataRW Lib “libwiringPi.so” Alias “wiringPiSPIDataRW” (channel As Integer, data As CString, len As Integer) As Integer
Return wpWiringPiSPIDataRW(channel, data, len)
#Endif
End Function
[/code]

Thanks for the help!

[quote=270681:@Tim Hare]It looks like spiread returns a single byte. They’re loading 2 bytes into a 16-bit integer. It would also appear that it’s 13 bits of data followed by 3 bits of control information. The first of those 3 bits is the status of the thermocouple.
[/quote]
Ah yeah now that I read the code closer - looks like you’d be about right on that

[quote=270687:@Bob Keeney]Thanks, Norman. This is what I came up with substituting the proper GPIO calls in. Not sure about the use the memory block though.
[/quote]

seems the mb is just the buffer it dumps data into but since its reading & writing bytes make the MB one byte & pass 1 as the length
you dont need any more than that