embedded software structure for sublevel screens and menus - c

I'm new into embedded c programming and I need some advice.
I'm trying to create a menu structure with underneath screens on an embedded system, the OS code and the drawing/menu libraries are ready, but I can't figure out how I'd design this system.
it'll have :
menus and submenus
password screens that user can authenticate before entering some menu items
and the screens beneath them.
I wrote some code that's working and still feels like I'm writing procedural spagetti code. And I'm new to embedded c systems so I don't know if I can use classes or objects inside the code. I'd be grateful if someone shows me the right way to do this. I'm experienced about programming anything else BTW so feel free with the tech talk. ;)
BTW the menu code is structured like this:
menu myMenu;
entry* myMenuEntries;
int selection = 0;
myMenuEntries[0] = [entry definer code];
...
myMenu.entries = (entry *) myMenuEntries;
selection = DisplayMenu(myMenu);
switch(selection){
case 0: exit(); break;
}
How can I create an object in embedded c like it's used in myMenu.entries?

I would definitely use object oriented C here, like you suspect is possible. There are lots of references to how to do oop in C on stack overflow. You make classes out of structs and use function pointers as the member functions which can then be overridden if you want polymorphism.
Each screen could have on onEnter(), for example, to initialize stuff when the screen is loaded, and you could have a drawable class that can encompass strings, primitives, images, etc that know how to draw themselves at any x,y coordinate. You could also have a standard screen object which is made up of a list of the objects on the screen, their coordinates, and contents. These could be initialized at compile time, too, in a big table.
(I've done all this on a DSP using C and ASM)
There are also ready made libraries that can do this. I know that micrium has one. I'm on my phone or I'd Google it and put the link here.

Related

Creating an array of buttons

I am more or less a complete beginner, so please excuse me if that Question does come across as too easy, but I try to create in QT an Array of Bottons. It should look similarly to the Minesweeper Game. Is there another way than to create every single butto or even have, when the program is running, the user choose how big the Array is, for example an 8x8 field.
My Goal is in the end that the user can make somewhat like a map where he can Color each button in the Array differently, but it would be really helpful if someone could tell me just how to create the Array. If possible in Detail, because I am not that used to work with QT or C++.
You can use nested QVector instead of arrays to save your buttons, and here is a peace of code which creates 8*8 buttons view on Qt MainWindow and saves the buttons in QVector, it may help
QGridLayout* layout = new QGridLayout();
ui->centralwidget->setLayout(layout);
QVector<QVector<QPushButton*>> buttons2DVector(8);
for (int i=0;i<8;i++){
buttons2DVector[i].resize(8);
for(int j=0;j<8;j++){
QPushButton *b = new QPushButton("button");
layout->addWidget(b,i,j);
buttons2DVector[i][j] = b;
}
}
Then you can easily access any of your buttons using
buttons2DVector[rowNum][columnNum]->func();

How do I get access to GUI elements in a IUP dialog loaded from a LED file?

I’m in love with IUP! However I cannot figure out how to get programmatic access (in C) to GUI elements in a dialog loaded by IupLoad() from a LED file.
One extremely laborious way would be to edit the LED file so as to manually give handle names to each single GUI element, then manually define corresponding variables for each element in C, then manually load handles into each variable by using IupGetHandle().
One comfortable way to do it would be to convert the LED file to a C header file using the built-in Layout Dialog tool. The resulting code makes each element available to the application in a simple array called Ihandle* containers[]. But this way deprives us of the benefits of LED files, such as the ability to edit GUI of a binary application by the user and keeping the C code small.
Is there no good way to do it?
Do I overrate the benefits of a third way, if it existed?
I cannot find any IupLoad() example in the directory with C examples.
My own example below explicitly defines one handle name for the top element (dialog) only. It features a very simple dialog where defining each element manually wouldn’t be a hard work at all. But this is only a test example for Stack Overflow and my question is relevant to complex dialogs.
C file:
#include <stdlib.h>
#include <iup.h>
int main(int argc, char **argv)
{
IupSetGlobal("UTF8MODE", "YES");
// IupSetGlobal("UTF8MODE_FILE", "YES");
IupOpen(&argc, &argv);
if(IupLoad("dropdown.led")) IupMessage("Error", "Failed to load LED.");
else {
Ihandle *dropdown = IupGetHandle("dropdown");
IupShow(dropdown);
IupMainLoop();
}
IupClose();
return EXIT_SUCCESS;
}
Corresponding dropdown.led file:
dropdown = DIALOG[TITLE=dropdown.led](
HBOX[CMARGIN=10x10,CGAP=10](
LIST[VALUE=3, 1=я, 2=ты, 3=оно, 4=мы, 5=вы, 6=они, DROPDOWN=YES](do_nothing),
LIST[VALUE=3, 1=ik, 2=je, 3=hij, 4=we, DROPDOWN=YES](do_nothing)
)
)
Which brings us to another question: how can I make Russian characters visible? But this issue is owed a separate thread which I will accordingly create.
All questions that pertain to this particular example:
How do I get access to GUI elements in a IUP dialog loaded from a LED file? (current)
How can I make Russian letters visible in a IUP dialog loaded from a LED file?
A gap in IUP dropdown lists
The way os to use IupGetHandle to get access to some element then use IupGetChild*, GetBrother, GetParent functions to get the element you want.
Another option is to use the NAME attribute. You set it on the element you want then use IupGetDialogChild to retrieve the element given the NAME value.

Text language based decision making

I am currently developing with MPLAB X (IDE) on PIC32 platforms using C programming language.
I have a MAJOR project coming up which includes displaying graphics and text on a TFT screen.
We develop our systems for multiple languages ,including languages written "right to left" & "left to right".
So some symbols also change their position, and texts change their alignments on the screen.
So far Iv'e seen my former developer implement the code in a way that every screen displayed, or every object presented on the screen is tested for the language selected and then takes actions accordingly.
e.g:
/**Some actions shared by all the languages**/
.
.
/* language specific actions: */
if(language == lang1){
.
.
/* Performs some actions */
/* e.g: */
displayText(txt, LEFT_ALIGNED);
.
.
} else if(language == lang2){
.
.
/**SAME** actions with only minor changes depending on the language selected. */
/* e.g: */
displayText(txt, RIGHT_ALIGNED);
.
.
} else if.... (and so on)
As you can see, it seems quite an overhead, and the code this way has a lot of superfluous code duplication, especially when this structure is repeated inside almost every function/source file.
I am trying to think of an approach for developing more of a "polymorphic" structure that would not require so many "if else" statements.
I tried to search the internet for some examples implementing text based decision making, but was unable to find what I need, mostly due to the fact that I find it hard to describe my question.
I hope this is not somewhat a vague question,
but is there any structural consensus for implementing this kind of situations?
Many Thanks,
Tsoof A.
I would go for an array of language/action structures, then search the array for the language in question, and execute the associated action:
typedef struct {
char *lang;
void (*action)();
} action_t;
action_t actions[] = {{"en", func1}, {"de", func2}, ...};
...
for(int i = 0; i < sizeof(actions)/sizeof(actions[0]); i++)
if(!strcmp(actions[i].lang, language)) {
actions[i].action();
break;
}

Ansi C dynamic include

I was assigned to edit part of Ansi C application but my knowledge of pure C is just basics. Anyway current situation is I have map1_data1.h, map1_data2.h, map2_data1.h, map2_data2.h and variables in those files are always connected to the map name = map1_structure in map1_data1.h and so on.
In app there is #include for each file and in code then something like
if (game->map == 1){
mapStructure = map1_structure
} else {
mapStructure = map2_structure
}
I have to extend this to be able to load the map dynamicly so something like
void loadMap(int mapId){
mapStructure = map*mapId*_structure // just short for what i want to achieve
}
My first idea to do so was removing map name connection in variables name in map1_data.h and have just structure variable in there. That requires only one header file at time to be loaded and thats where I'm stucked. Havent found any clues to do so on google.
I would like to have it as variable as possible so something like #include "map*mapId*_data1.h" but should be ok to have 1 switch in one place in whole app to decide on what map to be loaded.
One more thing, the app keeps running for more than 1 game = it will load various maps in one run.
Judging from the comments, you have a single type, call it Map, which is a structure type containing a collection of different data types, including 3D arrays and points and so on. You need to have some maps built into the program; later on, you will need to load new maps at runtime.
You have two main options for the runtime loading the maps:
Map in shared object (shared library, dynamically loaded library, aka DLL).
Map in data file.
Of these two, you will choose the data file over the shared object because it is, ultimately, simpler and more flexible.
Shared Object
With option 1, only someone who can compile a shared library can create the new maps. You'd have a 'library' consisting of one or more data objects, which can be looked up by name. On most Unix-like systems, you'd end up using dlopen() to load the library, and then dlsym() to find the symbol name in that library (specifying the name via a string). If it is present in the library, dlsym() will return you a pointer.
In outline:
typedef void *SO_Handle;
const char *path_to_library = "/usr/local/lib/your_game/libmap32.so";
const char *symbol_name = "map32_structure";
SO_Handle lib = dlopen(path_to_library, RTLD_NOW);
if (lib == 0)
...bail out...
map_structure = dlsym(lib, symbol_name);
if (map_structure == 0)
...bail out...
You have to have some way of generating the library name based on where the software is installed and where extensions are downloaded. You also have to have some way of knowing the name of the symbol to look for. The simplest system is to use a single fixed name (map_structure), but you are not constrained to do that.
After this, you have your general map_structure read for use. You can invent endless variations on the theme.
Data file
This is the more likely way you'll do it. You arrange to serialize the map structure into a disk file that can be read by your program. This will contain a convenient representation of the data. You should consider the TLV (type-length-value) encoding scheme, so that you can tell by looking at the type what sort of data follows, and the length tells you how many of them, and the value is the data. You can do this with binary data or with text data. It is easier to debug text data because you can look at and see what's going on. The chances are that the difference in performance between binary and text is small enough (swamped by the I/O time) that using text is the correct way to go.
With a text description of the map, you'd have information to identify the file as being a map file for your game (perhaps with a map format version number). Then you'd have sections describing each of the main elements in the Map structure. You'd allocate the Map (malloc() et al), and then load the data from the file into the structure.

Using Windows Forms and VC++ with Unmanaged Static Libraries

I am currently trying to write a UI for a Data Acquistion System in Visual Studio C++ 2010, and I am having a lot of trouble dealing with the interfacing of the third party libraries I am using and Windows Forms. The two libraries I am using are DAQX, a C library for a Data Acqustion System, and VITCam, a C++ library for a 1394 High Speed Camera. It's extremely frustrating trying to work with these libraries and any UI library that VS has to offer, as none of the function arguments ever get along.
DAQX uses windows types like WORD and DWORD, in normal C fashion, and when I'm writing a normal program, no UI involved, it works fine, but Windows Forms seems to hate anytime I want to make a simple DWORD Array inside the class.
VITCam is even worse. I can open the camera fine, but I am completely lost when it comes to trying to put the image on the screen somehow. I haven't uncovered an equivalanet, easy to follow way for putting it to the screen as to how the documentation puts it:
CDC* pDC=GetDC(); // obtain the device context for your window...
// move the image data
::SetDIBitsToDevice(pDC->m_hDC,0,0,
(int) (MyCam.GetDispBuf()->bmiHeader.biWidth),
(int) (MyCam.GetDispBuf()->bmiHeader.biHeight),
0,0,0,(WORD) (WORD) MyCam.GetDispBuf()->bmiHeader.biHeight,
MyCam.GetDispPixels(),MyCam.GetDispBuf(),
DIB_RGB_COLORS);
I can barely follow it as is. So, without doing to much blathering, How do most people work with static unmanaged libraries that were not developed with Windows Forms in mind? I've tried MFC as the VITCam documentations mentioned it, but it makes very little sense and isn't as intuitive as Windows Forms feels.
Edit:
This is the error message I get when trying to use a normal (at least to me) array.
Error 1 error C4368: cannot define 'buffer' as a member of managed 'WirelessHeadImpact::Form1': mixed types are not supported
And it points to this line:
private:
WORD buffer[BUFFSIZE*CHANCOUNT];
What I had before was this:
static array<WORD>^ _buffer;
And within a function I create the former array, pass it to the function, then return the latter after looping through and updating the array.
WORD buffer[BUFFSIZE*CHANCOUNT];
DWORD scansCollected = 0;
while (total_scans < SCANS) {
daqAdcTransferBufData(_handle, buffer, BUFFSIZE, DabtmWait, &scansCollected);
if (scansCollected > 0) {
for (WORD i=0;i<scansCollected;i++) {
_buffer[i] = buffer[i];
}
mixed type support is removed in Visual C++ 2005. If you want to associate a DWORD array to a managed class, use new (not gcnew) to allocate the array itself on the native heap and save the pointer of the array in the class.
by the way, you cannot pass addresses of objects on the managed heap to a native function without pinning the object, otherwise the GC is free to move the object at any time. If you want to pass a managed value to a native function, make sure your pass by value or the object is pinned.
It helps the readers if you post the actual error message you are getting, instead of having to guess out from your question.

Resources