Tiny protocol  0.9.3
Tiny communication protocol for microcontrollers
TinyProtocolHd.h
Go to the documentation of this file.
1 /*
2  Copyright 2017 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  Protocol Library is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Protocol Library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
28 #ifndef _TINY_PROTOCOL_H_
29 #define _TINY_PROTOCOL_H_
30 
31 #include "TinyPacket.h"
33 
34 #ifdef ARDUINO
35 # include <HardwareSerial.h>
36 #else
37 # include <string.h>
38 #endif
39 
40 namespace Tiny {
41 
54 class ProtoHd
55 {
56 private:
58  STinyHdData m_data;
59 
60 public:
67  inline ProtoHd(void * buffer,
68  int bufferSize,
69  void (*onReceive)(uint8_t *buf, int len))
70  :m_data{}
71  {
72  m_buffer = buffer;
73  m_bufferSize = bufferSize;
74  m_onReceive = onReceive;
75  }
76 
85  void begin (write_block_cb_t writecb,
86  read_block_cb_t readcb);
87 
88 #ifdef ARDUINO
89 
94  inline void beginToSerial()
95  {
96  begin([](void *p, const void *b, int s)->int { return Serial.write((const uint8_t *)b, s); },
97  [](void *p, void *b, int s)->int { return Serial.readBytes((uint8_t *)b, s); });
98  }
99 
100 #ifdef HAVE_HWSERIAL1
101 
106  inline void beginToSerial1()
107  {
108  begin([](void *p, const void *b, int s)->int { return Serial1.write((const uint8_t *)b, s); },
109  [](void *p, void *b, int s)->int { return Serial1.readBytes((uint8_t *)b, s); });
110  }
111 #endif
112 
113 #ifdef HAVE_HWSERIAL2
114 
119  inline void beginToSerial2()
120  {
121  begin([](void *p, const void *b, int s)->int { return Serial2.write((const uint8_t *)b, s); },
122  [](void *p, void *b, int s)->int { return Serial2.readBytes((uint8_t *)b, s); });
123  }
124 #endif
125 
126 #ifdef HAVE_HWSERIAL3
127 
132  inline void beginToSerial3()
133  {
134  begin([](void *p, const void *b, int s)->int { return Serial3.write((const uint8_t *)b, s); },
135  [](void *p, void *b, int s)->int { return Serial3.readBytes((uint8_t *)b, s); });
136  }
137 #endif
138 
139 
140 #ifdef HAVE_SERIALUSB
141 
146  inline void beginToSerialUSB()
147  {
148  begin([](void *p, const void *b, int s)->int { return SerialUSB.write((const char *)b, s); },
149  [](void *p, void *b, int s)->int { return SerialUSB.readBytes((char *)b, s); });
150  }
151 #endif
152 
153 #endif
154 
158  void end ();
159 
168  int write (char* buf, int size);
169 
178  int write (IPacket &pkt);
179 
188  int run ();
189 
195  void disableCrc ();
196 
204  bool enableCheckSum ();
205 
213  bool enableCrc16 ();
214 
223  bool enableCrc32 ();
224 
225 private:
227  void *m_buffer;
230  int m_bufferSize;
232  void (*m_onReceive)(uint8_t *buf, int len);
234  static void onReceiveInternal(void *handle, uint16_t uid, uint8_t *pdata, int size);
235 
236 };
237 
242 } // Tiny namespace
243 
244 #endif
245 
hdlc_crc_t
Definition: tiny_hdlc.h:33
Tiny protocol Arduino API.
If default is specified HDLC will auto select CRC option.
Definition: tiny_hdlc.h:35
Definition: TinyLightProtocol.h:39
void begin(write_block_cb_t writecb, read_block_cb_t readcb)
void beginToSerial()
Definition: TinyProtocolHd.h:94
Definition: TinyProtocolHd.h:54
ProtoHd(void *buffer, int bufferSize, void(*onReceive)(uint8_t *buf, int len))
Definition: TinyProtocolHd.h:67
void beginToSerial1()
Definition: TinyProtocolHd.h:106
int write(char *buf, int size)
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_types.h:139
void beginToSerial2()
Definition: TinyProtocolHd.h:119
void beginToSerial3()
Definition: TinyProtocolHd.h:132
bool enableCrc32()
Tiny Protocol Half Duplex API.
Definition: tiny_hd.h:47
Definition: TinyPacket.h:46
void disableCrc()
bool enableCheckSum()
bool enableCrc16()
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_types.h:128