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

added support for SoftwareSerial. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions ICSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void _ICSC::begin(unsigned char station, unsigned long baud, HardwareSerial *sde
begin(station, baud, sdev, -1);
}


#if defined(_USE_USB_FOR_SERIAL_)
void _ICSC::begin(unsigned char station, unsigned long baud, USBSerial *sdev)
{
Expand All @@ -80,6 +81,7 @@ void _ICSC::begin(unsigned char station, unsigned long baud, USBSerial *sdev)
void _ICSC::begin(unsigned char station, unsigned long baud, USBSerial *sdev, int dePin)
{
_hserial = NULL;
_sserial = NULL;
_userial = sdev;
_userial->begin(baud);
_station = station;
Expand Down Expand Up @@ -107,9 +109,39 @@ void _ICSC::begin(unsigned char station, unsigned long baud, USBSerial *sdev, in
}
#endif

void _ICSC::begin(unsigned char station, unsigned long baud, SoftwareSerial *sdev, int dePin) {
_userial = NULL;
_hserial = NULL;
_sserial = sdev;
_sserial->begin(baud);
_station = station;
_dePin = dePin;
_baud = baud;

#ifndef ICSC_NO_STATS
_stats.oob_bytes = 0;
_stats.tx_packets = 0;
_stats.tx_bytes = 0;
_stats.rx_packets = 0;
_stats.rx_bytes = 0;
_stats.cs_errors = 0;
_stats.cb_run = 0;
_stats.cb_bad = 0;
#endif

// Reset the state machine
reset();
if(_dePin != -1) {
pinMode(_dePin, OUTPUT);
digitalWrite(_dePin, LOW);
}

}

void _ICSC::begin(unsigned char station, unsigned long baud, HardwareSerial *sdev, int dePin)
{
_userial = NULL;
_sserial = NULL;
_hserial = sdev;
_hserial->begin(baud);
_station = station;
Expand Down Expand Up @@ -141,6 +173,7 @@ void _ICSC::serialFlush()
if (_hserial) {
_hserial->flush();
}

}

void _ICSC::serialWrite(unsigned char b)
Expand All @@ -149,6 +182,10 @@ void _ICSC::serialWrite(unsigned char b)
_hserial->write(b);
}

if (_sserial) {
_sserial->write(b);
}

#if defined(_USE_USB_FOR_SERIAL_)
if (_userial) {
_userial->write(b);
Expand All @@ -162,6 +199,10 @@ int _ICSC::serialRead()
return _hserial->read();
}

if(_sserial) {
return _sserial->read();
}

#if defined(_USE_USB_FOR_SERIAL_)
if (_userial) {
return _userial->read();
Expand All @@ -175,6 +216,10 @@ int _ICSC::serialAvailable()
return _hserial->available();
}

if(_sserial) {
return _sserial->available();
}

#if defined(_USE_USB_FOR_SERIAL_)
if (_userial) {
return _userial->available();
Expand Down
7 changes: 7 additions & 0 deletions ICSC.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <WProgram.h>
#endif

#include <SoftwareSerial.h>

// Uncomment the definition of ICSC_DYNAMIC if you want to use
// dynamic memory allocation
//#define ICSC_DYNAMIC
Expand Down Expand Up @@ -160,6 +162,8 @@ class _ICSC {

// Serial device in use
HardwareSerial *_hserial;
SoftwareSerial *_sserial;

#if defined(_USE_USB_FOR_SERIAL_)
USBSerial *_userial;
#else
Expand Down Expand Up @@ -195,6 +199,9 @@ class _ICSC {
#endif
void begin(unsigned char station, unsigned long baud, HardwareSerial *sdev);
void begin(unsigned char station, unsigned long baud, HardwareSerial *sdev, int dePin);

void begin(unsigned char station, unsigned long baud, SoftwareSerial *sdev, int dePin);

boolean send(unsigned char origin,unsigned char station, char command, unsigned char len=0, char *data=NULL);
boolean send(unsigned char station, char command, unsigned char len=0, char *data=NULL);
boolean send(unsigned char station, char command,char *str);
Expand Down
41 changes: 41 additions & 0 deletions examples/SoftwareSerialReceiver/SoftwareSerialReceiver.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <ICSC.h>
#include <SoftwareSerial.h>


SoftwareSerial mySerial(6, 5);
#define DE_PIN A1
#define NODE_ID 2




void setup() {
Serial.begin(57600);
// put your setup code here, to run once:
ICSC.begin(NODE_ID, 57600, &mySerial, DE_PIN);
ICSC.registerCommand('D', &demo);
}

void demo(unsigned char src, char command, unsigned char len, char *data)
{
Serial.print(millis());
Serial.print(": ");
Serial.print(src);
Serial.print(";");
Serial.print(command);
Serial.print(";");
Serial.print(len);
Serial.print(";");
Serial.println(data);

}

long last_sent = 0;


void loop() {
// put your main code here, to run repeatedly:
ICSC.process();


}
52 changes: 52 additions & 0 deletions examples/SoftwareSerialSender/SoftwareSerialSender.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <ICSC.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 6); // RX, TX
#define DE_PIN 3
// uncomment to be slave
#define MASTER
#define NODE_ID 1




void setup() {
Serial.begin(57600);
// put your setup code here, to run once:
ICSC.begin(NODE_ID, 57600, &mySerial, DE_PIN);
ICSC.registerCommand('D', &demo);
}

void demo(unsigned char src, char command, unsigned char len, char *data)
{
Serial.print(millis());
Serial.print(": ");
Serial.print(src);
Serial.print(";");
Serial.print(command);
Serial.print(";");
Serial.print(len);
Serial.print(";");
Serial.println(data);

}

unsigned long last_sent = 0;

void loop() {
// put your main code here, to run repeatedly:
ICSC.process();


// send D message every second
if ((millis() - last_sent) > 1000) {
char b[10];
sprintf(b, "%ld", last_sent);
Serial.print("Sending ");
Serial.println(b);
ICSC.send(2, 'D', 10, b);
last_sent = millis();
}


}