Yaasan K / DSgatewayMBED

Dependents:   DSGatewayMBED_Nucleo DSGatewayMBED_Nucleo_Step128

Files at this revision

API Documentation at this revision

Comitter:
yaasan
Date:
Fri Jan 23 22:28:51 2015 +0000
Parent:
0:96eb8cc345dc
Commit message:
Worked first version

Changed in this revision

DSGatewayMBED.cpp Show annotated file Show diff for this revision Revisions of this file
DSGatewayMBED.h Show annotated file Show diff for this revision Revisions of this file
diff -r 96eb8cc345dc -r 39249e22e9f5 DSGatewayMBED.cpp
--- 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
diff -r 96eb8cc345dc -r 39249e22e9f5 DSGatewayMBED.h
--- a/DSGatewayMBED.h	Sat Jan 17 01:48:50 2015 +0000
+++ b/DSGatewayMBED.h	Fri Jan 23 22:28:51 2015 +0000
@@ -61,6 +61,7 @@
 
 byte lowByte(short int low);
 byte highByte(short int high);
+unsigned char reverseByte(unsigned char inByte);
 
 /* Defined functions */
 class DSGatewayLib
@@ -81,8 +82,8 @@
     
     bool IsPower();
     
-    bool SetPower(boolean power);
-    bool SetPowerEx(boolean power);
+    bool SetPower(byte power);
+    bool SetPowerEx(byte power);
     bool SetLocoSpeed(word address, int inSpeed);
     bool SetLocoSpeedEx(word address, int inSpeed, int inProtcol);
     bool SetLocoFunction(word address, unsigned char inFunction, unsigned char inPower);