Need some hand holding with this code...
Don M
Posts: 1,652
What I'm trying to do is enter some numbers into a variable and display them on the lcd display. I've been tearing my hair out trying to solve this.
"key" is a byte returned from a touchscreen method. It returns the ascii numbers 0 - 9 ($30 - $39).
On the display (disp) it shows the string- "Enter year (2000 - 2099): " Lets say I want to enter 2013. The display will show 2 then 20 then 201 then 2013 as I type the numbers in. That is exactly how I want it to show.
With this code it stores those numbers in long variable YYYY and as I type the numbers in that variable is displayed on PST (for debugging only). As I type the numbers in variable YYYY shows: 32000000, 32300000, 32303100 then 32303133. It stores the ascii hex equivalents of the numbers.
So here in lies the problem. I want the variable YYYY to contain the number as a whole, the hex equivalent of 2013 which is $07DD not just the individual numbers.
So how do I get there? Somehow I need to take those 4 individual numbers and convert them into one number and store the hex value of it.
"key" is a byte returned from a touchscreen method. It returns the ascii numbers 0 - 9 ($30 - $39).
On the display (disp) it shows the string- "Enter year (2000 - 2099): " Lets say I want to enter 2013. The display will show 2 then 20 then 201 then 2013 as I type the numbers in. That is exactly how I want it to show.
With this code it stores those numbers in long variable YYYY and as I type the numbers in that variable is displayed on PST (for debugging only). As I type the numbers in variable YYYY shows: 32000000, 32300000, 32303100 then 32303133. It stores the ascii hex equivalents of the numbers.
So here in lies the problem. I want the variable YYYY to contain the number as a whole, the hex equivalent of 2013 which is $07DD not just the individual numbers.
So how do I get there? Somehow I need to take those 4 individual numbers and convert them into one number and store the hex value of it.
if tch == $01 case idx 0..3: YYYY.byte[idx] := key ' Year 4 bytes - 1 word repeat i from 0 to 3 disp.Put_Char(byte[@YYYY][i]) if idx == 3 disp.Move_Cursor($0005, $0001) disp.Put_String(@month) term.dec(idx) term.tx($20) term.hex(key, 2) term.tx($20) repeat i from 0 to 3 term.hex(byte[@YYYY][i], 2) 'term.str(byte[@YYYY][i]) term.tx(13) 4..5: term.dec(idx) term.tx($20) term.hex(key, 2) term.tx(13) disp.Move_Cursor($0005, $0001) disp.Put_String(@month) MM_DD_YYYY.byte[idx] := key ' Month 2 bytes repeat i from 4 to 5 disp.Put_Char(byte[@MM_DD_YYYY][i]) if idx == 5 disp.Move_Cursor($0006, $0001) disp.Put_String(@date) 6..7: term.dec(idx) term.tx($20) term.hex(key, 2) term.tx(13) disp.Move_Cursor($0006, $0001) disp.Put_String(@date) MM_DD_YYYY.byte[idx] := key ' Date 2 bytes repeat i from 6 to 7 disp.Put_Char(byte[@MM_DD_YYYY][i]) if idx == 7 disp.Move_Cursor($0008, $0001) disp.Put_String(@day1) disp.Move_Cursor($0009, $0001) disp.Put_String(@day2) disp.Move_Cursor($000B, $0001) disp.Put_String(@day3) 8: term.dec(idx) term.tx($20) term.hex(key, 2) term.tx(13) 'idx2 := idx - 7 disp.Move_Cursor($0008, $0001) ' Day 1 byte disp.Put_String(@day1) disp.Move_Cursor($0009, $0001) disp.Put_String(@day2) disp.Move_Cursor($000B, $0001) disp.Put_String(@day3) DW_HH_MM_SS.byte[idx - 8] := key disp.Put_Char(byte[@DW_HH_MM_SS][idx - 8]) if idx == 8 disp.Move_Cursor($000C, $0001) disp.Put_String(@hour) 9..10: 'idx2 := idx - 7 disp.Move_Cursor($000C, $0001) disp.Put_String(@hour) DW_HH_MM_SS.byte[idx - 8] := key ' Hour 2 bytes repeat i2 from 1 to 2 disp.Put_Char(byte[@DW_HH_MM_SS][i2]) if idx == 10 disp.Move_Cursor($000D, $0001) disp.Put_String(@minute) 11..12: 'idx2 := idx - 7 disp.Move_Cursor($000D, $0001) disp.Put_String(@minute) DW_HH_MM_SS.byte[idx - 8] := key ' Minutes 2 bytes repeat i2 from 3 to 4 disp.Put_Char(byte[@DW_HH_MM_SS][i2]) DW_HH_MM_SS.byte[5] := $30 ' Seconds 2 bytes DW_HH_MM_SS.byte[6] := $30 idx ++
Comments
date_val := date_val*10 + Byte[@YYYY]-$30
So, let's say you enter a 2, it's date_val := 0*10 + $32-$30 = 2
next number: 0 => date_val := 2*10 + $30-$30 = 20
next number: 1 => date_val := 20*10 + $31-$30 = 201
next number: 3 => date_val := 201*10 + $33-$30 = 2013
I think there are also methods like this in the "Numbers" or "SimpleNumber" object.
Edit: Sorry, I didn't read OP carefully enough. The methods I mentioned assume characters are stored as bytes.
I can change my touch key code to return the numbers 0 - 9 instead of their hex equivalents if the would make anything easier.
SRLM- I'm not well versed in C so I don't quite follow your code.
If the touch would return 0-9 instead of "0"-"9" you'd save the -$30 in the line of code I mentioned above. But on the other hand you'd need to do a +$30 before sending the number to the display. So, no real win!
I got your code snippet to work. Thanks very much.
Now I have another strange problem. See my new post.
If there is a simpler way to do this I'm all ears...
If I was to enter todays date- 2013 19 02 what I would expect the MM_DD_YYYY buffer to look like is 021307DD
It would be broken down like this: 02 is byte[3] and is the month in hex. 13 is byte[2] and is the date in hex. 07DD is bytes [0] and [1] and is the year in hex.
What I'm getting is this: 020007DD. For some reason this code won't work right for the "date" or "month" if there is a number greater than 0 in the tens column portion.
As I mentioned earlier if there is an easier way to accomplish this I'd appreciate it.
Thanks in advance.
I guess what I don't understand is why it works for the year portion and not the others...
I thought I was doing the math for each segment as it goes through the repeat loop. But I must not be.
And I do initialize all the variables to 0 in case you were wondering.
Ok now that is some pared down code! I don't understand it so I'll have to work with it a bit at a time and figure it out.
That fixed it! Thanks a million!
I still want to understand your code better and see if I can incorporate that into this. It has the appearance of being much simpler. Would you mind commenting your code for me? I'm not looking for you to incorporate it into my code but a better understanding of yours so that I may attempt at putting it to use myself.
Thanks again!