How to index STRING's
TC
Posts: 1,019
I am having trouble understanding how to index a string in data. This is a learning program. I am trying to find a way for it to display the day just by changing one value. Please give me some ideas.
Also I dont know how to post code on this new forum. the old one had a button.
DIG VAR Word
CHAR VAR Word
BRIGHT VAR Byte
SEGMENT VAR Byte
SEG_TEMP VAR Byte
DIGIT VAR Byte
DIG_TEMP VAR Byte
STRING_TEMP VAR Byte
idx1 var Word
DAY VAR Byte
' =========================================================================
PROGRAM Start
' =========================================================================
'
' Subroutine Declarations
'
Boost_Control SUB
Display_Load SUB
Get_Char SUB
Get_Dig SUB
String SUB
'
' Program Code
'
Start:
BLANKING = 0
BRIGHT = 200
Main:
for day = 0 to 6
for idx1 = 0 to 50
for digit = 0 to 7
Boost_Control
String
Display_Load
next
next
next
GOTO Main
'
' Subroutine Code
'
SUB Boost_Control
PWM Boost, BRIGHT, 1
return
SUB String
STRING_TEMP = DIGIT + DAY
READ DAY_MAP + STRING_TEMP, SEGMENT
return
SUB Display_Load
Get_Char
Get_Dig
SHIFTOUT SDATA, CLOCK, MSBFIRST, DIG\16
SHIFTOUT SDATA, CLOCK, MSBFIRST, CHAR\16
PULSOUT STROBE, 1
return
SUB Get_Char
SEG_TEMP = SEGMENT - $20
SEG_TEMP = SEG_TEMP * 2
READ FONT_MAP + SEG_TEMP, CHAR
return
SUB Get_Dig
DIG_TEMP = DIGIT * 2
READ DIG_MAP + DIG_TEMP, DIG
return
' =========================================================================
' User Data
' =========================================================================
DAY_MAP:
DATA "SUNDAY",0
DATA "MONDAY",0
DATA "TUESDAY",0
DATA "WEDNESDAY",0
DATA "THURSDAY",0
DATA "FRIDAY",0
DATA "SATURDAY",0
DIG_MAP:
WDATA %00000000_10000000
WDATA %00000000_01000000
WDATA %00000000_00100000
WDATA %00000000_00010000
WDATA %00000000_00001000
WDATA %00000000_00000100
WDATA %00000000_00000010
WDATA %00000000_00000001
FONT_MAP:
WDATA %00000000_00000000 'SPACE
WDATA %01100000_00000000 '!
WDATA %00000100_01000000 '"
WDATA %11111111_11111100 '#
WDATA %10110111_01001000 '$
WDATA %00100111_10110100 '%
WDATA %10110000_10110100 '&
WDATA %00000000_00100000 ''
WDATA %00000000_00110000 '(
Also I dont know how to post code on this new forum. the old one had a button.
DIG VAR Word
CHAR VAR Word
BRIGHT VAR Byte
SEGMENT VAR Byte
SEG_TEMP VAR Byte
DIGIT VAR Byte
DIG_TEMP VAR Byte
STRING_TEMP VAR Byte
idx1 var Word
DAY VAR Byte
' =========================================================================
PROGRAM Start
' =========================================================================
'
' Subroutine Declarations
'
Boost_Control SUB
Display_Load SUB
Get_Char SUB
Get_Dig SUB
String SUB
'
' Program Code
'
Start:
BLANKING = 0
BRIGHT = 200
Main:
for day = 0 to 6
for idx1 = 0 to 50
for digit = 0 to 7
Boost_Control
String
Display_Load
next
next
next
GOTO Main
'
' Subroutine Code
'
SUB Boost_Control
PWM Boost, BRIGHT, 1
return
SUB String
STRING_TEMP = DIGIT + DAY
READ DAY_MAP + STRING_TEMP, SEGMENT
return
SUB Display_Load
Get_Char
Get_Dig
SHIFTOUT SDATA, CLOCK, MSBFIRST, DIG\16
SHIFTOUT SDATA, CLOCK, MSBFIRST, CHAR\16
PULSOUT STROBE, 1
return
SUB Get_Char
SEG_TEMP = SEGMENT - $20
SEG_TEMP = SEG_TEMP * 2
READ FONT_MAP + SEG_TEMP, CHAR
return
SUB Get_Dig
DIG_TEMP = DIGIT * 2
READ DIG_MAP + DIG_TEMP, DIG
return
' =========================================================================
' User Data
' =========================================================================
DAY_MAP:
DATA "SUNDAY",0
DATA "MONDAY",0
DATA "TUESDAY",0
DATA "WEDNESDAY",0
DATA "THURSDAY",0
DATA "FRIDAY",0
DATA "SATURDAY",0
DIG_MAP:
WDATA %00000000_10000000
WDATA %00000000_01000000
WDATA %00000000_00100000
WDATA %00000000_00010000
WDATA %00000000_00001000
WDATA %00000000_00000100
WDATA %00000000_00000010
WDATA %00000000_00000001
FONT_MAP:
WDATA %00000000_00000000 'SPACE
WDATA %01100000_00000000 '!
WDATA %00000100_01000000 '"
WDATA %11111111_11111100 '#
WDATA %10110111_01001000 '$
WDATA %00100111_10110100 '%
WDATA %10110000_10110100 '&
WDATA %00000000_00100000 ''
WDATA %00000000_00110000 '(
Comments
For Example, Here is a SUB which I made to display a string or a character on a 2x16 display based on what line and character to start at.
And for a FUNC example :
To post code, you can use [ CODE ]Your Code Here[ / Code] (without the spaces) Hope this helps!
The code you gave looks interesting, but I have no idea what is going on. I only know PBASIC and I am trying to learn SX/B, that is why I have chosen this program. I want to start with baby steps and learn on the way. I am still trying to understand returning a variable (using FUNC). To me I cant understand the benefits of not declaring a variable, why should I return a variable.
The String SUB converted to a FUNC :
In this example, I used myvar = String 1, 3
The function will see that DIG_TEMP = 1 and DAY_TEMP = 3
The variable SEGMENT is set by the READ function based on the passed variables you give it. Since the variable SEGMENT is not automatically passed back to the rest of your code, you need to have another variable in your main code (myvar) to hold the information returned by the function.
Your SUB Boost_Control is good since there is no information needed to be sent back to your main code / loop.
You should also take a look at the READ function in the SX/B Help in your SX-Key program. It will explain how to retrieve the full data string instead of 1 character.
First, you should have a subroutine that can accept the address of the string you want and then do something with that. In many cases the strings will be terminated by a 0 or be a fixed length. Your routine will then start reading each byte and deal with it (Send it to a display, serial port, etc) then increment the variable used as the pointer and repeat. If it reads a 0 (or fixed length if your using that method) then exit the routine.
From your listing it seems that some of the issue may be finding the start of each string. You have an address for the first one but they aren't all the same length. To find the start of the string you want there are a few methods. Since they aren't the same size you can walk through them each time. You use the address of the first one. If the offset is 0 then return. If not then read bytes until you hit the termination character (0 in this case) and subtract 1 from your count. When it gets to 0 then you're pointing at the string you want. Alternately, you can just make another table that has the addresses of the beginning of each string.
If you make all the strings the same length then you can calculate the start of each string by adding the index of the string you want times the length of each entry (including termination character). That will give you the address of the beginning of the string you want.
And change your String sub to :
Since the Read function starts with the character number you specify, you should set all your DATA fields to have the same amount of characters. Yours would be set to 10 characters since Wednesday is the longest spelled word. When you call the String function, the READ function will start reading from the character at the calculated starting point.... example : DAY = 0 : STRING_TEMP = 0 * 10 which = 0. If you insert 2 for day, STRING_TEMP will equal 20 since 2 * 10 = 20 and will cause the READ function to start reading from character 20 which is the beginning of the third line. Check out the RFID example in the SX/B help section of the SX program for a better example. Hope this helps
http://forums.parallax.com/attachment.php?attachmentid=57791&d=1231117288
It's from this thread
http://forums.parallax.com/showthread.php?105312-SX-controlled-6432-Dot-Matrix-Bi-Tri-Color-LED-Display&p=746212
This program is just to help me to learn. I was in a coma for 9 months and it is a little hard to learn, but i will not give up. Thank you for the ideas. I like it best when somone dose not give me the answer but give me the idea how.
That will help me out alot thanks.
Thanks now you got my gears turning.