Showing posts with label data. Show all posts
Showing posts with label data. Show all posts

Friday, November 8, 2013

Petit Computer Journal #24


Transferring Data from Petit Computer to PC

It's no secret that you can save the graphic screen to your SD card. What is the problem is that the data isn't stored flat. It actually is saved in character format, with block-sized groupings at that.

Discostew (of PTC MegaMan 2) wrote an article on the subject and Randomous reposted it here:

http://petitcomputer.wikia.com/wiki/GRP_File_Format_(External)

Well, that's fine and dandy if you want to use a separate program on the PC. What if you don't want to use a separate program on PC? Can you do the conversion right on Petit Computer? Of course. The question is: how difficult is it?

This is just an extremely simple problem. All I have to do is reverse the process, and I'd be done. Except, for some reason, I couldn't get it right. After four tries, and a whole day has gone, this is what I came up with:



  1. ACLS

  2. FN$="G0":'GRAPHIC FILENAME
  3. LOAD "GRP1:"+FN$,FALSE
  4. PNLTYPE "OFF"
  5. WAIT 180

  6. CX=0:CY=0
  7. FOR J=0 TO 2:FOR I=0 TO 3
  8. FOR Y=0 TO 7:FOR X=0 TO 7
  9. FOR YY=0 TO 7:FOR XX=0 TO 7

  10. GPAGE 0,2,2
  11. GPSET (I*64+X*8+XX),(J*64+Y*8+YY),CX
  12. GPAGE 1,3,3
  13. GPSET (I*64+X*8+XX),(J*64+Y*8+YY),CY
  14. CX=(CX+1)%256
  15. IF CX==0 THEN CY=CY+1

  16. NEXT:NEXT
  17. NEXT:NEXT
  18. NEXT:NEXT

  19. WAIT 60

  20. FOR J=0 TO 191:FOR I=0 TO 255
  21. GPAGE 1,2,1
  22. X=GSPOIT(I,J)
  23. GPAGE 1,3,1
  24. Y=GSPOIT(I,J)
  25. GPAGE 1,1,1
  26. C=GSPOIT(I,J)
  27. GPAGE 0,0,0
  28. GPSET X,Y,C
  29. GPAGE 1,1,1
  30. 'GPSET X,Y,0
  31. NEXT:NEXT

  32. WAIT 600


That's a two-step process. First, I load the graphic on the bottom screen, and encode the coordinates on GRP2 for x-coordinates, and GRP3 for y-coordinates. Then I use such coordinates to re-arrange the points on the top screen. You see such convoluted GPAGE commands because I'm switching back and forth among the different pages.

It works, but hardly elegant. The problem is that for some reason, my brain just can't wrap around the problem. Intellectually, I know the problem is a simple referencing problem. A pointer problem, for those of you doing in C programming language. And yet, the answer eludes me.

It's not until I'm at McDonald, getting my StreetPasses, warm and fed, that my brain functions again. Of course, after so many tries to the problem, my brain did start to warm up to the problem.

It was so trivial. I sketched the solution on the back of the receipt, and five minutes later, I was done! Here is what I came up with:


  1. LOAD "GRP1:FILENAME",FALSE
  2. PNLTYPE "OFF"
  3. GOSUB @SCAN
  4. 'SAVE GRP0 here...
  5. END

  6. @SCAN
  7. CX=0:CY=0
  8. FOR J=0 TO 2:FOR I=0 TO 3
  9. FOR Y=0 TO 7:FOR X=0 TO 7
  10. FOR YY=0 TO 7:FOR XX=0 TO 7

  11. GPAGE 1
  12. C=GSPOIT(CX,CY)
  13. GPSET CX,CY,0
  14. GPAGE 0
  15. GPSET (I*64+X*8+XX),(J*64+Y*8+YY),C
  16. CX=(CX+1)%256
  17. IF CX==0 THEN CY=CY+1

  18. NEXT:NEXT
  19. NEXT:NEXT
  20. NEXT:NEXT

  21. RETURN


Quite a big difference, wouldn't you say? I had all the pieces all along, and when I compared the solution on the receipt with my code, I realized that the code had it backwards. All I have to do then is to just switched the direction the coordinates was transfered! That's it.

