Desktop Station gateway software for mbed

Dependents:   DSGatewayMBED_Nucleo DSGatewayMBED_Nucleo_Step128

This library provides to control DCC and Marklin Motorola 2 locomtoives and turnouts via DCC/MM2 Shield. Please check our wiki site(http://desktopstation.net/wiki/).

Revision:
1:39249e22e9f5
Parent:
0:96eb8cc345dc
--- a/DSGatewayMBED.cpp	Sat Jan 17 01:48:50 2015 +0000
+++ b/DSGatewayMBED.cpp	Fri Jan 23 22:28:51 2015 +0000
@@ -1,8 +1,15 @@
+/*********************************************************************
+ * Desktop Station Gateway Library for mbed platform
+ *
+ * Copyright (C) 2015 Yaasan
+ *
+ */
+
 #include "DSGatewayMBED.h"
 #include "mbed.h"
 
 SPI spiDS(SPI_MOSI, SPI_MISO, SPI_SCK);
-DigitalOut cs(D10);
+DigitalOut cs(PB_6);
 
 DSGatewayLib::DSGatewayLib()
 {
@@ -15,9 +22,10 @@
 {
   /* Open SPI */
 
-  spiDS.format(8, 0);
-  spiDS.frequency(1000000);
-
+  /* SPI mode is default( 8bit, mode 0, 1MHz) */
+  //spiDS.format(8, 0);
+  //spiDS.frequency(1000000);
+    cs = 1;
 }
 
 
@@ -53,16 +61,15 @@
     
     for( i = 0; i < SIZE_PACKET; i++)
     {
-        aReceived[i] = spiDS.write(inPackets[i]);
-        wait_us(10);
+       aReceived[i] = reverseByte(spiDS.write(reverseByte(inPackets[i])));
     }
     
     cs = 1;
     
     
     /* 遅延 */
-    wait_ms(30);
-    
+    wait_ms(5);
+   
     /* Check for receiving */
     if((aReceived[1] & 0xF0) == CMD_OK)
     {
@@ -109,13 +116,13 @@
 }
 
 
-bool DSGatewayLib::SetPower(bool power)
+bool DSGatewayLib::SetPower(byte power)
 {
     unsigned char aPacktes[SIZE_PACKET];
 
     clearMessage(aPacktes);
 
-    if (power) {
+    if (power == 1) {
         aPacktes[0] = CMD_PWR_ON | 0x02;
         aPacktes[1] = aPacktes[0];//CRC
         poweron = true;
@@ -131,13 +138,13 @@
     return exchangeMessage(aPacktes, TIME_REPLY);
 }
 
-bool DSGatewayLib::SetPowerEx(bool power)
+bool DSGatewayLib::SetPowerEx(byte power)
 {
     unsigned char aPacktes[SIZE_PACKET];
 
     clearMessage(aPacktes);
 
-    if (power) {
+    if (power == 1) {
         aPacktes[0] = CMD_PWR_ON | 0x03;
         aPacktes[1] = 3;//Extend command (DCC128 and MM2-28step)
         aPacktes[2] = generateCRC(aPacktes, 2);
@@ -363,4 +370,18 @@
     byte bytehigh = 0;
     bytehigh = ((high >> 8) & 0xFF);
     return bytehigh;
+}
+
+unsigned char reverseByte(unsigned char inByte)
+{
+    
+    unsigned char aTemp = 0;
+    
+    for( int i = 0; i < 8; i++)
+    {
+        aTemp = aTemp | (((inByte >> i) & 0x01) << (7 - i));
+    }   
+    
+    return aTemp;
+    
 }
\ No newline at end of file