Skip to content

Commit

Permalink
modified library to use serial device in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
humanoid2050 committed Mar 13, 2017
1 parent c0d2bd9 commit a0e7532
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 53 deletions.
56 changes: 8 additions & 48 deletions SBUS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,63 +39,23 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO
#endif

/* SBUS object, input the serial bus */
SBUS::SBUS(uint8_t bus){
_bus = bus; // serial bus
SBUS::SBUS(HardwareSerial& bus){
_bus = &bus;
}

/* starts the serial communication */
void SBUS::begin(){

// initialize parsing state
_fpos = 0;

// select the serial port
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) // Teensy 3.0 || Teensy 3.1/3.2 || Teensy LC

if(_bus == 3){
_port = &Serial3;
}
else if(_bus == 2){
_port = &Serial2;
}
else{
_port = &Serial1;
}

#endif

#if defined(__MK64FX512__) || defined(__MK66FX1M0__) // Teensy 3.5 || Teensy 3.6

if(_bus == 6){
_port = &Serial6;
}
else if(_bus == 5){
_port = &Serial5;
}
else if(_bus == 4){
_port = &Serial4;
}
else if(_bus == 3){
_port = &Serial3;
}
else if(_bus == 2){
_port = &Serial2;
}
else{
_port = &Serial1;
}

#endif

#if defined(__MK20DX128__) || defined(__MK20DX256__) // Teensy 3.0 || Teensy 3.1/3.2
// begin the serial port for SBUS
_port->begin(100000,SERIAL_8E1_RXINV_TXINV);
SERIALPORT = _port;
_bus->begin(100000,SERIAL_8E1_RXINV_TXINV);
SERIALPORT = _bus;
#endif

#if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__) // Teensy 3.5 || Teensy 3.6 || Teensy LC
// begin the serial port for SBUS
_port->begin(100000,SERIAL_8E2_RXINV_TXINV);
_bus->begin(100000,SERIAL_8E2_RXINV_TXINV);
#endif
}

Expand Down Expand Up @@ -175,9 +135,9 @@ bool SBUS::parse(){
if(sbusTime > SBUS_TIMEOUT){_fpos = 0;}

// see if serial data is available
while(_port->available() > 0){
while(_bus->available() > 0){
sbusTime = 0;
uint8_t c = _port->read();
uint8_t c = _bus->read();

// find the header
if(_fpos == 0){
Expand Down Expand Up @@ -264,7 +224,7 @@ void SBUS::write(uint16_t* channels){

#if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__) // Teensy 3.5 || Teensy 3.6 || Teensy LC
// write packet
_port->write(packet,25);
_bus->write(packet,25);
#endif
}

Expand Down
5 changes: 2 additions & 3 deletions SBUS.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO

class SBUS{
public:
SBUS(uint8_t bus);
SBUS(HardwareSerial& bus);
void begin();
bool read(uint16_t* channels, uint8_t* failsafe, uint16_t* lostFrames);
bool readCal(float* calChannels, uint8_t* failsafe, uint16_t* lostFrames);
void write(uint16_t* channels);
private:
uint8_t _bus;
uint8_t _fpos;
const uint16_t SBUS_TIMEOUT = 10000;
const float _sbusScale = 0.00122025625f;
Expand All @@ -47,7 +46,7 @@ class SBUS{
const uint8_t _sbusFailSafe = 0x08;
static const uint8_t _payloadSize = 24;
uint8_t _payload[_payloadSize];
HardwareSerial* _port;
HardwareSerial* _bus;

bool parse();
};
Expand Down
2 changes: 1 addition & 1 deletion examples/AIN_SBUS_example/AIN_SBUS_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO

// a SBUS object, which is on Teensy hardware
// serial port 1
SBUS x8r(1);
SBUS x8r(Serial1);

// analog read values, 16 bit counts
uint16_t ain[10];
Expand Down
2 changes: 1 addition & 1 deletion examples/SBUS_example/SBUS_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO

// a SBUS object, which is on Teensy hardware
// serial port 1
SBUS x8r(1);
SBUS x8r(Serial1);

// channel, fail safe, and lost frames data
uint16_t channels[16];
Expand Down

0 comments on commit a0e7532

Please sign in to comment.