Well, except it doesn't work, and Discostew pointed out that my latest code did double encoding, and the original "erroneous" code was right to begin with! Oh, well. So read in linear, and output blocks!

That's a reference, or pointer, problem. And guess what? As a programmer, you have to understand the concept of "reference". Like finger pointing to the moon. You look at the moon, instead of the finger. Simple, isn't it? Yet, so many people are confused.

So much so, that people are leaving C programming language for something easier to program. By that, they mean a programming language that does not use pointer. Guess what? The concept of reference is so universal in programming, it exists even in a pointer-less language such as BASIC.

Therefore, I say, you must learn the concept of pointers. If you don't, you just limit yourself to the problems you can solve.

Oh, and about this? I don't know why I was having such troubles. But I did solve it, and that's the important thing. The next time such a problem come up again, I'll be ready.

Tuesday, August 20, 2013

Petit Computer Journal #13



Importing Data Into Petit Computer


One of the most important aspect in computing is data. In fact, I would say that the most important factor in computing is data. Everything that is important in computer can be traced to a database. Whether it is searching book titles such as Amazon.com, email, or managing your finances, data is usually the most important part of computing experience.

This has not always been true. Such early computer, say, having only 4 kilobytes of memory, downplayed the importance of data. However, without a good data reader, such as tape recorder, or worse, punch cards, such computer capability is wasted. Sure,there was Pong, but what good is it? The seminal early program was Adventure, or Collosal Cave, modelling the Mammoth Cave in Kentucky. Data is very important!

How can we enter data into the computer, specifically Petit Computer? Well, we can use the DATA keyword and type it in. But what if we already have it in our main computer? Here are the steps I use to import stock market data into Petit Computer.

1. Download the Historical stock data into your computer as CSV (Comma Separated Values)

Goto Yahoo.com and download a historical stock prices of your favorite stock. Make sure to save it as CSV format.

2. Open the file into your spreadsheet.

I use Microsoft Works, a cheaper alternative to Microsoft Office. Whatever works. Import the CSV file into the spreadsheet. Remove any unwanted columns. Insert column in the beginning. Fill it with "DATA". Save.

3. Open the file into Text Editor

I use Notepad. Replace all "DATA" with DATA, where you type a space character right after the text DATA. Add DATA "End",-1,-1,-1,-1,-1 at the end. This is assuming you're keeping the same data as me. Adjust accordingly.

4. Open up PTCUtilities.

Open up PRG Editor. Copy and Paste the text into the window. Comment out the first line. Make QR code out of it.

5. Scan with Petit Computer

Pick up your Nintendo DSi. Run Petit Computer program. Select "Scan QR Code". Import it to your Petit Computer. Let's call it STOKDATA.

6. Open up your main program.

I'm using the stock candlestick chart. The source code is at the end of the file. Load the program. Then on the RUN window, type "APPEND STOKDATA". This will add the content of STOKDATA to your current program. Save accordingly.

And there you have it, an easy way to import data to your Petit Computer Program.



'Stock Chart
@MAINLOOP
ACLS
GPAINT 6,6,6

C=0:MN=9999:MX=0
RESTORE @STOK
@STEP1
READ A$,OP,HI,LO,CL,AD
IF OP==-1 GOTO @STEP2
C=C+1:?C
IF MN>LO THEN MN=LO
IF HI>MX THEN MX=HI
GOTO @STEP1

@STEP2
RESTORE @STOK

FOR I=C TO 1 STEP -1
READ A$,OP,HI,LO,CL,AD
X1=C:X2=I:X3=1:Y1=247:Y3=24
GOSUB @MAP:DX=Y2
X1=MN:X2=HI:X3=MX:Y1=190:Y3=20
GOSUB @MAP:DHI=Y2
X1=MN:X2=LO:X3=MX:Y1=190:Y3=20
GOSUB @MAP:DLO=Y2

X1=MN:X2=OP:X3=MX:Y1=190:Y3=20
GOSUB @MAP:DOP=Y2
X1=MN:X2=CL:X3=MX:Y1=190:Y3=20
GOSUB @MAP:DCL=Y2

