Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using mbed pin names and external defines #27

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/RS485.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@

#include "RS485.h"

#ifdef __MBED__
#include "pinDefinitions.h"
RS485Class::RS485Class(HardwareSerial& hwSerial, PinName txPin, PinName dePin, PinName rePin) :
_serial(&hwSerial),
_txPin(PinNameToIndex(txPin)),
_dePin(PinNameToIndex(dePin)),
_rePin(PinNameToIndex(rePin)),
_transmisionBegun(false)
{
}
#endif

RS485Class::RS485Class(HardwareSerial& hwSerial, int txPin, int dePin, int rePin) :
_serial(&hwSerial),
_txPin(txPin),
Expand Down Expand Up @@ -186,4 +198,8 @@ void RS485Class::setDelays(int predelay, int postdelay)
_postdelay = postdelay;
}

#ifdef RS485_SERIAL_PORT
RS485Class RS485(RS485_SERIAL_PORT, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
#else
RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
#endif
7 changes: 7 additions & 0 deletions src/RS485.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

#include <Arduino.h>

#ifndef RS485_DEFAULT_TX_PIN
#ifdef PIN_SERIAL1_TX
#define RS485_DEFAULT_TX_PIN PIN_SERIAL1_TX
#else
#define RS485_DEFAULT_TX_PIN 1
#endif
#endif

#ifdef __AVR__
#define RS485_DEFAULT_DE_PIN 2
Expand All @@ -35,16 +37,21 @@
#define RS485_DEFAULT_DE_PIN A1
#define RS485_DEFAULT_RE_PIN A0
#else
#ifndef RS485_DEFAULT_DE_PIN
#define RS485_DEFAULT_DE_PIN A6
#define RS485_DEFAULT_RE_PIN A5
#endif
#endif


#define RS485_DEFAULT_PRE_DELAY 50
#define RS485_DEFAULT_POST_DELAY 50

class RS485Class : public Stream {
public:
#ifdef __MBED__
RS485Class(HardwareSerial& hwSerial, PinName txPin, PinName dePin, PinName rePin);
#endif
RS485Class(HardwareSerial& hwSerial, int txPin, int dePin, int rePin);

virtual void begin(unsigned long baudrate);
Expand Down