|
I am sorry to reopen this, but I just need confirmation that I am on the right track in reorganizing the code.
I only hope I can describe my plan.
Basically I have 3 USB classes. I hesitate to call them “objects “ for now.
To bypass Arduino “help” ( trampling on my code ) I have this flow.
It may be overkill but it is working so far.
C_Main
Cprocess
USB (device) - “interface “ class member of Cprocess (my code)
(device)Controller – controls the USB device – mouse is working (mostly cut and paste)
USBHost - a native access to Arduino Due USB port for now used / tested as a state machine to retrieve USB configuration from attached USB device ( works so so )(also mostly cut and paste from ATmel)
I would like to have all of these USB(device), (device) Controller, and USBHost in some kind of hierarchy. As you pointed out to me , the “problem” is that USBHost class itself is “global” and some of the class parameters – for example state of the “state machine” are also global.
I can fix that.
Eventually I need to control all of the devices on the USB (bus) and would like your opinion if the above scheme is OK as a base.
Many thanks for your time , appreciate it very much
Vaclav
|
|
|
|
|
Some basics:
Is-a = inheritance. If you inherit type B from A, then you can use type B as type A. Also see Liskov Substitution Principle (LSP).
Example:
class Shape { virtual void Draw() = 0; };
class Circle : public Shape { void Draw(); };
A Circle is-a Shape, so type Circle inherits type Shape. Note that this is all about behavior (interface), so you can treat a pointer or reference to any subclass of Shape as if it was a Shape with respect to Shape's interface (in this case a single Draw() function).
Has-a = aggregating.
Example:
class Canvas {
public: void Draw();
private: set<Shape*> m_shapes;
};
A Canvas can hold one or more Shape's, of any concrete subtype, and most likely Canvas::Draw will iterate over the shapes it holds, calling their Draw method. For clarity, you may want to think of Canvas::Draw as Canvas::DrawShapes.
For the particular case where you are to have one, and only one, object of a specific type, you may want to have a look at making it a Singleton.
I hope this attempt at a simple explanation of the concepts can help you come up with a design. While drawing has nothing at all to do with USB, the concepts of inheritance and aggregation are the same. For project after project.
(NOTE: code-formatting was done for compactness, please do not copy as-is )
|
|
|
|
|
Mike,
thanks for reply.
I just found another copy of the USBHost class and in the few comments attached to it they use term singleton. So your comment arrived just in time.
I really need to dig into this because the sample code I have been playing with does not work when more than one USB devices are on the USB bus.
I think I got the "static class" concept but still struggling with how each inherited class uses global state variables - since each USB device maintains his own state machine.
Thanks
Vaclav
|
|
|
|
|
To me, just based on this comment, it seems you'd have:
A "base", the driver. The singleton. This owns one or more
USBBus objects, each handling a physical connection. These in turn owns zero or more
USBDevice's, each representing a device on that bus.
Each USBDevice would in turn probably have one or more "functions" (e.g. a USB mouse with buttons, wheel, and some more clickable buttons could easily present 3 or more functions/sub-devices).
It also seems logical that each device (and function and so on) might need (hold) a reference to its owner, to f.ex. be able to notify it about events. Not to mention the obvious - verify correctness in such a highly dynamic and unpredictable system
I see no obvious reason any of the USB types should ever touch the global/static parts. But should the need arise; don't give them access to the data (if you ever used MFC, consider that the anti-thesis of good design). Instead provide them an interface - and not only an interface that returns a ref or ptr to the global/static var's, but something that actually do some work.
Which brings me to another good rule-of-thumb (actually, this one is more like a law of nature):
- Interfaces shall be minimal and complete.
Additionally:
- Member functions should do two things; Use the object's state (directly or indirectly), and not leak the objects state to the outside. If a function is not using the object's state, it has no business being a member function (not counting corner cases). If it's not hiding the internal state, it's "leaking" it to other types to use or abuse, and making it hard-to-impossible to change the implementation later on.
That last point, not leaking internal state to the outside, is worth hammering in over and over until you say "Obviously, anything else would be insanity!".
Later, if needed, "on the side" you might even find it useful to have a "registry" of devices, no matter what bus they are on, to save lookup time or whatever. F.ex. the system input handler might poll you about input events, and then you could have one list of devices dedicated to HID input. Another case could be the user requests "Put all storage devices to sleep" and instead of walking the busses and their devices (which in turn may have an expander (hub-ish) with another bus to dive into) you have a single list to iterate. You get the idea.
++luck;
|
|
|
|
|
I am receiving Binary data of an image in 4K chunks through USB. I need to get this data and display the complete image(as GIF) using VC++. Please show me the way(examples preferred).
|
|
|
|
|
What are you receiving exactly?
|
|
|
|
|
|
|
Thank you. Its proving difficult as I am not used to working with hardware in C++.
|
|
|
|
|
What hardware? Your question is about reading an image file.
|
|
|
|
|
Yes the image data is passed from a scanner.. I am consolidating the data received in 4k chunks and displaying the image in a window.
|
|
|
|
|
And what is your problem?
|
|
|
|
|
Nothing right now. I wanted an overview as to how people would approach the problem.
|
|
|
|
|
Approach what problem? It is simply a matter of receiving each chunk of data and appending it to the previous chunk until you have the complete image.
|
|
|
|
|
True. But when I posted the question I had no idea about how the image was rendered onto the window.
|
|
|
|
|
But that has nothing to do with the issue of transferring a file across a network. And in my first reply I gave you a link to an MSDN page which explains how to display images.
|
|
|
|
|
That I figured out. You answered my question.
|
|
|
|
|
GIF is a format for image storage, it is not a way to display an image.
|
|
|
|
|
Visual Studio C++
I need a function that would be declared like this:
class Messages
{ ...
WCHAR get_string();
... }
and be called something like this
WCHAR new_string[ MAX ];
new_string = p_messages->get_string()
I have tried that syntax but all readers probably know the errors so they are omitted.
I am thinking it must be something more like
class Messages
{ ...
bool get_string( WCHAR * target, int target_size );
... }
and the function will have to check sizes and copy the source string into the target and set a return status value. Can the former be done? If so, how?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 16-Jan-15 12:14pm.
|
|
|
|
|
Yes, a function may return a WCHAR (and your function, misnamed as it is, does it). Howevever, in your code you are trying to assign it to an array name (and you can't do that).
The
bool get_string( WCHAR * target, int target_size );
function can properly modify the passed array (for instance, Windows API use such a mechanism).
Please note, in C++ you could use std::wstring[^] instead of an array of WCHAR s.
|
|
|
|
|
Point taken. What I really need is a function that returns a string, an array of WCHAR. I am pretty sure that can be done with CString, but I have had problems using CString.
The question more properly is: Can a function return a string of WCHAR?
You mentioned std::wstring rather than WCHAR. During startup this app reads a file of some 80,000 data items to describe telemetry parameters it must process. That file is text based. Each line of the text file is tokenized, put in a vector, then the fields can be easily processed to extract the needed info.
Speed is not real critical but so far things seem to work better with WCHAR. Do you have a preference for std::wstring over WCHAR? If so can you explain the reason or direct me to a good comparison of the two?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
Simply put, a function CAN return a string of WCHAR, but you have to return a pointer to the string. Therefore:
WCHAR* FunctionThatReturnsString()
{
WCHAR* NewString = new WCHAR[255];
return NewString;
}
The error you were making before was that you were returning a single WCHAR character, not a pointer.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Quote: but I have had problems using CString What problems?
bkelly13 wrote: The question more properly is: Can a function return a string of WCHAR? Yes, you can do that (and Richard Andrew x64 already shown you how) however it is error prone, because the allocation is performed by the called function while memory release is left to the caller (simply put: you should know what you are doing). std::wstring (or CString ), on the other hand handles the memory allocation/release mechanism internally.
Quote: ou mentioned std::wstring rather than WCHAR. During startup this app reads a file of some 80,000 data items to describe telemetry parameters it must process. That file is text based. Each line of the text file is tokenized, put in a vector, then the fields can be easily processed to extract the needed info.
Speed is not real critical but so far things seem to work better with WCHAR. Do you have a preference for std::wstring over WCHAR? If so can you explain the reason or direct me to a good comparison of the two? If speed is not critical I would definitely use std::wstring (or CString ) insted of WCHAR arrays (avoiding the hassle of memory handling with the benefit of a more compact and possibly robust code).
|
|
|
|
|
Regarding what problems with CString? I don't remember the details right now. I think my problem in that area is settling down with one representation for strings. I fetch some critical data using the API of another application (I'll update this when I get back to work and verify), then the code reads text from a file, tokenizes it, builds parameters names with odd formatting and does a lot of compares. I would like to make this app easy to port to a Unix/Linux platform but am not obsessed with that. Selecting a single character and string representation is not trivial.
I am receptive to suggestions as to my selection of the basic character/string representation. I am leaning towards WCHAR but can be convinced otherwise. The need for a simple tokenizer is strong.
Regarding the function returning a string, avoiding the problems of allocation is rather important. It seems that I cannot so something like:
WCHAR target[ MAX ];
...
target[ 4 ] = get_paremeter_class( );
Where get_string() will return a short phrase that is frequently used. The resultant string would look like: S01_ANA_0001, meaning "String 01, Analog, parameter 0001" or S01_DIG_0001.
I am thinking of writing a procedure that will accept a pointer to target along with a size and copy that short string into target. I want the higher level to look simple and let the frequently called function handle the ugly details.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
Quote: WCHAR target[ MAX ];
...
target[ 4 ] = get_paremeter_class( ); Again, what is target[4] supposed to hold? It can just hold a character, not a string.
|
|
|
|