Show Posts
Pages: 1 ... 62 63 [64] 65 66 ... 149
946  Development / Other Software Development / Re: [Update] dbg.h : debug library on: June 24, 2013, 06:37:04 am
I like the way you constructed your class, are you intending it to run on both AVR and SAM Arduinos?

I saw your thread in the programming questions forum, It seems like you are very interested in templates and meta-programming, so I'm doing up an example for you to see the power of templates, I also have some nice links I can dig up. There are many things you can do to make this a nice tool for Arduino like removing polymorphic behavior and new.
947  Using Arduino / Programming Questions / Re: Using Explicit on: June 18, 2013, 08:58:06 pm
I agree with you. Using const in a class is perfectly acceptable. In fact it makes it clear that initializing it is a once-off.

Initialization is always a once off, regardless of const-ness.

It's a matter of being practical

Replacing informative comments with code that does nothing but hinder an objects lifetime is not good practice, no point even trying to justify that one.

I think you need to re-read some posts, you keep pushing an irrelevant point:
There is no reason to have multiple objects pointing to the same pin.  Additional objects are a waste of memory

Which is why allowing one object to change its pin number to a different pin is better for clients of your code, not you the designer.

Your const forces multiple objects or unnecessary re-creation of objects on something that is very possible to happen. So no, your code is not well designed, nor is it well defined.

The 'operator=' is not just for copying a value, it is there to protect the class by allowing it to finish what it is doing and prepare to change state.

If you want constant objects, make the object const, not a member, then you still cannot change the pin, but clients who are comfortable with it can declare a non-const object.

I know you have made your decision, however I felt it is worth my replying after those terrible assumptions you made on const and threading.
948  Using Arduino / Programming Questions / Re: Boring "Sketch is Read-Only" dialog. on: June 18, 2013, 01:49:52 am
In 1.5.2 they are not marked read-only, but it does stop you saving in the IDE.
EDIT: the folder is marked read-only.
949  Using Arduino / Programming Questions / Re: Using Explicit on: June 17, 2013, 09:47:36 pm
As the object is not an aggregate, You'll find a temporary is created of the object type using the constructor.
Then the default assignment operator copies the temporary.

Problem solved by user-defined operator!
950  Using Arduino / Programming Questions / Re: Using Explicit on: June 17, 2013, 09:28:57 pm
What do you mean "hasn't been written"? My example stands on its own. It is written.

Your example does not include the assignment operator, therefore what are you even expecting?

Obviously, we would need to use the boost libraries to finish off the thread safety.  That's not part of Arduino from what I can see. 

The boost library is a helper tool, its not required for thread safety. If you use an RTOS or some other system supporting threading, they will provide their own method of mutual exclusion.

Allowing the client to change the pin on the same object is just another source of potential error.

Not at all, bad design is your problem there. Assignment operators are there to fix the error nick encountered, and to safely reuse objects.

Classes provide encapsulation for a reason, the private section is there for your purpose, so use it. There still has not been a remarkably valid reason to justify a const pin, all it is doing is hindering your own codes potential. Other programmers there to modify your code would benefit from a comprehensive comment over a const variable.
951  Using Arduino / Programming Questions / Re: Using Explicit on: June 17, 2013, 07:51:03 pm
You may want to re-think your approach. It is a little flawed.

The Morse class can only be thread safe if _pin is made const and there is nothing in the class's interface to permit changes.

const does not guarantee this! Think of the few scenarios:

  • Thread A owns the object thread B uses, or
  • Global memory has the object, both threads share, or
  • The opposite of the first point.

const prevents nothing from destroying the object while another thread is in the middle of using it. If the object is owned by a thread, what is stopping the thread from dying prematurely. If you can destroy the object without a thread knowing, then you can recreate it with different parameters, breaking your contract to the thread using it.

To protect data in a threaded environment you must use the blocking methods. Either through wait methods or mutual exclusion like semaphores or a mutex.

Furthermore, there is no legitimate reason to have more than one Morse object associated with any pin.  So, I have intentionally made its copy constructor and operator= private.  The class is non-copyable.  Non-copyable classes are quite legitimate in C++.  So, this kind of statement is prohibited

Maybe not, but multiple objects set to different pins is a valid possibility.

Clearly m is being created twice. And the second time it is created before the first one was destroyed. One wonders what that would do to class variables you might be keen to have valid, like pointers.

@Nick, I assume you already get this, but for clarity.
That outcome  is due to using code that hasn't been written. To assign to an already initialized object you must have an assignment operator, which there is none.

Morse m = 13;   //Initialisation
m = 14;  //Assignment
952  Using Arduino / Programming Questions / Re: Using Explicit on: June 17, 2013, 11:04:45 am
In C++ object reuse applies to inheritance, not implementation.

The basis of reusing an objects lifetime is inherently code reuse. You can only reuse an object when the implementation allows it, which is its definition or composition. Code reuse in C++ is achieved via composition, therefore a class that allows itself to be reused is reusable code.
953  Using Arduino / Programming Questions / Re: Using Explicit on: June 17, 2013, 10:28:45 am
It seems pretty strange to me to expect to detach and reattach objects to different pins. If it was sensible in a given application there's nothing wrong with supporting it, but I wouldn't consider this as normal.

