Hi,
im coming from rather Script languages and I'm quite new to Freepascal, im trying to build a function that can read a specific channel of the MCP3008 via SPI.
I have not found to much documentation on the SPI read, but looking at the datasheet i know i have to send over "what" i want to read and then parse the answer.
Issue is that i am really new to pointers and i have been struggling around for 2,3 days and been reading alot, i just cant get it to work.
Here is my code, if anyone could give me feedback on where i am going wrong. currently i get "FileHandling.lpr(35,14) Error: Incompatible types: got "Pointer" expected "AnsiString"
Which makes sense, i figured i can set a pointer to a variable using x = @Y but how do i get the data of the pointer back into a variable? Do i manually shift trough it for the 16 bit of answer?
Any help is appreciated, please be gentle, i just started programming shortly. Also, why does my compiler say "FileHandling.lpr(34,67) Warning: Local variable "DataBuffer" does not seem to be initialized" But OutBuffer is working fine?
Best regards,
Marcus
program DrawBMP;
{$mode delphi}{$H+}
uses
RaspberryPi4, {Include the RaspberryPi4 unit to give us network, filesystem etc}
GlobalConst,
Devices,
SPI,
GlobalTypes,
Threads,
Console,
GraphicsConsole, {Include the GraphicsConsole unit so we can create a graphics window}
Classes, {Include the Classes unit for the TFileStream class}
SysUtils;
var
SPIDevice:PSPIDevice;
Count:LongWord;
Value:Byte;
WindowHandle:TWindowHandle;
function ReadMCP3008(channel:int):AnsiString;
var
DataBuffer:Pointer;
OutBuffer:Pointer;
begin
channel := channel or $0011; // # start bit for channel + single-ended bit
channel := $00000000 or channel; // # Fill with 0 because function expects 16 BIT
OutBuffer := @channel;
IF SPIDeviceWriteRead(SPIDevice, SPI_CS_0, OutBuffer, DataBuffer, 16, SPI_TRANSFER_NONE, Count) = ERROR_SUCCESS then begin
result:=DataBuffer;
end;
end;
begin
SPIDevice:=PSPIDevice(DeviceFindByDescription('BCM2711 SPI0 Master')); {Device on Raspberry 4 is called BMC2711}
if SPIDeviceStart(SPIDevice, SPI_MODE_4WIRE, 100000, SPI_CLOCK_PHASE_LOW, SPI_CLOCK_POLARITY_LOW) = ERROR_SUCCESS then begin
end;
Sleep(3000);
WindowHandle:=ConsoleWindowCreate(ConsoleDeviceGetDefault,CONSOLE_POSITION_FULL,True);
ConsoleWindowWriteLn(WindowHandle,ReadMCP3008(0));
ThreadHalt(0);
end.