Jump to content



Photo
- - - - -

Generating Casio Basic files with Python.

fx-9750-gii

  • Please log in to reply
6 replies to this topic

#1 JohnThomas

JohnThomas

    Newbie

  • Members
  • Pip
  • 8 posts

  • Calculators:
    fx-9750GII

Posted 21 October 2023 - 12:47 PM

Hello! I am currently working on a python script that will generate a CASIO-BASIC file to draw a sprite to the screen (code below). I generally am quite happy with how it turned out and it only took about an hour (although right now I have replaced the special characters with plain text equivalents). However, there are a few things I have no idea how to figure out that I was hoping someone more experienced could help me with. Whenever I open existing program files, I notice some kind of header code on the first line mainly comprised of unknown characters and null characters. Is there any way I can replicate this in my code? I've tried just copying it, but apparently null characters don't belong in python code. I've tried taking existing code and modifying it, but whenever I save it Visual Studio Code does some reformatting that completely messes it up and makes it unreadable by the calculator. Is there some other text editor or way I should be doing this?

 

If anyone could help me out and demystify this file format a bit, it would be greatly appreciated. If there is no way to do what I'm doing, please let me know. Thanks for your time!

 

from PIL import Image
import numpy


img = Image.open("sprite.png")
width = img.size[0]
height = img.size[1]

text_file = open("TEST.g1m", "w")
 
i = 0
while i < width:
i2 = 0
while i2 < height:
color = numpy.array(img)[i][i2]

if(color[0] == 0):
 
text_file.write("SketchNormal F-Line " + str(round(-6.3 + i/10, 1)) + "+X/10," + str(-3 + i2/10) + "+Y/10," + str(round(-6.3 + i/10, 1)) + "+X/10," + str(-3 + i2/10) + "+Y/10")
 
i2 += 1
i += 1

 



#2 Sheepolution

Sheepolution

    Newbie

  • Members
  • Pip
  • 22 posts
  • Gender:Male
  • Location:The Netherlands

  • Calculators:
    CFX-9850GC PLUS

Posted 21 October 2023 - 02:16 PM

I'm not sure what you mean with the header code, but why do you want to replicate it? Is that header code required for the program to work? It sounds like something that is added automatically.

 

You might need to do some escaping or utf-8 stuff for the special characters to work. I don't think there is any character you cannot use in Python. Some might just a bit of effort.



#3 JohnThomas

JohnThomas

    Newbie

  • Members
  • Pip
  • 8 posts

  • Calculators:
    fx-9750GII

Posted 21 October 2023 - 02:41 PM

(CLRSCR is the name of my program)

Edit: The unrecognized and null characters don't show on the website

3PROGRAMsystemCLRSCR

There is also 4 trailing null characters at the end of the file


Without it I get a Data Conversion Failed error and it deletes the file.

I can't seem to replicate it in a format it accepts. I might try to use a plain text editor to see if I can edit an already existing CASIO-BASIC file while maintaining the weird non-ascii stuff it's doing.


Edited by JohnThomas, 21 October 2023 - 02:36 PM.


#4 JohnThomas

JohnThomas

    Newbie

  • Members
  • Pip
  • 8 posts

  • Calculators:
    fx-9750GII

Posted 21 October 2023 - 02:50 PM

Hm yeah it seems like they all at least convert the null characters into something that the calculator is not a big fan of. Maybe I could try a hex editor.



#5 JohnThomas

JohnThomas

    Newbie

  • Members
  • Pip
  • 8 posts

  • Calculators:
    fx-9750GII

Posted 21 October 2023 - 02:55 PM

Here is what a simple ClearScreen program looks like in VSCODE (The double unknown character symbols represent the "Text" program):

https://imgur.com/a/aOBUISa



#6 JohnThomas

JohnThomas

    Newbie

  • Members
  • Pip
  • 8 posts

  • Calculators:
    fx-9750GII

Posted 21 October 2023 - 07:59 PM

I have solved the problem! If you want to create a PROGRAM file on your own computer, you need to save it as a TXT document and write it in plain text with no special characters. If you try to use the g1m format by trying to copy already existing files, it will not work.

 

If you have the plain text equivalent for a function in a .TXT document, it will interpret that as the function's special character when it makes a .g1m equivalent.

 

This is vaguely hinted towards by the fx-9860GIII Manual section 13-8, and probably on other manuals as well.

 

For example, in my test file, "Locate 1,1,"Hi"" works if saved as a TXT document, but not as a g1m file.


Just leaving this here in case someone as stupid as me encounters the same fairly intuitive to solve issue that probably could have been solved if I just read the manual harder and followed the simple train of thought that should come after learning that you can import a TXT file into the PROGRAMS folder.



#7 HappyCalc

HappyCalc

    Casio Addict

  • Members
  • PipPipPip
  • 67 posts
  • Gender:Male
  • Location:Germany
  • Interests:Programming, On-Calc Programming(C.Basic), Gaming, Origami, Eating Chocolate

  • Calculators:
    fx-9860GII-2
    fx-CG50

Posted 21 November 2023 - 08:49 PM

iirc casio uses the same format (g1m/g1a/g3m/...) for most use cases. A good way to start is the fxReverse documentation (Section 3.3)

https://bible.planet...fxReverse2x.pdf

Also heres the file subheader for program files:

https://bible.planet...MFileFormat.htm

A while back I wrote a program that can import/export image files to png. Might find this somewhere.

And most commands (If, Goto, etc.) aren't stored as text, but as multi-byte structures instead.


Edited by HappyCalc, 21 November 2023 - 08:54 PM.



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users