[Lazarus] Where is hwiringPi?
Dennis
dec12 at avidsoft.com.hk
Sat Sep 19 06:55:00 CEST 2015
I have written a small program using FPC to run on Rasp Pi to connect,
via i2c, to a temperature/humidity sensor and save the readings into a
MySQL database.
Initially, I tried to use wiringPi, but I later found out it was very
simple to directly connect to i2c via fpRead() . It was as simple as:
const
I2C_SLAVE = 1795;
CMD_SOFT_RESET : byte = $FE;
CMD_READ_TEMP_NOHOLD : byte = $F3;
CMD_READ_HUM_NOHOLD : Byte = $F5;
var TheDeviceNo : integer = $40;
ThePath : String = '/dev/i2c-1';
DeviceHandle : integer;
.....
DeviceHandle := fpopen(ThePath,O_RDWR); //Open the I2C bus in
Read/Write mode
iio:= FpIOCtl(DeviceHandle, I2C_SLAVE, pointer(TheDeviceNo)); //Set
options
If (iio <> 0) or (DeviceHandle < 0) Then begin
fpclose(DeviceHandle);
DeviceHandle := 0;
raise Exception.Create('Failed to open '+ThePath+' device#
0x'+IntToStr(TheDeviceNo));
end;
//to read a floating variable:
function ReadFloat(const TheRegNo: byte; TheOffset, TheSlope: double):
double;
var aBytes : array[0..3] of byte;
i : integer;
begin
fpwrite(DeviceHandle, TheRegNo, 1);
Sleep(100); //for safety only. not sure if needed
Fillchar(aBytes, sizeof(aBytes), 0);
fpread(DeviceHandle, aBytes, 3);
//the following is only specific to the themometer which requires some
conversion in the data read.
if IsCRC8Ok(aBytes) then begin
result := ((aBytes[0] shl 8) or aBytes[1] ) and ($FFFC);
result :=result/65536;
result := result * TheSlope + TheOffset;
end else
raise ECRCError.Create('');
end;
//to reset the themometer
fpwrite(DeviceHandle, CMD_SOFT_RESET, 1);
More information about the Lazarus
mailing list