Pārlūkot izejas kodu

Merge pull request #38 from WoodsterDK/PL1167

Speed optimisation and bug fixes
Chris Mullins 8 gadi atpakaļ
vecāks
revīzija
f15cbdd660

+ 31 - 28
lib/MiLight/LT8900MiLightRadio.cpp

@@ -38,10 +38,10 @@ LT8900MiLightRadio::LT8900MiLightRadio(byte byCSPin, byte byResetPin, byte byPkt
 
   SPI.begin();
 
-  SPI.setFrequency(1000000);
   SPI.setDataMode(SPI_MODE1);
-  //SPI.setClockDivider(SPI_CLOCK_DIV64); // UNO and 328
-  SPI.setClockDivider(SPI_CLOCK_DIV128); // MEGA and FASTER
+  // The following speed settings depends upon the wiring and PCB
+  //SPI.setFrequency(8000000);
+  SPI.setFrequency(4000000);
   SPI.setBitOrder(MSBFIRST);
 
   //Initialize transceiver with correct settings
@@ -49,7 +49,7 @@ LT8900MiLightRadio::LT8900MiLightRadio(byte byCSPin, byte byResetPin, byte byPkt
   delay(50);
 
   // Check if HW is connected
-  bCheckRadioConnection();
+  _bConnected = bCheckRadioConnection();
 
   //Reset SPI MODE to default
   SPI.setDataMode(SPI_MODE0);
@@ -64,7 +64,7 @@ LT8900MiLightRadio::LT8900MiLightRadio(byte byCSPin, byte byResetPin, byte byPkt
 /**************************************************************************/
 bool LT8900MiLightRadio::bCheckRadioConnection(void)
 {
-	int iRetValue = 0;
+	bool bRetValue = false;
 	uint16_t value_0 = uiReadRegister(0);
 	uint16_t value_1 = uiReadRegister(1);
 
@@ -73,7 +73,7 @@ bool LT8900MiLightRadio::bCheckRadioConnection(void)
     #ifdef DEBUG_PRINTF
 		  Serial.println(F("Radio module running correctly..."));
     #endif
-		iRetValue = 1;
+		bRetValue = true;
 	}
 	else
 	{
@@ -82,7 +82,7 @@ bool LT8900MiLightRadio::bCheckRadioConnection(void)
     #endif
 	}
 
-	return iRetValue;
+	return bRetValue;
 }
 
 /**************************************************************************/
@@ -595,34 +595,37 @@ int LT8900MiLightRadio::resend()
 /**************************************************************************/
 bool LT8900MiLightRadio::sendPacket(uint8_t *data, size_t packetSize, byte byChannel)
 {
-  if (packetSize < 1 || packetSize > 255)
+  if(_bConnected) // Must be connected to module otherwise it might lookup waiting for _pin_pktflag
   {
-    return false;
-  }
+    if (packetSize < 1 || packetSize > 255)
+    {
+      return false;
+    }
 
-  uiWriteRegister(R_CHANNEL, 0x0000);
-  uiWriteRegister(R_FIFO_CONTROL, 0x8080);  //flush tx and RX
+    uiWriteRegister(R_CHANNEL, 0x0000);
+    uiWriteRegister(R_FIFO_CONTROL, 0x8080);  //flush tx and RX
 
-  digitalWrite(_csPin, LOW);        // Enable PL1167 SPI transmission
-  SPI.transfer(R_FIFO);             // Start writing PL1167's FIFO Data register
-  SPI.transfer(packetSize);         // Length of data buffer: x bytes
+    digitalWrite(_csPin, LOW);        // Enable PL1167 SPI transmission
+    SPI.transfer(R_FIFO);             // Start writing PL1167's FIFO Data register
+    SPI.transfer(packetSize);         // Length of data buffer: x bytes
 
-  for (byte iCounter = 0; iCounter < packetSize; iCounter++)
-  {
-    SPI.transfer((data[1+iCounter]));
-  }
-  digitalWrite(_csPin, HIGH);  // Disable PL1167 SPI transmission
-  delayMicroseconds(10);
+    for (byte iCounter = 0; iCounter < packetSize; iCounter++)
+    {
+      SPI.transfer((data[1+iCounter]));
+    }
+    digitalWrite(_csPin, HIGH);  // Disable PL1167 SPI transmission
+    delayMicroseconds(10);
 
-  uiWriteRegister(R_CHANNEL,  (byChannel & CHANNEL_MASK) | _BV(CHANNEL_TX_BIT));   //enable RX
+    uiWriteRegister(R_CHANNEL,  (byChannel & CHANNEL_MASK) | _BV(CHANNEL_TX_BIT));   //enable RX
 
-  //Wait until the packet is sent.
-  while (digitalRead(_pin_pktflag) == 0)
-  {
-      //do nothing.
-  }
+    //Wait until the packet is sent.
+    while (digitalRead(_pin_pktflag) == 0)
+    {
+        //do nothing.
+    }
 
-  return true;
+    return true;
+  }
 }
 
 const MiLightRadioConfig& LT8900MiLightRadio::config() {

+ 6 - 9
lib/MiLight/LT8900MiLightRadio.h

@@ -1,10 +1,3 @@
-/*
- * MiLightRadio.h
- *
- *  Created on: 29 May 2015
- *      Author: henryk
- */
-
 #ifdef ARDUINO
 #include "Arduino.h"
 #else
@@ -17,6 +10,8 @@
 #include <MiLightButtons.h>
 #include <MiLightRadio.h>
 
+//#define DEBUG_PRINTF
+
 // Register defines
 #define REGISTER_READ       0b10000000  //bin
 #define REGISTER_WRITE      0b00000000  //bin
@@ -40,7 +35,8 @@
 #define R_SYNCWORD3         38
 #define R_SYNCWORD4         39
 
-#define DEFAULT_TIME_BETWEEN_RETRANSMISSIONS_uS	350
+//#define DEFAULT_TIME_BETWEEN_RETRANSMISSIONS_uS	350
+#define DEFAULT_TIME_BETWEEN_RETRANSMISSIONS_uS	0
 
 #ifndef MILIGHTRADIOPL1167_LT8900_H_
 #define MILIGHTRADIOPL1167_LT8900_H_
@@ -78,9 +74,10 @@ class LT8900MiLightRadio : public MiLightRadio {
 
     byte _pin_pktflag;
     byte _csPin;
+    bool _bConnected;
 
     const MiLightRadioConfig& _config;
-    //uint32_t _prev_packet_id;
+
     uint8_t _channel;
     uint8_t _packet[10];
     uint8_t _out_packet[10];

+ 1 - 1
lib/MiLight/MiLightClient.h

@@ -7,7 +7,7 @@
 #ifndef _MILIGHTCLIENT_H
 #define _MILIGHTCLIENT_H
 
-// #define DEBUG_PRINTF
+//#define DEBUG_PRINTF
 
 #define MILIGHT_DEFAULT_RESEND_COUNT 10
 

+ 1 - 1
lib/MiLight/MiLightRadioFactory.cpp

@@ -6,7 +6,7 @@ MiLightRadioFactory* MiLightRadioFactory::fromSettings(const Settings& settings)
       return new NRF24Factory(settings.csnPin, settings.cePin);
 
     case LT8900:
-      return new LT8900Factory(settings.csnPin, settings.resetPin, settings.cePin);
+      return new LT8900Factory(settings.cePin, settings.resetPin, settings.csnPin);
 
     default:
       return NULL;