It is in no way abnormal. Reusing an objects lifetime can almost always be of benefit.
Consider a few examples:
  • An object created on the heap, which has a fixed lifetime.
  • A dynamically allocated object ( heap memory ) that has its lifetime re-used will prevent fragmentation; which could occur when indiscriminately throwing away objects, only to recreate them with different parameters.
  • A large object that is too big to justify multiple instances, or is just too big to fit in ram multiple times.
  • An object that is large or convoluted may take a long time to initialise/destroy, in time critical situations this can bring execution to a crawl. When comparing a write of a few primitives to creation/destruction of an object, there can be very noticeable differences.

If you want to protect a variable from being written, declare it as protected or private, then use member functions to access it. Inline functions simply returning a reference to a member are no less efficient than accessing its public counterpart directly.
You can justify const when using structs, or when applying it to things that are truly const, for example: your SREG register, or extended memory start will never change its location, which would justify a constant pointer.

which is closer to the paradigm of code re-use.
Not in any sense that I would use the term code reuse.

Once you use const in an object, especially on a value supplied by the client, you are making part of its functionality 'single-use', which instantly demotes it from being well-defined reusable code, as in you have to recreate it to reuse it.
954  Using Arduino / Project Guidance / Re: Using arduino to cool water on: June 17, 2013, 06:35:39 am
I have experimented with TEC modules:

You can use these devices to do controlled cooling, you can reverse the current and heat instead.
Also if you re-arrange the principle you can generate electricity off 2 surfaces differing in temperature.

Using PWM you can have fine control over the device.

They are in abundance on e-bay from $3 up.
955  Using Arduino / Programming Questions / Re: Control Structures and what variable types work on: June 17, 2013, 01:44:02 am
No worries, I'll have to have a look into that later, I always assumed a float would be interchangeable with double ( on the Arduino ).
956  Using Arduino / Programming Questions / Re: Control Structures and what variable types work on: June 17, 2013, 01:17:53 am
any primitive type is usable. floats are equivalent to double on the arduino ( no double precision ).

However a float set to 100.00 may not equal an int set to 100 ( a literal number is an integer by default ),

maybe try comparing like terms:
double d = 100.0f;

if( d == 100.0f ){  //f is float literal specifier.
957  Using Arduino / Programming Questions / Re: Using Explicit on: June 17, 2013, 12:09:03 am
If you aren't going to allow implicit use of constructors, be nice and add in an assignment operator. However as the pin is const the best option is to allow C++ to do its job. A well defined class enables the compiler to select the most efficient option and remove elements it doesn't need ( m=x could implicitly become m(x) ), but may select differently if you also used an assignment operator, which could reduce code duplication.

If you are worried about inputs, validate them in the constructor, which you still can't do due to being marked const.

If you insist on the pin being const, then maybe a template would be worth investigation.
The below is called a non-type template ( takes a value rather than a type in the template specification. )
template< unsigned char _P > class Morse{
    Morse( void );

    void dot(){
        digitalWrite( _P, HIGH ); //Use _P directly, no need for instance variable.
        digitalWrite( _P, LOW );

    void dash();

Morse< 13 > m; //No specific or copy constructor.

EDIT:If you intend on using more than one instance of the library, make the pin variable a 'variable' rather than const, then you can re-use an actual object, which is closer to the paradigm of code re-use.
958  Development / Suggestions for the Arduino Project / Re: Request Leonardo MEGA Board on: June 16, 2013, 02:15:56 am
There is a neat little board coming with wifi and USB powered with linux, this may have some nice USB possibilities.

Using the Bridge library in your sketches, you can link the 32u4 to Linux, launching programs and scripts, passing them parameters (sensor readings for example) and reading their output, thus creating a strong integration between the creativity of your sketch and the power of Linux. The Yún supports Shell and Python scripts out-of-the-box but you can install a wide range of open source software and tools.
959  Using Arduino / Programming Questions / Re: Function doesn't get executed on: June 15, 2013, 11:29:07 pm
What use would a prototype be at that point in the code?. I can't see a possible use and therefore would expect and error message.

The great thing about C++ is, if its allowed, there is usually a reason for it, even if its something really obscure.

Now with function prototypes, every one sees them as 'global' entities, and as such take the global scope for granted.
But prototypes follow the standard scope rules: "the prototype is only usable within the scope it has been declared in".

Maybe it can be seen as a "private non-member non-static function" ( no class ).

This sketch shows this effect:
#include "Arduino.h"
  //C++ will not see this prototype.
  //This is here to prevent the Arduino IDE from outputting its own prototype.
  void func();

void setup() {
  Serial.begin( 9600 );
  void func();
  func(); //Works fine.

void loop() {
  func(); //Error, cannot see prototype.

void func(){ Serial.println( "Call" ); }
960  Using Arduino / Programming Questions / Re: Change sub array data in one? on: June 14, 2013, 10:38:51 am
The structure alternative is almost the same, nothing really obscure about either though.

struct U64{
  void Set( void *v_Data ){

    byte *b_Data = ( byte* ) v_Data;

    for( char i = 0 ; i < 8 ; ++i )
      Data[ i ] = b_Data[ i ];
  unsigned char Data[8];

U64 Test[ 4 ] = {{2,4,16,32,63,65,127,64},{2,4,16,32,63,65,127,64},{1,3,7,15,31,63,127,255},{0,0,0,0,0,0,0,0}};

void setup() {
  byte myarray[8] = {27,3,19,31,53,5,12,60};

  Test[0].Set( myarray );
Pages: 1 ... 62 63 [64] 65 66 ... 149