Show Posts
Pages: 1 ... 4 5 [6] 7
76  Using Arduino / Programming Questions / Re: C++ getting address of a function on: September 25, 2011, 06:31:55 am
You can get the address of a function easily enough. Test case ... Header file (functionheader.h):
...
However having said that, you seem to be wanting to take the address of a member function of a class. In this case the function address isn't enough, you need the class instance address too (ie. the "this" variable).

Isn't this a bit convoluted for an ISR? Somehow taking the address of a function which is a class member?

Unfortunately I am having trouble with C++, not C.  So I agree that taking addresses of functions is easy, but I want to do it in a C++ kind of way.

According to http://www.newty.de/fpt/fpt.html#call , it should be easy:
Code:
int result3 = (instance1.*pt2Member)(12, 'a', 'b');   // C++
  but as you can see, I get the "was not declared in this scope" error.

I wouldn't expect that calling an object's function by reference in an ISR would be so convoluted.  To me, it makes perfect sense.  ...Oh, I know what you're thinking:  "GreyGnome, silly, the interrupt is an out-of-band action, divorced from normal flow of your sketch.  How would you expect that to work???"  Well hear me out a second:

When I used PinChangeInt to create my rotary encoder library, I noticed that I was doing some convoluted actions to get it to work.  I also noticed that I was doing convolutions on top of convolutions.  In other words, the library works by:
  • Calls the interrupt vector for the PORT on which the interrupt occurred (vector 0, 1, or 2).
  • The vector function figures out what pin caused the interrupt.
  • Based on that pin, call the user-defined function.
Well, because of the fact that the library was designed to call a user-defined function in a C way (ie, no this pointer allowed), I needed to create a static function.  That is:
  • The third step above was useless, because the user-defined function was always the same.  So trying to determine a pin-to-function correlation was unnecessary work.
  • I needed to repeat the second step in my static function.  That is, in my static function I figured out what pin caused the interrupt.  Additionally,
  • I created another array of structures containing all the encapsulated data I needed for each individual pin, so that the static function would grab the pin-specific structure from the array, then perform the proper functions.

It is because I performed that last item that I am looking to do it differently now. Because what is a "structure containing all the encapsulated data"?  ...Seems to me, they call that a "class".   smiley-wink  In other words, by using the Arduino it seems I must forgo the benefits that C++ classes bring to me and jump back into C. But as a relative n00b I'm not yet comfortable doing that.

If I was able to have the interrupt routine
  • Call the interrupt vector for the PORT on which the interrupt occurred (vector 0, 1, or 2).
  • The vector function figures out what pin caused the interrupt.
  • Based on that pin, call a class's method.
Then, based on the fact that a class is an encapsulation of my data and the actions I want to perform on that data, I think this way is a lot cleaner.  At least, it's more object-oriented.
77  Using Arduino / Programming Questions / Re: C++ getting address of a function on: September 25, 2011, 06:08:42 am
I am i wrong? maybe i am but i'm sure with arduino a class does need to be in it's own file and also i thought that constructors weren't allow? Well not in arduino anyway. You have to create a function to set all the stuff you would normally set in the constructor.

If what you say is true, this wouldn't work.  But it does:
Code:
#include <PinChangeInt.h>

#define TESTPIN 4

class it {
  int aPin;
  public:
    it(int arduinoPin);
    void onInterrupt(void);
  private:
};

it::it(int arduinoPin) {
  aPin=arduinoPin;
}

void it::onInterrupt(void) {
  Serial.print ("Test of class and constructor created in sketch... WORKS! ");
  Serial.println(aPin, DEC);
};

it testIt=it(TESTPIN);

void setup () {
  Serial.begin(115200);

  testIt.onInterrupt();
}

void loop () {
  delay(1000);
}

...The output is, "Test of class and constructor created in sketch... WORKS! 4"

So we have both a class created in a sketch, with a constructor.
78  Using Arduino / Programming Questions / Re: C++ getting address of a function on: September 24, 2011, 01:55:20 pm
Thanks for the reply, but I don't think we're there yet...

The it::it() is actually a constructor for the class.

I don't know why a class would need to be in its own file.  As a matter of fact, I don't get any complaints about my class until I try to get a pointer to a function in the class.  So the class seems to work. It's true that for a library you certainly want to organize your work but I don't think that's a requirement imposed by ide or compiler.
79  Using Arduino / Programming Questions / Re: Using cli() and sei() in a library on: September 24, 2011, 12:26:51 pm
Just got back to this conversation.  Fascinating.  Thanks for the info, bperrybap.  And for the erudite questions, Nick.
80  Using Arduino / Programming Questions / C++ getting address of a function on: September 24, 2011, 12:20:16 pm
Hi,
I am trying to get the address of a function so as to send it to another function upon interrupt.  However, I get "(my function) was not declared in this scope."  According to http://www.newty.de/fpt/fpt.html#call, calling a function using a function pointer seems to be pretty easy but I am at a loss.  Can you help correct my syntax?  Thanks.  BTW the below code is just a stub.  I want to get past this compiler issue before I move on into the meat of what I'm attempting to do.

