Show Posts
Pages: 1 ... 3 4 [5] 6 7
61  Using Arduino / Programming Questions / Re: How to give an Object as an Argument to a Function? (...not to a class method) on: January 21, 2012, 11:30:43 am
Ok, yes that makes sense now I suppose.  I'm not totally happy about it, but at least I understand.

Thanks again!
62  Using Arduino / Programming Questions / Re: How to give an Object as an Argument to a Function? (...not to a class method) on: January 21, 2012, 10:32:18 am
Ok, thanks... I got it now.  But I don't get the separation of files.  I have created classes in the sketch without issue.

My understanding of including files goes like this:  The C preprocessor finds the directive, and for all intents and purposes, what the C++ compiler is presented with is essentially the entire text of the included files, along with the file that contains the include directive, in one flow of textual data.

What difference does pulling the class out of the sketch make?  ...I mean, technically.  What is going on under the covers that we don't see?

Thanks again for your response(s).
63  Using Arduino / Programming Questions / How to give an Object as an Argument to a Function? (...not to a class method) on: January 21, 2012, 09:32:47 am
Hi,
I am trying to give an object as an argument to a function, but I am getting an error:
Code:
class myclass
{
  public:
 
  void yo() {
    Serial.println("Hello");
  }
};

myclass pbs=myclass();

void reportit(pbs& i) {
  //i.yo(); // If this didn't give an error, I would uncomment this method call.ß
  Serial.print("test");
};

void setup()
{
  Serial.begin(115200); Serial.println("---------------------------------------");
};

void loop() {
};

but I get this error:

tmp:-1: error: variable or field 'reportit' declared void
tmp:-1: error: 'pbs' was not declared in this scope
tmp:-1: error: 'i' was not declared in this scope
tmp:11: error: variable or field 'reportit' declared void
tmp:11: error: 'i' was not declared in this scope

...Can you tell me why?
64  Development / Suggestions for the Arduino Project / Re: How to give an Object as an Argument to a Function? (...not to a class method) on: January 21, 2012, 09:29:23 am
OOps, drat- I got a typo and confused the issue.  I will ask this in the programming forum section.

I don't know why people think you can't define classes in the sketch.  I do it all the time, with no issues.
65  Development / Suggestions for the Arduino Project / How to give an Object as an Argument to a Function? (...not to a class method) on: January 21, 2012, 01:38:08 am
Hi,
I am trying to give an object as an argument to a function, but I am getting an error:

Code:
class myclass
{
  public:
 
  void yo() {
    Serial.println("Hello");
  }
};

void reportit(pbs& i) {
  //i.yo(); // If this didn't give an error, I would uncomment this method call.
  Serial.print("test");
};


void setup()
{
  Serial.begin(115200); Serial.println("---------------------------------------");
};

void loop() {
};

But it gives:

tmp:-1: error: variable or field 'reportit' declared void
tmp:-1: error: 'pbs' was not declared in this scope
tmp:-1: error: 'i' was not declared in this scope
tmp:10: error: variable or field 'reportit' declared void
tmp:10: error: 'pbs' was not declared in this scope
tmp:10: error: 'i' was not declared in this scope

Can anyone help?  Thanks.
66  Development / Suggestions for the Arduino Project / Re: Delegate system for member function pointers on: January 21, 2012, 01:34:00 am
Very useful indeed.  Thanks for posting.
67  Using Arduino / Programming Questions / Re: C++ getting address of a function: CALLBACK! on: January 16, 2012, 06:44:55 pm
Code:
[code]
smiley-roll Well, here it is, months later and I've made much progress.

What I wanted to do all along was create a callback- virtual functions to the rescue!  The problem, as Nick mentioned, was that it's not enough in C++ to get the address of a method- in order to correctly identify a method the "this" pointer is required.  Because methods are defined in the Class, but instantiated as part of the payload of an object.

So I have found the below code (at the end) and made a little library (cb.h), and created an Interface (that's Java terminology, don't know if it applies to C++).  A full example will be given at my Tigger library at http://code.google.com/p/tigger/ when I upload it.

One subclasses the CallBackInterface class, e.g.:
Code:
class Tigger : public CallBackInterface {
        public:
        // constructors an whatnot go here
}
implement a method called
Code:
void cbmethod(void)
in your class ("Tigger", above), e.g.:
Code:
void Tigger::cbmethod() {
        uint8_t oldSREG = SREG;
        cli();
        tigermillis = timer0_millis;
        SREG = oldSREG;
        if (tigermillis-startTime <= _delay) return;
        startTime=tigermillis;
        count++;
}
Instantiate the class:
Code:
Tigger myTigger=new Tigger();

Then finally, call the cbmethod from, say, an interrupt (pseudo-code).  First, you need to setup the situation; call
Code:
    addPin(myTigger):
