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 
64 class Light
65 {
66 public:
67  inline Light()
68  : m_data{}
69  {
70  }
71 
80  void begin(write_block_cb_t writecb, read_block_cb_t readcb);
81 
82 #ifdef ARDUINO
83 
88  void beginToSerial();
89 
90 #ifdef HAVE_HWSERIAL1
91 
96  inline void beginToSerial1()
97  {
98  begin([](void *p, const void *b, int s) -> int { return Serial1.write((const uint8_t *)b, s); },
99  [](void *p, void *b, int s) -> int { return Serial1.readBytes((uint8_t *)b, s); });
100  }
101 #endif
102 
103 #ifdef HAVE_HWSERIAL2
104 
109  inline void beginToSerial2()
110  {
111  begin([](void *p, const void *b, int s) -> int { return Serial2.write((const uint8_t *)b, s); },
112  [](void *p, void *b, int s) -> int { return Serial2.readBytes((uint8_t *)b, s); });
113  }
114 #endif
115 
116 #ifdef HAVE_SERIALUSB
117 
122  inline void beginToSerialUSB()
123  {
124  begin([](void *p, const void *b, int s) -> int { return SerialUSB.write((const char *)b, s); },
125  [](void *p, void *b, int s) -> int { return SerialUSB.readBytes((char *)b, s); });
126  }
127 #endif
128 
129 #endif
130 
134  void end();
135 
144  int write(char *buf, int size);
145 
154  int read(char *buf, int size);
155 
164  int write(const IPacket &pkt);
165 
174  int read(IPacket &pkt);
175 
181  void disableCrc();
182 
188  void enableCrc(hdlc_crc_t crc);
189 
197  bool enableCheckSum();
198 
206  bool enableCrc16();
207 
216  bool enableCrc32();
217 
218 private:
219  STinyLightData m_data{};
220 
221  hdlc_crc_t m_crc = HDLC_CRC_DEFAULT;
222 };
223 
228 } // namespace tinyproto
229 
230 #endif
Light class encapsulates the Light protocol functionality.
Definition: TinyLightProtocol.h:64
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:81
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