AGirs
Listener.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2014, 2015, 2016 Bengt Martensson.
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at
7 your option) any later version.
8 
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see http://www.gnu.org/licenses/.
16 */
17 
18 #include "config.h"
19 #include "GirsLib/LedLcdManager.h"
20 #include "GirsLib/GirsUtils.h"
21 #include <IrReceiverSampler.h>
22 
23 #ifdef ETHERNET
24 
25 #include <Ethernet.h>
26 
27 #include <IPAddress.h>
28 #ifdef BEACON
29 #include <Beacon.h>
30 #endif
31 #endif // ETHERNET
32 
33 static const milliseconds_t beginTimeout = BEGINTIMEOUT; // not really relevant here
34 static const milliseconds_t endingTimeout = ENDINGTIMEOUT;
35 static const size_t captureSize = CAPTURESIZE;
36 static const microseconds_t markExcess = IRRECEIVER_MARK_EXCESS;
37 
38 #ifdef RECEIVELED
40 #endif
41 
42 static IrReader *irReceiver = NULL;
43 
44 #ifndef PROGNAME
45 #define PROGNAME "Listener"
46 #endif // ! PROGNAME
47 #ifdef VERSION
48 #undef VERSION
49 #endif // VERSION
50 #include "GirsLib/version.h"
51 
52 #ifdef ETHERNET
53 static EthernetUDP udp;
54 static IPAddress broadcastIp(BROADCAST_IP);
55 #endif // ETHERNET
56 
57 static char decode[100];
58 
59 static void readOneDecode() {
60  decode[0] = '\0';
61 #ifdef RECEIVELED
63 #endif
64  irReceiver->enable();
65  // Wait until something arrives
66  while (!irReceiver->isReady())
68 
69 #ifdef RECEIVELED
71 #endif
72 #ifdef DECODE
73  MultiDecoder multiDecoder(*irReceiver);
74 #ifdef LCD
75  if (multiDecoder.getType() > MultiDecoder::noise) {
76  LedLcdManager::lcdPrint(multiDecoder.getType() == MultiDecoder::nec_ditto
77  ? "." : multiDecoder.getDecode(),
78  multiDecoder.getType() != MultiDecoder::nec_ditto);
79  if (multiDecoder.getType() == MultiDecoder::nec)
80  LedLcdManager::lcdSetCursor(0, 1); // prepare for dittos
81  }
82 #endif
83  MultiDecoder::Type type = multiDecoder.getType();
84  strncpy(decode, multiDecoder.getDecode(), 99);
85 
87 #endif
88  irReceiver->disable();
89 }
90 
91 void setup() {
99 #ifdef LED
101 #endif
102 
103 #ifdef LCD
104 #ifdef ETHERNET
105 #define xstr(s) str(s)
106 #define str(s) #s
107  char ipstring[16];
108  sprintf(ipstring, "%d.%d.%d.%d", broadcastIp[0], broadcastIp[1], broadcastIp[2], broadcastIp[3]);
109  LedLcdManager::lcdPrint(ipstring, false, 0, 1);
111 #else // ! ETHERNET
112  LedLcdManager::lcdPrint(F("Serial"), false, 0, 1);
113 #endif // ! ETHERNET
114 #endif // LCD
115 
116 #ifdef ETHERNET
117 #ifdef SDCARD_ON_ETHERSHIELD_PIN
118  // disable the SD card, as recommended in the doc
119  pinMode(SDCARD_ON_ETHERSHIELD_PIN, OUTPUT);
120  digitalWrite(SDCARD_ON_ETHERSHIELD_PIN, LOW);
121 #endif
122 
123  byte mac[] = { MACADDRESS };
124 
125  Ethernet.begin(mac
126 #ifndef DHCP
127  , IPAddress(IPADDRESS), IPAddress(DNSSERVER), IPAddress(GATEWAY), IPAddress(SUBNETMASK)
128 #endif // !DHCP
129  );
130  sprintf(ipstring, "%d.%d.%d.%d", Ethernet.localIP()[0], Ethernet.localIP()[1], Ethernet.localIP()[2], Ethernet.localIP()[3]);
131  LedLcdManager::lcdPrint(ipstring, false, 0, 2);
132 
133 #ifdef BEACON
134  Beacon::setup(PROGNAME, "DE-AD-BE-EF-FE-ED", "Utility", "www.harctoolbox.org",
135  "", "", "", "http://arduino/nosuchfile.html");
136 #endif
137 
138 #define DUMMYPORT 8888
139  udp.begin(DUMMYPORT);
140 
141 #else // ! ETHERNET
142 
143  Serial.begin(SERIALBAUD);
144  Serial.setTimeout(SERIALTIMEOUT);
145 #if defined(ARDUINO_AVR_LEONARDO) | defined(ARDUINO_AVR_MICRO)
146  while (!Serial)
147  ; // wait for serial port to connect. "Needed for Leonardo only"
148 #endif
149  Serial.println(F(PROGNAME " " VERSION));
150 
151 #endif // ! ETHERNET
152 
153  irReceiver = IrReceiverSampler::newIrReceiverSampler(captureSize, IRRECEIVER_1_PIN,
155 }
156 
157 void loop() {
159 #ifdef BEACON
160  Beacon::checkSend();
161 #endif
162  readOneDecode();
163 #ifdef DECODE
164  if (decode[0] == '\0')
165  return;
166 #endif
167 
168 #ifdef ETHERNET
169  udp.beginPacket(broadcastIp, BROADCAST_PORT);
170 #ifdef DECODE
171  udp.write(decode);
172 #else
173  irReceiver->dump(udp);
174 #endif
175  udp.write(EOLCHAR);
176  udp.endPacket();
177 #else // ! ETHERNET
178 #ifdef DECODE
179  Serial.println(decode);
180 #else // ! DECODE
181  irReceiver->dump(Serial);
182  Serial.println();
183 #endif // ! DECODE
184 #endif // ! ETHERNET
185 }
#define SDCARD_ON_ETHERSHIELD_PIN
Pin to disable (put low) on Ethernet shield.
#define EOLCHAR
Character that ends the command lines.
#define SERIALTIMEOUT
Timeout for serial line.
#define SERIALBAUD
Configured speed of the serial port.
#define RECEIVELED
Number of logical LED to light when waiting for reception.
#define DHCP
Define to do IP configuration using DHCP.
#define IRRECEIVER_MARK_EXCESS
This quantity is added to all gaps and subtracted from all marks when receiving.
#define MACADDRESS
Must define MAC-Address to use here (!!).
uint8_t led_t
Definition: LedLcdManager.h:9
#define BEGINTIMEOUT
Definition: config.h:38
#define CAPTURESIZE
Definition: config.h:37
#define ENDINGTIMEOUT
Definition: config.h:36
#define BROADCAST_PORT
Definition: config.h:53
#define BROADCAST_IP
Definition: config.h:52
static const milliseconds_t beginTimeout
Definition: Listener.cpp:33
void setup()
Definition: Listener.cpp:91
static char decode[100]
Definition: Listener.cpp:57
static const size_t captureSize
Definition: Listener.cpp:35
#define DUMMYPORT
#define PROGNAME
Definition: Listener.cpp:45
static const milliseconds_t endingTimeout
Definition: Listener.cpp:34
static LED_PARAMETER_CONST led_t receiveled
Definition: Listener.cpp:39
static EthernetUDP udp
Definition: Listener.cpp:53
static void readOneDecode()
Definition: Listener.cpp:59
static IPAddress broadcastIp(BROADCAST_IP)
static const microseconds_t markExcess
Definition: Listener.cpp:36
#define xstr(s)
static IrReader * irReceiver
Definition: Listener.cpp:42
void loop()
Definition: Listener.cpp:157
static void setupLeds()
Definition: GirsUtils.h:115
static void setupReceivers()
Definition: GirsUtils.h:157
static led_t decode2logicalLed(MultiDecoder::Type type)
Definition: GirsUtils.h:17
static void lcdPrint(const char *str, bool clear=true, int x=invalidLine, int y=invalidLine)
static void setupShouldTimeout(led_t logicLed, bool state)
static void setupLedGroundPins()
static bool setLogicLed(led_t logicLed, LedState state)
static void lcdSetCursor(uint8_t x=0, uint8_t y=0)
static void setup(int8_t i2cAddress, uint8_t columns=defaultLcdColumns, uint8_t rows=defaultLcdRows, const pin_t physicalLeds[maxLeds]=NULL, const led_t logicalLeds[maxLeds]=NULL, const bool shouldTimeOut[maxLeds]=NULL)
Sets up the instance, to be called before using the instance.
static void selfTest(const char *text)
static void checkTurnoff()
Turn off if it is due.
#define LCD_WIDTH
#define SIGNAL_LED_7
#define SIGNAL_LED_4
#define LCD_HEIGHT
#define LED_PARAMETER_CONST
#define SIGNAL_LED_8
#define SIGNAL_LED_3
#define LCD_I2C_ADDRESS
#define SIGNAL_LED_5
#define SIGNAL_LED_2
#define SIGNAL_LED_1
#define SIGNAL_LED_6
#define IRRECEIVER_1_PULLUP_VALUE
#define IRRECEIVER_1_PIN
#define VERSION
Version of the current library.
Definition: version.h:7