Tiny protocol  0.7.0
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 
49 class ProtoHd
50 {
51 private:
53  STinyHdData m_data;
54 
55 public:
62  inline ProtoHd(void * buffer,
63  int bufferSize,
64  void (*onReceive)(uint8_t *buf, int len))
65  :m_data{0}
66  {
67  m_buffer = buffer;
68  m_bufferSize = bufferSize;
69  m_onReceive = onReceive;
70  }
71 
80  void begin (write_block_cb_t writecb,
81  read_block_cb_t readcb);
82 
83 #ifdef ARDUINO
84 
89  inline void beginToSerial()
90  {
91  begin([](void *p, const void *b, int s)->int { return Serial.write((const uint8_t *)b, s); },
92  [](void *p, void *b, int s)->int { return Serial.readBytes((uint8_t *)b, s); });
93  }
94 
95 #if HAVE_HWSERIAL1
96 
101  inline void beginToSerial1()
102  {
103  begin([](void *p, const void *b, int s)->int { return Serial1.write((const uint8_t *)b, s); },
104  [](void *p, void *b, int s)->int { return Serial1.readBytes((uint8_t *)b, s); });
105  }
106 #endif
107 
108 #if HAVE_HWSERIAL2
109 
114  inline void beginToSerial2()
115  {
116  begin([](void *p, const void *b, int s)->int { return Serial2.write((const uint8_t *)b, s); },
117  [](void *p, void *b, int s)->int { return Serial2.readBytes((uint8_t *)b, s); });
118  }
119 #endif
120 
121 #if HAVE_HWSERIAL3
122 
127  inline void beginToSerial3()
128  {
129  begin([](void *p, const void *b, int s)->int { return Serial3.write((const uint8_t *)b, s); },
130  [](void *p, void *b, int s)->int { return Serial3.readBytes((uint8_t *)b, s); });
131  }
132 #endif
133 
134 #endif
135 
139  void end ();
140 
149  int write (char* buf, int size);
150 
159  int write (Packet &pkt);
160 
169  int run ();
170 
176  void disableCrc ();
177 
185  bool enableCheckSum ();
186 
194  bool enableCrc16 ();
195 
204  bool enableCrc32 ();
205 
206 private:
208  void *m_buffer;
209  hdlc_crc_t m_crc = HDLC_CRC_DEFAULT;
211  int m_bufferSize;
213  void (*m_onReceive)(uint8_t *buf, int len);
215  static void onReceiveInternal(void *handle, uint16_t uid, uint8_t *pdata, int size);
216 
217 };
218 
219 
220 } // Tiny namespace
221 
222 #endif
223 
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_proto_types.h:114
Tiny protocol Arduino API.
Definition: TinyLightProtocol.h:39
void begin(write_block_cb_t writecb, read_block_cb_t readcb)
void beginToSerial()
Definition: TinyProtocolHd.h:89
Definition: TinyProtocolHd.h:49
ProtoHd(void *buffer, int bufferSize, void(*onReceive)(uint8_t *buf, int len))
Definition: TinyProtocolHd.h:62
void beginToSerial1()
Definition: TinyProtocolHd.h:101
int write(char *buf, int size)
void beginToSerial2()
Definition: TinyProtocolHd.h:114
void beginToSerial3()
Definition: TinyProtocolHd.h:127
bool enableCrc32()
Definition: TinyPacket.h:44
Tiny Protocol Half Duplex API.
Definition: tiny_hd.h:43
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_proto_types.h:125
void disableCrc()
bool enableCheckSum()
bool enableCrc16()