Input interface library for S88 decoders

Dependents:   DSGatewayMBED_Nucleo DSGatewayMBED_Nucleo_Step128

Files at this revision

API Documentation at this revision

Comitter:
yaasan
Date:
Sat Jan 31 22:14:57 2015 +0000
Commit message:
Support S88 decoders;

Changed in this revision

TrackReporterS88_DS.cpp Show annotated file Show diff for this revision Revisions of this file
TrackReporterS88_DS.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b32bb4602f54 TrackReporterS88_DS.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TrackReporterS88_DS.cpp	Sat Jan 31 22:14:57 2015 +0000
@@ -0,0 +1,99 @@
+/*********************************************************************
+ * Railuino - Hacking your Märklin
+ *
+ * Copyright (C) 2012 Joerg Pleumann
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * LICENSE file for more details.
+ */
+ 
+#include "TrackReporterS88_DS.h"
+
+// ===================================================================
+// === TrackReporterS88_DS ==============================================
+// ===================================================================
+	
+const int TIME = 50;
+
+DigitalOut do_load(PB_5);
+DigitalOut do_reset(PB_4);
+DigitalOut do_clock(PB_3);
+DigitalIn di_data(PA_0);
+
+TrackReporterS88_DS::TrackReporterS88_DS(int modules) {
+	mSize = modules;
+
+}
+
+void TrackReporterS88_DS::refresh()
+{
+	refresh(mSize);
+}
+
+void TrackReporterS88_DS::refresh(int inMaxSize)
+{
+	int myByte = 0;
+	int myBit = 0;
+
+	
+	for (int i = 0; i < sizeof(mSwitches); i++) {
+		mSwitches[i] = 0;
+	}
+
+	//digitalWrite(LOAD, HIGH);
+	do_load = 1;
+	wait_us( TIME);
+	do_clock = 1;
+	//digitalWrite(CLOCK, HIGH);
+	wait_us(TIME);
+	do_clock = 0;
+	//digitalWrite(CLOCK, LOW);
+	wait_us(TIME);
+	do_reset = 1;
+	//digitalWrite(RESET, HIGH);
+	wait_us(TIME);
+	do_reset = 0;
+	//digitalWrite(RESET, LOW);
+	wait_us(TIME);
+	//digitalWrite(LOAD, LOW);
+	do_load = 0;
+	
+	wait_us(TIME / 2);
+	bitWrite(mSwitches[myByte], myBit++, di_data);
+	wait_us(TIME / 2);
+
+	for (int i = 1; i < 16 * inMaxSize; i++) {
+		//digitalWrite(CLOCK, HIGH);
+		do_clock = 1;
+		wait_us(TIME);
+		do_clock = 0;
+		//digitalWrite(CLOCK, LOW);
+
+		wait_us(TIME / 2);
+		bitWrite(mSwitches[myByte], myBit++, di_data);
+
+		if (myBit == 8) {
+			myByte++;
+			myBit = 0;
+		}
+
+		wait_us(TIME / 2);
+	}
+	
+}
+
+boolean TrackReporterS88_DS::getValue(int index) {
+	index--;
+	return bitRead(mSwitches[index / 8], index % 8);
+}
+
+byte TrackReporterS88_DS::getByte(int index) {
+	return mSwitches[index];
+}
diff -r 000000000000 -r b32bb4602f54 TrackReporterS88_DS.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TrackReporterS88_DS.h	Sat Jan 31 22:14:57 2015 +0000
@@ -0,0 +1,93 @@
+/*********************************************************************
+ * Railuino - Hacking your Märklin
+ *
+ * Copyright (C) 2012 Joerg Pleumann
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * LICENSE file for more details.
+ */
+
+#include <mbed.h>
+
+#define bitWrite(b, c, bit)               b = bit ? (b | 1<<c) : (b & ~(1<<c))
+#define bitRead(b, c)                    ((b & (1<<c)) >>c)
+typedef unsigned char prog_uchar;
+typedef unsigned char prog_uint8_t;
+typedef unsigned int prog_uint16_t;
+typedef unsigned int prog_uint32_t;
+typedef unsigned char byte;
+typedef bool boolean;
+typedef unsigned char prog_uchar;
+typedef signed char prog_char;
+typedef signed long int word;
+
+// ===================================================================
+// === TrackReporterS88 DS ==============================================
+// ===================================================================
+
+/**
+ * Implements the S88 bus protocol for reporting the state of tracks.
+ * S88 is basically a long shift register where each bit corresponds
+ * to a single contact on the track. Flip-flops on each S88 board make
+ * sure activations are stored, so it is not necessary to query a
+ * contact at the exact time it is activated. This implementation
+ * allows a maximum of 512 bits or 32 full-width (16 bit) S88 boards.
+ * The S88 standard recommends a maximum of 30 boards, so we should be
+ * on the safe side.
+ */
+class TrackReporterS88_DS {
+
+  private:
+
+  /**
+   * The number of contacts available.
+   */
+  int mSize;
+
+  /**
+   * The most recent contact values we know.
+   */ 
+  byte mSwitches[64];
+
+  public:
+
+  /**
+   * Creates a new TrackReporter with the given number of modules
+   * being attached. While this value can be safely set to the
+   * maximum of 32, it makes sense to specify the actual number,
+   * since this speeds up reporting. The method assumes 16 bit
+   * modules. If you use 8 bit modules instead (or both) you need
+   * to do the math yourself.
+   */
+  TrackReporterS88_DS(int modules);
+  
+  /**
+   * Reads the current state of all contacts into the TrackReporter
+   * and clears the flip-flops on all S88 boards. Call this method
+   * periodically to have up-to-date values.
+   */
+  void refresh();
+  void refresh(int inMaxSize);
+
+  /**
+   * Returns the state of an individual contact. Valid index values
+   * are 1 to 512.
+   */
+  boolean getValue(int index);
+
+  /**
+   * Returns the state of contacts. Valid index values
+   * are 0 to 63.
+   */
+  
+  byte getByte(int index);
+
+
+};