1 | /*
|
---|
2 | Copyright (c) 2013 Arduino LLC. All right reserved.
|
---|
3 |
|
---|
4 | This library is free software; you can redistribute it and/or
|
---|
5 | modify it under the terms of the GNU Lesser General Public
|
---|
6 | License as published by the Free Software Foundation; either
|
---|
7 | version 2.1 of the License, or (at your option) any later version.
|
---|
8 |
|
---|
9 | This library is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
12 | Lesser General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU Lesser General Public
|
---|
15 | License along with this library; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #ifndef BRIDGE_H_
|
---|
20 | #define BRIDGE_H_
|
---|
21 |
|
---|
22 | #include <Arduino.h>
|
---|
23 | #include <Stream.h>
|
---|
24 |
|
---|
25 | class BridgeClass {
|
---|
26 | public:
|
---|
27 | BridgeClass(Stream &_stream);
|
---|
28 | void begin();
|
---|
29 |
|
---|
30 | // Methods to handle key/value datastore
|
---|
31 | void put(const char *key, const char *value);
|
---|
32 | void put(const String &key, const String &value)
|
---|
33 | {
|
---|
34 | put(key.c_str(), value.c_str());
|
---|
35 | }
|
---|
36 | unsigned int get(const char *key, uint8_t *buff, unsigned int size);
|
---|
37 | unsigned int get(const char *key, char *value, unsigned int maxlen)
|
---|
38 | {
|
---|
39 | return get(key, reinterpret_cast<uint8_t *>(value), maxlen);
|
---|
40 | }
|
---|
41 |
|
---|
42 | // Trasnfer a frame (with error correction and response)
|
---|
43 | uint16_t transfer(const uint8_t *buff1, uint16_t len1,
|
---|
44 | const uint8_t *buff2, uint16_t len2,
|
---|
45 | const uint8_t *buff3, uint16_t len3,
|
---|
46 | uint8_t *rxbuff, uint16_t rxlen);
|
---|
47 | // multiple inline versions of the same function to allow efficient frame concatenation
|
---|
48 | uint16_t transfer(const uint8_t *buff1, uint16_t len1)
|
---|
49 | {
|
---|
50 | return transfer(buff1, len1, NULL, 0);
|
---|
51 | }
|
---|
52 | uint16_t transfer(const uint8_t *buff1, uint16_t len1,
|
---|
53 | uint8_t *rxbuff, uint16_t rxlen)
|
---|
54 | {
|
---|
55 | return transfer(buff1, len1, NULL, 0, rxbuff, rxlen);
|
---|
56 | }
|
---|
57 | uint16_t transfer(const uint8_t *buff1, uint16_t len1,
|
---|
58 | const uint8_t *buff2, uint16_t len2,
|
---|
59 | uint8_t *rxbuff, uint16_t rxlen)
|
---|
60 | {
|
---|
61 | return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen);
|
---|
62 | }
|
---|
63 |
|
---|
64 | uint16_t getBridgeVersion()
|
---|
65 | {
|
---|
66 | return bridgeVersion;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static const int TRANSFER_TIMEOUT = 0xFFFF;
|
---|
70 |
|
---|
71 | private:
|
---|
72 | uint8_t index;
|
---|
73 | int timedRead(unsigned int timeout);
|
---|
74 | void dropAll();
|
---|
75 | uint16_t bridgeVersion;
|
---|
76 |
|
---|
77 | private:
|
---|
78 | void crcUpdate(uint8_t c);
|
---|
79 | void crcReset();
|
---|
80 | void crcWrite();
|
---|
81 | bool crcCheck(uint16_t _CRC);
|
---|
82 | uint16_t CRC;
|
---|
83 |
|
---|
84 | private:
|
---|
85 | static const char CTRL_C = 3;
|
---|
86 | Stream &stream;
|
---|
87 | bool started;
|
---|
88 | uint8_t max_retries;
|
---|
89 | };
|
---|
90 |
|
---|
91 | // This subclass uses a serial port Stream
|
---|
92 | class SerialBridgeClass : public BridgeClass {
|
---|
93 | public:
|
---|
94 | SerialBridgeClass(HardwareSerial &_serial)
|
---|
95 | : BridgeClass(_serial), serial(_serial) {
|
---|
96 | // Empty
|
---|
97 | }
|
---|
98 |
|
---|
99 | void begin(unsigned long baudrate = 250000) {
|
---|
100 | serial.begin(baudrate);
|
---|
101 | BridgeClass::begin();
|
---|
102 | }
|
---|
103 |
|
---|
104 | private:
|
---|
105 | HardwareSerial &serial;
|
---|
106 | };
|
---|
107 |
|
---|
108 | extern SerialBridgeClass Bridge;
|
---|
109 |
|
---|
110 | #endif /* BRIDGE_H_ */
|
---|
111 |
|
---|
112 | #include <Console.h>
|
---|
113 | #include <Process.h>
|
---|