Pages: [1]   Go Down
Author Topic: Is it possible to make global "defines" that work with libraries?  (Read 326 times)
0 Members and 1 Guest are viewing this topic.
Wellington, New Zealand
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   
Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8452
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
static SoftwareSerial * _mySs;

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


_____
Rob
« Last Edit: August 20, 2013, 03:53:06 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Wellington, New Zealand
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged


Wellington, New Zealand
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged


Wellington, New Zealand
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!!!   smiley-lol smiley-lol smiley-lol
Logged


Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4020
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Examples can be found in your IDE.

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10442
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#defines are processed in order - try
Code:
#define MY_DEFINE ...
#include <mylibrary.h>

I not sure but think that works...
Logged

[ I won't respond to messages, use the forum please ]

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4020
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Examples can be found in your IDE.

Pages: [1]   Go Up
Jump to: