Need help, Again. This time with arrays
Ravenkallen
Posts: 1,057
Hey, i can't figure out how to get a blasted array to work. I need read in keyboard data and store it in a array or string or something. Here is the code i used and i can't get the desired results.....
repeat
·Message[noparse][[/noparse]counter] := chardata2
·waitcnt(clkfreq/4 + cnt)
·counter++
i want to put single bytes into a array, but i do not want to have to declare every byte in the array....E.G
Message[noparse][[/noparse]0]
message[noparse][[/noparse]1]
message[noparse][[/noparse]2]
ect....
Can variables be used as a pointer to a array. This has been so annoying to get it work. IF you guys have any code snippets or ideas, please share with a newbie. I know you can do the lookup thing, but i don't know if you can write to it..... I am at my wits end. Thank you all, for allready helping me with previous posts. It is good to know that there are people who care about more than themselves.
repeat
·Message[noparse][[/noparse]counter] := chardata2
·waitcnt(clkfreq/4 + cnt)
·counter++
i want to put single bytes into a array, but i do not want to have to declare every byte in the array....E.G
Message[noparse][[/noparse]0]
message[noparse][[/noparse]1]
message[noparse][[/noparse]2]
ect....
Can variables be used as a pointer to a array. This has been so annoying to get it work. IF you guys have any code snippets or ideas, please share with a newbie. I know you can do the lookup thing, but i don't know if you can write to it..... I am at my wits end. Thank you all, for allready helping me with previous posts. It is good to know that there are people who care about more than themselves.
Comments
But you have to take care that the index is not bigger than the last possible index of the array. Nothing will check that for you - neither the propeller during runtime (like it's done in Java), nor the compiler during compile-time ( so you could have a message[noparse][[/noparse] 100 ] instruction somewhere even if the message array only has 40 bytes). When you leave the boundary of an array anything can happen ... reset ... hang ... destroy pins ....
What's the difference in an array and a string? Easy ... the string is terminated by a 0 (zero). If you read a ascii string using a interface (for example a serial interface) there is usually no string end sent. So, you should be sure not to call a string function without adding the string terminator by yourself.
A variable can also be used as a pointer to an array. Say you defined your Message variable and want to pass that to an object. Then you have to give the address to the object, as the object won't see the variable named Message.
VAR
byte Message[noparse][[/noparse]50]
PUB
obj1.fillMessage( @Message )
the function definition in the object would look like:
PUB fillMessage( buffer_addr )
and you can access the content of the array by:
byte[noparse][[/noparse] buffer_addr ]
or
byte[noparse][[/noparse] buffer_addr ][noparse][[/noparse] counter ]
in the first case you have to increase buffer_addr if you want to loop through the array. In the second case you can increase counter instead.
Message[noparse][[/noparse]counter] := chardata2
or
byte[noparse][[/noparse]@Message + counter] := chardata2
or
byte[noparse][[/noparse]@Message][noparse][[/noparse]counter] := chardata2
Dave
con
·_xinfreq = 5_000_000
·_clkmode = xtal1 + pll16X
obj
Debug: "fullduplexserial"
var
long erase[noparse][[/noparse]3], adcval, adcvaltwo, adcvalthree
byte chardata, message[noparse][[/noparse]22], shift, othervar, i1, j2, k3, l4, m5, n6, o7, p8, q9, r0, backspace[noparse][[/noparse]2], ss, tt, uu, vv, ww, xx, yy
byte counter, nextline,space, goback[noparse][[/noparse]2], counter2, chardata2, txbyte
pub main
·debug.start(3, 4, 0, 2400)
·erase := string(254, 81)
·debug.str(erase)
·backspace := string(254, 78)
·nextline := string(254, 69, 64)
·goback := string(254, 73)
repeat
·chardata := 0
·readadc
·if chardata > 1
· debug.tx(chardata)
· Message[noparse][[/noparse]counter] := chardata
· waitcnt(clkfreq/4 + cnt)
· counter++
·if counter == 20
· debug.str(nextline)
·if counter == 40
· debug.str(erase)
· counter2 := 0
· repeat 20
·· byte[noparse][[/noparse]@message + counter] := txbyte
·· debug.tx(txbyte)
·· counter++
· waitcnt(clkfreq * 2 + cnt)
· debug.str(erase)
· counter := 0
·
pub readadc
·
···
···
· ctra[noparse][[/noparse]30..26] := %01000
· ctra[noparse][[/noparse]5..0] := 13
· frqa := 1
· dira[noparse][[/noparse]13] := outa[noparse][[/noparse]13] := 1
· waitcnt(clkfreq/1_000 + cnt)
· phsa~
· dira[noparse][[/noparse]13]~
· waitcnt(clkfreq/200 + cnt)
· phsa := phsa/ 100
· adcval := phsa
·
·
·
· ctra[noparse][[/noparse]30..26] := %01000
· ctra[noparse][[/noparse]5..0] := 14
· frqa := 1
· dira[noparse][[/noparse]14] := outa[noparse][[/noparse]14] := 1
· waitcnt(clkfreq/1_000 + cnt)
· phsa~
· dira[noparse][[/noparse]14]~
· waitcnt(clkfreq/200 + cnt)
· phsa := phsa/ 100
· adcvaltwo := phsa
·
·
·
· ctra[noparse][[/noparse]30..26] := %01000
· ctra[noparse][[/noparse]5..0] := 15
· frqa := 1
· dira[noparse][[/noparse]15] := outa[noparse][[/noparse]15] := 1
· waitcnt(clkfreq/1_000 + cnt)
· phsa~
· dira[noparse][[/noparse]15]~
· waitcnt(clkfreq/200 + cnt)
· phsa := phsa/ 100
· adcvalthree := phsa
· if i1 == "1"
···
··· if adcvalthree < 40
···· debug.str(erase)
···· debug.str(goback)
···· counter := 0
····
··
·
·
· if adcval < 10
·· chardata := ss
·· adcval := 1_000
· if adcval < 21
·· chardata := tt
·· adcval := 1_000
· if adcval < 34
·· chardata := uu
·· adcval := 1_000
· if adcval < 46
·· chardata := vv
·· adcval := 1_000
· if adcval < 58
·· chardata := ww
·· adcval := 1_000
· if adcval < 70
·· chardata := xx
·· adcval := 1_000
··
· if adcval < 80
·· chardata := yy
·· adcval := 1_000
· if adcval < 94
·· chardata := "z"
·· adcval := 1_000
· if adcval < 105
··· i1 := "i"
··· j2 := "j"
··· k3 := "k"
··· l4 := "l"
··· m5 := "m"
··· n6 := "n"
··· o7 := "o"
··· p8 := "p"
··· q9 := "q"
··· r0 := "r"
··· ss := "s"
··· tt := "t"
··· uu := "u"
··· vv := "v"
··· ww := "w"
··· xx := "x"
··· yy := "y"
··· space := "a"
···
·· adcval := 1_000
··
· if adcval < 120
·· i1 := "1"
·· j2 := "2"
·· k3 := "3"
·· l4 := "4"
·· m5 := "5"
·· n6 := "6"
·· o7 := "7"
·· p8 := "8"
·· q9 := "9"
·· r0 := "0"
·· ss := ","
·· tt := "."
·· uu := "?"
·· vv := "-"
·· ww := "'"
·· xx := "+"
·· yy := " "
·· space := " "
·· adcval := 1_000
··
··
· if adcvaltwo < 10
·· chardata := i1
·· adcvaltwo := 1_000
· if adcvaltwo < 21
·· chardata := j2
·· adcvaltwo := 1_000
· if adcvaltwo < 34
·· chardata := k3
·· adcvaltwo := 1_000
· if adcvaltwo < 46
·· chardata := l4
·· adcvaltwo := 1_000
· if adcvaltwo < 58
·· chardata := m5
·· adcvaltwo := 1_000
· if adcvaltwo < 70
·· chardata := n6
·· adcvaltwo := 1_000
··
· if adcvaltwo < 80
·· chardata := o7
·· adcvaltwo := 1_000
· if adcvaltwo < 94
·· chardata := p8
·· adcvaltwo := 1_000
· if adcvaltwo < 105
·· chardata := q9
·· adcvaltwo := 1_000
··
· if adcvaltwo < 120
·· chardata := r0
·· adcvaltwo := 1_000
· if adcvalthree < 40
··· chardata := "a"
··· adcvalthree := 1_000
· if adcvalthree < 100
·· chardata := "b"
·· adcvalthree := 1_000
· if adcvalthree < 160
·· chardata := "c"
·· adcvalthree := 1_000
··
· if adcvalthree < 220
·· chardata := "d"
·· adcvalthree := 1_000
· if adcvalthree < 285
·· chardata := "e"
·· adcvalthree := 1_000
· if adcvalthree < 350
·· chardata := "f"
·· adcvalthree := 1_000
··
· if adcvalthree < 410
·· chardata := "g"
·· adcvalthree := 1_000
· if adcvalthree < 470
·· chardata := "h"
·· adcvalthree := 1_000