Girs.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. Copyright (C) 2014,2015,2017 Bengt Martensson.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at
  6. your option) any later version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program. If not, see http://www.gnu.org/licenses/.
  13. */
  14. #include "config.h"
  15. #include "GirsLib/LedLcdManager.h"
  16. #include "GirsLib/GirsUtils.h"
  17. #ifdef ARDUINO
  18. #include <avr/pgmspace.h>
  19. #else
  20. #define PROGMEM
  21. #endif
  22. // Conditional includes
  23. #if defined(ETHERNET) & !defined(ARDUINO)
  24. #error not supported
  25. #endif
  26. #ifdef ETHERNET
  27. #include <Ethernet.h>
  28. #include <IPAddress.h>
  29. #endif // ETHERNET
  30. #ifdef LCD
  31. #include <LiquidCrystal_I2C.h>
  32. #endif
  33. #ifdef RECEIVE
  34. #include <IrReceiverSampler.h>
  35. #endif
  36. #ifdef TRANSMIT
  37. #include <IrSenderPwm.h>
  38. #endif
  39. #if defined(RECEIVE)
  40. #include <IrReceiverSampler.h>
  41. #endif
  42. #ifdef CAPTURE
  43. #include <IrWidgetAggregating.h>
  44. #endif
  45. #ifdef NON_MOD
  46. #include <IrSenderNonMod.h>
  47. #endif
  48. #ifdef DECODER
  49. #ifndef RECEIVE
  50. #error DECODER without RECEIVE is nonsensical, aborting.
  51. #endif
  52. #include <MultiDecoder.h>
  53. #endif
  54. #ifdef RENDERER
  55. #ifndef TRANSMIT
  56. #error RENDER without TRANSMIT is nonsensical, aborting.
  57. #endif
  58. #include <IrSignal.h>
  59. #include <Nec1Renderer.h>
  60. #include <Rc5Renderer.h>
  61. #endif
  62. #ifdef NAMED_COMMANDS
  63. #error NAMED_COMMANDS is presently not supported
  64. #endif
  65. #ifdef BEACON
  66. #include <Beacon.h>
  67. #endif
  68. #ifdef PRONTO
  69. #include <Pronto.h>
  70. #endif
  71. #ifdef ARDUINO
  72. #else
  73. #include <string.h> // for strlen
  74. #endif
  75. #if defined(CONFIGURABLE_LEDS) & (! defined(PARAMETERS) | !defined(LED))
  76. #error CONFIGURABLE_LEDS defined but not PARAMETERS and LED, aborting.
  77. #endif
  78. #ifdef CONFIGURABLE_LEDS
  79. #define LED_PARAMETER_CONST
  80. #else
  81. #define LED_PARAMETER_CONST const
  82. #endif
  83. #ifdef PARAMETERS
  84. #define PARAMETERS_NAME Parameters
  85. #define PARAMETER_CONST
  86. #else
  87. #define PARAMETERS_NAME
  88. #define PARAMETER_CONST const
  89. #endif
  90. #ifdef LED
  91. #ifdef TRANSMITLED
  92. static LED_PARAMETER_CONST led_t transmitled = TRANSMITLED;
  93. #endif
  94. #ifdef RECEIVELED
  95. static LED_PARAMETER_CONST led_t receiveled = RECEIVELED;
  96. #endif
  97. #ifdef CAPTURELED
  98. static LED_PARAMETER_CONST led_t captureled = CAPTURELED;
  99. #endif
  100. #ifdef COMMANDLED
  101. static LED_PARAMETER_CONST led_t commandled = COMMANDLED;
  102. #endif
  103. #endif // LED
  104. #ifdef CAPTURE
  105. static PARAMETER_CONST unsigned long captureEndingTimeout = DEFAULT_CAPTURE_ENDINGTIMEOUT; // milliseconds
  106. static const unsigned sensorNo = 1; // presently, only one supported
  107. #endif
  108. #ifdef RECEIVE
  109. static PARAMETER_CONST unsigned long receiveEndingTimeout = DEFAULT_RECEIVE_ENDINGTIMEOUT; // milliseconds
  110. #endif
  111. #if defined(RECEIVE) | defined(CAPTURE)
  112. static PARAMETER_CONST unsigned long beginTimeout = DEFAULT_BEGINTIMEOUT; // milliseconds
  113. static PARAMETER_CONST uint16_t captureSize = DEFAULT_CAPTURESIZE;
  114. #endif
  115. #ifdef RECEIVE
  116. #ifdef IRRECEIVER_2_PIN
  117. static PARAMETER_CONST uint8_t receiverNo = 1;
  118. #else
  119. static const uint8_t receiverNo = 1;
  120. #endif
  121. #endif
  122. #ifdef ETHERNET
  123. EthernetServer server(PORT);
  124. #endif // ETHERNET
  125. #ifdef RESET
  126. bool reset = false;
  127. #endif
  128. #define modulesSupported EXPAND_AND_QUOTE(Base TRANSMIT_NAME CAPTURE_NAME RENDERER_NAME RECEIVE_NAME DECODER_NAME LED_NAME LCD_NAME PARAMETERS_NAME NAMED_COMMANDS_NAME PRONTO_NAME)
  129. #ifndef PROGNAME
  130. #define PROGNAME "AGirs"
  131. #endif
  132. #ifndef VERSION
  133. #include "GirsLib/version.h"
  134. #endif // VERSION
  135. #define okString "OK"
  136. #define errorString "ERROR"
  137. #define timeoutString "."
  138. #ifdef TRANSMIT
  139. static bool sendIrSignal(const IrSignal &irSignal, unsigned int noSends=1) {
  140. if (noSends == 0)
  141. return false;
  142. #ifdef TRANSMITLED
  143. LedLcdManager::setLogicLed(transmitled, LedLcdManager::on);
  144. #endif
  145. IrSender *irSender =
  146. #ifdef NON_MOD
  147. (irSignal.getFrequency() == 0) ? (IrSender*) new IrSenderNonMod(NON_MOD_PIN) :
  148. #endif
  149. (IrSender*) IrSenderPwm::getInstance(true);
  150. irSender->sendIrSignal(irSignal, noSends);
  151. #ifdef NON_MOD
  152. if (irSignal.getFrequency() == 0)
  153. delete irSender;
  154. else
  155. #endif
  156. IrSenderPwm::deleteInstance();
  157. #ifdef TRANSMITLED
  158. LedLcdManager::setLogicLed(transmitled, LedLcdManager::off);
  159. #endif
  160. return true;
  161. }
  162. #endif // TRANSMIT
  163. # if defined(RECEIVE) | defined(CAPTURE)
  164. static void flushIn(Stream &stream UNUSED) {
  165. #ifdef ARDUINO
  166. while (stream.available())
  167. stream.read();
  168. #endif
  169. }
  170. #endif
  171. #ifdef RECEIVE
  172. static void decodeOrDump(IrReader *irReader, Stream& stream) {
  173. #ifdef DECODER
  174. MultiDecoder multiDecoder(*irReader);
  175. #ifdef LCD
  176. if (multiDecoder.getType() > MultiDecoder::noise) {
  177. LedLcdManager::lcdPrint(multiDecoder.getType() == MultiDecoder::nec_ditto
  178. ? "." : multiDecoder.getDecode(),
  179. multiDecoder.getType() != MultiDecoder::nec_ditto);
  180. if (multiDecoder.getType() == MultiDecoder::nec)
  181. LedLcdManager::lcdSetCursor(0, 1); // prepare for dittos
  182. }
  183. #endif
  184. #ifdef DECODELED
  185. LedLcdManager::setLogicLed(DECODELED(multiDecoder.getType()), LedLcdManager::blink);
  186. #endif
  187. #endif
  188. #if defined(DECODER) & ! defined(DONT_REPORT_DECODES) // lircd does its own decoding
  189. switch (multiDecoder.getType()) {
  190. case MultiDecoder::noise:
  191. // ignore
  192. break;
  193. case MultiDecoder::undecoded:
  194. irReader->dump(stream); // report data of undecoded signals
  195. break;
  196. default:
  197. stream.println(multiDecoder.getDecode()); // also for timeout
  198. break;
  199. }
  200. #else // ! (defined(DECODER) & ! defined(DONT_REPORT_DECODES))
  201. if (irReader->isEmpty())
  202. stream.println(F(timeoutString));
  203. else
  204. irReader->dump(stream);
  205. #endif // !DECODER
  206. }
  207. static bool receive(Stream& stream) {
  208. IrReceiverSampler *irReceiver = IrReceiverSampler::getInstance();
  209. if (irReceiver == NULL)
  210. irReceiver = IrReceiverSampler::newIrReceiverSampler(captureSize,
  211. GirsUtils::receiverPin(receiverNo), GirsUtils::receiverPullup(receiverNo));
  212. if (irReceiver == NULL)
  213. return false;
  214. irReceiver->setEndingTimeout(receiveEndingTimeout);
  215. irReceiver->setBeginningTimeout(beginTimeout);
  216. irReceiver->setMarkExcess(IRRECEIVER_MARK_EXCESS);
  217. flushIn(stream);
  218. #ifdef RECEIVELED
  219. LedLcdManager::setLogicLed(receiveled, LedLcdManager::on);
  220. #endif
  221. irReceiver->enable();
  222. #ifdef ARDUINO
  223. while (!irReceiver->isReady() && stream.available() == 0)
  224. #else
  225. std::cout << "** Simulating timeout **" << std::endl;
  226. delay(beginTimeout);
  227. #endif
  228. LedLcdManager::checkTurnoff();
  229. bool ready = irReceiver->isReady();
  230. irReceiver->disable();
  231. #ifdef RECEIVELED
  232. LedLcdManager::setLogicLed(receiveled, LedLcdManager::off);
  233. #endif
  234. if (ready)
  235. decodeOrDump(irReceiver, stream);
  236. IrReceiverSampler::deleteInstance();
  237. return true;
  238. }
  239. #endif // RECEIVE
  240. #ifdef CAPTURE
  241. static bool capture(Stream& stream) {
  242. IrWidget *irWidget = IrWidgetAggregating::newIrWidgetAggregating(captureSize,
  243. GirsUtils::sensorPullup(sensorNo));
  244. if (irWidget == NULL)
  245. stream.println(F("This cannot happen"));
  246. irWidget->setEndingTimeout(captureEndingTimeout);
  247. irWidget->setBeginningTimeout(beginTimeout);
  248. irWidget->setMarkExcess(IRSENSOR_MARK_EXCESS);
  249. irWidget->reset();
  250. #ifdef CAPTURELED
  251. LedLcdManager::setLogicLed(captureled, LedLcdManager::on);
  252. #endif
  253. flushIn(stream);
  254. #ifdef ARDUINO
  255. irWidget->capture();
  256. #else
  257. std::cout << "** Simulating timeout **" << std::endl;
  258. delay(beginTimeout);
  259. #endif
  260. #ifdef CAPTURELED
  261. LedLcdManager::setLogicLed(captureled, LedLcdManager::off);
  262. #endif
  263. if (!irWidget->isEmpty()) {
  264. // Trying to decode the capture does not make sense,
  265. // that is what "receive" is for.
  266. irWidget->dump(stream);
  267. } else
  268. stream.println(F(timeoutString));
  269. IrWidgetAggregating::deleteInstance();
  270. return true;
  271. }
  272. #endif // CAPTURE
  273. #ifdef NAMED_COMMANDS
  274. // Defines a const IrNamedRemoteSet remoteSet with commands to be used.
  275. //#include "my_named_remotes.inc"
  276. extern const IrNamedRemoteSet remoteSet;
  277. static bool sendNamedCommand(Stream& stream, String& remoteName, String& commandName, unsigned int noSends) {
  278. const IrNamedRemote* remote = remoteSet.getIrNamedRemote(remoteName.c_str());
  279. if (remote == NULL) {
  280. stream.println(F("No such remote"));
  281. return false;
  282. }
  283. const IrNamedCommand* command = remote->getIrNamedCommand(commandName.c_str());
  284. if (command == NULL) {
  285. stream.println(F("No such command"));
  286. return false;
  287. }
  288. const IrSignal *irSignal = command->getIrSignal();
  289. bool status = sendIrSignal(*irSignal, noSends); // waits, blinks
  290. delete irSignal;
  291. return status;
  292. }
  293. static void dumpRemote(Stream& stream, String& name) {
  294. if (name.length() == 0) {
  295. for (unsigned int i = 0; i < remoteSet.getNoIrNamedRemotes(); i++) {
  296. stream.print(remoteSet.getIrNamedRemotes()[i]->getName());
  297. stream.print(" ");
  298. }
  299. stream.println();
  300. } else {
  301. const IrNamedRemote* remote = remoteSet.getIrNamedRemote(name.c_str());
  302. if (remote == NULL)
  303. stream.println(F("No such remote"));
  304. else {
  305. for (unsigned int i = 0; i < remote->getNoCommands(); i++) {
  306. stream.print(remote->getAllCommands()[i]->getName());
  307. stream.print(" ");
  308. }
  309. stream.println();
  310. }
  311. }
  312. }
  313. #endif
  314. void setup() {
  315. LedLcdManager::setupLedGroundPins();
  316. GirsUtils::setupReceivers();
  317. GirsUtils::setupSensors();
  318. #if defined(TRANSMIT)
  319. // Make sure that sender is quiet (if reset or such)
  320. IrSenderPwm::getInstance(true)->mute();
  321. #endif
  322. LedLcdManager::setup(LCD_I2C_ADDRESS, LCD_WIDTH, LCD_HEIGHT,
  323. (const pin_t[]) {SIGNAL_LED_1, SIGNAL_LED_2, SIGNAL_LED_3, SIGNAL_LED_4,
  324. SIGNAL_LED_5, SIGNAL_LED_6, SIGNAL_LED_7, SIGNAL_LED_8 });
  325. LedLcdManager::selfTest(PROGNAME "\n" VERSION);
  326. #ifdef LED
  327. LedLcdManager::setupShouldTimeout(transmitled, false);
  328. LedLcdManager::setupShouldTimeout(receiveled, false);
  329. LedLcdManager::setupShouldTimeout(captureled, false);
  330. LedLcdManager::setupShouldTimeout(commandled, false);
  331. #endif
  332. #ifdef LCD
  333. #ifdef ETHERNET
  334. LedLcdManager::lcdPrint(F("TCP"), false, 0, 2);
  335. #ifdef SERIAL_DEBUG
  336. LedLcdManager::lcdPrint(F(",SerialDbg"), false);
  337. #endif
  338. #else // ! ETHERNET
  339. LedLcdManager::lcdPrint(F("Serial"), false, 0, 2);
  340. #endif // ! ETHERNET
  341. #endif // LCD
  342. #ifdef ETHERNET
  343. #ifdef SDCARD_ON_ETHERSHIELD_PIN
  344. // disable the SD card, as recommended in the doc
  345. pinMode(SDCARD_ON_ETHERSHIELD_PIN, OUTPUT);
  346. digitalWrite(SDCARD_ON_ETHERSHIELD_PIN, LOW);
  347. #endif
  348. byte mac[] = { MACADDRESS };
  349. #ifdef DHCP
  350. Ethernet.begin(mac);
  351. #else // !DHCP
  352. Ethernet.begin(mac, IPAddress(IPADDRESS), IPAddress(DNSSERVER), IPAddress(GATEWAY), IPAddress(SUBNETMASK));
  353. #endif // !DHCP
  354. String ipstring = GirsUtils::ip2string(Ethernet.localIP());
  355. LedLcdManager::lcdPrint(ipstring, false, 0, 3);
  356. #ifdef BEACON
  357. Beacon::setup(PROGNAME, "DE-AD-BE-EF-FE-ED", "Utility", "www.harctoolbox.org",
  358. "", "", "", "http://arduino/nosuchfile.html");
  359. #endif
  360. server.begin();
  361. #endif // ETHERNET
  362. #if defined(ARDUINO) & !defined(ETHERNET) | defined(SERIAL_DEBUG)
  363. Serial.begin(SERIALBAUD);
  364. #if defined(ARDUINO_AVR_LEONARDO) | defined(ARDUINO_AVR_MICRO)
  365. while (!Serial)
  366. ; // wait for serial port to connect. "Needed for Leonardo only"
  367. #endif
  368. Serial.println(F(PROGNAME " " VERSION));
  369. Serial.setTimeout(SERIALTIMEOUT);
  370. #ifdef ETHERNET
  371. Serial.println(Ethernet.localIP());
  372. #endif
  373. #endif // defined(ARDUINO) & !defined(ETHERNET) | defined(SERIAL_DEBUG)
  374. }
  375. #ifdef INFO
  376. void info(Stream& stream) {
  377. stream.print("Board: ");
  378. #ifdef ARDUINO_AVR_MEGA2560
  379. stream.print(F("Arduino Mega2560"));
  380. #elif defined(ARDUINO_AVR_NANO)
  381. stream.print(F("Arduino Nano"));
  382. #elif defined(ARDUINO_AVR_LEONARDO)
  383. stream.print(F("Arduino Leonardo"));
  384. #elif defined(ARDUINO_AVR_MINI)
  385. stream.print(F("Arduino Leonardo"));
  386. #elif defined(ARDUINO_AVR_UNO)
  387. stream.print(F("Arduino Uno"));
  388. #else
  389. stream.print(F("Unknown"));
  390. #endif
  391. stream.print(F(", CPU frequency: " EXPAND_AND_QUOTE(F_CPU)));
  392. stream.println();
  393. }
  394. #endif
  395. static inline bool isPrefix(const String& cmd, const char *string) {
  396. return strncmp(cmd.c_str(), string, cmd.length()) == 0;
  397. }
  398. static inline bool isPrefix(const char *string, const String& cmd) {
  399. return strncmp(cmd.c_str(), string, strlen(string)) == 0;
  400. }
  401. #ifdef ARDUINO
  402. bool isPrefix(const String& cmd, const __FlashStringHelper *pstring) {
  403. return strncmp_PF(cmd.c_str(), (uint_farptr_t) pstring, cmd.length()) == 0;
  404. }
  405. bool isPrefix(const __FlashStringHelper *pstring, const String& cmd) {
  406. return strncmp_PF(cmd.c_str(), (uint_farptr_t) pstring, strlen_PF((uint_farptr_t) pstring)) == 0;
  407. }
  408. #endif
  409. static String readCommand(Stream& stream) {
  410. #if defined(COMMANDLED) & defined(LED)
  411. LedLcdManager::setLogicLed(commandled, LedLcdManager::on);
  412. #endif
  413. #ifdef ARDUINO
  414. //flushIn(stream);
  415. while (stream.available() == 0) {
  416. LedLcdManager::checkTurnoff();
  417. }
  418. String line = stream.readStringUntil(EOLCHAR);
  419. line.trim();
  420. #else
  421. (void) stream;
  422. LedLcdManager::checkTurnoff();
  423. static char str[1000];
  424. std::cin.getline(str, 1000);
  425. if (std::cin.eof()) {
  426. std::cout << "Bye!" << std::endl;
  427. exit(0);
  428. }
  429. char *s = str;
  430. while (isspace(*s))
  431. s++;
  432. while (isspace(s[strlen(s)-1]))
  433. s[strlen(s)-1] = '\0';
  434. String line(s);
  435. #endif
  436. #if defined(DEBUG_CMD)
  437. LedLcdManager::lcdPrint(line, true, 0, 0);
  438. #endif
  439. #if defined(COMMANDLED) & defined(LED)
  440. LedLcdManager::setLogicLed(commandled, LedLcdManager::off);
  441. #endif
  442. return line;
  443. }
  444. static bool processCommand(const String& line, Stream& stream) {
  445. #ifdef ETHERNET
  446. bool quit = false;
  447. #endif
  448. Tokenizer tokenizer(line);
  449. String cmd = tokenizer.getToken();
  450. // Decode command
  451. if (cmd.length() == 0) {
  452. // empty command, do nothing
  453. stream.println(F(okString));
  454. } else
  455. #ifdef CAPTURE
  456. if (cmd[0] == 'a' || cmd[0] == 'c') {
  457. capture(stream);
  458. } else
  459. #endif // CAPTURE
  460. #ifdef LCD
  461. if (isPrefix(cmd, F("lcd"))) { //LCD
  462. String rest = tokenizer.getRest();
  463. LedLcdManager::lcdPrint(rest);
  464. stream.println(F(okString));
  465. } else
  466. #endif // LCD
  467. #ifdef LED
  468. if (isPrefix(cmd, F("led"))) {
  469. pin_t no = (pin_t) tokenizer.getInt();
  470. String value = tokenizer.getToken();
  471. bool success = LedLcdManager::setLogicLed(no, value.c_str());
  472. stream.println(success ? F(okString) : F(errorString));
  473. } else
  474. #endif // LED
  475. if (isPrefix(cmd, F("modules"))) {
  476. stream.println(F(modulesSupported));
  477. } else
  478. #ifdef FREEMEM
  479. if (isPrefix(cmd, F("memory"))) {
  480. stream.println(GirsUtils::freeRam());
  481. } else
  482. #endif
  483. #ifdef INFO
  484. if (isPrefix(cmd, F("info"))) {
  485. info(stream);
  486. } else
  487. #endif
  488. #ifdef PARAMETERS
  489. if (cmd[0] == 'p') { // parameter
  490. String variableName = tokenizer.getToken();
  491. long value = tokenizer.getInt();
  492. unsigned long *variable32 = NULL;
  493. uint16_t *variable16 = NULL;
  494. uint8_t *variable8 = NULL;
  495. #if defined(RECEIVE) || defined(CAPTURE)
  496. if (isPrefix(F("beg"), variableName))
  497. variable32 = &beginTimeout;
  498. else
  499. #endif
  500. #ifdef CAPTURE
  501. if (isPrefix(F("capturee"), variableName))
  502. variable32 = &captureEndingTimeout;
  503. #endif
  504. #ifdef RECEIVE
  505. if (isPrefix(F("receivee"), variableName))
  506. variable32 = &receiveEndingTimeout;
  507. else
  508. #endif
  509. #if defined(RECEIVE) & defined(IRRECEIVER_2_PIN)
  510. if (isPrefix(variableName, F("receiver")))
  511. variable8 = &receiverNo;
  512. else
  513. #endif
  514. #ifdef CAPTURE
  515. if (isPrefix(F("captures"), variableName)) {
  516. // TODO: check evenness of value
  517. variable16 = &captureSize;
  518. } else
  519. #endif
  520. #ifdef LED
  521. #ifdef CONFIGURABLE_LEDS
  522. #ifdef TRANSMITLED
  523. if (isPrefix(F("transmitl"), variableName))
  524. variable8 = &transmitled;
  525. else
  526. #endif
  527. #ifdef CAPTURELED
  528. if (isPrefix(F("capturel"), variableName))
  529. variable8 = &captureled;
  530. else
  531. #endif
  532. #ifdef RECEIVELED
  533. if (isPrefix(F("receivel"), variableName))
  534. variable8 = &receiveled;
  535. else
  536. #endif
  537. #ifdef COMMANDLED
  538. if (isPrefix(F("commandl"), variableName))
  539. variable8 = &commandled;
  540. else
  541. #endif
  542. #endif
  543. // TODO: handle blinkTime. This currently means either break encapsulation
  544. // of LedLcdManager, or write a nasty special case :-~
  545. #endif
  546. {
  547. }
  548. if (variable32 != NULL) {
  549. if (value != Tokenizer::invalid)
  550. *variable32 = value;
  551. GirsUtils::printVariable(stream, variableName.c_str(), *variable32);
  552. } else if (variable16 != NULL) {
  553. if (value != Tokenizer::invalid)
  554. *variable16 = (uint16_t) value;
  555. GirsUtils::printVariable(stream, variableName.c_str(), *variable16);
  556. } else if (variable8 != NULL) {
  557. if (value != Tokenizer::invalid)
  558. *variable8 = (uint8_t) value;
  559. GirsUtils::printVariable(stream, variableName.c_str(), *variable8);
  560. } else
  561. stream.println(F("No such variable"));
  562. } else
  563. #endif // PARAMETERS
  564. #ifdef ETHERNET
  565. if (cmd[0] == 'q') { // quit
  566. quit = true;
  567. } else
  568. #endif
  569. #ifdef RECEIVE
  570. // TODO: option for force decoding off
  571. if (isPrefix(cmd, F("receive"))) { // receive
  572. bool status = receive(stream);
  573. if (!status)
  574. stream.println(F(errorString));
  575. } else
  576. #endif // RECEIVE
  577. #ifdef NAMED_COMMANDS
  578. if (cmd[0] == 'n') {
  579. uint16_t noSends = (uint16_t) tokenizer.getInt();
  580. String remoteName = tokenizer.getToken();
  581. String commandName = tokenizer.getToken();
  582. bool success = sendNamedCommand(stream, remoteName, commandName, noSends);
  583. if (success)
  584. stream.println(okString);
  585. } else
  586. if (isPrefix(cmd, "remote")) {
  587. String name = tokenizer.getToken();
  588. dumpRemote(stream, name);
  589. } else
  590. #endif
  591. #ifdef RESET
  592. if (isPrefix(cmd, "reset")) {
  593. reset = true;
  594. } else
  595. #endif
  596. #ifdef TRANSMIT
  597. if (cmd[0] == 's') { // send
  598. // TODO: handle unparsable data gracefully
  599. uint16_t noSends = (uint16_t) tokenizer.getInt();
  600. frequency_t frequency = tokenizer.getFrequency();
  601. uint16_t introLength = (uint16_t) tokenizer.getInt();
  602. uint16_t repeatLength = (uint16_t) tokenizer.getInt();
  603. uint16_t endingLength = (uint16_t) tokenizer.getInt();
  604. microseconds_t intro[introLength];
  605. microseconds_t repeat[repeatLength];
  606. microseconds_t ending[endingLength];
  607. for (uint16_t i = 0; i < introLength; i++)
  608. intro[i] = tokenizer.getMicroseconds();
  609. for (uint16_t i = 0; i < repeatLength; i++)
  610. repeat[i] = tokenizer.getMicroseconds();
  611. for (uint16_t i = 0; i < endingLength; i++)
  612. ending[i] = tokenizer.getMicroseconds();
  613. IrSignal irSignal(intro, introLength, repeat, repeatLength, ending, endingLength, frequency);
  614. bool status = sendIrSignal(irSignal, noSends); // waits
  615. stream.println(status ? F(okString) : F(errorString));
  616. } else
  617. #endif // TRANSMIT
  618. #ifdef PRONTO
  619. if (isPrefix(cmd, F("hex"))) { // pronto hex send
  620. uint16_t noSends = (uint16_t) tokenizer.getInt();
  621. String rest = tokenizer.getRest();
  622. IrSignal *irSignal = Pronto::parse(rest.c_str());
  623. bool status = false;
  624. if (irSignal != NULL) {
  625. status = sendIrSignal(*irSignal, noSends); // waits
  626. delete irSignal;
  627. }
  628. stream.println(status ? F(okString) : F(errorString));
  629. } else
  630. #endif // PRONTO
  631. #ifdef RENDERER
  632. if (cmd[0] == 't') { // transmit
  633. // TODO: handle unparseable data gracefully
  634. uint16_t noSends = (uint16_t) tokenizer.getInt();
  635. String protocol = tokenizer.getToken();
  636. const IrSignal *irSignal = NULL;
  637. if (isPrefix(protocol, F("nec1"))) {
  638. unsigned int D = (unsigned) tokenizer.getInt();
  639. unsigned int S = (unsigned) tokenizer.getInt();
  640. unsigned int F = (unsigned) tokenizer.getInt();
  641. irSignal = (F == Tokenizer::invalid)
  642. ? Nec1Renderer::newIrSignal(D, S)
  643. : Nec1Renderer::newIrSignal(D, S, F);
  644. } else if (isPrefix(protocol, F("rc5"))) {
  645. unsigned int D = (unsigned) tokenizer.getInt();
  646. unsigned int F = (unsigned) tokenizer.getInt();
  647. unsigned int T = (unsigned) tokenizer.getInt();
  648. irSignal = (T == Tokenizer::invalid)
  649. ? Rc5Renderer::newIrSignal(D, F)
  650. : Rc5Renderer::newIrSignal(D, F, T);
  651. } else {
  652. stream.print(F("no such protocol: "));
  653. stream.println(protocol);
  654. }
  655. bool status = false;
  656. if (irSignal != NULL) {
  657. status = sendIrSignal(*irSignal, noSends); // waits, blinks
  658. delete irSignal;
  659. }
  660. stream.println(status ? F(okString) : F(errorString));
  661. } else
  662. #endif // RENDERER
  663. if (cmd[0] == 'v') { // version
  664. stream.println(F(PROGNAME " " VERSION));
  665. } else {
  666. stream.println(F(errorString));
  667. }
  668. //flushIn(stream);
  669. #ifdef RESET
  670. if (reset)
  671. return false;
  672. #endif
  673. #ifdef ETHERNET
  674. if (quit)
  675. return false;
  676. #endif
  677. return true;
  678. }
  679. static bool readProcessOneCommand(Stream& stream) {
  680. String line = readCommand(stream);
  681. #ifdef SERIAL_DEBUG
  682. Serial.println("Command: " + line);
  683. #endif
  684. return processCommand(line, stream);
  685. }
  686. #if defined(ETHERNET)
  687. bool readProcessOneTcpCommand(EthernetClient& client) {
  688. while (client.available() == 0) {
  689. LedLcdManager::checkTurnoff();
  690. #ifdef BEACON
  691. Beacon::checkSend();
  692. #endif
  693. if (!client.connected())
  694. return false;
  695. }
  696. return readProcessOneCommand(client);
  697. }
  698. #endif
  699. void loop() {
  700. LedLcdManager::checkTurnoff();
  701. #ifdef ETHERNET
  702. #ifdef BEACON
  703. Beacon::checkSend();
  704. #endif
  705. EthernetClient client = server.available();
  706. if (!client)
  707. return;
  708. client.setTimeout(10000);
  709. #ifdef LCD
  710. LedLcdManager::lcdPrint(F("Connection!"), true, 0, 0);
  711. #endif
  712. #ifdef SERIAL_DEBUG
  713. Serial.println(F("Connection!"));
  714. #endif
  715. #if defined(COMMANDLED) & defined(LED)
  716. LedLcdManager::setLogicLed(commandled, LedLcdManager::on);
  717. #endif
  718. while (readProcessOneTcpCommand(client))
  719. #if defined(COMMANDLED) & defined(LED)
  720. LedLcdManager::setLogicLed(commandled, LedLcdManager::on)
  721. #endif
  722. ;
  723. #ifdef LCD
  724. LedLcdManager::lcdPrint(F("Connection closed!"), true, 0, 0);
  725. #endif
  726. #ifdef SERIAL_DEBUG
  727. Serial.println(F("Connection closed!"));
  728. #endif
  729. client.println(F("Bye"));
  730. #if defined(COMMANDLED) & defined(LED)
  731. LedLcdManager::setLogicLed(commandled, LedLcdManager::off);
  732. #endif
  733. if (client.connected())
  734. client.flush();
  735. client.stop();
  736. #else // ! ETHERNET
  737. #ifdef ARDUINO
  738. Stream& stream = Serial;
  739. #else
  740. Stream stream(std::cout);
  741. #endif
  742. readProcessOneCommand(stream);
  743. #endif // ! ETHERNET
  744. #ifdef RESET
  745. if (reset) {
  746. GirsUtils::reset();
  747. reset = false; // In case it does not work, do not keep trying
  748. }
  749. #endif
  750. }
  751. #ifndef ARDUINO
  752. int main() {
  753. setup();
  754. while (true)
  755. loop();
  756. }
  757. #endif