Shop OBEX P1 Docs P2 Docs Learn Events
Strategy of converting C multi-dimensional constants to Spin 2 DAT structures. — Parallax Forums

Strategy of converting C multi-dimensional constants to Spin 2 DAT structures.

ke4pjwke4pjw Posts: 1,096
edited 2023-05-09 03:01 in PASM2/Spin2 (P2)

I am attempting to convert a c program for Arduino to a Spin 2 program.

Is this a good strategy to represent a multi-dimensional array of byte constants in memory for Spin 2?

static const uint8_t NUM_ERROR_CORRECTION_BLOCKS[4][40] = {
    // Version: (note that index 0 is for padding, and is set to an illegal value)
    // 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40    Error correction level
    {  1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49},  // Medium
    {  1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25},  // Low
    {  1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81},  // High
    {  1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68},  // Quartile
};


static const int8_t NUM_ERROR_CORRECTION_BLOCKS[4] = {
    1, 1, 2, 2
};

Converts to:

NUM_ERROR_CORRECTION_BLOCKS
'   // Version: (note that index 0 is for padding, and is set to an illegal value)
byte   0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40    Error correction level
byte   1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49  ' Medium
byte   1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25  ' Low
byte   2, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81  ' High
byte   2, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68  ' Quartile

And accessed as byte[@NUM_ERROR_CORRECTION_BLOCKS + (rows*40) + cols]

Does this make sense? These arrays have me a bit noodled.

(Edited to ensure that [0][0] existed)
(Edited again because I left some data out)

Comments

  • The C code there is illegal, the two arrays have the same name.

    But in general, yes, that's about the way to do multi-dim arrays.

  • ke4pjwke4pjw Posts: 1,096
    edited 2023-05-09 03:02

    I thought that too, but somehow it works? I hate the Arduino coding style. Takes too long to peel back all the layers. I assumed it would set byte 0 of each row to the values listed.

    I am attempting to convert this code:

    https://github.com/ricmoo/QRCode

    https://wokwi.com/projects/318641692720759379

  • Wuerfel_21Wuerfel_21 Posts: 4,639
    edited 2023-05-09 08:29

    No, you copypasted the code wrong (???), the bigger array has a different name.

    EDIT: no wait there's a preprocessor thing going on, but still, copypasted wrong

  • maccamacca Posts: 745
    edited 2023-05-09 08:56

    @ke4pjw said:
    I thought that too, but somehow it works? I hate the Arduino coding style. Takes too long to peel back all the layers. I assumed it would set byte 0 of each row to the values listed.

    Arduino code is just C++ with some "shortcuts" and simplifications, like no need to declare functions in advance (sources are pre-processed to add declarations).
    And just like C and C++, array elements are numbered from 0 (don't understand your assumption, Arduino doesn't insert hidden data...).

    I am attempting to convert this code:
    https://github.com/ricmoo/QRCode

    The code use preprocessor directives to use one or the other array definition (LOCK_VERSION value, whatever it means), then seems you have used a commented row as data (guess is a copy / paste error).

    Converts to:

    NUM_ERROR_CORRECTION_BLOCKS
    '   // Version: (note that index 0 is for padding, and is set to an illegal value)
    byte   0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40    Error correction level
    byte   1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49  ' Medium
    byte   1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25  ' Low
    byte   2, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81  ' High
    byte   2, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68  ' Quartile
    

    Nope, more likely should be converted to this:

    NUM_ERROR_CORRECTION_BLOCKS
    '   // Version: (note that index 0 is for padding, and is set to an illegal value)
    '   // 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40    Error correction level
    byte   1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5,  5,  8,  9,  9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49  ' Medium
    byte   1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4,  4,  4,  4,  4,  6,  6,  6,  6,  7,  8,  8,  9,  9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25  ' Low
    byte   2, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81  ' High
    byte   2, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8,  8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68  ' Quartile
    
  • OK, so I see the top row I pasted was just wrong.

    Now the 2nd declaration of NUM_ERROR_CORRECTION_BLOCKS in c code. What's happening there? Or s that cruft reassigning 1,1,2,2 to the first 4 columns of row 4?

    static const int8_t NUM_ERROR_CORRECTION_BLOCKS[4] = {
        1, 1, 2, 2
    };
    
  • @ke4pjw said:
    Now the 2nd declaration of NUM_ERROR_CORRECTION_BLOCKS in c code. What's happening there? Or s that cruft reassigning 1,1,2,2 to the first 4 columns of row 4?

    static const int8_t NUM_ERROR_CORRECTION_BLOCKS[4] = {
        1, 1, 2, 2
    };
    

    It is a completely different array. The first is a 4x40 array, the above is a 4 elements only. It doesn't "reassign" anything. The preprocessor directive selects which one is defined, I guess somewhere in the code it handles the difference based on how LOCK_VERSION is defined (I know nothing about the barcode decoder so I don't know what the differences are...). The online code viewer seems to do a good job in locating all ocurrences of LOCK_VERSION, nice.

  • Oh, got it. Ug. A whole meta configuration at build time. Yuck.

Sign In or Register to comment.