-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDevice_HM10.h
76 lines (61 loc) · 1.8 KB
/
Device_HM10.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef DEVICE_HM10_H_
#define DEVICE_HM10_H_
#include <Arduino.h>
#include "Device.h"
enum ConnectionStatus {
STATUS_DISCONNECTED,
STATUS_INITIALIZING,
STATUS_CONNECTING,
STATUS_CONNECTED,
STATUS_DISCONNECTING
};
#if defined(HAVE_HWSERIAL1)
#include <HardwareSerial.h>
#else
// NOTE: The SoftwareSerial is too slow and the notifications too fast
// every now and then the internal circular buffer (64bytes max) is
// full and some data is overwritten/lost
// we end up with invalid packet.
// The DeviceHM10 implementation can recover from that - 1 byte is removed
// at a time until the correct header is found.
// #include <SoftwareSerial.h>
// AltSoftSerial uses two separate (non circular) buffer or RX (80 bytes) and RX
// It is faster than SoftwareSerial and does not seem to lose packets
#include <AltSoftSerial.h>
#define TX_PIN 8
#define RX_PIN 9
#endif
class DeviceHM10 : public Device {
Buffer * buffer;
bool connected = false;
bool newConnection = false;
int status;
const char * mac = "001C971267DF";
#if defined(HAVE_HWSERIAL1)
HardwareSerial * serial = NULL;
#else
//SoftwareSerial * serial = NULL;
AltSoftSerial * serial = NULL;
#endif
bool reset(const char * message);
void serialPrintf(const char *format, ...);
bool sendCommand(const char *cmd, const char *value);
bool isDeviceConnected();
bool checkConnectionStatus();
bool initDevice();
public:
bool isNewConnection();
bool isConnected();
void connect();
void disconnect();
void init();
void dump(const char * msg, const unsigned char * payload, size_t len);
void removeBytes(int bLen);
unsigned char getByte(unsigned int pos);
unsigned char * getPayload();
bool hasBytes(unsigned int bytes);
void write(const unsigned char *payload, int len);
DeviceHM10();
~DeviceHM10();
};
#endif