I’m writing an application using C and ncurses library. My program is intensively exchanging data with some sensors (sends and receives some data). There are many sensors (up to 500). Now I want to display information about each of the sensor in the ‘user-friendly’ way: it has to be kinda of scrollable list with items, each of which the user can choose. My best idea now is to use ncurses ‘Menus library’. The steps are following:
- Create list of items (actually some array of strings, where each string contains sensor number, type and other information.
- Create menu and add these items to menu
- Display menu
It might sound ok, but the problem is that information (actually state) of each sensor is dynamically updated. Ok, I can implement some ‘hot-button’ for updating information, but it might be very very time consuming: rebuilding and redisplaying all items is not a good idea.
So how would you do this?
You basically have a view of a data source and the data source is being updated and modified and you need to signal to the viewer that a particular data item has been modified so that the viewer can update its view.
Typically what I do in a situation like this is to have an event pathway so that when the data source that is shared between the viewer, the consumer of the data, and the producer, your sensor threads, the shared data is updated and then an event message is sent to the viewer.
The event messages are usually sent using some kind of a FIFO queuing mechanism so that the viewer will receive the events in the order in which they are sent by the producer.
With an event driven windowing system such as Windows, there is usually a
PostMessage()function that allows you to post a message to a particular window or to a particular thread.So the basic window design is to accept the messages which have a message identifier indicating that the source data has been updated along with some identifier or index to indicate which of the items was updated. The Viewer would then access the storage area to pull that particular data item from the area and then use that data to update the specific item text in the view.
This is pretty much standard MVC pattern. The nasty bit is how you communicate the source data update event to the viewer.