LT8900MiLightRadio.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * MiLightRadioPL1167_LT89000.cpp
  3. *
  4. * Created on: 31 March 2017
  5. * Author: WoodsterDK
  6. *
  7. * Very inspired by:
  8. * https://github.com/pmoscetta/authometion-milight/tree/master/Authometion-MiLight
  9. * https://bitbucket.org/robvanderveer/lt8900lib
  10. */
  11. #include "LT8900MiLightRadio.h"
  12. #include <SPI.h>
  13. /**************************************************************************/
  14. // Constructor
  15. /**************************************************************************/
  16. LT8900MiLightRadio::LT8900MiLightRadio(byte byCSPin, byte byResetPin, byte byPktFlag, const MiLightRadioConfig& config)
  17. : _config(config),
  18. _channel(0)
  19. {
  20. _csPin = byCSPin;
  21. _pin_pktflag = byPktFlag;
  22. pinMode(_pin_pktflag, INPUT);
  23. if (byResetPin > 0) // If zero then bypass hardware reset
  24. {
  25. pinMode(byResetPin, OUTPUT);
  26. digitalWrite(byResetPin, LOW);
  27. delay(200);
  28. digitalWrite(byResetPin, HIGH);
  29. delay(200);
  30. }
  31. pinMode(_csPin, OUTPUT);
  32. digitalWrite(_csPin, HIGH);
  33. SPI.begin();
  34. SPI.setDataMode(SPI_MODE1);
  35. // The following speed settings depends upon the wiring and PCB
  36. //SPI.setFrequency(8000000);
  37. SPI.setFrequency(4000000);
  38. SPI.setBitOrder(MSBFIRST);
  39. //Initialize transceiver with correct settings
  40. vInitRadioModule(config.type);
  41. delay(50);
  42. // Check if HW is connected
  43. _bConnected = bCheckRadioConnection();
  44. //Reset SPI MODE to default
  45. SPI.setDataMode(SPI_MODE0);
  46. _waiting = false;
  47. }
  48. /**************************************************************************/
  49. // Checks the connection to the radio module by verifying a register setting
  50. /**************************************************************************/
  51. bool LT8900MiLightRadio::bCheckRadioConnection(void)
  52. {
  53. bool bRetValue = false;
  54. uint16_t value_0 = uiReadRegister(0);
  55. uint16_t value_1 = uiReadRegister(1);
  56. if ((value_0 == 0x6fe0) && (value_1 == 0x5681))
  57. {
  58. #ifdef DEBUG_PRINTF
  59. Serial.println(F("Radio module running correctly..."));
  60. #endif
  61. bRetValue = true;
  62. }
  63. else
  64. {
  65. #ifdef DEBUG_PRINTF
  66. Serial.println(F("Failed initializing the radio module..."));
  67. #endif
  68. }
  69. return bRetValue;
  70. }
  71. /**************************************************************************/
  72. // Initialize radio module
  73. /**************************************************************************/
  74. void LT8900MiLightRadio::vInitRadioModule(MiLightRadioType type)
  75. {
  76. if (type == RGB_CCT)
  77. {
  78. bool bWriteDefaultDefault = true; // Is it okay to use the default power up values, without setting them
  79. regWrite16(0x00, 0x6F, 0xE0, 7); // Recommended value by PMmicro
  80. regWrite16(0x02, 0x66, 0x17, 7); // Recommended value by PMmicro
  81. regWrite16(0x04, 0x9C, 0xC9, 7); // Recommended value by PMmicro
  82. regWrite16(0x05, 0x66, 0x37, 7); // Recommended value by PMmicro
  83. regWrite16(0x07, 0x00, 0x4C, 7); // PL1167's TX/RX Enable and Channel Register, Default channel 76
  84. regWrite16(0x08, 0x6C, 0x90, 7); // Recommended value by PMmicro
  85. regWrite16(0x09, 0x48, 0x00, 7); // PA Control register
  86. regWrite16(0x0B, 0x00, 0x08, 7); // Recommended value by PMmicro
  87. regWrite16(0x0D, 0x48, 0xBD, 7); // Recommended value by PMmicro
  88. regWrite16(0x16, 0x00, 0xFF, 7); // Recommended value by PMmicro
  89. regWrite16(0x18, 0x00, 0x67, 7); // Recommended value by PMmicro
  90. regWrite16(0x1A, 0x19, 0xE0, 7); // Recommended value by PMmicro
  91. regWrite16(0x1B, 0x13, 0x00, 7); // Recommended value by PMmicro
  92. regWrite16(0x20, 0x48, 0x00, 7); // Recommended value by PMmicro
  93. regWrite16(0x21, 0x3F, 0xC7, 7); // Recommended value by PMmicro
  94. regWrite16(0x22, 0x20, 0x00, 7); // Recommended value by PMmicro
  95. regWrite16(0x23, 0x03, 0x00, 7); // Recommended value by PMmicro
  96. regWrite16(0x24, 0x72, 0x36, 7); // Sync R0
  97. regWrite16(0x27, 0x18, 0x09, 7); // Sync R3
  98. regWrite16(0x28, 0x44, 0x02, 7); // Recommended value by PMmicro
  99. regWrite16(0x29, 0xB0, 0x00, 7); // Recommended value by PMmicro
  100. regWrite16(0x2A, 0xFD, 0xB0, 7); // Recommended value by PMmicro
  101. if (bWriteDefaultDefault == true)
  102. {
  103. regWrite16(0x01, 0x56, 0x81, 7); // Recommended value by PMmicro
  104. regWrite16(0x0A, 0x7F, 0xFD, 7); // Recommended value by PMmicro
  105. regWrite16(0x0C, 0x00, 0x00, 7); // Recommended value by PMmicro
  106. regWrite16(0x17, 0x80, 0x05, 7); // Recommended value by PMmicro
  107. regWrite16(0x19, 0x16, 0x59, 7); // Recommended value by PMmicro
  108. regWrite16(0x1C, 0x18, 0x00, 7); // Recommended value by PMmicro
  109. regWrite16(0x25, 0x00, 0x00, 7); // Recommended value by PMmicro
  110. regWrite16(0x26, 0x00, 0x00, 7); // Recommended value by PMmicro
  111. regWrite16(0x2B, 0x00, 0x0F, 7); // Recommended value by PMmicro
  112. }
  113. }
  114. else if((type == RGBW) || (type == CCT) || (type == RGB) )
  115. {
  116. regWrite16(0, 111, 224, 7); // Recommended value by PMmicro
  117. regWrite16(1, 86, 129, 7); // Recommended value by PMmicro
  118. regWrite16(2, 102, 23, 7); // Recommended value by PMmicro
  119. regWrite16(4, 156, 201, 7); // Recommended value by PMmicro
  120. regWrite16(5, 102, 55, 7); // Recommended value by PMmicro
  121. regWrite16(7, 0, 76, 7); // PL1167's TX/RX Enable and Channel Register
  122. regWrite16(8, 108, 144, 7); // Recommended value by PMmicro
  123. regWrite16(9, 72, 0, 7); // PL1167's PA Control Register
  124. regWrite16(10, 127, 253, 7); // Recommended value by PMmicro
  125. regWrite16(11, 0, 8, 7); // PL1167's RSSI OFF Control Register -- ???
  126. regWrite16(12, 0, 0, 7); // Recommended value by PMmicro
  127. regWrite16(13, 72, 189, 7); // Recommended value by PMmicro
  128. regWrite16(22, 0, 255, 7); // Recommended value by PMmicro
  129. regWrite16(23, 128, 5, 7); // PL1167's VCO Calibration Enable Register
  130. regWrite16(24, 0, 103, 7); // Recommended value by PMmicro
  131. regWrite16(25, 22, 89, 7); // Recommended value by PMmicro
  132. regWrite16(26, 25, 224, 7); // Recommended value by PMmicro
  133. regWrite16(27, 19, 0, 7); // Recommended value by PMmicro
  134. regWrite16(28, 24, 0, 7); // Recommended value by PMmicro
  135. regWrite16(32, 72, 0, 7); // PL1167's Data Configure Register: LEN_PREAMBLE = 010 -> (0xAAAAAA) 3 bytes, LEN_SYNCWORD = 01 -> 32 bits, LEN_TRAILER = 000 -> (0x05) 4 bits, TYPE_PKT_DAT = 00 -> NRZ law data, TYPE_FEC = 00 -> No FEC
  136. regWrite16(33, 63, 199, 7); // PL1167's Delay Time Control Register 0
  137. regWrite16(34, 32, 0, 7); // PL1167's Delay Time Control Register 1
  138. regWrite16(35, 3, 0, 7); // PL1167's Power Management and Miscellaneous Register
  139. regWrite16(40, 68, 2, 7); // PL1167's FIFO and SYNCWORD Threshold Register
  140. regWrite16(41, 176, 0, 7); // PL1167's Miscellaneous Register: CRC_ON = 1 -> ON, SCR_ON = 0 -> OFF, EN_PACK_LEN = 1 -> ON, FW_TERM_TX = 1 -> ON, AUTO_ACK = 0 -> OFF, PKT_LEVEL = 0 -> PKT active high, CRC_INIT_DAT = 0
  141. regWrite16(42, 253, 176, 7); // PL1167's SCAN RSSI Register 0
  142. regWrite16(43, 0, 15, 7); // PL1167's SCAN RSSI Register 1
  143. delay(200);
  144. regWrite16(128, 0, 0, 7);
  145. regWrite16(129, 255, 255, 7);
  146. regWrite16(130, 0, 0, 7);
  147. regWrite16(132, 0, 0, 7);
  148. regWrite16(133, 255, 255, 7);
  149. regWrite16(135, 255, 255, 7);
  150. regWrite16(136, 0, 0, 7);
  151. regWrite16(137, 255, 255, 7);
  152. regWrite16(138, 0, 0, 7);
  153. regWrite16(139, 255, 255, 7);
  154. regWrite16(140, 0, 0, 7);
  155. regWrite16(141, 255, 255, 7);
  156. regWrite16(150, 0, 0, 7);
  157. regWrite16(151, 255, 255, 7);
  158. regWrite16(152, 0, 0, 7);
  159. regWrite16(153, 255, 255, 7);
  160. regWrite16(154, 0, 0, 7);
  161. regWrite16(155, 255, 255, 7);
  162. regWrite16(156, 0, 0, 7);
  163. regWrite16(160, 0, 0, 7);
  164. regWrite16(161, 255, 255, 7);
  165. regWrite16(162, 0, 0, 7);
  166. regWrite16(163, 255, 255, 7);
  167. regWrite16(168, 0, 0, 7);
  168. regWrite16(169, 255, 255, 7);
  169. regWrite16(170, 0, 0, 7);
  170. regWrite16(171, 255, 255, 7);
  171. regWrite16(7, 0, 0, 7); // Disable TX/RX and set radio channel to 0
  172. }
  173. }
  174. /**************************************************************************/
  175. // Set sync word
  176. /**************************************************************************/
  177. void LT8900MiLightRadio::vSetSyncWord(uint16_t syncWord3, uint16_t syncWord2, uint16_t syncWord1, uint16_t syncWord0)
  178. {
  179. uiWriteRegister(R_SYNCWORD1, syncWord0);
  180. uiWriteRegister(R_SYNCWORD2, syncWord1);
  181. uiWriteRegister(R_SYNCWORD3, syncWord1);
  182. uiWriteRegister(R_SYNCWORD4, syncWord3);
  183. }
  184. /**************************************************************************/
  185. // Low level register write with delay
  186. /**************************************************************************/
  187. void LT8900MiLightRadio::regWrite16(byte ADDR, byte V1, byte V2, byte WAIT)
  188. {
  189. digitalWrite(_csPin, LOW);
  190. SPI.transfer(ADDR);
  191. SPI.transfer(V1);
  192. SPI.transfer(V2);
  193. digitalWrite(_csPin, HIGH);
  194. delayMicroseconds(WAIT);
  195. }
  196. /**************************************************************************/
  197. // Low level register read
  198. /**************************************************************************/
  199. uint16_t LT8900MiLightRadio::uiReadRegister(uint8_t reg)
  200. {
  201. SPI.setDataMode(SPI_MODE1);
  202. digitalWrite(_csPin, LOW);
  203. SPI.transfer(REGISTER_READ | (REGISTER_MASK & reg));
  204. uint8_t high = SPI.transfer(0x00);
  205. uint8_t low = SPI.transfer(0x00);
  206. digitalWrite(_csPin, HIGH);
  207. SPI.setDataMode(SPI_MODE0);
  208. return (high << 8 | low);
  209. }
  210. /**************************************************************************/
  211. // Low level 16bit register write
  212. /**************************************************************************/
  213. uint8_t LT8900MiLightRadio::uiWriteRegister(uint8_t reg, uint16_t data)
  214. {
  215. uint8_t high = data >> 8;
  216. uint8_t low = data & 0xFF;
  217. digitalWrite(_csPin, LOW);
  218. uint8_t result = SPI.transfer(REGISTER_WRITE | (REGISTER_MASK & reg));
  219. SPI.transfer(high);
  220. SPI.transfer(low);
  221. digitalWrite(_csPin, HIGH);
  222. return result;
  223. }
  224. /**************************************************************************/
  225. // Start listening on specified channel and syncword
  226. /**************************************************************************/
  227. void LT8900MiLightRadio::vStartListening(uint uiChannelToListenTo)
  228. {
  229. _dupes_received = 0;
  230. vSetSyncWord(_config.syncword3, 0,0,_config.syncword0);
  231. //vSetChannel(uiChannelToListenTo);
  232. _channel = uiChannelToListenTo;
  233. uiWriteRegister(R_CHANNEL, _channel & CHANNEL_MASK); //turn off rx/tx
  234. delay(3);
  235. uiWriteRegister(R_FIFO_CONTROL, 0x0080); //flush rx
  236. uiWriteRegister(R_CHANNEL, (_channel & CHANNEL_MASK) | _BV(CHANNEL_RX_BIT)); //enable RX
  237. delay(5);
  238. }
  239. /**************************************************************************/
  240. // Resume listening - without changing the channel and syncword
  241. /**************************************************************************/
  242. void LT8900MiLightRadio::vResumeRX(void)
  243. {
  244. _dupes_received = 0;
  245. uiWriteRegister(R_CHANNEL, _channel & CHANNEL_MASK); //turn off rx/tx
  246. delay(3);
  247. uiWriteRegister(R_FIFO_CONTROL, 0x0080); //flush rx
  248. uiWriteRegister(R_CHANNEL, (_channel & CHANNEL_MASK) | _BV(CHANNEL_RX_BIT)); //enable RX
  249. }
  250. /**************************************************************************/
  251. // Check if data is available using the hardware pin PKT_FLAG
  252. /**************************************************************************/
  253. bool LT8900MiLightRadio::bAvailablePin()
  254. {
  255. //read the PKT_FLAG pin.
  256. if (digitalRead(_pin_pktflag) != 0)
  257. {
  258. return true;
  259. }
  260. return false;
  261. }
  262. /**************************************************************************/
  263. // Check if data is available using the PKT_FLAG state in the status register
  264. /**************************************************************************/
  265. bool LT8900MiLightRadio::bAvailableRegister()
  266. {
  267. //read the PKT_FLAG state; this can also be done with a hard wire.
  268. uint16_t value = uiReadRegister(R_STATUS);
  269. if (value & STATUS_PKT_BIT_MASK != 0)
  270. {
  271. return true;
  272. }
  273. return false;
  274. }
  275. /**************************************************************************/
  276. // Read the RX buffer
  277. /**************************************************************************/
  278. int LT8900MiLightRadio::iReadRXBuffer(uint8_t *buffer, size_t maxBuffer)
  279. {
  280. uint16_t value = uiReadRegister(R_STATUS);
  281. if (bitRead(value, STATUS_CRC_BIT) == 0)
  282. {
  283. uint16_t data = uiReadRegister(R_FIFO);
  284. uint8_t packetSize = data >> 8;
  285. if (maxBuffer < packetSize + 1)
  286. {
  287. //BUFFER TOO SMALL
  288. return -2;
  289. }
  290. uint8_t pos = 0;;
  291. buffer[pos++] = (data & 0xFF);
  292. while (pos < packetSize)
  293. {
  294. data = uiReadRegister(R_FIFO);
  295. buffer[pos++] = data >> 8;
  296. buffer[pos++] = data & 0xFF;
  297. }
  298. return packetSize;
  299. }
  300. else
  301. {
  302. //CRC error
  303. return -1;
  304. }
  305. }
  306. /**************************************************************************/
  307. // Set the active channel for the radio module
  308. /**************************************************************************/
  309. void LT8900MiLightRadio::vSetChannel(uint8_t channel)
  310. {
  311. _channel = channel;
  312. uiWriteRegister(R_CHANNEL, (_channel & CHANNEL_MASK));
  313. }
  314. /**************************************************************************/
  315. // Startup
  316. /**************************************************************************/
  317. int LT8900MiLightRadio::begin()
  318. {
  319. vSetChannel(_config.channels[0]);
  320. configure();
  321. available();
  322. return 0;
  323. }
  324. /**************************************************************************/
  325. // Configure the module according to type, and start listening
  326. /**************************************************************************/
  327. int LT8900MiLightRadio::configure()
  328. {
  329. vInitRadioModule(_config.type);
  330. vSetSyncWord(_config.syncword3, 0,0,_config.syncword0);
  331. vStartListening(_config.channels[0]);
  332. return 0;
  333. }
  334. /**************************************************************************/
  335. // Check if data is available
  336. /**************************************************************************/
  337. bool LT8900MiLightRadio::available()
  338. {
  339. _waiting = false;
  340. if(bAvailablePin())
  341. {
  342. int iStartTime = millis();
  343. int iUpdateStamp = 0;
  344. static byte byLastReceived[32];
  345. byte byaFramesSizes[20];
  346. byte byaFramesReceivedCount[20];
  347. byte byaFramesReceived[20][32];
  348. byte byFrameCounter = 0;
  349. bool bDifference = false;
  350. bool bRetVal = false;
  351. if (bAvailablePin())
  352. {
  353. unsigned long ulTimeStamp = millis();
  354. unsigned long ulElapsedTime = 0;
  355. do
  356. {
  357. if (bAvailablePin())
  358. {
  359. iUpdateStamp = millis();
  360. uint8_t buf[32];
  361. int packetSize = iReadRXBuffer(buf, 32);
  362. if (packetSize > 0)
  363. {
  364. bRetVal = true;
  365. bDifference = false;
  366. for (int iCounter = 0; iCounter < packetSize && bDifference == false; iCounter++)
  367. {
  368. if (buf[iCounter] != byLastReceived[iCounter])
  369. {
  370. bDifference = true;
  371. }
  372. }
  373. if (bDifference == true)
  374. {
  375. for (int i = 0; i < packetSize; i++)
  376. {
  377. byaFramesReceived[byFrameCounter][i] = buf[i];
  378. byLastReceived[i] = buf[i];
  379. byaFramesSizes[i] = packetSize;
  380. }
  381. byaFramesReceivedCount[byFrameCounter] = 1;
  382. byFrameCounter++;
  383. }
  384. else
  385. {
  386. byaFramesReceivedCount[byFrameCounter-1]++;
  387. }
  388. }
  389. else
  390. {
  391. if (packetSize < 0)
  392. {
  393. Serial.println(F("LT8900: Packet less than zero, buffer to small"));
  394. }
  395. else
  396. {
  397. Serial.println(F("LT8900: Packet read fail"));
  398. }
  399. }
  400. vResumeRX();
  401. }
  402. ulElapsedTime = millis();
  403. yield();
  404. ulElapsedTime = ulElapsedTime - ulTimeStamp;
  405. }while (ulElapsedTime < 1000 );
  406. #ifdef DEBUG_PRINTF
  407. printf_P(PSTR("ElapsedRX: %d\n"), iUpdateStamp - iStartTime);
  408. #endif
  409. if (byFrameCounter != 0)
  410. {
  411. #ifdef DEBUG_PRINTF
  412. printf_P(PSTR("Packets received: %d\n"), byFrameCounter);
  413. #endif
  414. for (byte byCounterFrame = 0; byCounterFrame < byFrameCounter; byCounterFrame++)
  415. {
  416. _dupes_received = byaFramesReceivedCount[byCounterFrame];
  417. #ifdef DEBUG_PRINTF
  418. printf_P(PSTR("Packet read OK, rec: %d Frame:\n"), byaFramesReceivedCount[byCounterFrame]);
  419. #endif
  420. //dump the packet.
  421. _packet[0]=byaFramesSizes[byCounterFrame];
  422. for (int iByteCounter = 0; iByteCounter < byaFramesSizes[byCounterFrame]; iByteCounter++)
  423. {
  424. byte byReceivedvalue = byaFramesReceived[byCounterFrame][iByteCounter];
  425. _packet[iByteCounter+1] = byReceivedvalue;
  426. #ifdef DEBUG_PRINTF
  427. printf_P(PSTR("%02X "), byReceivedvalue);
  428. #endif
  429. }
  430. _waiting = true;
  431. }
  432. #ifdef DEBUG_PRINTF
  433. Serial.println();
  434. #endif
  435. }
  436. }
  437. yield();
  438. }
  439. return _waiting;
  440. }
  441. int LT8900MiLightRadio::dupesReceived()
  442. {
  443. return _dupes_received;
  444. }
  445. /**************************************************************************/
  446. // Read received data from buffer to upper layer
  447. /**************************************************************************/
  448. int LT8900MiLightRadio::read(uint8_t frame[], size_t &frame_length)
  449. {
  450. if (!_waiting)
  451. {
  452. frame_length = 0;
  453. return -1;
  454. }
  455. if (frame_length > sizeof(_packet) - 1)
  456. {
  457. frame_length = sizeof(_packet) - 1;
  458. }
  459. if (frame_length > _packet[0])
  460. {
  461. frame_length = _packet[0];
  462. }
  463. memcpy(frame, _packet +1 , frame_length);
  464. _waiting = false;
  465. // Start RX mode again
  466. vResumeRX();
  467. return _packet[0];
  468. }
  469. /**************************************************************************/
  470. // Write data
  471. /**************************************************************************/
  472. int LT8900MiLightRadio::write(uint8_t frame[], size_t frame_length)
  473. {
  474. if (frame_length > sizeof(_out_packet) - 1) {
  475. return -1;
  476. }
  477. memcpy(_out_packet + 1, frame, frame_length);
  478. _out_packet[0] = frame_length;
  479. SPI.setDataMode(SPI_MODE1);
  480. int retval = resend();
  481. yield();
  482. SPI.setDataMode(SPI_MODE0);
  483. if (retval < 0) {
  484. return retval;
  485. }
  486. return frame_length;
  487. }
  488. /**************************************************************************/
  489. // Handle the transmission to regarding to freq diversity and repeats
  490. /**************************************************************************/
  491. int LT8900MiLightRadio::resend()
  492. {
  493. byte Length = _out_packet[0];
  494. for (size_t i = 0; i < MiLightRadioConfig::NUM_CHANNELS; i++)
  495. {
  496. sendPacket(_out_packet, Length, _config.channels[i]);
  497. delayMicroseconds(DEFAULT_TIME_BETWEEN_RETRANSMISSIONS_uS);
  498. }
  499. return 0;
  500. }
  501. /**************************************************************************/
  502. // The actual transmit happens here
  503. /**************************************************************************/
  504. bool LT8900MiLightRadio::sendPacket(uint8_t *data, size_t packetSize, byte byChannel)
  505. {
  506. if(_bConnected) // Must be connected to module otherwise it might lookup waiting for _pin_pktflag
  507. {
  508. if (packetSize < 1 || packetSize > 255)
  509. {
  510. return false;
  511. }
  512. uiWriteRegister(R_CHANNEL, 0x0000);
  513. uiWriteRegister(R_FIFO_CONTROL, 0x8080); //flush tx and RX
  514. digitalWrite(_csPin, LOW); // Enable PL1167 SPI transmission
  515. SPI.transfer(R_FIFO); // Start writing PL1167's FIFO Data register
  516. SPI.transfer(packetSize); // Length of data buffer: x bytes
  517. for (byte iCounter = 0; iCounter < packetSize; iCounter++)
  518. {
  519. SPI.transfer((data[1+iCounter]));
  520. }
  521. digitalWrite(_csPin, HIGH); // Disable PL1167 SPI transmission
  522. delayMicroseconds(10);
  523. uiWriteRegister(R_CHANNEL, (byChannel & CHANNEL_MASK) | _BV(CHANNEL_TX_BIT)); //enable RX
  524. //Wait until the packet is sent.
  525. while (digitalRead(_pin_pktflag) == 0)
  526. {
  527. //do nothing.
  528. }
  529. return true;
  530. }
  531. }
  532. const MiLightRadioConfig& LT8900MiLightRadio::config() {
  533. return _config;
  534. }