GlobalCovfefe
GlobalCovfefe.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2018 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 "GlobalCovfefe.h"
19 #include "version/version.h" // defines VERSION
20 
21 /* Global Cache error codes:
22 
23  * General errors:
24 ERR 001 Invalid request. Command not found.
25 ERR 002 Bad request syntax used with a known command.
26 ERR 003 Invalid or missing module and/or connector address.
27 ERR 004 No carriage return found.
28 ERR 005 Command not supported by current Flex Link Port setting.
29 ERR 006 Settings are locked.
30 
31  * IR errors:
32 ERR IR001 Invalid ID value.
33 ERR IR002 Invalid frequency.
34 ERR IR003 Invalid repeat.
35 ERR IR004 Invalid offset.
36 ERR IR005 Invalid IR pulse value.
37 ERR IR006 Odd amount of IR pulse values (must be even).
38 ERR IR007 Maximum pulse pair limit exceeded.
39 
40  * Serial Errors Explanation (not presently implemented)
41 ERR SL001 Invalid baud rate.
42 ERR SL002 Invalid flow control or duplex setting.
43 ERR SL003 Invalid parity setting.
44 ERR SL004 Invalid stop bits setting.
45 
46  * Relay & Sensor Errors Explanation (not presently implemented)
47 ERR RS001 Invalid logical relay type.
48 ERR RS002 Invalid logical relay state.
49 ERR RS003 Unsupported operation.
50 ERR RS004 Logical relay disabled or not available.
51 ERR RS008 Invalid sensor notify port value.
52 ERR RS009 Invalid sensor notify timer value.
53  */
54 
55 const char *GlobalCovfefe::version = VERSION;
56 
57 // Ugly
58 #define IRMODULE "1"
59 #define IRPORT "1"
60 
61 GlobalCovfefe::GlobalCovfefe(IrSender *irSender_, int commandLed_, int transmitLed_)
62  : irSender(irSender_), commandLed(commandLed_), transmitLed(transmitLed_) {
63  irSender->mute();
64  initLed(commandLed);
65  initLed(transmitLed);
66 }
67 
68 GlobalCovfefe::GlobalCovfefe(const GlobalCovfefe& orig) : irSender(orig.irSender) {
69 }
70 
72 }
73 
74 void GlobalCovfefe::initLed(int pin) const {
75  if (pin != invalidPin)
76  pinMode(pin, OUTPUT);
77 }
78 
79 void GlobalCovfefe::turnOnLed(int pin) const {
80  if (pin != invalidPin)
81  digitalWrite(pin, HIGH);
82 }
83 
84 void GlobalCovfefe::turnOffLed(int pin) const {
85  if (pin != invalidPin)
86  digitalWrite(pin, LOW);
87 }
88 
89 void GlobalCovfefe::turnOnOffLed(int pin, milliseconds_t ms) const {
90  if (pin != invalidPin) {
91  digitalWrite(pin, HIGH);
92  delay((unsigned long) ms);
93  digitalWrite(pin, LOW);
94  }
95 }
96 
97 void GlobalCovfefe::blink(unsigned int count, milliseconds_t ms) const {
98  for (unsigned int i = 0; i < count; i++) {
99  turnOnOffLed(commandLed, ms);
100  turnOnOffLed(transmitLed, ms);
101  }
102 }
103 
104 void GlobalCovfefe::readProcessCommand(Stream& stream) const {
105  char buf[bufSize];
106  turnOnLed(commandLed);
107  unsigned int n = stream.readBytesUntil(eolChar, buf, bufSize);
108  turnOffLed(commandLed);
109  char *b = buf;
110  while (!isalpha(b[0]) && n > 0) {
111  b++;
112  n--;
113  }
114  if (n < bufSize)
115  b[n] = '\0';
116  if (n > 0)
117  processCommand(stream, b);
118  stream.flush();
119 }
120 
121 void GlobalCovfefe::processCommand(Stream &stream, char* buf) const {
122  if (strncmp(buf, "getdevices", 10) == 0)
123  getdevices(stream);
124  else if (strncmp(buf, "getversion", 10) == 0)
125  getversion(stream);
126  else if (strncmp(buf, "blink", 5) == 0)
127  blink();
128  else if (strncmp(buf, "sendir", 6) == 0)
129  sendir(stream, buf);
130  else // Command unrecognized
131  stream.println(F("ERR 001"));
132 }
133 
134 static unsigned int noCommas(const char *buf) {
135  unsigned int n = 0;
136  for (const char *p = buf; *p != '\0'; p++) {
137  if (*p == ',')
138  n++;
139  }
140  return n;
141 }
142 
143 static void grok(microseconds_t *buf, size_t length, frequency_t freq) {
144  for (unsigned int i = 0; i < length; i++) {
145  int duration = atoi(strtok(NULL, ","));
146  buf[i] = (microseconds_t) (1000000UL * duration / freq);
147  }
148 }
149 
150 void GlobalCovfefe::sendir(Stream &stream, char *buf) const {
151  unsigned int commas = noCommas(buf);
152  strtok(buf, ",");
153  strtok(NULL, ","); // connector, ignore nor now
154  uint16_t id = atoi(strtok(NULL, ","));
155  frequency_t freq = atoi(strtok(NULL, ","));
156  uint16_t repeat = atoi(strtok(NULL, ","));
157  uint16_t offset = atoi(strtok(NULL, ","));
158  size_t lengthIntro = offset - 1;
159  microseconds_t intro[lengthIntro];
160  grok(intro, lengthIntro, freq);
161  size_t lengthRepetition = commas - offset - 4;
162  microseconds_t reps[lengthRepetition];
163  grok(reps, lengthRepetition, freq);
164 
165  IrSignal irSignal(intro, lengthIntro, reps, lengthRepetition, freq);
166  turnOnLed(transmitLed);
167  irSender->sendIrSignal(irSignal, repeat);
168  turnOffLed(transmitLed);
169  stream.print(F("completeir," IRMODULE ":" IRPORT ","));
170  stream.println(id);
171 }
172 
173 void GlobalCovfefe::getdevices(Stream &stream) const {
174  stream.println(F("device,0,0 ETHERNET"));
175  if (irSender != NULL)
176  stream.println(F("device," IRMODULE "," IRPORT " IR"));
177  stream.println(F("endlistdevices"));
178 }
179 
180 void GlobalCovfefe::getversion(Stream &stream) const {
181  stream.println(version);
182 }
static const int invalidPin
Definition: GlobalCovfefe.h:91
static unsigned int noCommas(const char *buf)
#define VERSION
Version of the current library.
Definition: version.h:6
void initLed(int pin) const
#define IRPORT
void getversion(Stream &stream) const
Implements the getversion command.
virtual ~GlobalCovfefe()
void turnOnOffLed(int pin, milliseconds_t delay) const
Turns on the pin as the first argument, waits delay milli seconds, then turns off the pin...
void turnOnLed(int pin) const
static const size_t bufSize
Max length on input line.
Definition: GlobalCovfefe.h:96
static void grok(microseconds_t *buf, size_t length, frequency_t freq)
This class emulates the IR sending of a GlobalCache device.
Definition: GlobalCovfefe.h:27
static const char eolChar
GlobalCache uses CR as line terminator.
Definition: GlobalCovfefe.h:89
GlobalCovfefe(IrSender *irSender, int commandLed=invalidPin, int transmitLed=invalidPin)
Main constructor.
void getdevices(Stream &stream) const
Implements the getdevices command.
virtual void processCommand(Stream &stream, char *buf) const
Processes one command from the supplied Stream argument.
void turnOffLed(int pin) const
virtual void blink(unsigned int count=blinkCount, milliseconds_t delay=blinkDelay) const
Implements the blink command (found in GC-100)
void sendir(Stream &stream, char *buf) const
Implements the sendir command.
#define IRMODULE
static const char * version
Version of the present program.
Definition: GlobalCovfefe.h:79
void readProcessCommand(Stream &stream) const
Reads and processes one command from the supplied Stream argument.