58
DyNet.cpp
@@ -0,0 +1,58 @@
+#include <Arduino.h>
+#include "DyNet.h"
+
+DyNet::DyNet() {
+
_rs485 = NULL;
+}
+
+DyNet::~DyNet () {
+
if (_rs485 != NULL) delete _rs485;
+}
+
+void DyNet::begin (int roPin, int rePin, int dePin, int diPin) {
+
_rePin = rePin;
_dePin = dePin;
pinMode (roPin, INPUT);
pinMode (rePin, OUTPUT);
pinMode (dePin, OUTPUT);
pinMode (diPin, OUTPUT);
_rs485 = new SoftwareSerial (roPin, diPin);
_rs485->begin (9600);
rxMode ();
+}
+
+void DyNet::rxMode () {
+
digitalWrite (_dePin, LOW);
digitalWrite (_rePin, LOW);
_rs485->listen();
+}
+
+void DyNet::txMode () {
+
digitalWrite (_rePin, HIGH);
digitalWrite (_dePin, HIGH);
+}
+
+void DyNet::receiveOpCode (void (*fn)(byte*)) {
+
delay (10);
byte opCode[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < 8; i++) {
if (_rs485->available ())
opCode[i] = _rs485->read ();
else
return;
delay (1);
if (checksum (opCode)) fn (opCode);
+}
+
+void DyNet::sendOpCode (byte *opCode) {
+
delay (10);
setchecksum (opCode);
txMode ();
_rs485->write (opCode, 8);
rxMode ();
+}
+
+byte DyNet::checksum (byte *opCode) { return (~(opCode[0] + opCode[1] + opCode[2]
+ opCode[3] + opCode[4] + opCode[5] + opCode[6]) + 1) & 0xFF; }
+void DyNet::setchecksum (byte *opCode) { opCode[7] = checksum (opCode); }
+bool DyNet::checkchecksum (byte *opCode) { return opCode[7] == checksum (opCode); }
DyNet.h
@@ -0,0 +1,27 @@
+#ifndef DYNET_H
+#define DYNET_H
+
+#include <Arduino.h>
+#include <SoftwareSerial.h>
+
+class DyNet {
+
+
public:
DyNet ();
~DyNet ();
int _rePin, _dePin;
void begin (int roPin, int rePin, int dePin, int diPin);
void receiveOpCode (void (*)(byte*));
void sendOpCode (byte*);
+
+
private:
SoftwareSerial* _rs485;
byte checksum (byte*);
void setchecksum (byte*);
bool checkchecksum (byte*);
void rxMode ();
void txMode ();
+
+};
+
+#endif
README
@@ -0,0 +1,39 @@
+Philips Dynalite DyNet I/O library
+---------------------------------+
+This library will use a MAX485 IC together with the SoftwareSerial library
+to send and receive Dynalite OpCodes directly onto the DyNet RS485 bus.
+
+The library requires an Aurdino with with a correctly wired MAX485 IC,
+using all 4 connections (RO, RE, DE, DI).
The DyNet bus uses 9600 baud,
+8 data bits, 1 start bit, 1 stop bit and no parity and it is strictly
+half duplex.
The functions in this library ensure half duplex by using
+the RE/DE pins.
+
+This libarary will also calculate the 8th checksum byte of OpCodes to be
+sent.
Any value in the opcode at this position is ignored and overwritten.
+Similarly, incoming data will be checked and not handed back to the calling
+function if the checksum value is wrong.
+
+
+Initialisation
+-------------+To use the library, simply create an instance of the DyNet object, and then
+call the begin function indicating which pins are allocated to each of the
+relevant pins on the MAX485.
+
+This library is built to be small and fast, and not double and triple check
+that everything has been set up correctly.
If you do NOT call begin, there's
+nothing stopping you from calling the other functions anyway and all kinds
+of unexpected things may happen.
It's a microcontroller.
+
+Functions
+--------+sendOpCode: Expects an 8 byte array that is the OpCode.
It is simply fired
+onto the serial bus after calculating the OpCode checksum.
+
+receiveOpCode: This will check the wire for awaiting incoming data that might
+be waiting in the buffer.
It'll fetch a complete OpCode provided all bytes
+are no more than 1ms apart (as per DyNet spec) and if the checksum works out,
+it'll call the supplied function with the opcode.
+function from the DDMC810 object.
For me this is the process