Simple example of I2c code " My World" request

Questions on control of serial busses
jmcdougall
Posts: 35
Joined: Tue Mar 22, 2022 11:17 pm

Re: Simple example of I2c code " My World" request

Post by jmcdougall »

That explains why I am confused. I am used to the VB context of String arrays where:

Dim x(20) as String defines an array of twenty elements where each element can contain up to 256 chars,

and Dim x(20 as String(20) defines an array of twenty elements where each element is limited to 20 chars

So is there a way to assign multiple strings into an array or do I have to keep reusing a single array i.e. define ProcStep(20)
and then sequentially assign a string value to it , use it and then assign a new string value. What I posted is only a sample of a ton of this kind of assignment for creating menus and processing displays.



basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Simple example of I2c code " My World" request

Post by basicchip »

It looks like your strings are all constants.

If that is the case you can build each constant string with a CONST definition

Then you can pass an address of the constant to the routine using BYREF

Code: Select all


CONST ProcStep1 as BYTE = "Pre-Wash"
CONST ProcStep2 as BYTE = "1st Developer"
CONST ProcStep3 as BYTE = "Wash"

sub dumpstr (byref pt2string as string)
	print pt2string

end sub

dim spointer(20) as string

main:

	spointer = ProcStep1
	dumpstr (spointer)
	
	spointer = ProcStep2
	dumpstr (spointer)

	spointer = ProcStep3
	dumpstr (spointer)

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Simple example of I2c code " My World" request

Post by basicchip »

My last example had some unnecessary copy of strings, you can send both constant and variable strings BYREF

Code: Select all

CONST ProcStep1 as BYTE = "Pre-Wash"
CONST ProcStep2 as BYTE = "1st Developer"
CONST ProcStep3 as BYTE = "Wash"

sub dumpstr (byref pt2string as string)
	print pt2string

end sub

main:

	dumpstr (ProcStep1)
	
	dumpstr (ProcStep2)

	dumpstr (ProcStep3)
And while there are no multi-dimensional arrays in this BASIC, you can emulate them. You could have an array of integers and each could point to a CONST STRING (or variable for that matter) by assigning the value ADDRESSOF (somestring), and then passing that BYREF to the routine.

The I2C routines use BYREF passing of strings, so you don't need to change anything there. You can always look at the source of the library routines which is in BASIC.

jmcdougall
Posts: 35
Joined: Tue Mar 22, 2022 11:17 pm

Re: Simple example of I2c code " My World" request

Post by jmcdougall »

That has clarified a number of issues. There is one issue that I am now trying to understand. The buttons on the display are mapped to characters. A through E, F G. When the button is pressed it puts an Uppercase value in the I2C buffer and when released can put the lowercase version in the buffer. These show as decimal values in LCD_Data(0). For example, if I do
print "Data0 = "; LCD_DATA(0) it will print 69 for a button press that is mapped to "E".

To exit the sub that is checking the buffer every 1 sec, I need to compare to see if I have non zero value goto to a label outside the loop and pass the actual value in the return from the sub. Since LCD_DATA is a string array, but the value in LCD_DATA(0) is a decimal value do I compare LCD_DATA(0) to a numeric value
i.e.
IF LCD_DATA(0) = 69
or do I compare
IF LCD_DATA(0) = "E"

and if I assign LCD_DATA(0)
keypadval = LCD_DATA(0) ' keypadval is a string variable that is a parm on the sub so I can return which button was pressed in order to take the appropriate action back in MAIN

Do I have to do a convert of some kind in order to return the text character "E" as keypadval?

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Simple example of I2c code " My World" request

Post by basicchip »

I think the help files talk about single character strings (they are a special case)

But you could just try it ---

Code: Select all

dim lcdData(10)
lcdData(0)=69
if lcdData(0) = "E" then print "they are the same"
run

...

compile done
Programming Flash *+*+
done
Executing...

they are the same

jmcdougall
Posts: 35
Joined: Tue Mar 22, 2022 11:17 pm

Re: Simple example of I2c code " My World" request

Post by jmcdougall »

I did the compare to "X" and that appears to work. As soon as I can manage to kill the breakaway program, I can implement the last fix on handling the keypadresponse in the MAIN.

jmcdougall
Posts: 35
Joined: Tue Mar 22, 2022 11:17 pm

Re: Simple example of I2c code " My World" request

Post by jmcdougall »

In the sub GetKeypad that retrieves the keypad value via i2c, doing a compare to a string char works.
i.e.
if LCD_DATA(0) = "A" then
goto EXIT1
EndIF
This works inside the sub

However if I pass LCD_DATA(0) into keypadval
keypadval = LCD_DATA(0)
and Return
Sub GetKeypad(BYREF keypadval as String)
the corresponding value on the call
In MAIN
GetKeypad(keypadresponse)

If I now do the same compare in MAIN
If keypadresponse(0) = "B" Then
FAILS

I had to do this in the sub
keypadval = chr(LCD_DATA(0))

This then returns a char string not the decimal value that does work for the compares in MAIN

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Simple example of I2c code " My World" request

Post by basicchip »

It looks like you are mixing strings and characters which will not do what you want it to do.

But I really need more context. Or better yet, try simple examples to see what is going on.

Code: Select all

keypadval = LCD_DATA

... or ...

keypadval(0) = LCD_DATA(0)
Is probably what you meant to do.

keypadval is a string, LCD_DATA(0) is a character

When you mix the 2 types, in your use, the character is treated like a number (by default) and then that number is converted to ASCII, which is can be 1,2 or 3 bytes (0-255). But when you use chr(x) that tells the compiler to convert the number to a byte and build a string around that.

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Simple example of I2c code " My World" request

Post by basicchip »

You might want to look at this in the forum

viewtopic.php?f=9&t=266&p=710

I am also thinking of breaking some of this string stuff into a separate topic.

The issue comes about when your code has mismatched types.

When the compiler sees

string_type = number_type ' the compiler does string_type = STR(number_type)

Now it could issue a warning, though that could lead to lots of warnings in existing code (the compiler is nearly 20 years old now). This is a handy way to build strings

str = "$" + dollars + "." + cents ' or you could use sprintf

Another issue you pointed out was single byte strings and numbers

When the left side of the equals is a string_type, "a" is treated as a string
But when the left side of the equals is an integer type, "a" is treated as a number.

Post Reply