WAIT 1
GLINE DX,DHI,DX,DLO,8
IF OP>CL THEN GFILL DX-1,DOP,DX+1,DCL,15
IF CL>OP THEN GFILL DX-1,DOP,DX+1,DCL,14
GBOX DX-1,DOP,DX+1,DCL,15

NEXT

FOR I=BUTTON(3) TO 1:I=BUTTON(3):NEXT
GOTO @MAINLOOP

@MAP
Y2=(((X2-X1)/(X3-X1))*(Y3-Y1))+Y1
RETURN



@STOK
'DATA "Date","Open","High","Low","Close","Adj Close"
DATA "2012-12-10",11.41,11.58,11.03,11.1,11.1
DATA "2012-12-03",11.56,11.7,11.18,11.48,11.48
DATA "2012-11-26",11.05,11.6,10.97,11.45,11.45
DATA "2012-11-19",10.65,11.1,10.65,11.1,11.1
DATA "2012-11-12",11.03,11.16,10.38,10.5,10.5
DATA "2012-11-05",11.15,11.59,10.71,10.93,10.93
DATA "2012-10-31",10.7,11.38,10.6,11.17,11.17
DATA "2012-10-22",10.14,10.49,9.97,10.36,10.31
DATA "2012-10-15",10.11,10.57,10.09,10.18,10.13
DATA "2012-10-08",10.06,10.26,9.95,10.12,10.07
DATA "2012-10-01",9.89,10.28,9.71,10.16,10.11
DATA "2012-09-24",10.3,10.4,9.81,9.86,9.81
DATA "2012-09-17",10.27,10.66,10.26,10.4,10.35
DATA "2012-09-10",10.08,10.57,10.06,10.53,10.48
DATA "2012-09-04",9.37,10.23,9.35,10.14,10.09
DATA "2012-08-27",9.51,9.52,9.25,9.34,9.29
DATA "2012-08-20",9.58,9.7,9.4,9.49,9.44
DATA "2012-08-13",9.35,9.67,9.25,9.63,9.58
DATA "2012-08-06",9.13,9.46,9.09,9.35,9.3
DATA "2012-07-30",9.04,9.42,8.82,9.09,9.05
DATA "2012-07-23",9.08,9.24,8.83,9,8.91
DATA "2012-07-16",9.22,9.52,9.12,9.21,9.12
DATA "2012-07-09",9.46,9.55,9.12,9.27,9.18
DATA "2012-07-02",9.5,9.79,9.3,9.5,9.4
DATA "2012-06-25",10.13,10.18,9.46,9.59,9.49
DATA "2012-06-18",10.35,10.74,10.18,10.19,10.09
DATA "2012-06-11",10.77,10.8,10.21,10.35,10.24
DATA "2012-06-04",10.15,10.78,9.91,10.66,10.55
DATA "2012-05-29",10.69,10.88,10.06,10.12,10.02
DATA "2012-05-21",10.02,10.68,10,10.6,10.49
DATA "2012-05-14",10.41,10.53,9.96,10.01,9.91
DATA "2012-05-07",10.53,10.86,10.4,10.58,10.47
DATA "2012-04-30",11.42,11.47,10.63,10.67,10.56
DATA "2012-04-23",11.15,12.04,11.15,11.6,11.43
DATA "2012-04-16",12.01,12.05,11.39,11.41,11.24
DATA "2012-04-09",12.26,12.29,11.65,11.92,11.75
DATA "2012-04-02",12.5,12.95,12.32,12.47,12.29
DATA "2012-03-26",12.45,12.61,12.18,12.48,12.3
DATA "2012-03-19",12.52,12.68,12.18,12.32,12.14
DATA "2012-03-12",12.57,13.04,12.37,12.51,12.33
DATA "2012-03-05",12.67,12.73,12,12.58,12.4
DATA "2012-02-27",12.11,12.94,11.99,12.72,12.54
DATA "2012-02-21",12.74,12.76,12.16,12.23,12.05
DATA "2012-02-13",12.74,12.88,12.33,12.75,12.57
DATA "2012-02-06",12.85,13,12.37,12.44,12.26
DATA "2012-01-30",12.06,12.84,12,12.79,12.6
DATA "2012-01-23",12.69,13.05,11.79,12.21,12.03
DATA "2012-01-17",12.2,12.72,11.96,12.59,12.36
DATA "2012-01-09",11.83,12.18,11.63,12.04,11.82
DATA "2012-01-03",11,11.8,10.99,11.71,11.5
DATA "2011-12-27",10.87,10.98,10.43,10.76,10.56
DATA "2011-12-19",10.25,11,9.99,10.95,10.75
DATA "END",-1,-1,-1,-1,-1



