Shop OBEX P1 Docs P2 Docs Learn Events
Can I Use a .h File in Spin2 Program Compiled by FlexProp? — Parallax Forums

Can I Use a .h File in Spin2 Program Compiled by FlexProp?

I have a .h file with image data in it that I want to use in native Spin2. While I can manually convert the C-style hex data to Spin-style hex data, it gets a little tedious. Is it possible to include a .h file in a Spin2 program that is compiled by FlexProp? If yes, I can write a translator program that prints out a native Spin2 data listing form the .h and it will work in Propeller Tool and FlexProp.

Comments

  • @JonnyMac said:
    I have a .h file with image data in it that I want to use in native Spin2. While I can manually convert the C-style hex data to Spin-style hex data, it gets a little tedious. Is it possible to include a .h file in a Spin2 program that is compiled by FlexProp? If yes, I can write a translator program that prints out a native Spin2 data listing form the .h and it will work in Propeller Tool and FlexProp.

    Unfortunately not in a useful way. Just #include would not work because the syntax inside the file is C instead of Spin2. You could include it with an OBJ declaration, but actually getting at the data is problematic -- you can only reference functions (methods) in the object.

    My suggestion would be to write a little C wrapper like:

    /*
     * print out the hex values in header file foo.h
     * we assume there's a single array "stuff"; if it's
     * named something different change the #define below
     */
    
    #define ARRAYNAME stuff
    
    #include <stdio.h>
    #include "foo.h"
    
    int main() {
        // find size of each element of the array in bytes
        int elemsiz = sizeof(ARRAYNAME[0]);
        // find the number of elements in the array
        int arraysiz = sizeof(ARRAYNAME) / elemsiz;
    
        // now print the elements
        for (int i = 0; i < arraysiz; i++) {
            // print each element
            switch (elemsiz) {
            case 1:
                printf("BYTE $%02x\n", ARRAYNAME[i] & 0xff); break;
            case 2:
                printf("WORD $%02x\n", ARRAYNAME[i] & 0xffff); break;
            case 4:
                printf("LONG $%02x\n", ARRAYNAME[i]); break;
            default:
                printf("ERROR: unsupported size %d\n", elemsiz); return 1;
            }
        }
        return 0;
    }
    #endif
    

    This may either be compiled to run on your PC with your C compiler of choice, or else compiled & run on the P2 with FlexProp.

  • Thanks, Eric. This is a partial version of one of the small files

    fontdatatype TinyFont[764] PROGMEM={
    0x08,0x08,0x20,0x5F,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // <Space>
    0x18,0x3C,0x3C,0x18,0x18,0x00,0x18,0x00, // !
    0x66,0x66,0x24,0x00,0x00,0x00,0x00,0x00, // "
    0x6C,0x6C,0xFE,0x6C,0xFE,0x6C,0x6C,0x00, // #
    0x18,0x3E,0x60,0x3C,0x06,0x7C,0x18,0x00, // $
    <snip>
    };
    

    PROGMEM is an Arduino thing, so it will need to be removed (I believe).

    The first four bytes define the font width (pixels), height (pixels), starting character, number of characters in set. I want to make a utility that will format it like this:

    dat
    
      TinyFont              byte    $08,$08,$20,$7E
                            byte    $00,$00,$00,$00,$00,$00,$00,$00    ' <Space>      
                            byte    $18,$3C,$3C,$18,$18,$00,$18,$00    ' !            
                            byte    $66,$66,$24,$00,$00,$00,$00,$00    ' "            
                            byte    $6C,$6C,$FE,$6C,$FE,$6C,$6C,$00    ' #            
                            byte    $18,$3E,$60,$3C,$06,$7C,$18,$00    ' $   
    

    Other that formatting, I want to change the last byte of the first line to the last legal character -- no need to do that math in the character drawing routine.

  • Ah... well, if you want to keep most of the formatting and comments, I guess you'll pretty much have to work with the original text, rather than trying to compile the data into bytes. I can see a few options:

    (1) Doing it in an editor via search & replace or macros. Depending on your editor this might be the easiest option.
    (2) Doing it programatically on a PC. I'd probably choose a text processing language like awk or perl for that. I'm not sure what languages you're comfortable with?
    (3) You had mentioned writing in Spin2... if so, you could use something like FILE "fontdata.h" to include the original text of the .h file, and then walk through the ASCII bytes in memory, outputtiing suitably modified versions of the text to serial, which then gets captured on the PC.

  • JonnyMacJonnyMac Posts: 9,104
    edited 2022-06-30 23:08

    if so, you could use something like FILE "fontdata.h"

    That's for binary files -- these are plain text.

    Doing it in an editor via search & replace

    Yep -- this is the approach I've done so far and will continue. Some of the line are really long, and I want to turn them into multiple lines. It gets tedious for large characters

  • ersmithersmith Posts: 6,053
    edited 2022-06-30 23:48

    @JonnyMac said:

    if so, you could use something like FILE "fontdata.h"

    That's for binary files -- these are plain text.

    FILE "fontdata.h" will include "fontdata.h" as a binary file, so it will have all of the ASCII characters of the file in it. That is, if the file starts

    fontdata ...
    

    the binary wil have the bytes "f", "o", "n", "t", and so on. Which means you have to parse it to get the binary data; but it sounds like you actually want the text data, if you want to output comments and so on.

  • I see what you mean.

Sign In or Register to comment.