Writing a simple function - Help!

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
everest
Posts: 96
Joined: 31 May 2010, 9:01 AM

Writing a simple function - Help!

Post 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
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Writing a simple function - Help!

Post 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.
- Don Kinzer
everest
Posts: 96
Joined: 31 May 2010, 9:01 AM

Post 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
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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.
- Don Kinzer
Post Reply