Skip to content

Commit

Permalink
update with some improvments:
Browse files Browse the repository at this point in the history
- check to not fill up the commands stack (and the memory): I can pile up max 100 commands
- try to resync when I get a XOR error message (mostly when the arduino lost its way)
- bugfix my TCO table array
- update schema: my command GPIO is 7, not 38 now
  • Loading branch information
nzin committed Oct 7, 2013
1 parent ca24adb commit 1eb512c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README
Expand Up @@ -12,7 +12,7 @@ So you need to take HardwareSerial.h and HardwareSerial.cpp and replace these in

Wiring
======
You will need a Max485cpa chip, and pay attention to the wiring image provided. I'm a bit confused on the RJ12 where is the GND and where is the VCC.
You will need a Max485cpa chip, and pay attention to the wiring image provided. I'm a bit confused on the RJ12 where is the GND and where is the VCC. Also, there is a sense between A and B. If not in the correct position, my program don't decode it properly (I guess bits arrived in the reverse state, i.e. 1 is seen as 0 and 0 is seen as 1). In brief: I don't have code a "auto-reverse".

Code
====
Expand Down
Binary file modified arduino.wiring.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion arduino.wiring.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion arduinoXpressnet/XpressCommand.cpp
Expand Up @@ -34,7 +34,7 @@ void XpressCommand::writeData(int commandPin,HardwareSerial&serial) {

// wait flush
delayMicroseconds(220*(_position+1));

digitalWrite(commandPin,LOW);

if (_debug) {
Expand All @@ -49,11 +49,16 @@ void XpressCommand::writeData(int commandPin,HardwareSerial&serial) {


void XpressCommand::pushStackCommand(XpressCommand *&link, XpressCommand *newCommand) {
int count=0;
if (link==NULL) {
link=newCommand;
return;
}
while (link->_next!=NULL) {
count++;
if (count>100) {
// drop it, we have too much commands!
}
link=link->_next;
}
link->_next=newCommand;
Expand Down
26 changes: 24 additions & 2 deletions arduinoXpressnet/arduinoXpressnet.ino
Expand Up @@ -7,7 +7,7 @@

void sendSwitchCommand(int address,int up, int activate);
/********************* TCO state *******************/
// pin id, turnout id, isLeftOn1, currentState
// pin id, turnout id, isLeftOn1 (i.e. reverse the command state), currentState
int button[][4]={
{31, 37, LOW, LOW},
{32, 38, HIGH,LOW},
Expand All @@ -31,17 +31,22 @@ void initiateTCOState() {

void checkTCOState() {
int state;
for (int i=0;i<sizeof(button)/(3*sizeof(int));i++) {
for (int i=0;i<sizeof(button)/(4*sizeof(int));i++) {
state=digitalRead(button[i][0]);
// state changed
if (state!=button[i][3]) {
Serial.print("state (");
Serial.print(state);
Serial.print(") changed for button");
Serial.println(button[i][1]);
if (state==button[i][2]) {
sendSwitchCommand(button[i][1],0,1);
sendSwitchCommand(button[i][1],0,0);
} else {
sendSwitchCommand(button[i][1],1,1);
sendSwitchCommand(button[i][1],1,0);
}
button[i][3]=state;
}
}
}
Expand All @@ -51,6 +56,7 @@ void checkTCOState() {
#define GIO_RXTX 7

// first stage decoding
#define OPERATION_RESYNC -1
#define OPERATION_NONE 0
#define OPERATION_NORMAL_INQUIRY 1
#define OPERATION_REQUEST_ACKNOWLEDGEbMENT 2
Expand Down Expand Up @@ -300,6 +306,19 @@ void poolEvent() {
int decodeXpressnet(int data) {
int address;

// after a XOR problem
if (currentOperation==OPERATION_RESYNC) {
address=data&0x1f;
if ( ! (((data & 0x60) == 0x40) && (address==MYADDRESS))) { // P10AAAAA
Serial.print("state=");
Serial.print(currentOperation);
Serial.print(" data=");
Serial.println(data,HEX);
return FALSE;
}
currentOperation=OPERATION_NONE;
}

// other xpress device?
if (data<=255 && currentOperation==OPERATION_NONE) {
Serial.println();
Expand Down Expand Up @@ -372,6 +391,9 @@ int decodeXpressnet(int data) {
} else { //XOR
if (xorCheck!=(data&0xff)) {
Serial.println("incorrect XOR byte");
currentOperation=OPERATION_RESYNC;
dataPosition=-1;
return FALSE;
}
//treat
Serial.print("header=");
Expand Down

0 comments on commit 1eb512c

Please sign in to comment.