Multi-line array initialization - Page 1
Forum section: electronics / projects

author: ricko_uk
author_rank: Super Contributor
timestamp: October 31, 2021, 05:32:45 pm
content: Hi,I have some very large two-dimensional arrays that represent a double buffer for two B&W images and they need initialising to specific values and each element value is different. And in the editor each "image line" is placed in new line like this:uint8_t image_1 = { { 0x21, 0x11, ....... 0x23, 0x44, /*Image 1 line 1*/0x11, 0x34, ....... 0x55, 0x77, /*Image 1 line 2*/...0x22, 0x23, ....... 0x34, 0x55 }, /*Image 1 line 300*/{ 0x21, 0x11, ....... 0x23, 0x44, /*Image 2 line 1*/0x11, 0x34, ....... 0x55, 0x77, /*Image 2 line 2*/...0x22, 0x23, ....... 0x34, 0x55 } } /*Image 2 line 300*/For readability I want to keep that formatting/layout of the initialisation values. But I don't want to insert the initialisation values in the declaration because I might need to change those values (i.e. the entire image). I want to keep all the init-values (i.e. the different images) in a separate file and just change which one to use to initialise the array just by changing the right hand side values.So ideally I would need something along these lines:uint8_t image_1 = { { IMAGE_1 }, { IMAGE_1 } };where IMAGE_1 is the list of values (macro or file) BUT, as mentioned above, they are formatted so they appear in multiple lines. Which means a #define would not work unless I append a "\" to each line.I tried replacing IMAGE_1 with an #include"image_1.img" but that does not compile.What is the best way to do it?Thank you

author: ataradov
author_rank: Super Contributor
timestamp: October 31, 2021, 05:42:01 pm
content: Code:uint8_t image_1 = { {#incliude "image_1.img"}, {#incliude "image_1.img"} };I'm not clear how the array switched from 2 dimensional to one dimensional here, but the idea is that #include must be on its own line. Technically "#" must be the first character on the line too, but compilers generally accept spaces before it. But you can't use preprocessor directives inside the line.

author: ricko_uk
author_rank: Super Contributor
timestamp: October 31, 2021, 05:43:39 pm
content: Thank you AlexI was putting the #include in the same line and obviously that's why it was not working.Thank you

author: Renate
author_rank: Super Contributor
timestamp: October 31, 2021, 05:47:18 pm
content: Me? I'd make a utility to take the source image (in PNG or JPEG) and convert them to object files to link with your program.

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 31, 2021, 06:11:57 pm
content: Yes, or generate a C source file automatically. And not sure what the deal is with the code formatting here either - the content for images is not "human-readable" anyway, so why format it with a specific layout? That's where a small tool for generating code is handy. You may have very specific needs in terms of data structure, but otherwise, I would use an exisiting tool.I mostly use "LCD Image converter" (for images) and 'The dot factory" (for fonts) for this. There are others but those have served me well.https://github.com/pavius/The-Dot-Factoryhttps://lcd-image-converter.riuson.com/en/about/

author: ricko_uk
author_rank: Super Contributor
timestamp: October 31, 2021, 06:56:05 pm
content: Thank you both!@SiliconWizard, you suggested I think you suggested lcd-image-converter previously and have been using that since, it works very well!Ref the code formatting, I need it during development/debugging so I can easily see/change single pixels visually when testing certain patterns and comparing the printed output. Easier to see line by line.Thank you

author: Jeroen3
author_rank: Super Contributor
timestamp: October 31, 2021, 07:02:48 pm
content: Code:#if IMAGE_OPTION_Auint8_t *selectedimage = image_1;#elseuint8_t *selectedimage = image_2;#endifBoth get compiled (checked for syntax and correctness). Removed during linking because unreferenced.

author: Renate
author_rank: Super Contributor
timestamp: October 31, 2021, 08:30:29 pm
content: Quote from: ricko_uk on October 31, 2021, 06:56:05 pmHaven't you ever heard of MS Paint?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: October 31, 2021, 08:53:48 pm
content: Inimages.c, declare the (single dimensional) arrays.Code:uint8_t image1 = { 0x21, 0x11, ....... 0x23, 0x44, /*Image 1 line 1*/...};uint8_t image2 = { 0x21, 0x11, ....... 0x23, 0x44, /*Image 2 line 1*/...};Or split them into individual files, or headers and include them, etc.In main.c (or wherever this is), use:Code:uint8_t* image_1[] = {image1, image2};I don't see why you're against macros (a '\' at the end of a line, isn't much more than the comma, CR(LF) and tabs that are already there?) but those can be used too. Technically C std only guarantees this up to 4096 characters of macro, but in practice, all mainstream compilers handle essentially up to system memory, shouldn't be an issue.This is just organization/readability, to put things in files other than main code, right?Tim