AsciiMassage
MassagePacker.h
1 /* * Copyright (C) 2017 Thomas O. Fredericks, Sofian Audry
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy of
4  * this software and associated documentation files (the "Software"), to deal in
5  * the Software without restriction, including without limitation the rights to
6  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7  * the Software, and to permit persons to whom the Software is furnished to do so,
8  * subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all
11  * copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef MassagePacker_h
22 #define MassagePacker_h
23 
24 #if defined(ARDUINO) && ARDUINO >= 100
25 #include "Arduino.h"
26 #else
27 #include "WProgram.h"
28 #endif
29 
30 #ifndef MASSAGE_PACKER_BUFFERSIZE
31 
35 #define MASSAGE_PACKER_BUFFERSIZE 256
36 #endif
37 
38 
41 {
42 public:
45  flush();
46  }
47 
48  // Virtual destructor.
49  virtual ~MassagePacker() {}
50 
52  void flush() {
53  _messageSize = 0;
54  }
55 
57  size_t size() const {
58  return _messageSize;
59  }
60 
61  // Returns a pointer to the buffer.
62  const uint8_t* buffer() const {
63  return _buffer;
64  }
65 
67  virtual void beginPacket(const char* address) = 0;
68 
70  virtual void addByte(uint8_t value) = 0;
71 
73  virtual void addInt(int16_t value) = 0;
74 
76  virtual void addLong(int32_t value) = 0;
77 
79  virtual void addFloat(float value) = 0;
80 
82  virtual void addString(const char* value) = 0;
83 
85  virtual void endPacket() = 0;
86 
88  virtual void streamPacket(Stream* stream) {
89  endPacket();
90  stream->write( buffer(), size() );
91  }
92 
94  virtual void packEmpty(const char *address)
95  {
96  beginPacket(address);
97  endPacket();
98  }
99 
101  virtual void packOneByte(const char *address, uint8_t value)
102  {
103  beginPacket(address);
104  addByte(value);
105  endPacket();
106  }
107 
109  virtual void packOneInt(const char *address, int16_t value)
110  {
111  beginPacket(address);
112  addInt(value);
113  endPacket();
114  }
115 
117  virtual void packOneLong(const char *address, int32_t value)
118  {
119  beginPacket(address);
120  addLong(value);
121  endPacket();
122  }
123 
125  virtual void packOneFloat(const char *address, float value)
126  {
127  beginPacket(address);
128  addFloat(value);
129  endPacket();
130  }
131 
133  virtual void packOneString(const char *address, const char* value)
134  {
135  beginPacket(address);
136  addString(value);
137  endPacket();
138  }
139 
141  virtual void streamEmpty(Stream* stream, const char *address)
142  {
143  beginPacket(address);
144  streamPacket(stream);
145  }
146 
148  virtual void streamOneByte(Stream* stream, const char *address, uint8_t value)
149  {
150  beginPacket(address);
151  addByte(value);
152  streamPacket(stream);
153  }
154 
156  virtual void streamOneInt(Stream* stream, const char *address, int16_t value)
157  {
158  beginPacket(address);
159  addInt(value);
160  streamPacket(stream);
161  }
162 
164  virtual void streamOneLong(Stream* stream, const char *address, int32_t value)
165  {
166  beginPacket(address);
167  addLong(value);
168  streamPacket(stream);
169  }
170 
172  virtual void streamOneFloat(Stream* stream, const char *address, float value)
173  {
174  beginPacket(address);
175  addFloat(value);
176  streamPacket(stream);
177  }
178 
180  virtual void streamOneString(Stream* stream, const char *address, const char * s)
181  {
182  beginPacket(address);
183  addString(s);
184  streamPacket(stream);
185  }
186 
187 
188 protected:
189  // Writes single byte to buffer (returns false if buffer is full and cannot be written to).
190  bool _store(uint8_t value)
191  {
192  if (_messageSize >= MASSAGE_PACKER_BUFFERSIZE)
193  return false;
194 
195  _buffer[_messageSize++] = value;
196  return true;
197  }
198 
199  // Current size of message in buffer.
200  size_t _messageSize;
201 
202  // Buffer that holds the data for current message to be sent.
203  uint8_t _buffer[MASSAGE_PACKER_BUFFERSIZE];
204 };
205 
206 
207 
208 
209 #endif
virtual void packOneString(const char *address, const char *value)
Create a packet with a single string value.
Definition: MassagePacker.h:133
virtual void streamEmpty(Stream *stream, const char *address)
Stream a packet with no arguments.
Definition: MassagePacker.h:141
virtual void streamPacket(Stream *stream)
Ends the sending of a message and sends it through a Stream.
Definition: MassagePacker.h:88
size_t size() const
Returns size of buffer.
Definition: MassagePacker.h:57
virtual void packOneLong(const char *address, int32_t value)
Create a packet with a single long value.
Definition: MassagePacker.h:117
virtual void addString(const char *value)=0
Adds a float.
virtual void packOneByte(const char *address, uint8_t value)
Create a packet with a single byte value.
Definition: MassagePacker.h:101
virtual void addLong(int32_t value)=0
Adds a long.
virtual void streamOneLong(Stream *stream, const char *address, int32_t value)
Stream a packet with a single long value.
Definition: MassagePacker.h:164
virtual void streamOneString(Stream *stream, const char *address, const char *s)
Stream a packet with a single string value.
Definition: MassagePacker.h:180
void flush()
Flushes current message in buffer (if any).
Definition: MassagePacker.h:52
virtual void packEmpty(const char *address)
Create a packet with no arguments.
Definition: MassagePacker.h:94
virtual void endPacket()=0
Ends the sending of a message.
MassagePacker()
Constructor.
Definition: MassagePacker.h:44
virtual void beginPacket(const char *address)=0
Begins the sending of a message.
virtual void packOneFloat(const char *address, float value)
Create a packet with a single float value.
Definition: MassagePacker.h:125
virtual void streamOneFloat(Stream *stream, const char *address, float value)
Stream a packet with a single float value.
Definition: MassagePacker.h:172
virtual void addByte(uint8_t value)=0
Adds a byte.
virtual void streamOneByte(Stream *stream, const char *address, uint8_t value)
Stream a packet with a single byte value.
Definition: MassagePacker.h:148
virtual void packOneInt(const char *address, int16_t value)
Create a packet with a single int value.
Definition: MassagePacker.h:109
virtual void addInt(int16_t value)=0
Adds an int.
virtual void addFloat(float value)=0
Adds a float.
virtual void streamOneInt(Stream *stream, const char *address, int16_t value)
Stream a packet with a single int value.
Definition: MassagePacker.h:156
Main MassagePacker abstract class.
Definition: MassagePacker.h:40