Code:
#include <PinChangeInt.h>

#define TESTPIN 4

class it {
  int aPin;
  public:
    it(int arduinoPin);
    void onInterrupt(void);
  private:
};

it::it(int arduinoPin) {
  aPin=arduinoPin;
}

void it::onInterrupt(void) {
  Serial.println(aPin, DEC);
};


void setup () {
  Serial.begin(115200);
  it testIt=it(TESTPIN);
  testIt.*onInterrupt; // ERROR here, and/or below (if I uncomment the below).
  // PCintPort::attachInterrupt(TESTPIN, (testIt.*onInterrupt), CHANGE);
}

void loop () {
  delay(1000);
}
81  Using Arduino / Programming Questions / Re: Using cli() and sei() in a library on: September 10, 2011, 12:43:26 pm
Thanks for the replies.  Can you explain how I might include it?  ...Some modification to the <code>#include</code> directive, perhaps?  As mentioned above, just doing a #include fails.

Thanks.
-GreyGnome
82  Using Arduino / Programming Questions / Using cli() and sei() in a library on: September 02, 2011, 04:36:40 pm
Hi,
I have some library code in which I need to disable interrupts for a short moment.  When I try to use cli() / sei(), I get:

Code:
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp:290: error: 'cli' was not declared in this scope
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp:295: error: 'sei' was not declared in this scope

I find that I cannot include interrupt.h in my library .cpp file, either (this would be the interrupt.h from AVR library, which on Mac is in /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/etc/options/gcc-version/include/avr/interrupt.h).

Can anyone explain why cli() / sei() are not able to be used in my library?  Is there a way I can include an AVR .h file, or anything I can do that would be more elegant that, say, copying the macros from the interrupt.h file?

Thanks.
83  Using Arduino / Programming Questions / Re: Need to call an uninstantiated method from object method on: August 09, 2011, 07:44:40 am
Thanks, that helps.  But now I need to pass "this" as an argument to my function.

The function is specified in the PinChangeInt.h like this:
Code:
typedef void (*PCIntvoidFuncPtr)(void);
So I have tried to do this:
Code:
typedef void (*PCIntvoidFuncPtr)(void *);
So that I can do this in my Class:
Code:
PCintPort::attachInterrupt(pinA, interruptWrapper((void *) this), CHANGE);
but I get this error:
Code:
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp: In member function 'void AdaEncoder::attachInterrupt(int, int)':
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp:68: error: invalid use of void expression

if I do:
Code:
PCintPort::attachInterrupt(pinA, &interruptWrapper((void *) this), CHANGE);
I get this:
Code:
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp: In member function 'void AdaEncoder::attachInterrupt(int, int)':
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp:68: error: lvalue required as unary '&' operand

I can't figure out how to pass this through my static wrapper so as to open up the world of possibilities to me!  I need it so that I know which object the interrupt applies to.

Thanks again.
84  Using Arduino / Programming Questions / Need to call an uninstantiated method from object method on: August 09, 2011, 12:04:32 am
Hi,
I am using the PinChangeInt library to set up a number of interrupts for a (relatively) arbitrary number of rotary encoders on my Arduino Uno.

I am creating my own Encoder library, which relies on the PinChangeInt library.

I have a call in my Encoder library that looks like this:
Code:
void AdaEncoder::attachInterrupt(int pinA, int pinB) {
    PCintPort::attachInterrupt(pinA, encoderInterrupt, CHANGE);
}

Note the AdaEncoder::attachInterrupt() method, above, is not static.  But the PCintPort::attachInterrupt() method is.  I have a stub encoderInterrupt() method:

Code:
void encoderInterrupt() {}

The error I get is this:
Code:
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/AdaEncoder.cpp:65: error: no matching function for call to 'PCintPort::attachInterrupt(int&, <unresolved overloaded function type>, int)'
/Users/schwager/Documents/Arduino/libraries/AdaEncoder/../PinChangeInt/PinChangeInt.h:87: note: candidates are: static void PCintPort::attachInterrupt(uint8_t, void (*)(), int)

I think what I have to do is make the
Code:
encoderInterrupt()
method static, but I'd like to avoid that.  Is there any way I can do so?  Or do I need to make encoderInterrupt() static, then solve my problem by utilizing static pointers or arrays?