Monday, August 19, 2013

Petit Computer Journal #12



Packing, Saving, and Loading Data

One of the most desirable feature of computer programming is data loading and saving. Nintendo DS is used mainly for gaming, and the ability to save and load large amount of data is mostly an afterthought. However, there is a built-in feature to do so: MEM$. Basically, we simply assign the the string to MEM$, and save that.

We can enter multiple data into MEM$ by using splitting numbers technique: MID$. Let's say that we put the data for score in MEM$ location 10-15. We can extract that by using MID$, like this:

SCORE=MID$(MEM$,10,5)

A similar technique is to pack numbers into one large number. You can split them out like so:

CLS
?"FOR BITS:"
?"DEC:1=10;2=100;3=1000"
?"BIN:1=2;2=4;3=8;4=16"
INPUT "NUMBER,BITS";N,B
@LOOP
?N%B
N=FLOOR(N/B)
IF N!=0 GOTO @LOOP
?"DONE!"

SAMPLE OUTPUT:
123456,100
56
34
12


Let's say that we have data in multiple column format, that we want to save. I'm going to make it easy for testing, and just use 192*16*16. First, we make the data, then we save it two ways: Graphic and MEM$. We also load it 2 ways. Finally, we're going to put some timer on each operation.

'Make up data
DIM D[50000]
FOR X=0 TO 191
FOR Y=0 TO 15
FOR Z=0 TO 15
D[X*256+Y*16+Z]=RND(256)
NEXT:NEXT:NEXT

'SAVE DATA GRAPH
T0=MAINCNTL
FOR X=0 TO 191
FOR Y=0 TO 15
FOR Z=0 TO 15
GPSET (Y*16+Z),X,D[X*256+Y*16+Z]
NEXT:NEXT:NEXT
?"SAVE GRP0:TEST1"
T1=MAINCNTL

'SAVE DATA MEM$
T2=MAINCNTL
FOR X=0 TO 191
MEM$=""
FOR Y=0 TO 15
FOR Z=0 TO 15
MEM$=MEM$+CHR$(D[X*256+Y*16+Z])
NEXT:NEXT
S$="MEM:MEM"+RIGHT$("000"+STR$(X),3):?"SAVE ";S$
NEXT
T3=MAINCNTL

?"GRAPH:",T1-T0
?"MEM$: ",T3-T2

However way I look at it, the MEM$ version is harder, longer, and slower. I purposely skip the actual saving process because it asks whether or not you want to overwrite existing files, which in MEM$ case, rather ponderous. Even so, GRAPH method is faster than MEM$

GRAPH:  300
MEM$:   632

Now let's do LOAD:

'LOAD DATA GRAPH
T0=MAINCNTL
?"LOAD GRP0:TEST1"
FOR X=0 TO 191
FOR Y=0 TO 15
FOR Z=0 TO 15
D[X*256+Y*16+Z]=GSPOIT(Y*16+Z,x)
NEXT:NEXT:NEXT
T1=MAINCNTL

'LOAD DATA MEM$
MEM$="A"*256
T2=MAINCNTL
FOR X=0 TO 191
S$="MEM:MEM"+RIGHT$("000"+STR$(X),3):?"LOAD ";S$
FOR Y=0 TO 15
FOR Z=0 TO 15
D[X*256+Y*16+Z]=VAL(MID$(MEM$,Y*16+Z,1))
NEXT:NEXT:NEXT
T3=MAINCNTL

?"GRAPH:",T1-T0
?"MEM$: ",T3-T2

And the result?

GRAPH:  266
MEM$:   840

In all cases, the Graph version is leaner, cleaner, and faster than the use of multiple MEM$.