The addPin method would look something like this:
Code:
void PCintPort::addPin(CallBackInterface* cbIface)
{
        InterruptPin p=createPin(arduinoPin, CHANGE);
        // ...nefarious details deleted for brevity
        // cbIface is the object instantiated from a subclass of CallBackInterface
        CallBack<CallBackInterface, void>* my_callback=new CallBack<CallBackInterface, void> (cbIface, &CallBackInterface::cbmethod);
        p->pinCallBack=my_callback;
In the Interrupt code, call the CallBack at your leisure.  All variables in the object are accessible.  This call will actually call the cbmethod passed to it above, and previously defined in the class:
Code:
       (*(p->pinCallBack))();

Here's the code I found:
Code:
/*
  **********************************************************************
  * cb by GreyGnome aka Mike Schwager                                  *
  * version 1.0 Mon Jan 16 09:25:59 CST 2012                           *
  *                                                                    *
  * based on:                                                          *
  *      Variable Parameter Call-Back Template (version 0.0.1)         *
  *                                                                    *
  * Author: Arash Partow - 2000                                        *
  * URL: http://www.partow.net/programming/templatecallback/index.html *
  *                                                                    *
  * Copyright Notice:                                                  *
  * Free use of this library is permitted under the guidelines and     *
  * in accordance with the most current version of the Common Public   *
  * License.                                                           *
  * http://www.opensource.org/licenses/cpl.php                         *
  *                                                                    *
  **********************************************************************
*/


#ifndef INCLUDE_CALLBACK_H
#define INCLUDE_CALLBACK_H

class CallBackInterface
{
   public:

     CallBackInterface() {};

     virtual void cbmethod() {
     };

};

template < class Class, typename ReturnType>
class CallBack
{
   public:

    typedef ReturnType (Class::*Method)(void);

    CallBack(Class* _class_instance, Method _method)
    {
       class_instance = _class_instance;
       method         = _method;
    };

    ReturnType operator()()
    {
       return (class_instance->*method)();
    };

    ReturnType execute()
    {
       return operator()();
    };

    private:

      Class*  class_instance;
      Method  method;

};
#endif
[/code]
68  Using Arduino / Programming Questions / Re: Wire.write vs Wire.send. new problems on: January 15, 2012, 04:35:49 pm
Code:
Wire.write((uint8_t) value);

should work in the way that you describe.  You could use a byte for the value and I don't think the compiler will complain.  In any event, your technique sounds good:  The only difference you should see is that
  • They replaced send() with write
  • They removed send(int value), because in that one it was doing a cast to a uint8_t under the covers, thereby trashing the topmost byte of your 16-bit int.

Significantly, though, the code works the same under the covers.

You must have something else wrong with your code.
69  Community / Exhibition / Gallery / PinChangeInt library- To attach interrupts to multiple Arduino (Uno/Mega) pins on: January 15, 2012, 08:24:39 am
Version 1.4 is now available at http://code.google.com/p/arduino-pinchangeint/

The PinChangeInt library implements Pin Change interrupts for the Arduino environment. It is a drop in replacement for the PCint library.

What are Pin Change interrupts? The ATmega328p processor at the heart of the Arduino has two different kinds of interrupts: “external”, and “pin change”. There are only two external interrupt pins, INT0 and INT1, and they are mapped to Arduino pins 2 and 3. These interrupts can be set to trigger on RISING or FALLING signal edges, or on low level. The triggers are interpreted by hardware, and the interrupt is very fast. On the other hand there are only 2 such pins on the ATmega328p in the Arduino Uno and Duemilanove.

On the other hand the pin change interrupts can be enabled on any or all of the Arduino's signal pins. They are triggered equally on RISING or FALLING signal edges, so it is up to the interrupt code to set the proper pins to receive interrupts, to determine what happened (which pin? ...did the signal rise, or fall?), and to handle it properly. Furthermore, the pin change interrupts are grouped into 3 “port”s on the MCU, so there are only 3 interrupt vectors (subroutines) for the entire body of 20 pins. This makes the job of resolving the action on a single interrupt even more complicated. The interrupt routine should be fast, but complication is the enemy of speed. The PinChangeInt library is designed to handle the Arduino's pin change interrupts as quickly and reasonably as possible (without using Assembly, that is).

Version 1.4 puts all the code in the .h file, to allow the programmer to configure it through their sketch for minimum ram usage. In earlier versions, it must be configured through the PinChangeIntConfig header file instead.

See the Wiki pages for more information.
70  Using Arduino / Programming Questions / Re: Best practice for libraries sharing hardware resources like interrupts? on: January 15, 2012, 08:14:28 am
BTW, version 1.4 of PinChangeInt now allows you to modify your #define's in your sketch.  So they can be different on a sketch-by-sketch basis.  See http://code.google.com/p/arduino-pinchangeint/

As Nick said, if you have an Arduino Uno and have only 2 interrupt pins, the "External Interrupt" pins (pins 2 and 3 of the Arduino) are probably your best bet.  If you need to interrupt more than 2 pins, Pin Change interrupts are the only way to go.
71  Using Arduino / Programming Questions / Re: Reason for missing new() / delete()? on: October 05, 2011, 07:24:14 am
The sense I get is that if you're writing a library, you should use malloc/free (and in fact, you may have to, since new/delete are gone). If you're writing a sketch for a particular purpose (i.e., the majority of Arduino developers), you should use singleton variables or fixed-size arrays, and not malloc/free/new/delete.

Well said!  I think this sums it up nicely, and reflects my conundrum.  I'm writing a library, so I have the need to use dynamic memory (and to use a new/delete provided on the Internet).

If I was writing a sketch for something static, then yes I may prefer to use fixed-size arrays.

72  Using Arduino / Programming Questions / Re: Reason for missing new() / delete()? on: October 03, 2011, 09:20:45 am
Quote
Can anyone give me a concise reason that Arduino is missing new() and delete()?
Because in a RAM-constrained embedded environment, you shouldn't be using them.
(Contentious, or what?)

That's what I thought.  But then one ends up with this:
Code:
static PCintPin pinDataAlloc[MAX_PIN_CHANGE_PINS];
as I mention above.  If I create a library which depends on the other library, and I essentially hide the details of the other library, now I've got an issue because the developer who uses my library has to deal with implementation-specific details, such as the fact that if they don't understand the library that I'm using, they won't know to change MAX_PIN_CHANGE_PINS.  So they'll either A) be wasting the very RAM that our new()-less environment was supposed to conserve, or B) end up with a weird bug.   

Indeed, in my AdaEncoder library, I dealt with the absence of new() by creating a linked list of structures using malloc().  And when all was said and done, I said to myself, "Damn, all this malarkey I had to go through to use malloc() in a C-stylin' kind of way, rather than just using new().  And did it avail me anything?  I mean, what's the big difference between a struct and a class anyway?  According to Stroustroup, a class is essentially a struct..."

...Of course, this is the very contention you're alluding to.   smiley-razz
73  Using Arduino / Programming Questions / Re: Reason for missing new() / delete()? on: October 03, 2011, 07:57:40 am
BTW, according to this article: http://arduinoetcetera.blogspot.com/2010/04/implementing-new-and-delete-c.html which points more importantly to this discussion:  http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=410870, the new() operator can be a little more involved
Quote
because if you are using gcc 4.x and newer (I didn't check it with older versions) and you want to use templates, virtual inheritance and so on you must define some additional functions, which is not used now by compiler, but must be present to satisfy linker

So the final code should look like:
Code:
#ifndef cppfix
#define cppfix

#include <stdlib.h>

__extension__ typedef int __guard __attribute__((mode (__DI__)));

void * operator new(size_t size);
void operator delete(void * ptr);

int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
void __cxa_guard_abort (__guard *) {};

void * operator new(size_t size)
{
return malloc(size);
}

void operator delete(void * ptr)
{
free(ptr);
}

#endif
74  Using Arduino / Programming Questions / Re: Reason for missing new() / delete()? on: October 03, 2011, 07:53:10 am
Thanks, Nick.  I feel better about  feeling bad about not having new(), and wanting to implement it in apparent violation of the wishes of the IDE developers.

As a matter of fact, what the developers of the PinChangeInt library did was allocate space ahead of time, by defining an array of objects:
Code:
static PCintPin pinDataAlloc[MAX_PIN_CHANGE_PINS];

...developers who use this library need to poke around in the .h files to discover what MAX_PIN_CHANGE_PINS does, and set it appropriately.  If one neglects to do so, and leaves MAX_PIN_CHANGE_PINS at its default of 8, then if you use this library to catch interrupts on 1 pin, space is allocated for 7 extra PCintPin objects that you won't use.  And I, creating a library that uses this library, now have to expose gory details to the developer when I'd rather they have a nice easy interface.

I'd much rather use a dynamically-created linked list and have it grow as large or as small as I need.  But I preach to the choir.  In short:  thanks.  I'm going to use new() guilt-free.   smiley-cool
75  Using Arduino / Programming Questions / Reason for missing new() / delete()? on: October 02, 2011, 11:33:19 pm
Hi,
Can anyone give me a concise reason that Arduino is missing new() and delete()?

I am finding that I need to jump through hoops to create static methods to do some things, because I can't allocate C++ classes... I need to use malloc() on some data structures.

After having done so, I can only think... "Geez, I wish I had new()".

BTW, I know that there is a new() operator implementation out there such that I can define it myself and I plan to use it... barring a good "Only n00bs use new(), that's why it's left out of the Arduino C++ stuff" explanation.

Thanks.
Pages: 1 ... 3 4 [5] 6 7