Thanks.
85  Using Arduino / Project Guidance / Convert Arduino pins to AVR PORTs on: August 06, 2011, 03:49:47 pm
Hi,
I am trying to write an interrupt that uses bitSet(), bitClear(), bitRead(), etc., to manipulate the ports for speed, rather than digitalRead() and digitalWrite().  Why do I need that speed?  I don't, actually.  But I want to do it because I want to learn more about the AVR, I want to get my chops up, and because it's there (tm).

But bitSet() has two arguments: The PORT, and the pin in the PORT.  However, I would like my library that includes this interrupt to take the Arduino pin as its argument; I don't want the programmer to have to figure the PORT and pin.

Can anyone explain to me why this works:
Code:
uint8_t sPort = digitalPinToPort(13);
bitSet(sPort, 5);

But this does not?
Code:
bitSet(digitalPinToPort(13), 5);
...This one says, "error: lvalue required as left operand of assignment".

Thanks.

86  Using Arduino / Networking, Protocols, and Devices / Re: DS3231.h --- Library for DS3231 (or DS3232) real-time clock on: May 29, 2011, 02:19:33 pm
Thanks, Eric.  This is cool, I'll be needing that chip in the future and I am glad to have your work!
 smiley-grin
87  Using Arduino / Networking, Protocols, and Devices / Re: I2C trouble with the MPR084 on: May 18, 2011, 08:54:51 pm
Well, here it is, a couple of months later and a few Wiki pages.  Finally, I got my MPR084 to squawk!  I just got it working an hour ago, and now on my second celebratory beer I am posting my results.

One thing I determined, I wasn't going to get this to work properly without a deep understanding of the whole catastrophe.  I'm not there yet, but I'm on my way.  I've created two Wiki pages, one to log my deep dive into the whole thing:  http://arduino.cc/playground/Code/ATMELTWI  and the other because I found the Wire library documentation lacking, so I expanded upon it:  http://arduino.cc/playground/Main/WireLibraryDetailedReference

I hope those are found useful one day by someone.  If I was able to see at all, it was only by standing on the shoulders of giants.  I am very grateful to all who have created this interesting platform.

Without further ado, here is the code that got the MPR084 to talk to me:

#include <Wire.h>
// For reference. These are the ANALOG pins:
const int i2cSCLPin =  5; // the number of the I2C interface, SCL pin
const int i2cSDAPin = 4;  // SDA pin

#define MPR084_ADDR 0x5C  // assumes the MPR084 AD0 pin is low
#define regINFO 0x14      // Sensor Information Register

char c;
void setup() {
  delay (1000);
  Serial.begin(19200); // For reporting
  Serial.print("MPR084 test.\n");
  Wire.begin();
}

void loop() {
  Wire.beginTransmission(MPR084_ADDR);
  Wire.send(regINFO); // This would be my command byte
  Wire.endTransmission();
  Wire.requestFrom(MPR084_ADDR, 32); // Not sure how much data there is, let's try this.
  Wire.endTransmission();
  while (Wire.available()) {
    c = Wire.receive();
    if (c != 0) {
      Serial.print(c);
    }
  }
  Serial.print('\n');
  delay(1000);
}


And here is an example of my glorious output:

VER:1_0_0Freescale,PN:MPR084,QUAL:EXTERNAL,

And that's it...!
- GreyGnome
88  Using Arduino / Networking, Protocols, and Devices / Detailed Wire Library Reference for I2C Communication on: May 04, 2011, 05:18:25 pm
Hello,
I have been trying to use the Wire library and have found the documentation lacking.  So I am putting up a detailed reference here:

http://arduino.cc/playground/Main/WireLibraryDetailedReference

It is a work in progress.  Release early and often, as they say.  Let me know if you see any issues, problems, or joys.
-GreyGnome
89  Using Arduino / Networking, Protocols, and Devices / Re: I2C trouble with the MPR084 on: March 08, 2011, 11:59:35 pm
Another thing to keep in mind, because it's a Freescale device, is that it may require the use of a repeated start, instead of a stop bit, after sending the command....

This is true!  Page 5 of the Datasheet shows the repeated start!  Oh boy, now I have a bit of work to do... thanks for the information!
90  Using Arduino / Networking, Protocols, and Devices / Re: I2C trouble with the MPR084 on: March 08, 2011, 11:50:47 pm
Can you post the code you are using?  Make sure the address you are using matches the address of the IC with address line.  Can you post a schematic also so we can see if you have it wired up correctly?  

Here is a picture of my schematic.
http://flic.kr/p/9oYkDp

As I'm a working/family man, I have yet to check some of the other suggestions in these replies. I have yet to procure a USB<->Serial cable to read the output of my Arduino.

Thanks, I appreciate the attention to this problem.  I will certainly post my code when I get this figured out.
Pages: 1 ... 4 5 [6] 7