Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =LET(
- Range; B23:Z3540;
- Header; "#define REGISTER_LIST {";
- MinSeperation; 3;
- Note; "Read this forumla buttom-up, as a c-program.";
- IsolateColumn; LAMBDA(cell;col_letter; LET(
- Usage; "(A7, 'Z') returns Z7";
- rows; ROW(cell);
- new_range; col_letter & MIN(rows) & ":" & col_letter & MAX(rows);
- INDIRECT(new_range)
- ));
- IsolateColumns; LAMBDA(range;col_letters; LET(
- Usage; "(A7:D20, D:G) returns D7:G20";
- rows; ROW(range);
- cols; TEXTSPLIT(col_letters; ":");
- start_col; INDEX(cols; 1);
- end_col; INDEX(cols; 2);
- new_range; start_col & MIN(rows) & ":" & end_col & MAX(rows);
- INDIRECT(new_range)
- ));
- Indent1; REPT(" "; MinSeperation);
- checks2bin_Desc; "Range to a binary string. All x or X is 1, otherwise it's 0";
- checks2bin; LAMBDA(range; TEXTJOIN(""; TRUE; IF(ISTEXT(range); IF(UPPER(range)="X"; "1"; "0"); "0")));
- bin16_2hex_Desc; "16-bit string to 4 cifer hex string";
- bin16_2hex; LAMBDA(binStr; BIN2HEX(LEFT(binStr;8);2)&BIN2HEX(RIGHT(binStr;8);2));
- FixStringLength_Desc; "Truncates or pads a string to a fixed length.";
- FixStringLength; LAMBDA(str;length; LEFT(str & REPT(" "; length); length));
- RegAttributeValues; LAMBDA(row; HEX2DEC(bin16_2hex(checks2bin(INDEX(RegAttributes; row; 0)))));
- GetStringMaxLength; LAMBDA(range; MAX(LEN(range)));
- RegAddress; IsolateColumn(Range; "B");
- RegCategory; IsolateColumn(Range; "D");
- RegName; IsolateColumn(Range; "E");
- RegDescription; IsolateColumn(Range; "F");
- RegType; IsolateColumn(Range; "G");
- RegFormatType; IsolateColumns(Range; "H:H");
- RegValue; IsolateColumn(Range; "I");
- RegAttributes; IsolateColumns(Range; "J:Y");
- RegAddressMaxLength; MinSeperation + LEN("0x0000");
- RegCategoryMaxLength; MinSeperation + GetStringMaxLength(RegCategory) +2;
- RegNameMaxLength; MinSeperation + LEN("X_BITFIELD(") + GetStringMaxLength(RegName)*2;
- RegTypeMaxLength; MinSeperation + GetStringMaxLength(RegType);
- RegValueMaxLength; RegAddressMaxLength + RegTypeMaxLength + RegAddressMaxLength*2;
- RegAttributeLength; RegAddressMaxLength;
- FindLast; LAMBDA(row;search_string; LET(
- Usage; "Return the last value of a column scanning upwards";
- search_upper; UPPER(search_string);
- rows_to_check; SEQUENCE(row; 1; row; -1);
- matching_row; XLOOKUP(search_upper; UPPER(INDEX(RegFormatType; rows_to_check)); rows_to_check; "");
- IF(matching_row = ""; ""; INDEX(RegName; matching_row))
- ));
- ToCString; LAMBDA(string0; LET(
- string1; SUBSTITUTE(string0; "\"; "\\");
- string2; SUBSTITUTE(string1; CHAR(13)&CHAR(10); "\n");
- string3; SUBSTITUTE(string2; CHAR(13); "\n");
- string4; SUBSTITUTE(string3; CHAR(10); "\n");
- string5; SUBSTITUTE(string4; CHAR(9); "\t");
- string6; SUBSTITUTE(string5; CHAR(34); "\""");
- string6
- ));
- OutRegName; LAMBDA(row;prefix;
- FixStringLength(Indent & prefix & "(" & INDEX(RegName; row) & ","; RegNameMaxLength)
- );
- OutRegAddress; LAMBDA(row;
- FixStringLength("0x" & DEC2HEX(INDEX(RegAddress; row); 4) & ","; RegAddressMaxLength)
- );
- OutRegType; LAMBDA(row;
- FixStringLength(INDEX(RegType; row) & ","; RegTypeMaxLength)
- );
- OutCategory; LAMBDA(row;
- FixStringLength("""" & INDEX(RegCategory; row) & """" & ","; RegCategoryMaxLength)
- );
- OutRegValue; LAMBDA(row;
- FixStringLength("0x" & DEC2HEX(RegAttributeValues(row); 4) & ","; RegAttributeLength)
- );
- OutDescription; LAMBDA(row;
- """" & ToCString(INDEX(RegDescription; row)) & """"
- );
- OutDetailName; LAMBDA(row;prefix;
- FixStringLength(Indent & Indent & prefix & "(" & FindLast(row; IF(UPPER(prefix)="X_ITEM"; "List"; "Bitfield")) & "_" & INDEX(RegName; row) & ","; RegNameMaxLength)
- );
- OutDetailValue; LAMBDA(row;
- FixStringLength(INDEX(RegValue; row) & ","; RegValueMaxLength)
- );
- Finalize; LAMBDA(row;type; LET(
- next_format; IF(row < ROWS(RegFormatType); INDEX(RegFormatType; row+1); "");
- IF(UPPER(next_format) <> UPPER(type); ") )"; ")")
- ));
- Format_Data; LAMBDA(row;
- OutRegName(row; "X") &
- OutRegAddress(row) &
- OutRegType(row) &
- OutRegValue(row) &
- OutCategory(row) &
- OutDescription(row) & " )"
- );
- Format_List; LAMBDA(row;
- OutRegName(row; "X_LIST") &
- OutRegAddress(row) &
- OutRegType(row) &
- OutRegValue(row) &
- OutCategory(row) &
- OutDescription(row) & ","
- );
- Format_Item; LAMBDA(row;
- OutDetailName(row; "X_ITEM") &
- OutDetailValue(row) &
- OutDescription(row) & Finalize(row; "ITEM")
- );
- Format_Bitfield; LAMBDA(row;
- OutRegName(row; "X_BITFIELD") &
- OutRegAddress(row) &
- OutRegType(row) &
- OutRegValue(row) &
- OutCategory(row) &
- OutDescription(row) & ","
- );
- Format_Bit; LAMBDA(row;
- OutDetailName(row; "X_BIT") &
- OutDetailValue(row) &
- OutDescription(row) & Finalize(row; "BIT")
- );
- Main; LAMBDA(row; LET(
- Usage; "Process a single row depending on it's format.";
- format; INDEX(RegFormatType; row);
- SWITCH( UPPER(format);
- "DATA"; Format_Data(row);
- "LIST"; Format_List(row);
- "ITEM"; Format_Item(row);
- "BITFIELD"; Format_Bitfield(row);
- "BIT"; Format_Bit(row);
- ""; " ";
- "Error: Unknown Type Detail"
- )));
- TrimSpaces; LAMBDA(Range; LET(
- SpaceStrings; MAP(Range; LAMBDA(cell; INDEX(TEXTSPLIT(cell; ",";; TRUE); 2)));
- SpacesInStrings; MAP(SpaceStrings; LAMBDA(str; IFERROR(LEN(str) - LEN(SUBSTITUTE(str; " "; "")); "")));
- SpacesToRemove;MIN(SpacesInStrings) - MinSeperation;
- RemoveString;REPT(" ";SpacesToRemove);
- SUBSTITUTE(Range;RemoveString;"";1)
- ));
- MainOutput_Desc; "Iterates the rows in Range, converts to lines in a c header file, intented for an x-macro. Adds header and ensures all lines are continued at the same char. Then adds footer. "
- MainOutput; MAP(SEQUENCE(ROWS(Range)); Main);
- TrimmedOutput; TrimSpaces(MainOutput);
- AddLineContinuation; MAP(VSTACK(Header; TrimmedOutput); LAMBDA(line; FixStringLength(line; GetStringMaxLength(TrimmedOutput)) & " \"));
- VSTACK(AddLineContinuation; " }")
- )
Advertisement
Comments
-
- This is an early version of the header export formula. Needs a bit of cleanup.
- Exceeds Excel's max 8192 bytes formula size.
- It's recommended to edit the formula in Notepad++ or similar, which highlights selected words and add some syntax coloring.
Add Comment
Please, Sign In to add comment
Advertisement