tinyproto
TinyLightProtocol.h
Go to the documentation of this file.
1 /*
2  Copyright 2017-2019,2022 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  GNU General Public License Usage
7 
8  Protocol Library is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Protocol Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
20 
21  Commercial License Usage
22 
23  Licensees holding valid commercial Tiny Protocol licenses may use this file in
24  accordance with the commercial license agreement provided in accordance with
25  the terms contained in a written agreement between you and Alexey Dynda.
26  For further information contact via email on github account.
27 */
28 
36 #ifndef _TINY_LIGHT_PROTOCOL_H_
37 #define _TINY_LIGHT_PROTOCOL_H_
38 
39 #include "TinyPacket.h"
40 #include "proto/light/tiny_light.h"
41 
42 #ifdef ARDUINO
43 #include <HardwareSerial.h>
44 #else
45 #include <string.h>
46 #endif
47 
48 namespace tinyproto
49 {
50 
61 class Light
62 {
63 public:
64  inline Light()
65  : m_data{}
66  {
67  }
68 
77  void begin(write_block_cb_t writecb, read_block_cb_t readcb);
78 
79 #ifdef ARDUINO
80 
85  void beginToSerial();
86 
87 #ifdef HAVE_HWSERIAL1
88 
93  inline void beginToSerial1()
94  {
95  begin([](void *p, const void *b, int s) -> int { return Serial1.write((const uint8_t *)b, s); },
96  [](void *p, void *b, int s) -> int { return Serial1.readBytes((uint8_t *)b, s); });
97  }
98 #endif
99 
100 #ifdef HAVE_HWSERIAL2
101 
106  inline void beginToSerial2()
107  {
108  begin([](void *p, const void *b, int s) -> int { return Serial2.write((const uint8_t *)b, s); },
109  [](void *p, void *b, int s) -> int { return Serial2.readBytes((uint8_t *)b, s); });
110  }
111 #endif
112 
113 #ifdef HAVE_SERIALUSB
114 
119  inline void beginToSerialUSB()
120  {
121  begin([](void *p, const void *b, int s) -> int { return SerialUSB.write((const char *)b, s); },
122  [](void *p, void *b, int s) -> int { return SerialUSB.readBytes((char *)b, s); });
123  }
124 #endif
125 
126 #endif
127 
131  void end();
132 
141  int write(char *buf, int size);
142 
151  int read(char *buf, int size);
152 
161  int write(const IPacket &pkt);
162 
171  int read(IPacket &pkt);
172 
178  void disableCrc();
179 
185  void enableCrc(hdlc_crc_t crc);
186 
194  bool enableCheckSum();
195 
203  bool enableCrc16();
204 
213  bool enableCrc32();
214 
215 private:
216  STinyLightData m_data{};
217 
218  hdlc_crc_t m_crc = HDLC_CRC_DEFAULT;
219 };
220 
225 } // namespace tinyproto
226 
227 #endif
ProtoLight class incapsulates Protocol functionality.
Definition: TinyLightProtocol.h:61
void disableCrc()
Disable CRC field in the protocol.
Definition: TinyLightProtocol.cpp:78
This is Tiny protocol implementation for microcontrollers.
Describes packet entity and provides API methods to manipulate the packet.
Definition: TinyPacket.h:56
bool enableCrc16()
Enables CRC 16-bit field in the protocol.
Definition: TinyLightProtocol.cpp:98
void enableCrc(hdlc_crc_t crc)
Enables CRC by specified bit-size.
Definition: TinyLightProtocol.cpp:83
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
The function reads data from communication channel.
Definition: tiny_types.h:185
bool enableCrc32()
Enables CRC 32-bit field in the protocol.
Definition: TinyLightProtocol.cpp:108
int write(char *buf, int size)
Sends data block over communication channel.
Definition: TinyLightProtocol.cpp:55
void end()
Resets protocol state.
Definition: TinyLightProtocol.cpp:50
This structure contains information about communication channel and its state.
Definition: tiny_light.h:66
int read(char *buf, int size)
Reads data block from communication channel.
Definition: TinyLightProtocol.cpp:60
This is Tiny Light protocol implementation for microcontrollers.
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
The function writes data to communication channel port.
Definition: tiny_types.h:174
Definition: TinySerial.cpp:22
bool enableCheckSum()
Enables CRC 8-bit field in the protocol.
Definition: TinyLightProtocol.cpp:88
void begin(write_block_cb_t writecb, read_block_cb_t readcb)
Initializes protocol internal variables.
Definition: TinyLightProtocol.cpp:44