Go Down

Topic: Is it possible to make global "defines" that work with libraries? (Read 574 times) previous topic - next topic

Protoneer

I am trying to make my application a little more configurable.

My project consists out of a library that use SoftwareSerial. I want to be able to change the pins used by SoftwareSerial from inside my sketch.
Problem is SoftwareSerial only sets its pin with its constructor.

Is there a way that I can do a "Define" in my sketch that applies it to my library that instantiates SoftwareSerial and applies my define at compile time?

Other option I have is to maybe define SoftwareSerial in my main sketch and pass SS to my library.... (Not sure how to thought)
Or Change the SS library to allow for Pin change.(Don't want to move away from using standard libraries.)

Any suggestions would be appreciated, thanks.   
Delta 3D Printer with Auto Bed Leveling - http://www.kickstarter.com/projects/ttstam/openbeam-kossel-pro-a-new-type-of-3d-printer/posts
http://blog.Protoneer.co.nz

Graynomad

#1
Aug 20, 2013, 10:47 am Last Edit: Aug 20, 2013, 10:53 am by Graynomad Reason: 1
You can create a pointer to a SS instance then use "new" or call the begin() or similar. I've done it before but can't remember the syntax offhand so I'll look for some code.


EDIT: Found some code

Code: [Select]
static SoftwareSerial * _mySs;

_mySs = new SoftwareSerial(rxpin, txpin);
_mySs->begin (baud);
_mySs->write (c);
if (_mySs->available() {
    c = _mySs->read();
}



_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Protoneer


First, if you post your code is more simple to suggest a solution.
Second, you can create a method for your class like Init() or Begin() and ask as parameter the pin for SoftwareSerial.

For example Ethernet class have a constructor but in setup() you must use Ethernet.begin()


This is my library:
https://github.com/Protoneer/RS485-Arduino-Library/blob/master/RS485.h
https://github.com/Protoneer/RS485-Arduino-Library/blob/master/RS485.cpp

I think the issue is the Serial Object being global in the library....
CPP-L4  SoftwareSerial rs485 (RX_PIN, TX_PIN);
Delta 3D Printer with Auto Bed Leveling - http://www.kickstarter.com/projects/ttstam/openbeam-kossel-pro-a-new-type-of-3d-printer/posts
http://blog.Protoneer.co.nz

Protoneer


You can create a pointer to a SS instance then use "new" or call the begin() or similar. I've done it before but can't remember the syntax offhand so I'll look for some code.


EDIT: Found some code

Code: [Select]
static SoftwareSerial * _mySs;

_mySs = new SoftwareSerial(rxpin, txpin);
_mySs->begin (baud);
_mySs->write (c);
if (_mySs->available() {
    c = _mySs->read();
}



_____
Rob



That seems more like what I am after. What is the reason for making it static?
Delta 3D Printer with Auto Bed Leveling - http://www.kickstarter.com/projects/ttstam/openbeam-kossel-pro-a-new-type-of-3d-printer/posts
http://blog.Protoneer.co.nz

Protoneer


You can create a pointer to a SS instance then use "new" or call the begin() or similar. I've done it before but can't remember the syntax offhand so I'll look for some code.


EDIT: Found some code

Code: [Select]
static SoftwareSerial * _mySs;

_mySs = new SoftwareSerial(rxpin, txpin);
_mySs->begin (baud);
_mySs->write (c);
if (_mySs->available() {
    c = _mySs->read();
}



_____
Rob


Works like a charm!!!! Now I can config my pins and not have duplicate code... Developers heaven!!!   XD XD XD
Delta 3D Printer with Auto Bed Leveling - http://www.kickstarter.com/projects/ttstam/openbeam-kossel-pro-a-new-type-of-3d-printer/posts
http://blog.Protoneer.co.nz

GoForSmoke

Setting up Serial also includes the buffer. When you switch, take care about that or more specifically what is in that or you could end up with funny results.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

MarkT

#defines are processed in order - try
Code: [Select]

#define MY_DEFINE ...
#include <mylibrary.h>


I not sure but think that works...
[ I won't respond to messages, use the forum please ]

GoForSmoke

The variables should be in the library and extern in the main code. Having the main code set those for the library is kind of putting the cart before the horse.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up