Page 1 of 1

Writing a simple function - Help!

Posted: 03 June 2010, 12:36 PM
by everest
I'm trying to write a very simple function and I'm struggling. All I want to create is a function that returns a single bit when called. I'm coming from a PBASIC world so I'm using the example in the conversion document as a basis for writing some simple functions. Here's where I am:

Code: Select all

Private Const Input_Clock as Byte = 7		' clock pin
Private Const Input_Data as Byte = 10		' data pin
Private Const Input_Load as Byte = 6		' load pin

Function ReadInput(ByVal value as Bit) as Bit
	Call PutPin(Input_Load, zxOutputHigh)
	Call PutPin(Input_Clock, zxOutputLow)
	Call PulseOut(Input_Load, 5, 0)
	Dim InputByte as Byte
	InputByte = ShiftIn(Input_Data, Input_Clock, 8)
	ReadInput = GetBit(InputByte, value)
End Function

Sub Main()
	Dim RoofOpenState as Bit
	Dim RoofOpen as Bit = 1
	RoofOpenState = ReadInput(RoofOpen)
End Sub
For whatever reason I'm getting this error:
"Simple Function.bas:11: Error: assignment type conflict; left:Bit, right:Byte"

I can't get my head around this. . .ReadInput should be a bit, and the output from GetBit should be a bit. . .??? So why this error?

I know this is an extremely elementary concept for most everyone, but I'm really having problems with this. Any help would be appreciated. Thanks!

-Jeff

Re: Writing a simple function - Help!

Posted: 03 June 2010, 13:18 PM
by dkinzer
everest wrote:ReadInput should be a bit, and the output from GetBit should be a bit. . .??? So why this error?
The error occurs because the value returned from GetBit() is actually a Byte. The reason for this is largely historical: GetBit() was introduced in BasicX and it doesn't have the sub-byte types Bit and Byte. The ZBasic implementation of GetBit() was designed to be compatible with BasicX.

In any event, it is easy to work around this anomaly:

Code: Select all

ReadInput = CBit(GetBit(InputByte, value))
Note, too, that I added code tags in your original post to make the code more readable.

Posted: 03 June 2010, 13:52 PM
by everest
aaHa! Thank you. . .I would never have guessed that GetBit would return a Byte!! I see how you are using the conversion routine now though. Thank you!

-Jeff

Posted: 03 June 2010, 14:27 PM
by dkinzer
everest wrote:I would never have guessed that GetBit would return a Byte!
Fortunately, there is no need to guess. The manual page for GetBit() clearly indicates the return type.